You are here

function apachesolr_stats_block_frequent_keywords in Apache Solr Statistics 7

Same name and namespace in other branches
  1. 6.3 apachesolr_stats.module \apachesolr_stats_block_frequent_keywords()
  2. 6 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_view in ./apachesolr_stats.module
Implements hook_block_view().
1 string reference to 'apachesolr_stats_block_frequent_keywords'
apachesolr_stats_uninstall in ./apachesolr_stats.install
Implements hook_uninstall().

File

./apachesolr_stats.module, line 913
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 > REQUEST_TIME) {
    return $cached->data;
  }
  $keywords = array();

  // Return keywords only last week's logged queries.
  $timestamp = REQUEST_TIME - 3600 * 24 * 7;
  $result = db_query_range("SELECT keywords FROM {apachesolr_stats} WHERE\n      numfound > :numfound AND timestamp > :timestamp AND page_id = :pageid\n      ORDER BY timestamp DESC", 0, 5000, array(
    ':numfound' => 0,
    ':timestamp' => $timestamp,
    ':pageid' => $page_id,
  ));
  foreach ($result as $record) {
    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 = REQUEST_TIME + 600;
  cache_set($cid, $links, 'cache_block', $expire);
  return $links;
}