You are here

function _search_log_items in Search Log 6

Internal function to generate cached top search terms.

Block cache is 1 hour and cleared on cron.

1 call to _search_log_items()
search_log_block in ./search_log.module
Implementation of hook_block().

File

./search_log.module, line 304
Replaces default report of top search phrases.

Code

function _search_log_items() {
  if ($cache = cache_get(SEARCH_LOG_BLOCK_CACHE, SEARCH_LOG_BLOCK_CACHE_TABLE)) {
    $items = unserialize($cache->data);
  }
  else {
    $max = variable_get('search_log_block_max', 10);
    $terms_manual = array();
    $terms = explode("\n", variable_get('search_log_block_manual', NULL));
    foreach ($terms as $term) {
      if ($term) {
        list($item['q'], $item['module'], $item['count']) = explode('|', $term);
        if (isset($item['q']) && isset($item['module'])) {
          $items[] = $item;
          $terms_manual[] = $item['q'];
          $max--;
        }
        unset($item);
      }
    }
    $query_where_days = '';
    if ($days = variable_get('search_log_block_days', 0)) {
      $today = _search_log_get_time();
      $from_time = $today - $days * 86400;
      $query_where_days = ' AND day >= ' . $from_time;
    }
    $modules = array_flip(variable_get('search_log_block_modules', array()));
    $query_where_modules = '';
    unset($modules[0]);
    if (!empty($modules)) {
      $query_where_modules = ' AND module IN ("' . implode('","', $modules) . '")';
    }
    if ($max > 0) {
      $query = db_query_range('SELECT q, module, SUM(counter) as count FROM {search_log} WHERE result >= 0' . $query_where_days . $query_where_modules . ' GROUP BY q, module ORDER BY count DESC', 0, $max);
      while ($max && ($item = db_fetch_array($query))) {
        if (!in_array($item['q'], $terms_manual)) {
          $items[] = $item;
          $max--;
        }
      }
    }
    uasort($items, '_search_log_items_sort');
    cache_set(SEARCH_LOG_BLOCK_CACHE, serialize($items), SEARCH_LOG_BLOCK_CACHE_TABLE, time() + 3600);
  }
  return $items;
}