| 1 |
<?php
|
| 2 |
// $Id: statistics.admin.inc,v 1.34 2009/10/09 01:00:03 dries 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 |
*/
|
| 12 |
function statistics_recent_hits() {
|
| 13 |
$header = array(
|
| 14 |
array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
|
| 15 |
array('data' => t('Page'), 'field' => 'a.path'),
|
| 16 |
array('data' => t('User'), 'field' => 'u.name'),
|
| 17 |
array('data' => t('Operations'))
|
| 18 |
);
|
| 19 |
|
| 20 |
$query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
|
| 21 |
$query->join('users', 'u', 'a.uid = u.uid');
|
| 22 |
$query
|
| 23 |
->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
|
| 24 |
->fields('u', array('name'))
|
| 25 |
->limit(30)
|
| 26 |
->orderByHeader($header);
|
| 27 |
|
| 28 |
$result = $query->execute();
|
| 29 |
$rows = array();
|
| 30 |
foreach ($result as $log) {
|
| 31 |
$rows[] = array(
|
| 32 |
array('data' => format_date($log->timestamp, 'short'), 'class' => array('nowrap')),
|
| 33 |
_statistics_format_item($log->title, $log->path),
|
| 34 |
theme('username', array('account' => $log)),
|
| 35 |
l(t('details'), "admin/reports/access/$log->aid"));
|
| 36 |
}
|
| 37 |
|
| 38 |
if (empty($rows)) {
|
| 39 |
$rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
|
| 40 |
}
|
| 41 |
|
| 42 |
$build['statistics_table'] = array(
|
| 43 |
'#theme' => 'table',
|
| 44 |
'#header' => $header,
|
| 45 |
'#rows' => $rows,
|
| 46 |
);
|
| 47 |
$build['statistics_pager'] = array('#theme' => 'pager');
|
| 48 |
return $build;
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Menu callback; presents the "top pages" page.
|
| 53 |
*/
|
| 54 |
function statistics_top_pages() {
|
| 55 |
$header = array(
|
| 56 |
array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
|
| 57 |
array('data' => t('Page'), 'field' => 'path'),
|
| 58 |
array('data' => t('Average page generation time'), 'field' => 'average_time'),
|
| 59 |
array('data' => t('Total page generation time'), 'field' => 'total_time')
|
| 60 |
);
|
| 61 |
|
| 62 |
$query = db_select('accesslog', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
|
| 63 |
$query->addExpression('COUNT(path)', 'hits');
|
| 64 |
// MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list
|
| 65 |
$query->addExpression('MAX(title)', 'title');
|
| 66 |
$query->addExpression('AVG(timer)', 'average_time');
|
| 67 |
$query->addExpression('SUM(timer)', 'total_time');
|
| 68 |
|
| 69 |
$query
|
| 70 |
->fields('accesslog', array('path'))
|
| 71 |
->groupBy('path')
|
| 72 |
->limit(30)
|
| 73 |
->orderByHeader($header);
|
| 74 |
|
| 75 |
$count_query = db_select('accesslog', array('target' => 'slave'));
|
| 76 |
$count_query->addExpression('COUNT(DISTINCT path)');
|
| 77 |
$query->setCountQuery($count_query);
|
| 78 |
|
| 79 |
$result = $query->execute();
|
| 80 |
$rows = array();
|
| 81 |
foreach ($result as $page) {
|
| 82 |
$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)));
|
| 83 |
}
|
| 84 |
|
| 85 |
if (empty($rows)) {
|
| 86 |
$rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
|
| 87 |
}
|
| 88 |
|
| 89 |
drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
|
| 90 |
$build['statistics_top_pages_table'] = array(
|
| 91 |
'#theme' => 'table',
|
| 92 |
'#header' => $header,
|
| 93 |
'#rows' => $rows,
|
| 94 |
);
|
| 95 |
$build['statistics_top_pages_pager'] = array('#theme' => 'pager');
|
| 96 |
return $build;
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Menu callback; presents the "top visitors" page.
|
| 101 |
*/
|
| 102 |
function statistics_top_visitors() {
|
| 103 |
|
| 104 |
$header = array(
|
| 105 |
array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
|
| 106 |
array('data' => t('Visitor'), 'field' => 'u.name'),
|
| 107 |
array('data' => t('Total page generation time'), 'field' => 'total'),
|
| 108 |
array('data' => user_access('block IP addresses') ? t('Operations') : '', 'colspan' => 2),
|
| 109 |
);
|
| 110 |
$query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
|
| 111 |
$query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip');
|
| 112 |
$query->leftJoin('users', 'u', 'a.uid = u.uid');
|
| 113 |
|
| 114 |
$query->addExpression('COUNT(a.uid)', 'hits');
|
| 115 |
$query->addExpression('SUM(a.timer)', 'total');
|
| 116 |
$query
|
| 117 |
->fields('a', array('uid', 'hostname'))
|
| 118 |
->fields('u', array('name'))
|
| 119 |
->fields('bl', array('iid'))
|
| 120 |
->groupBy('a.hostname')
|
| 121 |
->groupBy('a.uid')
|
| 122 |
->groupBy('u.name')
|
| 123 |
->groupBy('bl.iid')
|
| 124 |
->limit(30)
|
| 125 |
->orderByHeader($header);
|
| 126 |
|
| 127 |
$count_query = db_select('accesslog');
|
| 128 |
$count_query->addExpression('COUNT(DISTINCT CONCAT(CAST(uid AS char), hostname))');
|
| 129 |
$query->setCountQuery($count_query);
|
| 130 |
|
| 131 |
$result = $query->execute();
|
| 132 |
$rows = array();
|
| 133 |
$destination = drupal_get_destination();
|
| 134 |
foreach ($result as $account) {
|
| 135 |
$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));
|
| 136 |
$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 : '');
|
| 137 |
}
|
| 138 |
|
| 139 |
if (empty($rows)) {
|
| 140 |
$rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
|
| 141 |
}
|
| 142 |
|
| 143 |
drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
|
| 144 |
$build['statistics_top_visitors_table'] = array(
|
| 145 |
'#theme' => 'table',
|
| 146 |
'#header' => $header,
|
| 147 |
'#rows' => $rows,
|
| 148 |
);
|
| 149 |
$build['statistics_top_visitors_pager'] = array('#theme' => 'pager');
|
| 150 |
return $build;
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Menu callback; presents the "referrer" page.
|
| 155 |
*/
|
| 156 |
function statistics_top_referrers() {
|
| 157 |
drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
|
| 158 |
|
| 159 |
$header = array(
|
| 160 |
array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
|
| 161 |
array('data' => t('Url'), 'field' => 'url'),
|
| 162 |
array('data' => t('Last visit'), 'field' => 'last'),
|
| 163 |
);
|
| 164 |
$query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');
|
| 165 |
|
| 166 |
$query->addExpression('COUNT(url)', 'hits');
|
| 167 |
$query->addExpression('MAX(timestamp)', 'last');
|
| 168 |
$query
|
| 169 |
->fields('a', array('url'))
|
| 170 |
->where('LOWER(url) NOT LIKE :host', array(':host' => '%' . $_SERVER['HTTP_HOST'] . '%'))
|
| 171 |
->condition('url', '', '<>')
|
| 172 |
->groupBy('url')
|
| 173 |
->limit(30)
|
| 174 |
->orderByHeader($header);
|
| 175 |
|
| 176 |
$count_query = db_select('accesslog', array('target' => 'slave'));
|
| 177 |
$count_query->addExpression('COUNT(DISTINCT url)');
|
| 178 |
$count_query
|
| 179 |
->where('LOWER(url) NOT LIKE :host', array(':host' => '%' . $_SERVER['HTTP_HOST'] . '%'))
|
| 180 |
->condition('url', '', '<>');
|
| 181 |
$query->setCountQuery($count_query);
|
| 182 |
|
| 183 |
$result = $query->execute();
|
| 184 |
$rows = array();
|
| 185 |
foreach ($result as $referrer) {
|
| 186 |
$rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(REQUEST_TIME - $referrer->last))));
|
| 187 |
}
|
| 188 |
|
| 189 |
if (empty($rows)) {
|
| 190 |
$rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 3));
|
| 191 |
}
|
| 192 |
|
| 193 |
$build['statistics_top_referrers_table'] = array(
|
| 194 |
'#theme' => 'table',
|
| 195 |
'#header' => $header,
|
| 196 |
'#rows' => $rows,
|
| 197 |
);
|
| 198 |
$build['statistics_top_referrers_pager'] = array('#theme' => 'pager');
|
| 199 |
return $build;
|
| 200 |
}
|
| 201 |
|
| 202 |
/**
|
| 203 |
* Menu callback; Displays recent page accesses.
|
| 204 |
*/
|
| 205 |
function statistics_access_log($aid) {
|
| 206 |
$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();
|
| 207 |
if ($access) {
|
| 208 |
$rows[] = array(
|
| 209 |
array('data' => t('URL'), 'header' => TRUE),
|
| 210 |
l(url($access->path, array('absolute' => TRUE)), $access->path)
|
| 211 |
);
|
| 212 |
// It is safe to avoid filtering $access->title through check_plain because
|
| 213 |
// it comes from drupal_get_title().
|
| 214 |
$rows[] = array(
|
| 215 |
array('data' => t('Title'), 'header' => TRUE),
|
| 216 |
$access->title
|
| 217 |
);
|
| 218 |
$rows[] = array(
|
| 219 |
array('data' => t('Referrer'), 'header' => TRUE),
|
| 220 |
($access->url ? l($access->url, $access->url) : '')
|
| 221 |
);
|
| 222 |
$rows[] = array(
|
| 223 |
array('data' => t('Date'), 'header' => TRUE),
|
| 224 |
format_date($access->timestamp, 'long')
|
| 225 |
);
|
| 226 |
$rows[] = array(
|
| 227 |
array('data' => t('User'), 'header' => TRUE),
|
| 228 |
theme('username', array('account' => $access))
|
| 229 |
);
|
| 230 |
$rows[] = array(
|
| 231 |
array('data' => t('Hostname'), 'header' => TRUE),
|
| 232 |
check_plain($access->hostname)
|
| 233 |
);
|
| 234 |
|
| 235 |
$build['statistics_table'] = array(
|
| 236 |
'#theme' => 'table',
|
| 237 |
'#rows' => $rows,
|
| 238 |
);
|
| 239 |
return $build;
|
| 240 |
}
|
| 241 |
else {
|
| 242 |
drupal_not_found();
|
| 243 |
}
|
| 244 |
}
|
| 245 |
|
| 246 |
/**
|
| 247 |
* Form builder; Configure access logging.
|
| 248 |
*
|
| 249 |
* @ingroup forms
|
| 250 |
* @see system_settings_form()
|
| 251 |
*/
|
| 252 |
function statistics_settings_form() {
|
| 253 |
// Access log settings.
|
| 254 |
$form['access'] = array(
|
| 255 |
'#type' => 'fieldset',
|
| 256 |
'#title' => t('Access log settings'),
|
| 257 |
);
|
| 258 |
$form['access']['statistics_enable_access_log'] = array(
|
| 259 |
'#type' => 'checkbox',
|
| 260 |
'#title' => t('Enable access log'),
|
| 261 |
'#default_value' => 0,
|
| 262 |
'#description' => t('Log each page access. Required for referrer statistics.'),
|
| 263 |
);
|
| 264 |
$form['access']['statistics_flush_accesslog_timer'] = array(
|
| 265 |
'#type' => 'select',
|
| 266 |
'#title' => t('Discard access logs older than'),
|
| 267 |
'#default_value' => 259200,
|
| 268 |
'#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
|
| 269 |
'#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'))),
|
| 270 |
);
|
| 271 |
|
| 272 |
// Content counter settings.
|
| 273 |
$form['content'] = array(
|
| 274 |
'#type' => 'fieldset',
|
| 275 |
'#title' => t('Content viewing counter settings'),
|
| 276 |
);
|
| 277 |
$form['content']['statistics_count_content_views'] = array(
|
| 278 |
'#type' => 'checkbox',
|
| 279 |
'#title' => t('Count content views'),
|
| 280 |
'#default_value' => 0,
|
| 281 |
'#description' => t('Increment a counter each time content is viewed.'),
|
| 282 |
);
|
| 283 |
|
| 284 |
return system_settings_form($form);
|
| 285 |
}
|