5 * Admin page callbacks for the Statistics module.
9 * Page callback: Displays the "recent hits" page.
11 * This displays the pages with recent hits in a given time interval that
12 * haven't been flushed yet. The flush interval is set on the statistics
13 * settings form, but is dependent on cron running.
16 * A render array containing information about the most recent hits.
18 function statistics_recent_hits() {
20 array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
21 array('data' => t('Page'), 'field' => 'a.path'),
22 array('data' => t('User'), 'field' => 'u.name'),
23 array('data' => t('Operations'))
26 $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
27 $query->join('users', 'u', 'a.uid = u.uid');
29 ->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
30 ->fields('u', array('name'))
32 ->orderByHeader($header);
34 $result = $query->execute();
36 foreach ($result as $log) {
38 array('data' => format_date($log->timestamp, 'short'), 'class' => array('nowrap')),
39 _statistics_format_item($log->title, $log->path),
40 theme('username', array('account' => $log)),
41 l(t('details'), "admin/reports/access/$log->aid"));
44 $build['statistics_table'] = array(
48 '#empty' => t('No statistics available.'),
50 $build['statistics_pager'] = array('#theme' => 'pager');
55 * Page callback: Displays statistics for the "top pages" (most accesses).
57 * This displays the pages with the most hits (the "top pages") within a given
58 * time period that haven't been flushed yet. The flush interval is set on the
59 * statistics settings form, but is dependent on cron running.
62 * A render array containing information about the top pages.
64 function statistics_top_pages() {
66 array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
67 array('data' => t('Page'), 'field' => 'path'),
68 array('data' => t('Average page generation time'), 'field' => 'average_time'),
69 array('data' => t('Total page generation time'), 'field' => 'total_time')
72 $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
73 $query->addExpression('COUNT(path)', 'hits');
74 // MAX(title) avoids having empty node titles which otherwise causes
75 // duplicates in the top pages list.
76 $query->addExpression('MAX(title)', 'title');
77 $query->addExpression('AVG(timer)', 'average_time');
78 $query->addExpression('SUM(timer)', 'total_time');
81 ->fields('a', array('path'))
84 ->orderByHeader($header);
86 $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
87 $count_query->addExpression('COUNT(DISTINCT path)');
88 $query->setCountQuery($count_query);
90 $result = $query->execute();
92 foreach ($result as $page) {
93 $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)));
96 drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
97 $build['statistics_top_pages_table'] = array(
101 '#empty' => t('No statistics available.'),
103 $build['statistics_top_pages_pager'] = array('#theme' => 'pager');
108 * Page callback: Displays the "top visitors" page.
110 * This displays the pages with the top number of visitors in a given time
111 * interval that haven't been flushed yet. The flush interval is set on the
112 * statistics settings form, but is dependent on cron running.
115 * A render array containing the top visitors information.
117 function statistics_top_visitors() {
120 array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
121 array('data' => t('Visitor'), 'field' => 'u.name'),
122 array('data' => t('Total page generation time'), 'field' => 'total'),
123 array('data' => user_access('block IP addresses') ? t('Operations') : '', 'colspan' => 2),
125 $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
126 $query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip');
127 $query->leftJoin('users', 'u', 'a.uid = u.uid');
129 $query->addExpression('COUNT(a.uid)', 'hits');
130 $query->addExpression('SUM(a.timer)', 'total');
132 ->fields('a', array('uid', 'hostname'))
133 ->fields('u', array('name'))
134 ->fields('bl', array('iid'))
135 ->groupBy('a.hostname')
140 ->orderByHeader($header)
141 ->orderBy('a.hostname');
143 $uniques_query = db_select('accesslog')->distinct();
144 $uniques_query->fields('accesslog', array('uid', 'hostname'));
145 $count_query = db_select($uniques_query);
146 $count_query->addExpression('COUNT(*)');
147 $query->setCountQuery($count_query);
149 $result = $query->execute();
151 $destination = drupal_get_destination();
152 foreach ($result as $account) {
153 $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));
154 $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 : '');
157 drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
158 $build['statistics_top_visitors_table'] = array(
160 '#header' => $header,
162 '#empty' => t('No statistics available.'),
164 $build['statistics_top_visitors_pager'] = array('#theme' => 'pager');
169 * Page callback: Displays the "top referrers" in the access logs.
171 * This displays the pages with the top referrers in a given time interval that
172 * haven't been flushed yet. The flush interval is set on the statistics
173 * settings form, but is dependent on cron running.
176 * A render array containing the top referrers information.
178 function statistics_top_referrers() {
179 drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
182 array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
183 array('data' => t('Url'), 'field' => 'url'),
184 array('data' => t('Last visit'), 'field' => 'last'),
186 $query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');
188 $query->addExpression('COUNT(url)', 'hits');
189 $query->addExpression('MAX(timestamp)', 'last');
191 ->fields('a', array('url'))
192 ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
193 ->condition('url', '', '<>')
196 ->orderByHeader($header);
198 $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
199 $count_query->addExpression('COUNT(DISTINCT url)');
201 ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
202 ->condition('url', '', '<>');
203 $query->setCountQuery($count_query);
205 $result = $query->execute();
207 foreach ($result as $referrer) {
208 $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(REQUEST_TIME - $referrer->last))));
211 $build['statistics_top_referrers_table'] = array(
213 '#header' => $header,
215 '#empty' => t('No statistics available.'),
217 $build['statistics_top_referrers_pager'] = array('#theme' => 'pager');
222 * Page callback: Gathers page access statistics suitable for rendering.
225 * The unique accesslog ID.
228 * A render array containing page access statistics. If information for the
229 * page was not found, drupal_not_found() is called.
231 function statistics_access_log($aid) {
232 $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();
235 array('data' => t('URL'), 'header' => TRUE),
236 l(url($access->path, array('absolute' => TRUE)), $access->path)
238 // It is safe to avoid filtering $access->title through check_plain because
239 // it comes from drupal_get_title().
241 array('data' => t('Title'), 'header' => TRUE),
245 array('data' => t('Referrer'), 'header' => TRUE),
246 ($access->url ? l($access->url, $access->url) : '')
249 array('data' => t('Date'), 'header' => TRUE),
250 format_date($access->timestamp, 'long')
253 array('data' => t('User'), 'header' => TRUE),
254 theme('username', array('account' => $access))
257 array('data' => t('Hostname'), 'header' => TRUE),
258 check_plain($access->hostname)
261 $build['statistics_table'] = array(
267 return MENU_NOT_FOUND;
271 * Form constructor for the statistics administration form.
274 * @see system_settings_form()
276 function statistics_settings_form() {
277 // Access log settings.
278 $form['access'] = array(
279 '#type' => 'fieldset',
280 '#title' => t('Access log settings'),
282 $form['access']['statistics_enable_access_log'] = array(
283 '#type' => 'checkbox',
284 '#title' => t('Enable access log'),
285 '#default_value' => variable_get('statistics_enable_access_log', 0),
286 '#description' => t('Log each page access. Required for referrer statistics.'),
288 $form['access']['statistics_flush_accesslog_timer'] = array(
290 '#title' => t('Discard access logs older than'),
291 '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
292 '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
293 '#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'))),
296 // Content counter settings.
297 $form['content'] = array(
298 '#type' => 'fieldset',
299 '#title' => t('Content viewing counter settings'),
301 $form['content']['statistics_count_content_views'] = array(
302 '#type' => 'checkbox',
303 '#title' => t('Count content views'),
304 '#default_value' => variable_get('statistics_count_content_views', 0),
305 '#description' => t('Increment a counter each time content is viewed.'),
307 $form['content']['statistics_count_content_views_ajax'] = array(
308 '#type' => 'checkbox',
309 '#title' => t('Use Ajax to increment the counter'),
310 '#default_value' => variable_get('statistics_count_content_views_ajax', 0),
311 '#description' => t('Perform the count asynchronously after page load rather than during page generation.'),
314 ':input[name="statistics_count_content_views"]' => array('checked' => FALSE),
319 return system_settings_form($form);