/[drupal]/contributions/modules/search404/search404.module
ViewVC logotype

Diff of /contributions/modules/search404/search404.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.15.2.12, Fri Jun 26 10:47:06 2009 UTC revision 1.15.2.13, Fri Jul 10 08:28:08 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  /* $Id: search404.module,v 1.15.2.11 2009/06/15 08:23:54 zyxware Exp $ */  /* $Id: search404.module,v 1.15.2.11 2009/06/15 08:23:54 zyxware Exp $ */
3    
4    /* @file
5     * The search404 module files, does all the searching
6     * when a 404 occurs
7     */
8    
9  /**  /**
10   * Implementation of hook_menu().   * Implementation of hook_menu().
11   */   */
12  function search404_menu() {  function search404_menu() {
13    $items = array();    $items = array();
14    
15    $items['search404'] = array(    $items['search404'] = array(
16      'title' => t('Page not found'),      'title' => t('Page not found'),
17                  'access callback' => TRUE,      'access callback' => TRUE,
18      'page callback' => 'search404_page',      'page callback' => 'search404_page',
19                  'type' => MENU_LOCAL_TASK      'type' => MENU_LOCAL_TASK
20          );    );
21    
22    $items['admin/settings/search404'] = array(    $items['admin/settings/search404'] = array(
23      'title' => t('Search 404 settings'),      'title' => t('Search 404 settings'),
24      'description' => t('Administer search 404.'),      'description' => t('Administer search 404.'),
25      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
26      'page arguments' => array('search404_settings'),      'page arguments' => array('search404_settings'),
27                  'access callback' => 'user_access',      'access callback' => 'user_access',
28      'access arguments' => array('administer site configuration'),      'access arguments' => array('administer site configuration'),
29      'type' => MENU_NORMAL_ITEM,      'type' => MENU_NORMAL_ITEM,
30    );    );
# Line 36  function search404_get_keys() { Line 42  function search404_get_keys() {
42    $extensions = preg_split('/\s+/', variable_get('search404_ignore_query', 'gif jpg jpeg bmp png'));    $extensions = preg_split('/\s+/', variable_get('search404_ignore_query', 'gif jpg jpeg bmp png'));
43    $extensions = trim(implode('|', $extensions));    $extensions = trim(implode('|', $extensions));
44    if (!empty($extensions) && preg_match("/\.($extensions)$/", $_REQUEST['destination'])) {    if (!empty($extensions) && preg_match("/\.($extensions)$/", $_REQUEST['destination'])) {
45      return false;      return FALSE;
46    }    }
47    $keys = $_REQUEST['destination'] ? $_REQUEST['destination'] : $_REQUEST['q'];    $keys = $_REQUEST['destination'] ? $_REQUEST['destination'] : $_REQUEST['q'];
48    $regex_filter = variable_get('search404_regex', '');    $regex_filter = variable_get('search404_regex', '');
49    $keys_array[] = $keys;    $keys_array[] = $keys;
50    if (!empty($regex_filter)) {    if (!empty($regex_filter)) {
51            $keys = preg_replace("/".$regex_filter."/", '', $keys);      $keys = preg_replace("/". $regex_filter ."/", '', $keys);
52   }    }
53    // Ingore certain extensions from query    // Ingore certain extensions from query
54    $extensions = preg_split('/\s+/', variable_get('search404_ignore_extensions', 'htm html php'));    $extensions = preg_split('/\s+/', variable_get('search404_ignore_extensions', 'htm html php'));
55    $extensions = trim(implode('|', $extensions));    $extensions = trim(implode('|', $extensions));
56    if (!empty($extensions)) {    if (!empty($extensions)) {
57       $keys = preg_replace("/\.($extensions)$/", '', $keys);      $keys = preg_replace("/\.($extensions)$/", '', $keys);
58    }    }
59    
60    $keys = preg_split('/['. PREG_CLASS_SEARCH_EXCLUDE .']+/u', $keys);    $keys = preg_split('/['. PREG_CLASS_SEARCH_EXCLUDE .']+/u', $keys);
# Line 59  function search404_get_keys() { Line 65  function search404_get_keys() {
65    foreach ($keys as $a => $b) {    foreach ($keys as $a => $b) {
66      $keys[$a] = check_plain($b);      $keys[$a] = check_plain($b);
67    }    }
68    $modifier = variable_get('search404_use_or', false) ? ' OR ' : ' ';    $modifier = variable_get('search404_use_or', FALSE) ? ' OR ' : ' ';
69    $keys = trim(implode($modifier, $keys));    $keys = trim(implode($modifier, $keys));
70    return $keys;    return $keys;
71  }  }
# Line 81  function search404_search_engine_query() Line 87  function search404_search_engine_query()
87    $query_string = $parsed_url['query'];    $query_string = $parsed_url['query'];
88    parse_str($query_string, $query);    parse_str($query_string, $query);
89    
90    if (!$parsed_url === false && !empty($remote_host) && !empty($query_string) && count($query)) {    if (!$parsed_url === FALSE && !empty($remote_host) && !empty($query_string) && count($query)) {
91      foreach ($engines as $host => $key) {      foreach ($engines as $host => $key) {
92        if (strpos($remote_host, $host) !== false && array_key_exists($key, $query)) {        if (strpos($remote_host, $host) !== FALSE && array_key_exists($key, $query)) {
93          return trim($query[$key]);          return trim($query[$key]);
94        }        }
95      }      }
96    }    }
97    
98    return false;    return FALSE;
99  }  }
100    
101  /**  /**
# Line 104  function search404_page() { Line 110  function search404_page() {
110      $keys = "";      $keys = "";
111      // if apachesolr_search is installed use apachesolr_search instead default node search      // if apachesolr_search is installed use apachesolr_search instead default node search
112      $type_search = (module_exists('apachesolr_search')) ? 'apachesolr_search' : 'node';      $type_search = (module_exists('apachesolr_search')) ? 'apachesolr_search' : 'node';
113      if (variable_get('search404_use_search_engine', false)) {      if (variable_get('search404_use_search_engine', FALSE)) {
114        $keys = search404_search_engine_query();        $keys = search404_search_engine_query();
115      }      }
116      if (!$keys) {      if (!$keys) {
# Line 112  function search404_page() { Line 118  function search404_page() {
118      }      }
119      if ($keys) {      if ($keys) {
120        // TODO: watchdog?        // TODO: watchdog?
121        if (module_exists('google') && user_access('search Google CSE') && variable_get('search404_do_google_cse', false)) {        if (module_exists('google') && user_access('search Google CSE') && variable_get('search404_do_google_cse', FALSE)) {
122          drupal_set_message(t('The page you requested does not exist. For your convenience, a google search was performed using the query %keys.', array('%keys' => check_plain($keys))), 'error');          drupal_set_message(t('The page you requested does not exist. For your convenience, a google search was performed using the query %keys.', array('%keys' => check_plain($keys))), 'error');
123          drupal_goto ('search/google/'. $keys);          drupal_goto('search/google/'. $keys);
124        } else {        }
125          else {
126          $results = module_invoke($type_search, 'search', 'search', $keys);          $results = module_invoke($type_search, 'search', 'search', $keys);
127          if (isset($results) && is_array($results) && count($results) == 1 && variable_get('search404_jump', false)) {          if (isset($results) && is_array($results) && count($results) == 1 && variable_get('search404_jump', FALSE)) {
128            // First, check to see if there is exactly 1 result            // First, check to see if there is exactly 1 result
129            drupal_set_message(t('The page you requested does not exist. A search for %keys resulted in this page.', array('%keys' => check_plain($keys))), 'status');            drupal_set_message(t('The page you requested does not exist. A search for %keys resulted in this page.', array('%keys' => check_plain($keys))), 'status');
130            // overwrite $_REQUEST['destination'] because it is set by drupal_not_found()            // overwrite $_REQUEST['destination'] because it is set by drupal_not_found()
131            $_REQUEST['destination'] = 'node/'. $results[0]['node']->nid;            $_REQUEST['destination'] = 'node/'. $results[0]['node']->nid;
132            drupal_goto();            drupal_goto();
133          }          }
134          elseif (isset($results) && is_array($results) && count($results) > 1 && variable_get('search404_first', false)) {          elseif (isset($results) && is_array($results) && count($results) > 1 && variable_get('search404_first', FALSE)) {
135            drupal_set_message(t('The page you requested does not exist. A search for %keys resulted in this page.', array('%keys' => check_plain($keys))), 'status');            drupal_set_message(t('The page you requested does not exist. A search for %keys resulted in this page.', array('%keys' => check_plain($keys))), 'status');
136            // overwrite $_REQUEST['destination'] because it is set by drupal_not_found()            // overwrite $_REQUEST['destination'] because it is set by drupal_not_found()
137            $_REQUEST['destination'] = 'node/'. $results[0]['node']->nid;            $_REQUEST['destination'] = 'node/'. $results[0]['node']->nid;
# Line 154  function search404_page() { Line 161  function search404_page() {
161      // Construct the search form.      // Construct the search form.
162      $output = drupal_get_form('search_form', NULL, $keys, $type_search) . $results;      $output = drupal_get_form('search_form', NULL, $keys, $type_search) . $results;
163    }    }
164    
165    /*    /*
166     * Start EVIL HAXX 2     * Start EVIL HAXX 2
167     * This was done to display sidebars left and right of page, if the option is set from Search404     * This was done to display sidebars left and right of page, if the option is set from Search404
168     * Settings Page.     * Settings Page.
169     */     */
170    if (variable_get('search404_block_show', false)) {    if (variable_get('search404_block_show', FALSE)) {
171      print theme('page', $output);      print theme('page', $output);
172      drupal_page_footer();      drupal_page_footer();
173      unset($output);      unset($output);
174      exit (0);      exit(0);
175    }    }
176    else {    else {
177      return $output;      return $output;
178    }    }
179    // End of EVIL HAXX 2    // End of EVIL HAXX 2
180    
# Line 180  function search404_settings() { Line 187  function search404_settings() {
187    $form['search404_jump'] = array(    $form['search404_jump'] = array(
188      '#type' => 'checkbox',      '#type' => 'checkbox',
189      '#title' => t('Jump directly to the search result when there is only one result.'),      '#title' => t('Jump directly to the search result when there is only one result.'),
190      '#default_value' => variable_get('search404_jump', false),      '#default_value' => variable_get('search404_jump', FALSE),
191    );    );
192    $form['search404_first'] = array(    $form['search404_first'] = array(
193      '#type' => 'checkbox',      '#type' => 'checkbox',
194      '#title' => t('Jump directly to the first search result even when there are multiple results.'),      '#title' => t('Jump directly to the first search result even when there are multiple results.'),
195      '#default_value' => variable_get('search404_first', false),      '#default_value' => variable_get('search404_first', FALSE),
196    );    );
197    //Added for displaying blocks for 404 searches    //Added for displaying blocks for 404 searches
198    $form['search404_block_show'] = array(    $form['search404_block_show'] = array(
199      '#type' => 'checkbox',      '#type' => 'checkbox',
200      '#title' => t('Show left and right sidebars in page when a 404 search occurs'),      '#title' => t('Show left and right sidebars in page when a 404 search occurs'),
201      '#default_value' => variable_get('search404_block_show', false),      '#default_value' => variable_get('search404_block_show', FALSE),
202    );    );
203    
204    $form['search404_do_google_cse'] = array(    $form['search404_do_google_cse'] = array(
205      '#type' => 'checkbox',      '#type' => 'checkbox',
206      '#title' => t('Do a Google CSE Search instead of a Drupal Search when a 404 Occurs'),      '#title' => t('Do a Google CSE Search instead of a Drupal Search when a 404 Occurs'),
207      '#description' => t('Requires Google CSE and Google CSE Search Modules to be Enabled'),      '#description' => t('Requires Google CSE and Google CSE Search Modules to be Enabled'),
208      '#attributes' => module_exists('google') ? array()  : array ('disabled' => 'disabled'),      '#attributes' => module_exists('google') ? array()  : array('disabled' => 'disabled'),
209      '#default_value' => variable_get('search404_do_google_cse', false),      '#default_value' => variable_get('search404_do_google_cse', FALSE),
210    );    );
211    
212    $form['advanced'] = array(    $form['advanced'] = array(
213      '#type' => 'fieldset',      '#type' => 'fieldset',
214      '#title' => t('Advanced settings'),      '#title' => t('Advanced settings'),
# Line 212  function search404_settings() { Line 219  function search404_settings() {
219    $form['advanced']['search404_use_or'] = array(    $form['advanced']['search404_use_or'] = array(
220      '#type' => 'checkbox',      '#type' => 'checkbox',
221      '#title' => t('Use OR between keywords when searching.'),      '#title' => t('Use OR between keywords when searching.'),
222      '#default_value' => variable_get('search404_use_or', false),      '#default_value' => variable_get('search404_use_or', FALSE),
223    );    );
224    $form['advanced']['search404_use_search_engine'] = array(    $form['advanced']['search404_use_search_engine'] = array(
225      '#type' => 'checkbox',      '#type' => 'checkbox',
226      '#title' => t('Use auto-detection of keywords from search engine referer.'),      '#title' => t('Use auto-detection of keywords from search engine referer.'),
227      '#description' => t('This feature will conduct a search based on the query string got from a search engine if the URL of the search result points to a 404 page in the current website. Currently supported search engines: Google, Yahoo, Altavista, Lycos, Bing and AOL'),      '#description' => t('This feature will conduct a search based on the query string got from a search engine if the URL of the search result points to a 404 page in the current website. Currently supported search engines: Google, Yahoo, Altavista, Lycos, Bing and AOL'),
228      '#default_value' => variable_get('search404_use_search_engine', false),      '#default_value' => variable_get('search404_use_search_engine', FALSE),
229    );    );
230    $form['advanced']['search404_ignore'] = array(    $form['advanced']['search404_ignore'] = array(
231      '#type' => 'textfield',      '#type' => 'textfield',
# Line 242  function search404_settings() { Line 249  function search404_settings() {
249      '#type' => 'textfield',      '#type' => 'textfield',
250      '#title' => t('PCRE filter'),      '#title' => t('PCRE filter'),
251      '#description' => t('This regular expression will be applied to filter all queries. The parts of the path that match the expression will be EXCLUDED from the search. You do NOT have to enclose the regex in forward slashes when defining the PCRE. e.g.: use "[foo]bar" instead of "/[foo]bar/". On how to use a PCRE Regex please refer <a href="http://php.net/pcre">PCRE pages in the PHP Manual</a>.'),      '#description' => t('This regular expression will be applied to filter all queries. The parts of the path that match the expression will be EXCLUDED from the search. You do NOT have to enclose the regex in forward slashes when defining the PCRE. e.g.: use "[foo]bar" instead of "/[foo]bar/". On how to use a PCRE Regex please refer <a href="http://php.net/pcre">PCRE pages in the PHP Manual</a>.'),
252      '#default_value' => variable_get('search404_regex',''),      '#default_value' => variable_get('search404_regex', ''),
253    );    );
254    return system_settings_form($form);    return system_settings_form($form);
255  }  }

Legend:
Removed from v.1.15.2.12  
changed lines
  Added in v.1.15.2.13

  ViewVC Help
Powered by ViewVC 1.1.2