You are here

public function Search404Controller::search404GetKeys in Search 404 8

Same name and namespace in other branches
  1. 2.x src/Controller/Search404Controller.php \Drupal\search404\Controller\Search404Controller::search404GetKeys()

Function for searchkeys.

Get the keys that are to be used for the search based either on the keywords from the URL or from the keys from the search that resulted in the 404.

1 call to Search404Controller::search404GetKeys()
Search404Controller::search404Page in src/Controller/Search404Controller.php

File

src/Controller/Search404Controller.php, line 336

Class

Search404Controller
Route controller for search.

Namespace

Drupal\search404\Controller

Code

public function search404GetKeys() {
  $keys = [];

  // Try to get keywords from the search result (if it was one)
  // that resulted in the 404 if the config is set.
  if (\Drupal::config('search404.settings')
    ->get('search404_use_search_engine')) {
    $keys = $this
      ->search404SearchEngineQuery();
  }

  // If keys are not yet populated from a search engine referer
  // use keys from the path that resulted in the 404.
  if (empty($keys)) {
    $path = \Drupal::service('path.current')
      ->getPath();
    $path = urldecode($path);
    $path = preg_replace('/[_+-.,!@#$^&*();\'"?=]|[|]|[{}]|[<>]/', '/', $path);
    $paths = explode('/', $path);

    // Removing the custom search path value from the keyword search.
    if (\Drupal::config('search404.settings')
      ->get('search404_do_custom_search')) {
      $custom_search_path = \Drupal::config('search404.settings')
        ->get('search404_custom_search_path');
      $custom_search = explode('/', $custom_search_path);
      $search_path = array_diff($custom_search, [
        "@keys",
      ]);
      $keywords = array_diff($paths, $search_path);
      $keys = array_filter($keywords);
    }
    else {
      $keys = array_filter($paths);
    }

    // Split the keys with - and space.
    $keys = preg_replace('/-/', ' ', $keys);
    foreach ($keys as $key => $value) {
      $keys_with_space_hypen[$key] = explode(' ', $value);
      $keys_with_space_hypen[$key] = array_filter($keys_with_space_hypen[$key]);
    }
    if (!empty($keys)) {
      $keys = call_user_func_array('array_merge', $keys_with_space_hypen);
    }
  }

  // Abort query on certain extensions, e.g: gif jpg jpeg png.
  $extensions = explode(' ', \Drupal::config('search404.settings')
    ->get('search404_ignore_query'));
  $extensions = trim(implode('|', $extensions));
  if (!empty($extensions)) {
    foreach ($keys as $key) {
      if (preg_match("/\\.({$extensions})\$/i", $key)) {
        return FALSE;
      }
    }
  }

  // PCRE filter from query.
  $regex_filter = \Drupal::config('search404.settings')
    ->get('search404_regex');
  if (!empty($regex_filter)) {

    // Get filtering patterns as array.
    $filter_data = explode('[', $regex_filter);
    for ($i = 0; $i < count($filter_data); $i++) {
      if (!empty($filter_data[$i])) {
        $filter_query = explode(']', $filter_data[$i]);

        // Make the pattern for replacement.
        $regex_pattern[0] = '/' . $filter_query[0] . '/ix';
        $filter_patterns[] = trim($regex_pattern[0]);
      }
    }

    // Pattern filtering.
    $keys = preg_replace($filter_patterns, '', $keys);
    $keys = array_filter($keys);
  }

  // Ignore certain extensions from query.
  $extensions = explode(' ', \Drupal::config('search404.settings')
    ->get('search404_ignore_extensions'));
  if (!empty($extensions)) {
    $keys = array_diff($keys, $extensions);
  }

  // Ignore certain words (use case insensitive search).
  $keys = array_udiff($keys, explode(' ', \Drupal::config('search404.settings')
    ->get('search404_ignore')), 'strcasecmp');

  // Sanitize the keys.
  foreach ($keys as $a => $b) {
    $keys[$a] = Html::escape($b);
  }

  // When using keywords with OR operator.
  if (\Drupal::config('search404.settings')
    ->get('search404_use_or')) {
    $keys = trim(implode(' OR ', $keys));

    // Removing the custom path string from the keywords.
    if (\Drupal::config('search404.settings')
      ->get('search404_do_custom_search')) {
      $custom_search_path = \Drupal::config('search404.settings')
        ->get('search404_custom_search_path');
      $custom_search = explode('/', $custom_search_path);
      $custom_path = array_diff($custom_search, [
        "@keys",
      ]);
      $keys = str_replace($custom_path[0], '', $keys);
      $keys = trim(rtrim($keys, ' OR '));
    }
  }
  else {
    $keys = trim(implode(' ', $keys));

    // Removing the custom path string from the keywords.
    if (\Drupal::config('search404.settings')
      ->get('search404_do_custom_search')) {
      $custom_search_path = \Drupal::config('search404.settings')
        ->get('search404_custom_search_path');
      $custom_search = explode('/', $custom_search_path);
      $custom_path = array_diff($custom_search, [
        "@keys",
      ]);
      $keys = trim(str_replace($custom_path[0], '', $keys));
    }
  }
  return $keys;
}