You are here

function apachesolr_stats_block_frequent_keywords in Apache Solr Statistics 6.3

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

Return a listing of keywords for the Popular Searches block.

1 call to apachesolr_stats_block_frequent_keywords()
apachesolr_stats_block in ./apachesolr_stats.module
Implementation of hook_block().
1 string reference to 'apachesolr_stats_block_frequent_keywords'
apachesolr_stats_uninstall in ./apachesolr_stats.install
Implementation of hook_uninstall().

File

./apachesolr_stats.module, line 880
Keeps and reports statistics about Apache Solr usage and performance.

Code

function apachesolr_stats_block_frequent_keywords($page_id, $limit = 10) {
  $search_page = apachesolr_search_page_load($page_id);
  $search_path = $search_page['search_path'];
  $cid = "apachesolr_stats_block_frequent_keywords_{$page_id}";
  $cached = cache_get($cid, 'cache_block');
  if ($cached && $cached->data && $cached->expire > time()) {
    return $cached->data;
  }
  $keywords = array();

  // Return keywords only last week's logged queries.
  $timestamp = time() - 3600 * 24 * 7;
  $result = db_query_range("SELECT keywords FROM {apachesolr_stats} WHERE\n      numfound > 0 AND timestamp > %d AND page_id = '%s'\n      ORDER BY timestamp DESC", $timestamp, $page_id, 0, 5000);
  while ($record = db_fetch_object($result)) {
    if (trim($record->keywords) != "") {

      // Keep track of individual keywords used
      $keys_filtered = drupal_strtolower(trim($record->keywords));
      if (isset($keywords[$keys_filtered])) {
        $keywords[$keys_filtered]++;
      }
      else {
        $keywords[$keys_filtered] = 1;
      }
    }
  }

  // Sort by most frequent first.
  arsort($keywords);

  // Get first $limit items.
  $keywords = array_slice($keywords, 0, $limit);
  $links = array();
  foreach ($keywords as $key => $frequency) {
    $links[] = l($key, $search_path . '/' . $key);
  }

  // Cache information for 10 minutes.
  $expire = time() + 600;
  cache_set($cid, $links, 'cache_block', $expire);
  return $links;
}