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 and displays access statistics for a site.

Functions

Namesort descending Description
statistics_block_configure Implements hook_block_configure().
statistics_block_info Implements hook_block_info().
statistics_block_save Implements hook_block_save().
statistics_block_view Implements hook_block_view().
statistics_cron Implements hook_cron().
statistics_exit Implements hook_exit().
statistics_get Retrieves a node's "view statistics".
statistics_help Implements hook_help().
statistics_menu Implements hook_menu().
statistics_node_delete Implements hook_node_delete().
statistics_node_view Implements hook_node_view().
statistics_permission Implements hook_permission().
statistics_ranking Implements hook_ranking().
statistics_title_list Returns the most viewed content of all time, today, or the last-viewed node.
statistics_update_index Implements hook_update_index().
statistics_user_cancel Implements hook_user_cancel().
statistics_user_delete Implements hook_user_delete().
_statistics_format_item Formats an item for display, including both the item title and the link.
_statistics_link Generates a link to a path, truncating the displayed text to a given width.

File

modules/statistics/statistics.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Logs and displays access statistics for a site.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function statistics_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#statistics':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Statistics module shows you how often a given page is viewed, who viewed it, the previous page the user visited (referrer URL), and when it was viewed. These statistics are useful in determining how users are visiting and navigating your site. For more information, see the online handbook entry for the <a href="@statistics">Statistics module</a>.', array('@statistics' => url('http://drupal.org/documentation/modules/statistics/'))) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('Managing logs') . '</dt>';
  18. $output .= '<dd>' . t('To enable collection of statistics, the <em>Enable access log</em> checkbox on the <a href="@statistics-settings">Statistics settings page</a> must be checked. The <em>Discard access logs older than</em> setting on the settings page specifies the length of time entries are kept in the log before they are deleted. This setting requires a correctly configured <a href="@cron">cron maintenance task</a> to run.', array('@statistics-settings' => url('admin/config/system/statistics'), '@cron' => 'http://drupal.org/cron')) . '</dd>';
  19. $output .= '<dt>' . t('Viewing site usage') . '</dt>';
  20. $output .= '<dd>' . t('The Statistics module can help you break down details about your users and how they are using the site. The module offers four reports:');
  21. $output .= '<ul><li>' . t('<a href="@recent-hits">Recent hits</a> displays information about the latest activity on your site, including the URL and title of the page that was accessed, the user name (if available) and the IP address of the viewer.', array('@recent-hits' => url('admin/reports/hits'))) . '</li>';
  22. $output .= '<li>' . t('<a href="@top-referrers">Top referrers</a> displays where visitors came from (referrer URL).', array('@top-referrers' => url('admin/reports/referrers'))) . '</li>';
  23. $output .= '<li>' . t('<a href="@top-pages">Top pages</a> displays a list of pages ordered by how often they were viewed.', array('@top-pages' => url('admin/reports/pages'))) . '</li>';
  24. $output .= '<li>' . t('<a href="@top-visitors">Top visitors</a> shows you the most active visitors for your site and allows you to ban abusive visitors.', array('@top-visitors' => url('admin/reports/visitors'))) . '</li></ul>';
  25. $output .= '<dt>' . t('Displaying popular content') . '</dt>';
  26. $output .= '<dd>' . t('The module includes a <em>Popular content</em> block that displays the most viewed pages today and for all time, and the last content viewed. To use the block, enable <em>Count content views</em> on the <a href="@statistics-settings">statistics settings page</a>, and then you can enable and configure the block on the <a href="@blocks">blocks administration page</a>.', array('@statistics-settings' => url('admin/config/system/statistics'), '@blocks' => url('admin/structure/block'))) . '</dd>';
  27. $output .= '<dt>' . t('Page view counter') . '</dt>';
  28. $output .= '<dd>' . t('The Statistics module includes a counter for each page that increases whenever the page is viewed. To use the counter, enable <em>Count content views</em> on the <a href="@statistics-settings">statistics settings page</a>, and set the necessary <a href="@permissions">permissions</a> (<em>View content hits</em>) so that the counter is visible to the users.', array('@statistics-settings' => url('admin/config/system/statistics'), '@permissions' => url('admin/people/permissions', array('fragment' => 'module-statistics')))) . '</dd>';
  29. $output .= '</dl>';
  30. return $output;
  31. case 'admin/config/system/statistics':
  32. 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>';
  33. case 'admin/reports/hits':
  34. return '<p>' . t("This page displays the site's most recent hits.") . '</p>';
  35. case 'admin/reports/referrers':
  36. return '<p>' . t('This page displays all external referrers, or external references to your website.') . '</p>';
  37. case 'admin/reports/visitors':
  38. 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>';
  39. }
  40. }
  41. /**
  42. * Implements hook_exit().
  43. *
  44. * Gathers statistics for page accesses.
  45. */
  46. function statistics_exit() {
  47. global $user;
  48. // When serving cached pages with the 'page_cache_without_database'
  49. // configuration, system variables need to be loaded. This is a major
  50. // performance decrease for non-database page caches, but with Statistics
  51. // module, it is likely to also have 'statistics_enable_access_log' enabled,
  52. // in which case we need to bootstrap to the session phase anyway.
  53. drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES);
  54. if (variable_get('statistics_count_content_views', 0) && !variable_get('statistics_count_content_views_ajax', 0)) {
  55. // We are counting content views.
  56. if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == NULL) {
  57. // A node has been viewed, so update the node's counters.
  58. db_merge('node_counter')
  59. ->key(array('nid' => arg(1)))
  60. ->fields(array(
  61. 'daycount' => 1,
  62. 'totalcount' => 1,
  63. 'timestamp' => REQUEST_TIME,
  64. ))
  65. ->expression('daycount', 'daycount + 1')
  66. ->expression('totalcount', 'totalcount + 1')
  67. ->execute();
  68. }
  69. }
  70. if (variable_get('statistics_enable_access_log', 0)) {
  71. drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  72. // For anonymous users unicode.inc will not have been loaded.
  73. include_once DRUPAL_ROOT . '/includes/unicode.inc';
  74. // Log this page access.
  75. db_insert('accesslog')
  76. ->fields(array(
  77. 'title' => truncate_utf8(strip_tags(drupal_get_title()), 255),
  78. 'path' => truncate_utf8($_GET['q'], 255),
  79. 'url' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
  80. 'hostname' => ip_address(),
  81. 'uid' => $user->uid,
  82. 'sid' => session_id(),
  83. 'timer' => (int) timer_read('page'),
  84. 'timestamp' => REQUEST_TIME,
  85. ))
  86. ->execute();
  87. }
  88. }
  89. /**
  90. * Implements hook_permission().
  91. */
  92. function statistics_permission() {
  93. return array(
  94. 'administer statistics' => array(
  95. 'title' => t('Administer statistics'),
  96. ),
  97. 'access statistics' => array(
  98. 'title' => t('View content access statistics'),
  99. ),
  100. 'view post access counter' => array(
  101. 'title' => t('View content hits'),
  102. ),
  103. );
  104. }
  105. /**
  106. * Implements hook_node_view().
  107. */
  108. function statistics_node_view($node, $view_mode) {
  109. // Attach Ajax node count statistics if configured.
  110. if (variable_get('statistics_count_content_views', 0) && variable_get('statistics_count_content_views_ajax', 0)) {
  111. if (!empty($node->nid) && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
  112. $node->content['#attached']['js'] = array(
  113. drupal_get_path('module', 'statistics') . '/statistics.js' => array(
  114. 'scope' => 'footer'
  115. ),
  116. );
  117. $settings = array('data' => array('nid' => $node->nid), 'url' => url(drupal_get_path('module', 'statistics') . '/statistics.php'));
  118. $node->content['#attached']['js'][] = array(
  119. 'data' => array('statistics' => $settings),
  120. 'type' => 'setting',
  121. );
  122. }
  123. }
  124. if ($view_mode != 'rss') {
  125. if (user_access('view post access counter')) {
  126. $statistics = statistics_get($node->nid);
  127. if ($statistics) {
  128. $links['statistics_counter']['title'] = format_plural($statistics['totalcount'], '1 read', '@count reads');
  129. $node->content['links']['statistics'] = array(
  130. '#theme' => 'links__node__statistics',
  131. '#links' => $links,
  132. '#attributes' => array('class' => array('links', 'inline')),
  133. );
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * Implements hook_menu().
  140. */
  141. function statistics_menu() {
  142. $items['admin/reports/hits'] = array(
  143. 'title' => 'Recent hits',
  144. 'description' => 'View pages that have recently been visited.',
  145. 'page callback' => 'statistics_recent_hits',
  146. 'access arguments' => array('access statistics'),
  147. 'file' => 'statistics.admin.inc',
  148. );
  149. $items['admin/reports/pages'] = array(
  150. 'title' => 'Top pages',
  151. 'description' => 'View pages that have been hit frequently.',
  152. 'page callback' => 'statistics_top_pages',
  153. 'access arguments' => array('access statistics'),
  154. 'weight' => 1,
  155. 'file' => 'statistics.admin.inc',
  156. );
  157. $items['admin/reports/visitors'] = array(
  158. 'title' => 'Top visitors',
  159. 'description' => 'View visitors that hit many pages.',
  160. 'page callback' => 'statistics_top_visitors',
  161. 'access arguments' => array('access statistics'),
  162. 'weight' => 2,
  163. 'file' => 'statistics.admin.inc',
  164. );
  165. $items['admin/reports/referrers'] = array(
  166. 'title' => 'Top referrers',
  167. 'description' => 'View top referrers.',
  168. 'page callback' => 'statistics_top_referrers',
  169. 'access arguments' => array('access statistics'),
  170. 'file' => 'statistics.admin.inc',
  171. );
  172. $items['admin/reports/access/%'] = array(
  173. 'title' => 'Details',
  174. 'description' => 'View access log.',
  175. 'page callback' => 'statistics_access_log',
  176. 'page arguments' => array(3),
  177. 'access arguments' => array('access statistics'),
  178. 'file' => 'statistics.admin.inc',
  179. );
  180. $items['admin/config/system/statistics'] = array(
  181. 'title' => 'Statistics',
  182. 'description' => 'Control details about what and how your site logs access statistics.',
  183. 'page callback' => 'drupal_get_form',
  184. 'page arguments' => array('statistics_settings_form'),
  185. 'access arguments' => array('administer statistics'),
  186. 'file' => 'statistics.admin.inc',
  187. 'weight' => -15,
  188. );
  189. $items['user/%user/track/navigation'] = array(
  190. 'title' => 'Track page visits',
  191. 'page callback' => 'statistics_user_tracker',
  192. 'access callback' => 'user_access',
  193. 'access arguments' => array('access statistics'),
  194. 'type' => MENU_LOCAL_TASK,
  195. 'weight' => 2,
  196. 'file' => 'statistics.pages.inc',
  197. );
  198. $items['node/%node/track'] = array(
  199. 'title' => 'Track',
  200. 'page callback' => 'statistics_node_tracker',
  201. 'access callback' => 'user_access',
  202. 'access arguments' => array('access statistics'),
  203. 'type' => MENU_LOCAL_TASK,
  204. 'weight' => 2,
  205. 'file' => 'statistics.pages.inc',
  206. );
  207. return $items;
  208. }
  209. /**
  210. * Implements hook_user_cancel().
  211. */
  212. function statistics_user_cancel($edit, $account, $method) {
  213. switch ($method) {
  214. case 'user_cancel_reassign':
  215. db_update('accesslog')
  216. ->fields(array('uid' => 0))
  217. ->condition('uid', $account->uid)
  218. ->execute();
  219. break;
  220. }
  221. }
  222. /**
  223. * Implements hook_user_delete().
  224. */
  225. function statistics_user_delete($account) {
  226. db_delete('accesslog')
  227. ->condition('uid', $account->uid)
  228. ->execute();
  229. }
  230. /**
  231. * Implements hook_cron().
  232. */
  233. function statistics_cron() {
  234. $statistics_timestamp = variable_get('statistics_day_timestamp', '');
  235. if ((REQUEST_TIME - $statistics_timestamp) >= 86400) {
  236. // Reset day counts.
  237. db_update('node_counter')
  238. ->fields(array('daycount' => 0))
  239. ->execute();
  240. variable_set('statistics_day_timestamp', REQUEST_TIME);
  241. }
  242. // Clean up expired access logs (if applicable).
  243. if (variable_get('statistics_flush_accesslog_timer', 259200) > 0) {
  244. db_delete('accesslog')
  245. ->condition('timestamp', REQUEST_TIME - variable_get('statistics_flush_accesslog_timer', 259200), '<')
  246. ->execute();
  247. }
  248. }
  249. /**
  250. * Returns the most viewed content of all time, today, or the last-viewed node.
  251. *
  252. * @param $dbfield
  253. * The database field to use, one of:
  254. * - 'totalcount': Integer that shows the top viewed content of all time.
  255. * - 'daycount': Integer that shows the top viewed content for today.
  256. * - 'timestamp': Integer that shows only the last viewed node.
  257. * @param $dbrows
  258. * The number of rows to be returned.
  259. *
  260. * @return SelectQuery|FALSE
  261. * A query result containing the node ID, title, user ID that owns the node,
  262. * and the username for the selected node(s), or FALSE if the query could not
  263. * be executed correctly.
  264. */
  265. function statistics_title_list($dbfield, $dbrows) {
  266. if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) {
  267. $query = db_select('node', 'n');
  268. $query->addTag('node_access');
  269. $query->join('node_counter', 's', 'n.nid = s.nid');
  270. $query->join('users', 'u', 'n.uid = u.uid');
  271. return $query
  272. ->fields('n', array('nid', 'title'))
  273. ->fields('u', array('uid', 'name'))
  274. ->condition($dbfield, 0, '<>')
  275. ->condition('n.status', 1)
  276. ->orderBy($dbfield, 'DESC')
  277. ->range(0, $dbrows)
  278. ->execute();
  279. }
  280. return FALSE;
  281. }
  282. /**
  283. * Retrieves a node's "view statistics".
  284. *
  285. * @param $nid
  286. * The node ID.
  287. *
  288. * @return
  289. * An associative array containing:
  290. * - totalcount: Integer for the total number of times the node has been
  291. * viewed.
  292. * - daycount: Integer for the total number of times the node has been viewed
  293. * "today". For the daycount to be reset, cron must be enabled.
  294. * - timestamp: Integer for the timestamp of when the node was last viewed.
  295. */
  296. function statistics_get($nid) {
  297. if ($nid > 0) {
  298. // Retrieve an array with both totalcount and daycount.
  299. return db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchAssoc();
  300. }
  301. }
  302. /**
  303. * Implements hook_block_info().
  304. */
  305. function statistics_block_info() {
  306. $blocks = array();
  307. if (variable_get('statistics_count_content_views', 0)) {
  308. $blocks['popular']['info'] = t('Popular content');
  309. // Too dynamic to cache.
  310. $blocks['popular']['cache'] = DRUPAL_NO_CACHE;
  311. }
  312. return $blocks;
  313. }
  314. /**
  315. * Implements hook_block_configure().
  316. */
  317. function statistics_block_configure($delta = '') {
  318. // Popular content block settings
  319. $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
  320. $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.'));
  321. $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.'));
  322. $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.'));
  323. return $form;
  324. }
  325. /**
  326. * Implements hook_block_save().
  327. */
  328. function statistics_block_save($delta = '', $edit = array()) {
  329. variable_set('statistics_block_top_day_num', $edit['statistics_block_top_day_num']);
  330. variable_set('statistics_block_top_all_num', $edit['statistics_block_top_all_num']);
  331. variable_set('statistics_block_top_last_num', $edit['statistics_block_top_last_num']);
  332. }
  333. /**
  334. * Implements hook_block_view().
  335. */
  336. function statistics_block_view($delta = '') {
  337. if (user_access('access content')) {
  338. $content = array();
  339. $daytop = variable_get('statistics_block_top_day_num', 0);
  340. if ($daytop && ($result = statistics_title_list('daycount', $daytop)) && ($node_title_list = node_title_list($result, t("Today's:")))) {
  341. $content['top_day'] = $node_title_list;
  342. $content['top_day']['#suffix'] = '<br />';
  343. }
  344. $alltimetop = variable_get('statistics_block_top_all_num', 0);
  345. if ($alltimetop && ($result = statistics_title_list('totalcount', $alltimetop)) && ($node_title_list = node_title_list($result, t('All time:')))) {
  346. $content['top_all'] = $node_title_list;
  347. $content['top_all']['#suffix'] = '<br />';
  348. }
  349. $lasttop = variable_get('statistics_block_top_last_num', 0);
  350. if ($lasttop && ($result = statistics_title_list('timestamp', $lasttop)) && ($node_title_list = node_title_list($result, t('Last viewed:')))) {
  351. $content['top_last'] = $node_title_list;
  352. $content['top_last']['#suffix'] = '<br />';
  353. }
  354. if (count($content)) {
  355. $block['content'] = $content;
  356. $block['subject'] = t('Popular content');
  357. return $block;
  358. }
  359. }
  360. }
  361. /**
  362. * Generates a link to a path, truncating the displayed text to a given width.
  363. *
  364. * @param $path
  365. * The path to generate the link for.
  366. * @param $width
  367. * The width to set the displayed text of the path.
  368. *
  369. * @return
  370. * A string as a link, truncated to the width, linked to the given $path.
  371. */
  372. function _statistics_link($path, $width = 35) {
  373. $title = drupal_get_path_alias($path);
  374. $title = truncate_utf8($title, $width, FALSE, TRUE);
  375. return l($title, $path);
  376. }
  377. /**
  378. * Formats an item for display, including both the item title and the link.
  379. *
  380. * @param $title
  381. * The text to link to a path; will be truncated to a maximum width of 35.
  382. * @param $path
  383. * The path to link to; will default to '/'.
  384. *
  385. * @return
  386. * An HTML string with $title linked to the $path.
  387. */
  388. function _statistics_format_item($title, $path) {
  389. $path = ($path ? $path : '/');
  390. $output = ($title ? "$title<br />" : '');
  391. $output .= _statistics_link($path);
  392. return $output;
  393. }
  394. /**
  395. * Implements hook_node_delete().
  396. */
  397. function statistics_node_delete($node) {
  398. // clean up statistics table when node is deleted
  399. db_delete('node_counter')
  400. ->condition('nid', $node->nid)
  401. ->execute();
  402. }
  403. /**
  404. * Implements hook_ranking().
  405. */
  406. function statistics_ranking() {
  407. if (variable_get('statistics_count_content_views', 0)) {
  408. return array(
  409. 'views' => array(
  410. 'title' => t('Number of views'),
  411. 'join' => array(
  412. 'type' => 'LEFT',
  413. 'table' => 'node_counter',
  414. 'alias' => 'node_counter',
  415. 'on' => 'node_counter.nid = i.sid',
  416. ),
  417. // Inverse law that maps the highest view count on the site to 1 and 0 to 0.
  418. 'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * CAST(:scale AS DECIMAL))',
  419. 'arguments' => array(':scale' => variable_get('node_cron_views_scale', 0)),
  420. ),
  421. );
  422. }
  423. }
  424. /**
  425. * Implements hook_update_index().
  426. */
  427. function statistics_update_index() {
  428. variable_set('node_cron_views_scale', 1.0 / max(1, db_query('SELECT MAX(totalcount) FROM {node_counter}')->fetchField()));
  429. }

Comments

Hi i need the help for the configure the statistics module.