Code coverage for /20081101/modules/statistics/statistics.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: statistics.admin.inc,v 1.14 2008/10/13 00:33:04 webchick Exp $
3
4
/**
5
 * @file
6
 * Admin page callbacks for the statistics module.
7
 */
8
9
/**
10
 * Menu callback; presents the "recent hits" page.
11
 */
122
function statistics_recent_hits() {
13
  $header = array(
140
    array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' =>
'desc'),
150
    array('data' => t('Page'), 'field' => 'a.path'),
160
    array('data' => t('User'), 'field' => 'u.name'),
170
    array('data' => t('Operations'))
180
  );
19
200
  $sql = 'SELECT a.aid, a.path, a.title, a.uid, u.name, a.timestamp FROM
{accesslog} a LEFT JOIN {users} u ON u.uid = a.uid' .
tablesort_sql($header);
21
220
  $result = pager_query($sql, 30);
230
  $rows = array();
240
  while ($log = db_fetch_object($result)) {
250
    $rows[] = array(
260
      array('data' => format_date($log->timestamp, 'small'), 'class' =>
'nowrap'),
270
      _statistics_format_item($log->title, $log->path),
280
      theme('username', $log),
290
      l(t('details'), "admin/reports/access/$log->aid"));
300
  }
31
320
  if (empty($rows)) {
330
    $rows[] = array(array('data' => t('No statistics available.'),
'colspan' => 4));
340
  }
35
360
  $output = theme('table', $header, $rows);
370
  $output .= theme('pager', NULL, 30, 0);
380
  return $output;
390
}
40
41
/**
42
 * Menu callback; presents the "top pages" page.
43
 */
442
function statistics_top_pages() {
45
  // MAX(title) avoids having empty node titles which otherwise causes
duplicates in the top pages list
460
  $sql = "SELECT COUNT(path) AS hits, path, MAX(title) AS title, AVG(timer)
AS average_time, SUM(timer) AS total_time FROM {accesslog} GROUP BY path";
470
  $sql_cnt = "SELECT COUNT(DISTINCT(path)) FROM {accesslog}";
48
49
  $header = array(
500
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
510
    array('data' => t('Page'), 'field' => 'path'),
520
    array('data' => t('Average page generation time'), 'field' =>
'average_time'),
530
    array('data' => t('Total page generation time'), 'field' =>
'total_time')
540
  );
550
  $sql .= tablesort_sql($header);
560
  $result = pager_query($sql, 30, 0, $sql_cnt);
57
580
  $rows = array();
590
  while ($page = db_fetch_object($result)) {
600
    $rows[] = array($page->hits, _statistics_format_item($page->title,
$page->path), t('%time ms', array('%time' => round($page->average_time))),
format_interval(round($page->total_time / 1000)));
610
  }
62
630
  if (empty($rows)) {
640
    $rows[] = array(array('data' => t('No statistics available.'),
'colspan' => 4));
650
  }
66
670
  drupal_set_title(t('Top pages in the past %interval', array('%interval'
=> format_interval(variable_get('statistics_flush_accesslog_timer',
259200)))), PASS_THROUGH);
680
  $output = theme('table', $header, $rows);
690
  $output .= theme('pager', NULL, 30, 0);
700
  return $output;
710
}
72
73
/**
74
 * Menu callback; presents the "top visitors" page.
75
 */
762
function statistics_top_visitors() {
77
78
  $header = array(
792
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
802
    array('data' => t('Visitor'), 'field' => 'u.name'),
812
    array('data' => t('Total page generation time'), 'field' => 'total'),
822
    array('data' => user_access('block IP addresses') ? t('Operations') :
'', 'colspan' => 2),
832
  );
84
852
  $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname,
SUM(a.timer) AS total, bl.iid FROM {accesslog} a LEFT JOIN {blocked_ips} bl
ON a.hostname = bl.ip LEFT JOIN {users} u ON a.uid = u.uid GROUP BY
a.hostname, a.uid, u.name, bl.iid" . tablesort_sql($header);
862
  $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(uid, hostname))) FROM
{accesslog}";
872
  $result = pager_query($sql, 30, 0, $sql_cnt);
88
892
  $rows = array();
902
  while ($account = db_fetch_object($result)) {
912
    $qs = drupal_get_destination();
922
    $ban_link =  $account->iid ? l(t('unblock IP address'),
"admin/settings/ip-blocking/delete/$account->iid", array('query' => $qs)) :
l(t('block IP address'), "admin/settings/ip-blocking/$account->hostname",
array('query' => $qs));
932
    $rows[] = array($account->hits, ($account->uid ? theme('username',
$account) : $account->hostname), format_interval(round($account->total /
1000)), (user_access('block IP addresses') && !$account->uid) ? $ban_link :
'');
942
  }
95
962
  if (empty($rows)) {
970
    $rows[] = array(array('data' => t('No statistics available.'),
'colspan' => 4));
980
  }
99
1002
  drupal_set_title(t('Top visitors in the past %interval',
array('%interval' =>
format_interval(variable_get('statistics_flush_accesslog_timer',
259200)))), PASS_THROUGH);
1012
  $output = theme('table', $header, $rows);
1022
  $output .= theme('pager', NULL, 30, 0);
1032
  return $output;
1040
}
105
106
/**
107
 * Menu callback; presents the "referrer" page.
108
 */
1092
function statistics_top_referrers() {
1100
  $query = "SELECT url, COUNT(url) AS hits, MAX(timestamp) AS last FROM
{accesslog} WHERE url NOT LIKE :host AND url <> '' GROUP BY url";
1110
  $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <>
'' AND url NOT LIKE :host";
1120
  drupal_set_title(t('Top referrers in the past %interval',
array('%interval' =>
format_interval(variable_get('statistics_flush_accesslog_timer',
259200)))), PASS_THROUGH);
113
114
  $header = array(
1150
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
1160
    array('data' => t('Url'), 'field' => 'url'),
1170
    array('data' => t('Last visit'), 'field' => 'last'),
1180
  );
119
1200
  $query .= tablesort_sql($header);
1210
  $result = pager_query($query, 30, 0, $query_cnt, array(':host' => '%'.
$_SERVER['HTTP_HOST'] .'%'));
122
1230
  $rows = array();
1240
  while ($referrer = db_fetch_object($result)) {
1250
    $rows[] = array($referrer->hits, _statistics_link($referrer->url),
t('@time ago', array('@time' => format_interval(REQUEST_TIME -
$referrer->last))));
1260
  }
127
1280
  if (empty($rows)) {
1290
    $rows[] = array(array('data' => t('No statistics available.'),
'colspan' => 3));
1300
  }
131
1320
  $output = theme('table', $header, $rows);
1330
  $output .= theme('pager', NULL, 30, 0);
1340
  return $output;
1350
}
136
137
/**
138
 * Menu callback; Displays recent page accesses.
139
 */
1402
function statistics_access_log($aid) {
1410
  $result = db_query('SELECT a.*, u.name FROM {accesslog} a LEFT JOIN
{users} u ON a.uid = u.uid WHERE aid = %d', $aid);
1420
  if ($access = db_fetch_object($result)) {
1430
    $rows[] = array(
1440
      array('data' => t('URL'), 'header' => TRUE),
1450
      l(url($access->path, array('absolute' => TRUE)), $access->path)
1460
    );
147
    // It is safe to avoid filtering $access->title through check_plain
because
148
    // it comes from drupal_get_title().
1490
    $rows[] = array(
1500
      array('data' => t('Title'), 'header' => TRUE),
1510
      $access->title
1520
    );
1530
    $rows[] = array(
1540
      array('data' => t('Referrer'), 'header' => TRUE),
1550
      ($access->url ? l($access->url, $access->url) : '')
1560
    );
1570
    $rows[] = array(
1580
      array('data' => t('Date'), 'header' => TRUE),
1590
      format_date($access->timestamp, 'large')
1600
    );
1610
    $rows[] = array(
1620
      array('data' => t('User'), 'header' => TRUE),
1630
      theme('username', $access)
1640
    );
1650
    $rows[] = array(
1660
      array('data' => t('Hostname'), 'header' => TRUE),
1670
      check_plain($access->hostname)
1680
    );
169
1700
    return theme('table', array(), $rows);
1710
  }
172
  else {
1730
    drupal_not_found();
174
  }
1750
}
176
177
/**
178
 * Form builder; Configure access logging.
179
 *
180
 * @ingroup forms
181
 * @see system_settings_form()
182
 */
1832
function statistics_access_logging_settings() {
184
  // Access log settings:
1850
  $options = array('1' => t('Enabled'), '0' => t('Disabled'));
1860
  $form['access'] = array(
1870
    '#type' => 'fieldset',
1880
    '#title' => t('Access log settings'));
1890
  $form['access']['statistics_enable_access_log'] = array(
1900
    '#type' => 'radios',
1910
    '#title' => t('Enable access log'),
1920
    '#default_value' => variable_get('statistics_enable_access_log', 0),
1930
    '#options' => $options,
1940
    '#description' => t('Log each page access. Required for referrer
statistics.'));
1950
  $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400,
172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800),
'format_interval');
1960
  $form['access']['statistics_flush_accesslog_timer'] = array(
1970
    '#type' => 'select',
1980
    '#title' => t('Discard access logs older than'),
1990
    '#default_value'   => variable_get('statistics_flush_accesslog_timer',
259200),
2000
    '#options' => $period,
2010
    '#description' => t('Older access log entries (including referrer
statistics) will be automatically discarded. (Requires a correctly
configured <a href="@cron">cron maintenance task</a>.)', array('@cron' =>
url('admin/reports/status'))));
202
203
  // count content views settings
2040
  $form['content'] = array(
2050
    '#type' => 'fieldset',
2060
    '#title' => t('Content viewing counter settings'));
2070
  $form['content']['statistics_count_content_views'] = array(
2080
    '#type' => 'radios',
2090
    '#title' => t('Count content views'),
2100
    '#default_value' => variable_get('statistics_count_content_views', 0),
2110
    '#options' => $options,
2120
    '#description' => t('Increment a counter each time content is
viewed.'));
213
2140
  return system_settings_form($form);
2150
}
2162