You are here

function search404_get_keys in Search 404 7

Same name and namespace in other branches
  1. 5 search404.module \search404_get_keys()
  2. 6 search404.module \search404_get_keys()

Get the keys that are to be used for the search.

The search is based either on the keywords from the URL or from the keys from the search that resulted in the 404.

2 calls to search404_get_keys()
search404_block_view in ./search404.module
Implements hook_block_view().
search404_page in ./search404.page.inc
Main search function.

File

./search404.page.inc, line 14
The search404 module search page related functions.

Code

function search404_get_keys() {
  $keys = '';

  // Try to get keywords from the search result (if it was one)
  // that resulted in the 404 if the config is set.
  if (variable_get('search404_use_search_engine', FALSE)) {
    $keys = search404_search_engine_query();
  }

  // If keys are not yet populated from a search engine referer
  // use keys from the path that resulted in the 404.
  // TODO: Figure out why $_GET['destination'] is not getting
  // populated. https://www.drupal.org/node/1445186
  if (!$keys && isset($_GET['destination'])) {
    $keys = $_GET['destination'];
    drupal_alter('search404_keys', $keys);
  }

  // Abort query on certain extensions, e.g: gif jpg jpeg png.
  $extensions = explode(' ', variable_get('search404_ignore_query', 'gif jpg jpeg bmp png'));
  $extensions = trim(implode('|', $extensions));
  if (!empty($extensions) && preg_match("/\\.({$extensions})\$/i", $keys)) {
    return FALSE;
  }

  /* TODO - How does this work in D7
    // Remove the Language Prefix Appended to
    // Search String (http://drupal.org/node/560426)
    if (LANGUAGE_NEGOTIATION_PATH_DEFAULT && $language->language) {
    $keys = preg_replace("/^" . $language->language . "\//i", '', $keys);
    }*/
  $regex_filter = variable_get('search404_regex', '');
  if (!empty($regex_filter)) {
    $keys = preg_replace("/" . $regex_filter . "/i", '', $keys);
  }

  // Ignore certain extensions from query.
  $extensions = explode(' ', variable_get('search404_ignore_extensions', 'htm html php'));
  $extensions = trim(implode('|', $extensions));
  if (!empty($extensions)) {
    $keys = preg_replace("/\\.({$extensions})\$/i", '', $keys);
  }
  $keys = preg_split('/[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']+/u', $keys);

  // Ignore certain words (use case insensitive search).
  $keys = array_udiff($keys, explode(' ', variable_get('search404_ignore', t('and or the'))), 'strcasecmp');

  // Sanitize the keys.
  foreach ($keys as $a => $b) {
    $keys[$a] = check_plain($b);
  }
  $modifier = variable_get('search404_use_or', FALSE) ? ' OR ' : ' ';
  $keys = trim(implode($modifier, $keys));
  return $keys;
}