You are here

function statistics_top_referrers in Drupal 7

Same name and namespace in other branches
  1. 4 modules/statistics.module \statistics_top_referrers()
  2. 5 modules/statistics/statistics.module \statistics_top_referrers()
  3. 6 modules/statistics/statistics.admin.inc \statistics_top_referrers()

Page callback: Displays the "top referrers" in the access logs.

This displays the pages with the top referrers in a given time interval that haven't been flushed yet. The flush interval is set on the statistics settings form, but is dependent on cron running.

Return value

A render array containing the top referrers information.

1 string reference to 'statistics_top_referrers'
statistics_menu in modules/statistics/statistics.module
Implements hook_menu().

File

modules/statistics/statistics.admin.inc, line 178
Admin page callbacks for the Statistics module.

Code

function statistics_top_referrers() {
  drupal_set_title(t('Top referrers in the past %interval', array(
    '%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)),
  )), PASS_THROUGH);
  $header = array(
    array(
      'data' => t('Hits'),
      'field' => 'hits',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Url'),
      'field' => 'url',
    ),
    array(
      'data' => t('Last visit'),
      'field' => 'last',
    ),
  );
  $query = db_select('accesslog', 'a')
    ->extend('PagerDefault')
    ->extend('TableSort');
  $query
    ->addExpression('COUNT(url)', 'hits');
  $query
    ->addExpression('MAX(timestamp)', 'last');
  $query
    ->fields('a', array(
    'url',
  ))
    ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
    ->condition('url', '', '<>')
    ->groupBy('url')
    ->limit(30)
    ->orderByHeader($header);
  $count_query = db_select('accesslog', 'a', array(
    'target' => 'slave',
  ));
  $count_query
    ->addExpression('COUNT(DISTINCT url)');
  $count_query
    ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
    ->condition('url', '', '<>');
  $query
    ->setCountQuery($count_query);
  $result = $query
    ->execute();
  $rows = array();
  foreach ($result as $referrer) {
    $rows[] = array(
      $referrer->hits,
      _statistics_link($referrer->url),
      t('@time ago', array(
        '@time' => format_interval(REQUEST_TIME - $referrer->last),
      )),
    );
  }
  $build['statistics_top_referrers_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No statistics available.'),
  );
  $build['statistics_top_referrers_pager'] = array(
    '#theme' => 'pager',
  );
  return $build;
}