statistics.module

You are here

  1. 7 modules/statistics/statistics.module
  2. 4.6 modules/statistics.module
  3. 4.7 modules/statistics.module
  4. 5 modules/statistics/statistics.module
  5. 6 modules/statistics/statistics.module
  6. 8 core/modules/statistics/statistics.module

Logs access statistics for your site.

Functions

Namesort descending Description
statistics_block Implementation of hook_block().
statistics_cron Implementation of hook_cron().
statistics_exit Implementation of hook_exit().
statistics_get Retrieves a node's "view statistics".
statistics_help Implementation of hook_help().
statistics_link Implementation of hook_link().
statistics_menu Implementation of hook_menu().
statistics_nodeapi Implementation of hook_nodeapi().
statistics_perm Implementation of hook_perm().
statistics_title_list Returns all time or today top or last viewed node(s).
statistics_user Implementation of hook_user().
_statistics_format_item
_statistics_link It is possible to adjust the width of columns generated by the statistics module.

File

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