statistics.admin.inc

  1. drupal
    1. 6 modules/statistics/statistics.admin.inc
    2. 7 modules/statistics/statistics.admin.inc
    3. 8 core/modules/statistics/statistics.admin.inc

Admin page callbacks for the statistics module.

Functions & methods

NameDescription
statistics_access_logMenu callback; Displays recent page accesses.
statistics_recent_hitsMenu callback; presents the "recent hits" page.
statistics_settings_formForm builder; Configure access logging.
statistics_top_pagesMenu callback; presents the "top pages" page.
statistics_top_referrersMenu callback; presents the "referrer" page.
statistics_top_visitorsMenu callback; presents the "top visitors" page.

File

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. $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
  17. $query->join('users', 'u', 'a.uid = u.uid');
  18. $query
  19. ->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
  20. ->fields('u', array('name'))
  21. ->limit(30)
  22. ->orderByHeader($header);
  23. $result = $query->execute();
  24. $rows = array();
  25. foreach ($result as $log) {
  26. $rows[] = array(
  27. array('data' => format_date($log->timestamp, 'short'), 'class' => array('nowrap')),
  28. _statistics_format_item($log->title, $log->path),
  29. theme('username', array('account' => $log)),
  30. l(t('details'), "admin/reports/access/$log->aid"));
  31. }
  32. $build['statistics_table'] = array(
  33. '#theme' => 'table',
  34. '#header' => $header,
  35. '#rows' => $rows,
  36. '#empty' => t('No statistics available.'),
  37. );
  38. $build['statistics_pager'] = array('#theme' => 'pager');
  39. return $build;
  40. }
  41. /**
  42. * Menu callback; presents the "top pages" page.
  43. */
  44. function statistics_top_pages() {
  45. $header = array(
  46. array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
  47. array('data' => t('Page'), 'field' => 'path'),
  48. array('data' => t('Average page generation time'), 'field' => 'average_time'),
  49. array('data' => t('Total page generation time'), 'field' => 'total_time')
  50. );
  51. $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
  52. $query->addExpression('COUNT(path)', 'hits');
  53. // MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list
  54. $query->addExpression('MAX(title)', 'title');
  55. $query->addExpression('AVG(timer)', 'average_time');
  56. $query->addExpression('SUM(timer)', 'total_time');
  57. $query
  58. ->fields('a', array('path'))
  59. ->groupBy('path')
  60. ->limit(30)
  61. ->orderByHeader($header);
  62. $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
  63. $count_query->addExpression('COUNT(DISTINCT path)');
  64. $query->setCountQuery($count_query);
  65. $result = $query->execute();
  66. $rows = array();
  67. foreach ($result as $page) {
  68. $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)));
  69. }
  70. drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
  71. $build['statistics_top_pages_table'] = array(
  72. '#theme' => 'table',
  73. '#header' => $header,
  74. '#rows' => $rows,
  75. '#empty' => t('No statistics available.'),
  76. );
  77. $build['statistics_top_pages_pager'] = array('#theme' => 'pager');
  78. return $build;
  79. }
  80. /**
  81. * Menu callback; presents the "top visitors" page.
  82. */
  83. function statistics_top_visitors() {
  84. $header = array(
  85. array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
  86. array('data' => t('Visitor'), 'field' => 'u.name'),
  87. array('data' => t('Total page generation time'), 'field' => 'total'),
  88. array('data' => user_access('block IP addresses') ? t('Operations') : '', 'colspan' => 2),
  89. );
  90. $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
  91. $query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip');
  92. $query->leftJoin('users', 'u', 'a.uid = u.uid');
  93. $query->addExpression('COUNT(a.uid)', 'hits');
  94. $query->addExpression('SUM(a.timer)', 'total');
  95. $query
  96. ->fields('a', array('uid', 'hostname'))
  97. ->fields('u', array('name'))
  98. ->fields('bl', array('iid'))
  99. ->groupBy('a.hostname')
  100. ->groupBy('a.uid')
  101. ->groupBy('u.name')
  102. ->groupBy('bl.iid')
  103. ->limit(30)
  104. ->orderByHeader($header);
  105. $uniques_query = db_select('accesslog')->distinct();
  106. $uniques_query->fields('accesslog', array('uid', 'hostname'));
  107. $count_query = db_select($uniques_query);
  108. $count_query->addExpression('COUNT(*)');
  109. $query->setCountQuery($count_query);
  110. $result = $query->execute();
  111. $rows = array();
  112. $destination = drupal_get_destination();
  113. foreach ($result as $account) {
  114. $ban_link = $account->iid ? l(t('unblock IP address'), "admin/config/people/ip-blocking/delete/$account->iid", array('query' => $destination)) : l(t('block IP address'), "admin/config/people/ip-blocking/$account->hostname", array('query' => $destination));
  115. $rows[] = array($account->hits, ($account->uid ? theme('username', array('account' => $account)) : $account->hostname), format_interval(round($account->total / 1000)), (user_access('block IP addresses') && !$account->uid) ? $ban_link : '');
  116. }
  117. drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
  118. $build['statistics_top_visitors_table'] = array(
  119. '#theme' => 'table',
  120. '#header' => $header,
  121. '#rows' => $rows,
  122. '#empty' => t('No statistics available.'),
  123. );
  124. $build['statistics_top_visitors_pager'] = array('#theme' => 'pager');
  125. return $build;
  126. }
  127. /**
  128. * Menu callback; presents the "referrer" page.
  129. */
  130. function statistics_top_referrers() {
  131. drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
  132. $header = array(
  133. array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
  134. array('data' => t('Url'), 'field' => 'url'),
  135. array('data' => t('Last visit'), 'field' => 'last'),
  136. );
  137. $query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');
  138. $query->addExpression('COUNT(url)', 'hits');
  139. $query->addExpression('MAX(timestamp)', 'last');
  140. $query
  141. ->fields('a', array('url'))
  142. ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
  143. ->condition('url', '', '<>')
  144. ->groupBy('url')
  145. ->limit(30)
  146. ->orderByHeader($header);
  147. $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
  148. $count_query->addExpression('COUNT(DISTINCT url)');
  149. $count_query
  150. ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
  151. ->condition('url', '', '<>');
  152. $query->setCountQuery($count_query);
  153. $result = $query->execute();
  154. $rows = array();
  155. foreach ($result as $referrer) {
  156. $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(REQUEST_TIME - $referrer->last))));
  157. }
  158. $build['statistics_top_referrers_table'] = array(
  159. '#theme' => 'table',
  160. '#header' => $header,
  161. '#rows' => $rows,
  162. '#empty' => t('No statistics available.'),
  163. );
  164. $build['statistics_top_referrers_pager'] = array('#theme' => 'pager');
  165. return $build;
  166. }
  167. /**
  168. * Menu callback; Displays recent page accesses.
  169. */
  170. function statistics_access_log($aid) {
  171. $access = db_query('SELECT a.*, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE aid = :aid', array(':aid' => $aid))->fetch();
  172. if ($access) {
  173. $rows[] = array(
  174. array('data' => t('URL'), 'header' => TRUE),
  175. l(url($access->path, array('absolute' => TRUE)), $access->path)
  176. );
  177. // It is safe to avoid filtering $access->title through check_plain because
  178. // it comes from drupal_get_title().
  179. $rows[] = array(
  180. array('data' => t('Title'), 'header' => TRUE),
  181. $access->title
  182. );
  183. $rows[] = array(
  184. array('data' => t('Referrer'), 'header' => TRUE),
  185. ($access->url ? l($access->url, $access->url) : '')
  186. );
  187. $rows[] = array(
  188. array('data' => t('Date'), 'header' => TRUE),
  189. format_date($access->timestamp, 'long')
  190. );
  191. $rows[] = array(
  192. array('data' => t('User'), 'header' => TRUE),
  193. theme('username', array('account' => $access))
  194. );
  195. $rows[] = array(
  196. array('data' => t('Hostname'), 'header' => TRUE),
  197. check_plain($access->hostname)
  198. );
  199. $build['statistics_table'] = array(
  200. '#theme' => 'table',
  201. '#rows' => $rows,
  202. );
  203. return $build;
  204. }
  205. else {
  206. drupal_not_found();
  207. }
  208. }
  209. /**
  210. * Form builder; Configure access logging.
  211. *
  212. * @ingroup forms
  213. * @see system_settings_form()
  214. */
  215. function statistics_settings_form() {
  216. // Access log settings.
  217. $form['access'] = array(
  218. '#type' => 'fieldset',
  219. '#title' => t('Access log settings'),
  220. );
  221. $form['access']['statistics_enable_access_log'] = array(
  222. '#type' => 'checkbox',
  223. '#title' => t('Enable access log'),
  224. '#default_value' => variable_get('statistics_enable_access_log', 0),
  225. '#description' => t('Log each page access. Required for referrer statistics.'),
  226. );
  227. $form['access']['statistics_flush_accesslog_timer'] = array(
  228. '#type' => 'select',
  229. '#title' => t('Discard access logs older than'),
  230. '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
  231. '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
  232. '#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'))),
  233. );
  234. // Content counter settings.
  235. $form['content'] = array(
  236. '#type' => 'fieldset',
  237. '#title' => t('Content viewing counter settings'),
  238. );
  239. $form['content']['statistics_count_content_views'] = array(
  240. '#type' => 'checkbox',
  241. '#title' => t('Count content views'),
  242. '#default_value' => variable_get('statistics_count_content_views', 0),
  243. '#description' => t('Increment a counter each time content is viewed.'),
  244. );
  245. return system_settings_form($form);
  246. }
Login or register to post comments