You are here

function apachesolr_autocomplete_get_stopwords in Apache Solr Autocomplete 7.2

Same name and namespace in other branches
  1. 6 apachesolr_autocomplete.module \apachesolr_autocomplete_get_stopwords()
  2. 7 apachesolr_autocomplete.module \apachesolr_autocomplete_get_stopwords()

Gets the current stopwords list configured in Solr.

1 call to apachesolr_autocomplete_get_stopwords()
apachesolr_autocomplete_suggest in ./apachesolr_autocomplete.module

File

./apachesolr_autocomplete.module, line 501
Alters search forms to suggest terms using Apache Solr using AJAX.

Code

function apachesolr_autocomplete_get_stopwords(DrupalApacheSolrServiceInterface $solr) {
  static $words = array(), $cached_flag = false;

  // Try static cache.
  if ($cached_flag) {
    return $words;
  }

  // Try Drupal cache.
  $cid = 'apachesolr_autocomplete_stopwords';
  $cached = cache_get($cid);
  if ($cached && $cached->expire > REQUEST_TIME) {
    return $cached->data;
  }
  try {
    $response = $solr
      ->makeServletRequest('admin/file', array(
      'file' => 'stopwords.txt',
    ));
  } catch (Exception $e) {
    $cached_flag = TRUE;
    return $words;
  }
  foreach (explode("\n", $response->data) as $line) {
    if (drupal_substr($line, 0, 1) == "#") {
      continue;
    }
    if ($word = trim($line)) {
      $words[] = $word;
    }
  }

  // Cache in Drupal cache for 10 minutes.
  cache_set($cid, $words, 'cache', REQUEST_TIME + 600);
  $cached_flag = TRUE;
  return $words;
}