annotate modules/statistics/statistics.admin.inc @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children
rev   line source
webmaster@1 1 <?php
webmaster@1 2 // $Id: statistics.admin.inc,v 1.6 2008/01/08 10:35:42 goba Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * Admin page callbacks for the statistics module.
webmaster@1 7 */
webmaster@1 8
webmaster@1 9 /**
webmaster@1 10 * Menu callback; presents the "recent hits" page.
webmaster@1 11 */
webmaster@1 12 function statistics_recent_hits() {
webmaster@1 13 $header = array(
webmaster@1 14 array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
webmaster@1 15 array('data' => t('Page'), 'field' => 'a.path'),
webmaster@1 16 array('data' => t('User'), 'field' => 'u.name'),
webmaster@1 17 array('data' => t('Operations'))
webmaster@1 18 );
webmaster@1 19
webmaster@1 20 $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);
webmaster@1 21
webmaster@1 22 $result = pager_query($sql, 30);
webmaster@1 23 $rows = array();
webmaster@1 24 while ($log = db_fetch_object($result)) {
webmaster@1 25 $rows[] = array(
webmaster@1 26 array('data' => format_date($log->timestamp, 'small'), 'class' => 'nowrap'),
webmaster@1 27 _statistics_format_item($log->title, $log->path),
webmaster@1 28 theme('username', $log),
webmaster@1 29 l(t('details'), "admin/reports/access/$log->aid"));
webmaster@1 30 }
webmaster@1 31
webmaster@1 32 if (empty($rows)) {
webmaster@1 33 $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
webmaster@1 34 }
webmaster@1 35
webmaster@1 36 $output = theme('table', $header, $rows);
webmaster@1 37 $output .= theme('pager', NULL, 30, 0);
webmaster@1 38 return $output;
webmaster@1 39 }
webmaster@1 40
webmaster@1 41 /**
webmaster@1 42 * Menu callback; presents the "top pages" page.
webmaster@1 43 */
webmaster@1 44 function statistics_top_pages() {
webmaster@1 45 // MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list
webmaster@1 46 $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";
webmaster@1 47 $sql_cnt = "SELECT COUNT(DISTINCT(path)) FROM {accesslog}";
webmaster@1 48
webmaster@1 49 $header = array(
webmaster@1 50 array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
webmaster@1 51 array('data' => t('Page'), 'field' => 'path'),
webmaster@1 52 array('data' => t('Average page generation time'), 'field' => 'average_time'),
webmaster@1 53 array('data' => t('Total page generation time'), 'field' => 'total_time')
webmaster@1 54 );
webmaster@1 55 $sql .= tablesort_sql($header);
webmaster@1 56 $result = pager_query($sql, 30, 0, $sql_cnt);
webmaster@1 57
webmaster@1 58 $rows = array();
webmaster@1 59 while ($page = db_fetch_object($result)) {
webmaster@1 60 $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)));
webmaster@1 61 }
webmaster@1 62
webmaster@1 63 if (empty($rows)) {
webmaster@1 64 $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
webmaster@1 65 }
webmaster@1 66
webmaster@1 67 drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
webmaster@1 68 $output = theme('table', $header, $rows);
webmaster@1 69 $output .= theme('pager', NULL, 30, 0);
webmaster@1 70 return $output;
webmaster@1 71 }
webmaster@1 72
webmaster@1 73 /**
webmaster@1 74 * Menu callback; presents the "top visitors" page.
webmaster@1 75 */
webmaster@1 76 function statistics_top_visitors() {
webmaster@1 77
webmaster@1 78 $header = array(
webmaster@1 79 array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
webmaster@1 80 array('data' => t('Visitor'), 'field' => 'u.name'),
webmaster@1 81 array('data' => t('Total page generation time'), 'field' => 'total'),
webmaster@1 82 array('data' => t('Operations'))
webmaster@1 83 );
webmaster@1 84
webmaster@1 85 $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, ac.aid FROM {accesslog} a LEFT JOIN {access} ac ON ac.type = 'host' AND LOWER(a.hostname) LIKE (ac.mask) LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.hostname, a.uid, u.name, ac.aid". tablesort_sql($header);
webmaster@1 86 $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(uid, hostname))) FROM {accesslog}";
webmaster@1 87 $result = pager_query($sql, 30, 0, $sql_cnt);
webmaster@1 88
webmaster@1 89 $rows = array();
webmaster@1 90 while ($account = db_fetch_object($result)) {
webmaster@1 91 $qs = drupal_get_destination();
webmaster@1 92 $ban_link = $account->aid ? l(t('unban'), "admin/user/rules/delete/$account->aid", array('query' => $qs)) : l(t('ban'), "admin/user/rules/add/$account->hostname/host", array('query' => $qs));
webmaster@1 93 $rows[] = array($account->hits, ($account->uid ? theme('username', $account) : $account->hostname), format_interval(round($account->total / 1000)), $ban_link);
webmaster@1 94 }
webmaster@1 95
webmaster@1 96 if (empty($rows)) {
webmaster@1 97 $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4));
webmaster@1 98 }
webmaster@1 99
webmaster@1 100 drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
webmaster@1 101 $output = theme('table', $header, $rows);
webmaster@1 102 $output .= theme('pager', NULL, 30, 0);
webmaster@1 103 return $output;
webmaster@1 104 }
webmaster@1 105
webmaster@1 106 /**
webmaster@1 107 * Menu callback; presents the "referrer" page.
webmaster@1 108 */
webmaster@1 109 function statistics_top_referrers() {
webmaster@1 110 $query = "SELECT url, COUNT(url) AS hits, MAX(timestamp) AS last FROM {accesslog} WHERE url NOT LIKE '%%%s%%' AND url <> '' GROUP BY url";
webmaster@1 111 $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%%%s%%'";
webmaster@1 112 drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
webmaster@1 113
webmaster@1 114 $header = array(
webmaster@1 115 array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
webmaster@1 116 array('data' => t('Url'), 'field' => 'url'),
webmaster@1 117 array('data' => t('Last visit'), 'field' => 'last'),
webmaster@1 118 );
webmaster@1 119
webmaster@1 120 $query .= tablesort_sql($header);
webmaster@1 121 $result = pager_query($query, 30, 0, $query_cnt, $_SERVER['HTTP_HOST']);
webmaster@1 122
webmaster@1 123 $rows = array();
webmaster@1 124 while ($referrer = db_fetch_object($result)) {
webmaster@1 125 $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(time() - $referrer->last))));
webmaster@1 126 }
webmaster@1 127
webmaster@1 128 if (empty($rows)) {
webmaster@1 129 $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 3));
webmaster@1 130 }
webmaster@1 131
webmaster@1 132 $output = theme('table', $header, $rows);
webmaster@1 133 $output .= theme('pager', NULL, 30, 0);
webmaster@1 134 return $output;
webmaster@1 135 }
webmaster@1 136
webmaster@1 137 /**
webmaster@1 138 * Menu callback; Displays recent page accesses.
webmaster@1 139 */
webmaster@1 140 function statistics_access_log($aid) {
webmaster@1 141 $result = db_query('SELECT a.*, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE aid = %d', $aid);
webmaster@1 142 if ($access = db_fetch_object($result)) {
webmaster@1 143 $rows[] = array(
webmaster@1 144 array('data' => t('URL'), 'header' => TRUE),
webmaster@1 145 l(url($access->path, array('absolute' => TRUE)), $access->path)
webmaster@1 146 );
webmaster@1 147 // It is safe to avoid filtering $access->title through check_plain because
webmaster@1 148 // it comes from drupal_get_title().
webmaster@1 149 $rows[] = array(
webmaster@1 150 array('data' => t('Title'), 'header' => TRUE),
webmaster@1 151 $access->title
webmaster@1 152 );
webmaster@1 153 $rows[] = array(
webmaster@1 154 array('data' => t('Referrer'), 'header' => TRUE),
webmaster@1 155 ($access->url ? l($access->url, $access->url) : '')
webmaster@1 156 );
webmaster@1 157 $rows[] = array(
webmaster@1 158 array('data' => t('Date'), 'header' => TRUE),
webmaster@1 159 format_date($access->timestamp, 'large')
webmaster@1 160 );
webmaster@1 161 $rows[] = array(
webmaster@1 162 array('data' => t('User'), 'header' => TRUE),
webmaster@1 163 theme('username', $access)
webmaster@1 164 );
webmaster@1 165 $rows[] = array(
webmaster@1 166 array('data' => t('Hostname'), 'header' => TRUE),
webmaster@1 167 check_plain($access->hostname)
webmaster@1 168 );
webmaster@1 169
webmaster@1 170 return theme('table', array(), $rows);
webmaster@1 171 }
webmaster@1 172 else {
webmaster@1 173 drupal_not_found();
webmaster@1 174 }
webmaster@1 175 }
webmaster@1 176
webmaster@1 177 /**
webmaster@1 178 * Form builder; Configure access logging.
webmaster@1 179 *
webmaster@1 180 * @ingroup forms
webmaster@1 181 * @see system_settings_form()
webmaster@1 182 */
webmaster@1 183 function statistics_access_logging_settings() {
webmaster@1 184 // Access log settings:
webmaster@1 185 $options = array('1' => t('Enabled'), '0' => t('Disabled'));
webmaster@1 186 $form['access'] = array(
webmaster@1 187 '#type' => 'fieldset',
webmaster@1 188 '#title' => t('Access log settings'));
webmaster@1 189 $form['access']['statistics_enable_access_log'] = array(
webmaster@1 190 '#type' => 'radios',
webmaster@1 191 '#title' => t('Enable access log'),
webmaster@1 192 '#default_value' => variable_get('statistics_enable_access_log', 0),
webmaster@1 193 '#options' => $options,
webmaster@1 194 '#description' => t('Log each page access. Required for referrer statistics.'));
webmaster@1 195 $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
webmaster@1 196 $form['access']['statistics_flush_accesslog_timer'] = array(
webmaster@1 197 '#type' => 'select',
webmaster@1 198 '#title' => t('Discard access logs older than'),
webmaster@1 199 '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
webmaster@1 200 '#options' => $period,
webmaster@1 201 '#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'))));
webmaster@1 202
webmaster@1 203 // count content views settings
webmaster@1 204 $form['content'] = array(
webmaster@1 205 '#type' => 'fieldset',
webmaster@1 206 '#title' => t('Content viewing counter settings'));
webmaster@1 207 $form['content']['statistics_count_content_views'] = array(
webmaster@1 208 '#type' => 'radios',
webmaster@1 209 '#title' => t('Count content views'),
webmaster@1 210 '#default_value' => variable_get('statistics_count_content_views', 0),
webmaster@1 211 '#options' => $options,
webmaster@1 212 '#description' => t('Increment a counter each time content is viewed.'));
webmaster@1 213
webmaster@1 214 return system_settings_form($form);
webmaster@1 215 }