statistics.admin.inc 9.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

/**
 * @file
 * Admin page callbacks for the statistics module.
 */

/**
 * Menu callback; presents the "recent hits" page.
 */
function statistics_recent_hits() {
  $header = array(
    array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
    array('data' => t('Page'), 'field' => 'a.path'),
    array('data' => t('User'), 'field' => 'u.name'),
    array('data' => t('Operations'))
  );

19
  $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
20 21 22 23 24
  $query->join('users', 'u', 'a.uid = u.uid');
  $query
    ->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
    ->fields('u', array('name'))
    ->limit(30)
25
    ->orderByHeader($header);
26 27

  $result = $query->execute();
28
  $rows = array();
29
  foreach ($result as $log) {
30
    $rows[] = array(
31
      array('data' => format_date($log->timestamp, 'short'), 'class' => array('nowrap')),
32
      _statistics_format_item($log->title, $log->path),
33
      theme('username', array('account' => $log)),
34
      l(t('details'), "admin/reports/access/$log->aid"));
35 36
  }

37
  $build['statistics_table'] = array(
38 39
    '#theme' => 'table',
    '#header' => $header,
40
    '#rows' => $rows,
41
    '#empty' => t('No statistics available.'),
42 43 44
  );
  $build['statistics_pager'] = array('#theme' => 'pager');
  return $build;
45 46 47 48 49 50 51 52 53 54 55 56 57
}

/**
 * Menu callback; presents the "top pages" page.
 */
function statistics_top_pages() {
  $header = array(
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
    array('data' => t('Page'), 'field' => 'path'),
    array('data' => t('Average page generation time'), 'field' => 'average_time'),
    array('data' => t('Total page generation time'), 'field' => 'total_time')
  );

58
  $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
59 60 61 62 63
  $query->addExpression('COUNT(path)', 'hits');
  // MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list
  $query->addExpression('MAX(title)', 'title');
  $query->addExpression('AVG(timer)', 'average_time');
  $query->addExpression('SUM(timer)', 'total_time');
64

65
  $query
66
    ->fields('a', array('path'))
67 68
    ->groupBy('path')
    ->limit(30)
69
    ->orderByHeader($header);
70

71
  $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
72 73 74 75
  $count_query->addExpression('COUNT(DISTINCT path)');
  $query->setCountQuery($count_query);

  $result = $query->execute();
76
  $rows = array();
77
  foreach ($result as $page) {
78 79 80
    $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)));
  }

81
  drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
82
  $build['statistics_top_pages_table'] = array(
83 84
    '#theme' => 'table',
    '#header' => $header,
85
    '#rows' => $rows,
86
    '#empty' => t('No statistics available.'),
87 88 89
  );
  $build['statistics_top_pages_pager'] = array('#theme' => 'pager');
  return $build;
90 91 92 93 94 95 96 97 98 99 100
}

/**
 * Menu callback; presents the "top visitors" page.
 */
function statistics_top_visitors() {

  $header = array(
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
    array('data' => t('Visitor'), 'field' => 'u.name'),
    array('data' => t('Total page generation time'), 'field' => 'total'),
101
    array('data' => user_access('block IP addresses') ? t('Operations') : '', 'colspan' => 2),
102
  );
103
  $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
104 105 106 107 108 109 110 111 112 113 114 115 116 117
  $query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip');
  $query->leftJoin('users', 'u', 'a.uid = u.uid');

  $query->addExpression('COUNT(a.uid)', 'hits');
  $query->addExpression('SUM(a.timer)', 'total');
  $query
    ->fields('a', array('uid', 'hostname'))
    ->fields('u', array('name'))
    ->fields('bl', array('iid'))
    ->groupBy('a.hostname')
    ->groupBy('a.uid')
    ->groupBy('u.name')
    ->groupBy('bl.iid')
    ->limit(30)
118
    ->orderByHeader($header);
119

120 121 122 123
  $uniques_query = db_select('accesslog')->distinct();
  $uniques_query->fields('accesslog', array('uid', 'hostname'));
  $count_query = db_select($uniques_query);
  $count_query->addExpression('COUNT(*)');
124
  $query->setCountQuery($count_query);
125

126
  $result = $query->execute();
127
  $rows = array();
128
  $destination = drupal_get_destination();
129
  foreach ($result as $account) {
130
    $ban_link = $account->iid ? l(t('unblock IP address'), "admin/config/people/ip-blocking/delete/$account->iid", array('query' => $destination)) : l(t('block IP address'), "admin/config/people/ip-blocking/$account->hostname", array('query' => $destination));
131
    $rows[] = array($account->hits, ($account->uid ? theme('username', array('account' => $account)) : $account->hostname), format_interval(round($account->total / 1000)), (user_access('block IP addresses') && !$account->uid) ? $ban_link : '');
132 133
  }

134
  drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
135
  $build['statistics_top_visitors_table'] = array(
136 137
    '#theme' => 'table',
    '#header' => $header,
138
    '#rows' => $rows,
139
    '#empty' => t('No statistics available.'),
140 141 142
  );
  $build['statistics_top_visitors_pager'] = array('#theme' => 'pager');
  return $build;
143 144 145 146 147 148
}

/**
 * Menu callback; presents the "referrer" page.
 */
function statistics_top_referrers() {
149
  drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
150 151 152 153 154 155

  $header = array(
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
    array('data' => t('Url'), 'field' => 'url'),
    array('data' => t('Last visit'), 'field' => 'last'),
  );
156 157 158 159 160 161
  $query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');

  $query->addExpression('COUNT(url)', 'hits');
  $query->addExpression('MAX(timestamp)', 'last');
  $query
    ->fields('a', array('url'))
162
    ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
163 164 165
    ->condition('url', '', '<>')
    ->groupBy('url')
    ->limit(30)
166
    ->orderByHeader($header);
167

168
  $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
169 170
  $count_query->addExpression('COUNT(DISTINCT url)');
  $count_query
171
    ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
172 173 174 175
    ->condition('url', '', '<>');
  $query->setCountQuery($count_query);

  $result = $query->execute();
176
  $rows = array();
177
  foreach ($result as $referrer) {
178
    $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(REQUEST_TIME - $referrer->last))));
179 180
  }

181
  $build['statistics_top_referrers_table'] = array(
182 183
    '#theme' => 'table',
    '#header' => $header,
184
    '#rows' => $rows,
185
    '#empty' => t('No statistics available.'),
186 187 188
  );
  $build['statistics_top_referrers_pager'] = array('#theme' => 'pager');
  return $build;
189 190 191 192 193 194
}

/**
 * Menu callback; Displays recent page accesses.
 */
function statistics_access_log($aid) {
195 196
  $access = db_query('SELECT a.*, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE aid = :aid', array(':aid' => $aid))->fetch();
  if ($access) {
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    $rows[] = array(
      array('data' => t('URL'), 'header' => TRUE),
      l(url($access->path, array('absolute' => TRUE)), $access->path)
    );
    // It is safe to avoid filtering $access->title through check_plain because
    // it comes from drupal_get_title().
    $rows[] = array(
      array('data' => t('Title'), 'header' => TRUE),
      $access->title
    );
    $rows[] = array(
      array('data' => t('Referrer'), 'header' => TRUE),
      ($access->url ? l($access->url, $access->url) : '')
    );
    $rows[] = array(
      array('data' => t('Date'), 'header' => TRUE),
213
      format_date($access->timestamp, 'long')
214 215 216
    );
    $rows[] = array(
      array('data' => t('User'), 'header' => TRUE),
217
      theme('username', array('account' => $access))
218 219 220 221 222 223
    );
    $rows[] = array(
      array('data' => t('Hostname'), 'header' => TRUE),
      check_plain($access->hostname)
    );

224
    $build['statistics_table'] = array(
225
      '#theme' => 'table',
226 227 228
      '#rows' => $rows,
    );
    return $build;
229 230 231 232 233 234 235 236 237 238
  }
  else {
    drupal_not_found();
  }
}

/**
 * Form builder; Configure access logging.
 *
 * @ingroup forms
239
 * @see system_settings_form()
240
 */
241
function statistics_settings_form() {
242
  // Access log settings.
243 244
  $form['access'] = array(
    '#type' => 'fieldset',
245 246
    '#title' => t('Access log settings'),
  );
247
  $form['access']['statistics_enable_access_log'] = array(
248
    '#type' => 'checkbox',
249
    '#title' => t('Enable access log'),
250
    '#default_value' => variable_get('statistics_enable_access_log', 0),
251 252
    '#description' => t('Log each page access. Required for referrer statistics.'),
  );
253 254 255
  $form['access']['statistics_flush_accesslog_timer'] = array(
    '#type' => 'select',
    '#title' => t('Discard access logs older than'),
256
    '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
257
    '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
258 259
    '#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'))),
  );
260

261
  // Content counter settings.
262 263
  $form['content'] = array(
    '#type' => 'fieldset',
264 265
    '#title' => t('Content viewing counter settings'),
  );
266
  $form['content']['statistics_count_content_views'] = array(
267
    '#type' => 'checkbox',
268
    '#title' => t('Count content views'),
269
    '#default_value' => variable_get('statistics_count_content_views', 0),
270 271
    '#description' => t('Increment a counter each time content is viewed.'),
  );
272

273
  return system_settings_form($form);
274
}