statistics.admin.inc

  1. 7.x drupal-7.x/modules/statistics/statistics.admin.inc
  2. 6.x drupal-6.x/modules/statistics/statistics.admin.inc

Admin page callbacks for the statistics module.

File

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