annotate modules/statistics/statistics.module @ 20:e3d20ebd63d1 tip

Added tag 6.9 for changeset 3edae6ecd6c6
author Franck Deroche <franck@defr.org>
date Thu, 15 Jan 2009 10:16:10 +0100
parents c1f4ac30525a
children
rev   line source
webmaster@1 1 <?php
webmaster@1 2 // $Id: statistics.module,v 1.272 2008/01/04 09:31:48 goba Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * Logs access statistics for your site.
webmaster@1 7 */
webmaster@1 8
webmaster@1 9 /**
webmaster@1 10 * Implementation of hook_help().
webmaster@1 11 */
webmaster@1 12 function statistics_help($path, $arg) {
webmaster@1 13 switch ($path) {
webmaster@1 14 case 'admin/help#statistics':
webmaster@1 15 $output = '<p>'. t('The statistics module keeps track of numerous site usage statistics, including the number of times, and from where, each of your posts is viewed. These statistics are useful in determining how users are interacting with each other and with your site, and are required for the display of some Drupal blocks.') .'</p>';
webmaster@1 16 $output .= '<p>'. t('The statistics module provides:') .'</p>';
webmaster@1 17 $output .= '<ul><li>'. t('a counter for each post on your site that increments each time the post is viewed. (Enable <em>Count content views</em> on the <a href="@accesslog">access log settings page</a>, and determine if the post access counters should be visible to any user roles on the <a href="@permissions">permissions page</a>.)', array('@accesslog' => url('admin/reports/settings'), '@permissions' => url('admin/user/permissions'))) .'</li>';
webmaster@1 18 $output .= '<li>'. t('a <a href="@recent-hits">recent hits</a> log that displays information about the latest activity on your site, including the URL and title of the page accessed, the user name (if available) and IP address of the accessing party.', array('@recent-hits' => url('admin/reports/hits'))) .'</li>';
webmaster@1 19 $output .= '<li>'. t('a <a href="@top-referrers">top referrers</a> log that displays the referring parties for your site visits (where your visitors came from).', array('@top-referrers' => url('admin/reports/referrers'))) .'</li>';
webmaster@1 20 $output .= '<li>'. t('a <a href="@top-pages">top pages</a> log that displays site content in descending order by number of views.', array('@top-pages' => url('admin/reports/pages'))) .'</li>';
webmaster@1 21 $output .= '<li>'. t('a <a href="@top-visitors">top visitors</a> log that displays the most active users on your site.', array('@top-visitors' => url('admin/reports/visitors'))) .'</li>';
webmaster@1 22 $output .= '<li>'. t('a <em>Popular content</em> block that displays the day\'s most viewed content, the all-time most viewed content, and the last content viewed. (Enable the <em>Popular content</em> block on the <a href="@blocks">blocks administration page</a>.)', array('@blocks' => url('admin/build/block'))) .'</li></ul>';
webmaster@1 23 $output .= '<p>'. t('Configuring the statistics module') .'</p>';
webmaster@1 24 $output .= '<ul><li>'. t('When the <em>Enable access log</em> setting on the <a href="@accesslog">access log settings page</a> is enabled, data about every page accessed (including the remote host\'s IP address, referrer, node accessed, and user name) is stored in the access log. The access log must be enabled for the <a href="@recent-hits">recent hits</a>, <a href="@top-referrers">top referrers</a>, <a href="@top-pages">top pages</a>, and <a href="@top-visitors">top visitors</a> log pages to function. Enabling the access log adds one additional database call per page displayed by Drupal.', array('@accesslog' => url('admin/reports/settings'), '@recent-hits' => url('admin/reports/hits'), '@top-referrers' => url('admin/reports/referrers'), '@top-pages' => url('admin/reports/pages'), '@top-visitors' => url('admin/reports/visitors'))) .'</li>';
webmaster@1 25 $output .= '<li>'. t('The <em>Discard access logs older than</em> setting on the <a href="@accesslog">access log settings page</a> specifies the length of time entries are retained in the access log before they are deleted. Automatic access log entry deletion requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@accesslog' => url('admin/reports/settings'), '@cron' => url('admin/reports/status'))) .'</li>';
webmaster@1 26 $output .= '<li>'. t('The <em>Count content views</em> setting on the <a href="@accesslog">access log settings page</a> enables a counter for each post on your site that increments each time the post is viewed. This option must be enabled to provide post-specific access counts. Enabling this option adds one additional database call per each post displayed by Drupal.', array('@accesslog' => url('admin/reports/settings'))) .'</li></ul>';
webmaster@1 27 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@statistics">Statistics module</a>.', array('@statistics' => 'http://drupal.org/handbook/modules/statistics/')) .'</p>';
webmaster@1 28 return $output;
webmaster@1 29 case 'admin/reports/settings':
webmaster@1 30 return '<p>'. t('Settings for the statistical information that Drupal will keep about the site. See <a href="@statistics">site statistics</a> for the actual information.', array('@statistics' => url('admin/reports/hits'))) .'</p>';
webmaster@1 31 case 'admin/reports/hits':
webmaster@1 32 return '<p>'. t("This page displays the site's most recent hits.") .'</p>';
webmaster@1 33 case 'admin/reports/referrers':
webmaster@1 34 return '<p>'. t('This page displays all external referrers, or external references to your website.') .'</p>';
webmaster@1 35 case 'admin/reports/visitors':
webmaster@1 36 return '<p>'. t("When you ban a visitor, you prevent the visitor's IP address from accessing your site. Unlike blocking a user, banning a visitor works even for anonymous users. This is most commonly used to block resource-intensive bots or web crawlers.") .'</p>';
webmaster@1 37 }
webmaster@1 38 }
webmaster@1 39
webmaster@1 40 /**
webmaster@1 41 * Implementation of hook_exit().
webmaster@1 42 *
webmaster@1 43 * This is where statistics are gathered on page accesses.
webmaster@1 44 */
webmaster@1 45 function statistics_exit() {
webmaster@1 46 global $user, $recent_activity;
webmaster@1 47
webmaster@1 48 drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
webmaster@1 49
webmaster@1 50 if (variable_get('statistics_count_content_views', 0)) {
webmaster@1 51 // We are counting content views.
webmaster@1 52 if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') {
webmaster@1 53 // A node has been viewed, so update the node's counters.
webmaster@1 54 db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), arg(1));
webmaster@1 55 // If we affected 0 rows, this is the first time viewing the node.
webmaster@1 56 if (!db_affected_rows()) {
webmaster@1 57 // We must create a new row to store counters for the new node.
webmaster@1 58 db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES (%d, 1, 1, %d)', arg(1), time());
webmaster@1 59 }
webmaster@1 60 }
webmaster@1 61 }
webmaster@1 62 if ((variable_get('statistics_enable_access_log', 0)) && (module_invoke('throttle', 'status') == 0)) {
webmaster@1 63 // Log this page access.
webmaster@1 64 db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) values('%s', '%s', '%s', '%s', %d, '%s', %d, %d)", strip_tags(drupal_get_title()), $_GET['q'], referer_uri(), ip_address(), $user->uid, session_id(), timer_read('page'), time());
webmaster@1 65 }
webmaster@1 66 }
webmaster@1 67
webmaster@1 68 /**
webmaster@1 69 * Implementation of hook_perm().
webmaster@1 70 */
webmaster@1 71 function statistics_perm() {
webmaster@1 72 return array('access statistics', 'view post access counter');
webmaster@1 73 }
webmaster@1 74
webmaster@1 75 /**
webmaster@1 76 * Implementation of hook_link().
webmaster@1 77 */
webmaster@1 78 function statistics_link($type, $node = NULL, $teaser = FALSE) {
webmaster@1 79 global $id;
webmaster@1 80 $links = array();
webmaster@1 81
webmaster@1 82 if ($type != 'comment' && user_access('view post access counter')) {
webmaster@1 83 $statistics = statistics_get($node->nid);
webmaster@1 84 if ($statistics) {
webmaster@1 85 $links['statistics_counter']['title'] = format_plural($statistics['totalcount'], '1 read', '@count reads');
webmaster@1 86 }
webmaster@1 87 }
webmaster@1 88
webmaster@1 89 return $links;
webmaster@1 90 }
webmaster@1 91
webmaster@1 92 /**
webmaster@1 93 * Implementation of hook_menu().
webmaster@1 94 */
webmaster@1 95 function statistics_menu() {
webmaster@1 96 $items['admin/reports/hits'] = array(
webmaster@1 97 'title' => 'Recent hits',
webmaster@1 98 'description' => 'View pages that have recently been visited.',
webmaster@1 99 'page callback' => 'statistics_recent_hits',
webmaster@1 100 'access arguments' => array('access statistics'),
webmaster@1 101 'file' => 'statistics.admin.inc',
webmaster@1 102 );
webmaster@1 103 $items['admin/reports/pages'] = array(
webmaster@1 104 'title' => 'Top pages',
webmaster@1 105 'description' => 'View pages that have been hit frequently.',
webmaster@1 106 'page callback' => 'statistics_top_pages',
webmaster@1 107 'access arguments' => array('access statistics'),
webmaster@1 108 'weight' => 1,
webmaster@1 109 'file' => 'statistics.admin.inc',
webmaster@1 110 );
webmaster@1 111 $items['admin/reports/visitors'] = array(
webmaster@1 112 'title' => 'Top visitors',
webmaster@1 113 'description' => 'View visitors that hit many pages.',
webmaster@1 114 'page callback' => 'statistics_top_visitors',
webmaster@1 115 'access arguments' => array('access statistics'),
webmaster@1 116 'weight' => 2,
webmaster@1 117 'file' => 'statistics.admin.inc',
webmaster@1 118 );
webmaster@1 119 $items['admin/reports/referrers'] = array(
webmaster@1 120 'title' => 'Top referrers',
webmaster@1 121 'description' => 'View top referrers.',
webmaster@1 122 'page callback' => 'statistics_top_referrers',
webmaster@1 123 'access arguments' => array('access statistics'),
webmaster@1 124 'file' => 'statistics.admin.inc',
webmaster@1 125 );
webmaster@1 126 $items['admin/reports/access/%'] = array(
webmaster@1 127 'title' => 'Details',
webmaster@1 128 'description' => 'View access log.',
webmaster@1 129 'page callback' => 'statistics_access_log',
webmaster@1 130 'page arguments' => array(3),
webmaster@1 131 'access arguments' => array('access statistics'),
webmaster@1 132 'type' => MENU_CALLBACK,
webmaster@1 133 'file' => 'statistics.admin.inc',
webmaster@1 134 );
webmaster@1 135 $items['admin/reports/settings'] = array(
webmaster@1 136 'title' => 'Access log settings',
webmaster@1 137 'description' => 'Control details about what and how your site logs.',
webmaster@1 138 'page callback' => 'drupal_get_form',
webmaster@1 139 'page arguments' => array('statistics_access_logging_settings'),
webmaster@1 140 'access arguments' => array('administer site configuration'),
webmaster@1 141 'type' => MENU_NORMAL_ITEM,
webmaster@1 142 'weight' => 3,
webmaster@1 143 'file' => 'statistics.admin.inc',
webmaster@1 144 );
webmaster@1 145 $items['user/%user/track/navigation'] = array(
webmaster@1 146 'title' => 'Track page visits',
webmaster@1 147 'page callback' => 'statistics_user_tracker',
webmaster@1 148 'access callback' => 'user_access',
webmaster@1 149 'access arguments' => array('access statistics'),
webmaster@1 150 'type' => MENU_LOCAL_TASK,
webmaster@1 151 'weight' => 2,
webmaster@1 152 'file' => 'statistics.pages.inc',
webmaster@1 153 );
webmaster@1 154 $items['node/%node/track'] = array(
webmaster@1 155 'title' => 'Track',
webmaster@1 156 'page callback' => 'statistics_node_tracker',
webmaster@1 157 'access callback' => 'user_access',
webmaster@1 158 'access arguments' => array('access statistics'),
webmaster@1 159 'type' => MENU_LOCAL_TASK,
webmaster@1 160 'weight' => 2,
webmaster@1 161 'file' => 'statistics.pages.inc',
webmaster@1 162 );
webmaster@1 163
webmaster@1 164 return $items;
webmaster@1 165 }
webmaster@1 166
webmaster@1 167 /**
webmaster@1 168 * Implementation of hook_user().
webmaster@1 169 */
webmaster@1 170 function statistics_user($op, &$edit, &$user) {
webmaster@1 171 if ($op == 'delete') {
webmaster@1 172 db_query('UPDATE {accesslog} SET uid = 0 WHERE uid = %d', $user->uid);
webmaster@1 173 }
webmaster@1 174 }
webmaster@1 175
webmaster@1 176 /**
webmaster@1 177 * Implementation of hook_cron().
webmaster@1 178 */
webmaster@1 179 function statistics_cron() {
webmaster@1 180 $statistics_timestamp = variable_get('statistics_day_timestamp', '');
webmaster@1 181
webmaster@1 182 if ((time() - $statistics_timestamp) >= 86400) {
webmaster@1 183 // Reset day counts.
webmaster@1 184 db_query('UPDATE {node_counter} SET daycount = 0');
webmaster@1 185 variable_set('statistics_day_timestamp', time());
webmaster@1 186 }
webmaster@1 187
webmaster@1 188 // Clean up expired access logs.
webmaster@1 189 db_query('DELETE FROM {accesslog} WHERE timestamp < %d', time() - variable_get('statistics_flush_accesslog_timer', 259200));
webmaster@1 190 }
webmaster@1 191
webmaster@1 192 /**
webmaster@1 193 * Returns all time or today top or last viewed node(s).
webmaster@1 194 *
webmaster@1 195 * @param $dbfield
webmaster@1 196 * one of
webmaster@1 197 * - 'totalcount': top viewed content of all time.
webmaster@1 198 * - 'daycount': top viewed content for today.
webmaster@1 199 * - 'timestamp': last viewed node.
webmaster@1 200 *
webmaster@1 201 * @param $dbrows
webmaster@1 202 * number of rows to be returned.
webmaster@1 203 *
webmaster@1 204 * @return
webmaster@1 205 * A query result containing n.nid, n.title, u.uid, u.name of the selected node(s)
webmaster@1 206 * or FALSE if the query could not be executed correctly.
webmaster@1 207 */
webmaster@1 208 function statistics_title_list($dbfield, $dbrows) {
webmaster@1 209 if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) {
webmaster@1 210 return db_query_range(db_rewrite_sql("SELECT n.nid, n.title, u.uid, u.name FROM {node} n INNER JOIN {node_counter} s ON n.nid = s.nid INNER JOIN {users} u ON n.uid = u.uid WHERE s.". $dbfield ." != 0 AND n.status = 1 ORDER BY s.". $dbfield ." DESC"), 0, $dbrows);
webmaster@1 211 }
webmaster@1 212 return FALSE;
webmaster@1 213 }
webmaster@1 214
webmaster@1 215
webmaster@1 216 /**
webmaster@1 217 * Retrieves a node's "view statistics".
webmaster@1 218 *
webmaster@1 219 * @param $nid
webmaster@1 220 * node ID
webmaster@1 221 *
webmaster@1 222 * @return
webmaster@1 223 * An array with three entries: [0]=totalcount, [1]=daycount, [2]=timestamp
webmaster@1 224 * - totalcount: count of the total number of times that node has been viewed.
webmaster@1 225 * - daycount: count of the total number of times that node has been viewed "today".
webmaster@1 226 * For the daycount to be reset, cron must be enabled.
webmaster@1 227 * - timestamp: timestamp of when that node was last viewed.
webmaster@1 228 */
webmaster@1 229 function statistics_get($nid) {
webmaster@1 230
webmaster@1 231 if ($nid > 0) {
webmaster@1 232 // Retrieve an array with both totalcount and daycount.
webmaster@1 233 $statistics = db_fetch_array(db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = %d', $nid));
webmaster@1 234 }
webmaster@1 235
webmaster@1 236 return $statistics;
webmaster@1 237 }
webmaster@1 238
webmaster@1 239 /**
webmaster@1 240 * Implementation of hook_block().
webmaster@1 241 */
webmaster@1 242 function statistics_block($op = 'list', $delta = 0, $edit = array()) {
webmaster@1 243 switch ($op) {
webmaster@1 244 case 'list':
webmaster@1 245 if (variable_get('statistics_count_content_views', 0)) {
webmaster@1 246 $blocks[0]['info'] = t('Popular content');
webmaster@1 247 // Too dynamic to cache.
webmaster@1 248 $blocks[0]['cache'] = BLOCK_NO_CACHE;
webmaster@1 249 return $blocks;
webmaster@1 250 }
webmaster@1 251 break;
webmaster@1 252
webmaster@1 253 case 'configure':
webmaster@1 254 // Popular content block settings
webmaster@1 255 $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
webmaster@1 256 $form['statistics_block_top_day_num'] = array('#type' => 'select', '#title' => t("Number of day's top views to display"), '#default_value' => variable_get('statistics_block_top_day_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "day" list.'));
webmaster@1 257 $form['statistics_block_top_all_num'] = array('#type' => 'select', '#title' => t('Number of all time views to display'), '#default_value' => variable_get('statistics_block_top_all_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "all time" list.'));
webmaster@1 258 $form['statistics_block_top_last_num'] = array('#type' => 'select', '#title' => t('Number of most recent views to display'), '#default_value' => variable_get('statistics_block_top_last_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "recently viewed" list.'));
webmaster@1 259 return $form;
webmaster@1 260
webmaster@1 261 case 'save':
webmaster@1 262 variable_set('statistics_block_top_day_num', $edit['statistics_block_top_day_num']);
webmaster@1 263 variable_set('statistics_block_top_all_num', $edit['statistics_block_top_all_num']);
webmaster@1 264 variable_set('statistics_block_top_last_num', $edit['statistics_block_top_last_num']);
webmaster@1 265 break;
webmaster@1 266
webmaster@1 267 case 'view':
webmaster@1 268 if (user_access('access content')) {
webmaster@1 269 $content = array();
webmaster@1 270
webmaster@1 271 $daytop = variable_get('statistics_block_top_day_num', 0);
webmaster@1 272 if ($daytop && ($result = statistics_title_list('daycount', $daytop)) && ($node_title_list = node_title_list($result, t("Today's:")))) {
webmaster@1 273 $content[] = $node_title_list;
webmaster@1 274 }
webmaster@1 275
webmaster@1 276 $alltimetop = variable_get('statistics_block_top_all_num', 0);
webmaster@1 277 if ($alltimetop && ($result = statistics_title_list('totalcount', $alltimetop)) && ($node_title_list = node_title_list($result, t('All time:')))) {
webmaster@1 278 $content[] = $node_title_list;
webmaster@1 279 }
webmaster@1 280
webmaster@1 281 $lasttop = variable_get('statistics_block_top_last_num', 0);
webmaster@1 282 if ($lasttop && ($result = statistics_title_list('timestamp', $lasttop)) && ($node_title_list = node_title_list($result, t('Last viewed:')))) {
webmaster@1 283 $content[] = $node_title_list;
webmaster@1 284 }
webmaster@1 285
webmaster@1 286 if (count($content)) {
webmaster@1 287 $block['content'] = implode('<br />', $content);
webmaster@1 288 $block['subject'] = t('Popular content');
webmaster@1 289 return $block;
webmaster@1 290 }
webmaster@1 291 }
webmaster@1 292 }
webmaster@1 293 }
webmaster@1 294
webmaster@1 295 /**
webmaster@1 296 * It is possible to adjust the width of columns generated by the
webmaster@1 297 * statistics module.
webmaster@1 298 */
webmaster@1 299 function _statistics_link($path, $width = 35) {
webmaster@1 300 $title = drupal_get_path_alias($path);
webmaster@1 301 $title = truncate_utf8($title, $width, FALSE, TRUE);
webmaster@1 302 return l($title, $path);
webmaster@1 303 }
webmaster@1 304
webmaster@1 305 function _statistics_format_item($title, $path) {
webmaster@1 306 $path = ($path ? $path : '/');
webmaster@1 307 $output = ($title ? "$title<br />" : '');
webmaster@1 308 $output .= _statistics_link($path);
webmaster@1 309 return $output;
webmaster@1 310 }
webmaster@1 311
webmaster@1 312 /**
webmaster@1 313 * Implementation of hook_nodeapi().
webmaster@1 314 */
webmaster@1 315 function statistics_nodeapi(&$node, $op, $arg = 0) {
webmaster@1 316 switch ($op) {
webmaster@1 317 case 'delete':
webmaster@1 318 // clean up statistics table when node is deleted
webmaster@1 319 db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
webmaster@1 320 }
webmaster@1 321 }