| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: statistics.module,v 1.272.2.2 2010/06/02 12:14:48 goba Exp $ 3 4 /** 5 * @file 6 * Logs access statistics for your site. 7 */ 8 9 /** 10 * Implementation of hook_help(). 11 */ 12 function statistics_help($path, $arg) { 13 switch ($path) { 14 case 'admin/help#statistics': 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>'; 16 $output .= '<p>'. t('The statistics module provides:') .'</p>'; 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>'; 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>'; 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>'; 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>'; 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>'; 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>'; 23 $output .= '<p>'. t('Configuring the statistics module') .'</p>'; 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>'; 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>'; 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>'; 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>'; 28 return $output; 29 case 'admin/reports/settings': 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>'; 31 case 'admin/reports/hits': 32 return '<p>'. t("This page displays the site's most recent hits.") .'</p>'; 33 case 'admin/reports/referrers': 34 return '<p>'. t('This page displays all external referrers, or external references to your website.') .'</p>'; 35 case 'admin/reports/visitors': 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>'; 37 } 38 } 39 40 /** 41 * Implementation of hook_exit(). 42 * 43 * This is where statistics are gathered on page accesses. 44 */ 45 function statistics_exit() { 46 global $user, $recent_activity; 47 48 drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH); 49 50 if (variable_get('statistics_count_content_views', 0)) { 51 // We are counting content views. 52 if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') { 53 // A node has been viewed, so update the node's counters. 54 db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), arg(1)); 55 // If we affected 0 rows, this is the first time viewing the node. 56 if (!db_affected_rows()) { 57 // We must create a new row to store counters for the new node. 58 db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES (%d, 1, 1, %d)', arg(1), time()); 59 } 60 } 61 } 62 if ((variable_get('statistics_enable_access_log', 0)) && (module_invoke('throttle', 'status') == 0)) { 63 // Log this page access. 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()); 65 } 66 } 67 68 /** 69 * Implementation of hook_perm(). 70 */ 71 function statistics_perm() { 72 return array('access statistics', 'view post access counter'); 73 } 74 75 /** 76 * Implementation of hook_link(). 77 */ 78 function statistics_link($type, $node = NULL, $teaser = FALSE) { 79 global $id; 80 $links = array(); 81 82 if ($type == 'node' && user_access('view post access counter')) { 83 $statistics = statistics_get($node->nid); 84 if ($statistics) { 85 $links['statistics_counter']['title'] = format_plural($statistics['totalcount'], '1 read', '@count reads'); 86 } 87 } 88 89 return $links; 90 } 91 92 /** 93 * Implementation of hook_menu(). 94 */ 95 function statistics_menu() { 96 $items['admin/reports/hits'] = array( 97 'title' => 'Recent hits', 98 'description' => 'View pages that have recently been visited.', 99 'page callback' => 'statistics_recent_hits', 100 'access arguments' => array('access statistics'), 101 'file' => 'statistics.admin.inc', 102 ); 103 $items['admin/reports/pages'] = array( 104 'title' => 'Top pages', 105 'description' => 'View pages that have been hit frequently.', 106 'page callback' => 'statistics_top_pages', 107 'access arguments' => array('access statistics'), 108 'weight' => 1, 109 'file' => 'statistics.admin.inc', 110 ); 111 $items['admin/reports/visitors'] = array( 112 'title' => 'Top visitors', 113 'description' => 'View visitors that hit many pages.', 114 'page callback' => 'statistics_top_visitors', 115 'access arguments' => array('access statistics'), 116 'weight' => 2, 117 'file' => 'statistics.admin.inc', 118 ); 119 $items['admin/reports/referrers'] = array( 120 'title' => 'Top referrers', 121 'description' => 'View top referrers.', 122 'page callback' => 'statistics_top_referrers', 123 'access arguments' => array('access statistics'), 124 'file' => 'statistics.admin.inc', 125 ); 126 $items['admin/reports/access/%'] = array( 127 'title' => 'Details', 128 'description' => 'View access log.', 129 'page callback' => 'statistics_access_log', 130 'page arguments' => array(3), 131 'access arguments' => array('access statistics'), 132 'type' => MENU_CALLBACK, 133 'file' => 'statistics.admin.inc', 134 ); 135 $items['admin/reports/settings'] = array( 136 'title' => 'Access log settings', 137 'description' => 'Control details about what and how your site logs.', 138 'page callback' => 'drupal_get_form', 139 'page arguments' => array('statistics_access_logging_settings'), 140 'access arguments' => array('administer site configuration'), 141 'type' => MENU_NORMAL_ITEM, 142 'weight' => 3, 143 'file' => 'statistics.admin.inc', 144 ); 145 $items['user/%user/track/navigation'] = array( 146 'title' => 'Track page visits', 147 'page callback' => 'statistics_user_tracker', 148 'access callback' => 'user_access', 149 'access arguments' => array('access statistics'), 150 'type' => MENU_LOCAL_TASK, 151 'weight' => 2, 152 'file' => 'statistics.pages.inc', 153 ); 154 $items['node/%node/track'] = array( 155 'title' => 'Track', 156 'page callback' => 'statistics_node_tracker', 157 'access callback' => 'user_access', 158 'access arguments' => array('access statistics'), 159 'type' => MENU_LOCAL_TASK, 160 'weight' => 2, 161 'file' => 'statistics.pages.inc', 162 ); 163 164 return $items; 165 } 166 167 /** 168 * Implementation of hook_user(). 169 */ 170 function statistics_user($op, &$edit, &$user) { 171 if ($op == 'delete') { 172 db_query('UPDATE {accesslog} SET uid = 0 WHERE uid = %d', $user->uid); 173 } 174 } 175 176 /** 177 * Implementation of hook_cron(). 178 */ 179 function statistics_cron() { 180 $statistics_timestamp = variable_get('statistics_day_timestamp', ''); 181 182 if ((time() - $statistics_timestamp) >= 86400) { 183 // Reset day counts. 184 db_query('UPDATE {node_counter} SET daycount = 0'); 185 variable_set('statistics_day_timestamp', time()); 186 } 187 188 // Clean up expired access logs. 189 if (variable_get('statistics_flush_accesslog_timer', 259200) > 0) { 190 db_query('DELETE FROM {accesslog} WHERE timestamp < %d', time() - variable_get('statistics_flush_accesslog_timer', 259200)); 191 } 192 } 193 194 /** 195 * Returns all time or today top or last viewed node(s). 196 * 197 * @param $dbfield 198 * one of 199 * - 'totalcount': top viewed content of all time. 200 * - 'daycount': top viewed content for today. 201 * - 'timestamp': last viewed node. 202 * 203 * @param $dbrows 204 * number of rows to be returned. 205 * 206 * @return 207 * A query result containing n.nid, n.title, u.uid, u.name of the selected node(s) 208 * or FALSE if the query could not be executed correctly. 209 */ 210 function statistics_title_list($dbfield, $dbrows) { 211 if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) { 212 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); 213 } 214 return FALSE; 215 } 216 217 218 /** 219 * Retrieves a node's "view statistics". 220 * 221 * @param $nid 222 * node ID 223 * 224 * @return 225 * An array with three entries: [0]=totalcount, [1]=daycount, [2]=timestamp 226 * - totalcount: count of the total number of times that node has been viewed. 227 * - daycount: count of the total number of times that node has been viewed "today". 228 * For the daycount to be reset, cron must be enabled. 229 * - timestamp: timestamp of when that node was last viewed. 230 */ 231 function statistics_get($nid) { 232 233 if ($nid > 0) { 234 // Retrieve an array with both totalcount and daycount. 235 $statistics = db_fetch_array(db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = %d', $nid)); 236 } 237 238 return $statistics; 239 } 240 241 /** 242 * Implementation of hook_block(). 243 */ 244 function statistics_block($op = 'list', $delta = 0, $edit = array()) { 245 switch ($op) { 246 case 'list': 247 if (variable_get('statistics_count_content_views', 0)) { 248 $blocks[0]['info'] = t('Popular content'); 249 // Too dynamic to cache. 250 $blocks[0]['cache'] = BLOCK_NO_CACHE; 251 return $blocks; 252 } 253 break; 254 255 case 'configure': 256 // Popular content block settings 257 $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40)); 258 $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.')); 259 $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.')); 260 $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.')); 261 return $form; 262 263 case 'save': 264 variable_set('statistics_block_top_day_num', $edit['statistics_block_top_day_num']); 265 variable_set('statistics_block_top_all_num', $edit['statistics_block_top_all_num']); 266 variable_set('statistics_block_top_last_num', $edit['statistics_block_top_last_num']); 267 break; 268 269 case 'view': 270 if (user_access('access content')) { 271 $content = array(); 272 273 $daytop = variable_get('statistics_block_top_day_num', 0); 274 if ($daytop && ($result = statistics_title_list('daycount', $daytop)) && ($node_title_list = node_title_list($result, t("Today's:")))) { 275 $content[] = $node_title_list; 276 } 277 278 $alltimetop = variable_get('statistics_block_top_all_num', 0); 279 if ($alltimetop && ($result = statistics_title_list('totalcount', $alltimetop)) && ($node_title_list = node_title_list($result, t('All time:')))) { 280 $content[] = $node_title_list; 281 } 282 283 $lasttop = variable_get('statistics_block_top_last_num', 0); 284 if ($lasttop && ($result = statistics_title_list('timestamp', $lasttop)) && ($node_title_list = node_title_list($result, t('Last viewed:')))) { 285 $content[] = $node_title_list; 286 } 287 288 if (count($content)) { 289 $block['content'] = implode('<br />', $content); 290 $block['subject'] = t('Popular content'); 291 return $block; 292 } 293 } 294 } 295 } 296 297 /** 298 * It is possible to adjust the width of columns generated by the 299 * statistics module. 300 */ 301 function _statistics_link($path, $width = 35) { 302 $title = drupal_get_path_alias($path); 303 $title = truncate_utf8($title, $width, FALSE, TRUE); 304 return l($title, $path); 305 } 306 307 function _statistics_format_item($title, $path) { 308 $path = ($path ? $path : '/'); 309 $output = ($title ? "$title<br />" : ''); 310 $output .= _statistics_link($path); 311 return $output; 312 } 313 314 /** 315 * Implementation of hook_nodeapi(). 316 */ 317 function statistics_nodeapi(&$node, $op, $arg = 0) { 318 switch ($op) { 319 case 'delete': 320 // clean up statistics table when node is deleted 321 db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid); 322 } 323 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |