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-7.x/modules/statistics/statistics.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the Statistics module.
  5. */
  6. /**
  7. * Page callback: Displays the "recent hits" page.
  8. *
  9. * This displays the pages with recent hits in a given time interval that
  10. * haven't been flushed yet. The flush interval is set on the statistics
  11. * settings form, but is dependent on cron running.
  12. *
  13. * @return
  14. * A render array containing information about the most recent hits.
  15. */
  16. function statistics_recent_hits() {
  17. $header = array(
  18. array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
  19. array('data' => t('Page'), 'field' => 'a.path'),
  20. array('data' => t('User'), 'field' => 'u.name'),
  21. array('data' => t('Operations'))
  22. );
  23. $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
  24. $query->join('users', 'u', 'a.uid = u.uid');
  25. $query
  26. ->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
  27. ->fields('u', array('name'))
  28. ->limit(30)
  29. ->orderByHeader($header);
  30. $result = $query->execute();
  31. $rows = array();
  32. foreach ($result as $log) {
  33. $rows[] = array(
  34. array('data' => format_date($log->timestamp, 'short'), 'class' => array('nowrap')),
  35. _statistics_format_item($log->title, $log->path),
  36. theme('username', array('account' => $log)),
  37. l(t('details'), "admin/reports/access/$log->aid"));
  38. }
  39. $build['statistics_table'] = array(
  40. '#theme' => 'table',
  41. '#header' => $header,
  42. '#rows' => $rows,
  43. '#empty' => t('No statistics available.'),
  44. );
  45. $build['statistics_pager'] = array('#theme' => 'pager');
  46. return $build;
  47. }
  48. /**
  49. * Page callback: Displays statistics for the "top pages" (most accesses).
  50. *
  51. * This displays the pages with the most hits (the "top pages") within a given
  52. * time period that haven't been flushed yet. The flush interval is set on the
  53. * statistics settings form, but is dependent on cron running.
  54. *
  55. * @return
  56. * A render array containing information about the the top pages.
  57. */
  58. function statistics_top_pages() {
  59. $header = array(
  60. array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
  61. array('data' => t('Page'), 'field' => 'path'),
  62. array('data' => t('Average page generation time'), 'field' => 'average_time'),
  63. array('data' => t('Total page generation time'), 'field' => 'total_time')
  64. );
  65. $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
  66. $query->addExpression('COUNT(path)', 'hits');
  67. // MAX(title) avoids having empty node titles which otherwise causes
  68. // duplicates in the top pages list.
  69. $query->addExpression('MAX(title)', 'title');
  70. $query->addExpression('AVG(timer)', 'average_time');
  71. $query->addExpression('SUM(timer)', 'total_time');
  72. $query
  73. ->fields('a', array('path'))
  74. ->groupBy('path')
  75. ->limit(30)
  76. ->orderByHeader($header);
  77. $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
  78. $count_query->addExpression('COUNT(DISTINCT path)');
  79. $query->setCountQuery($count_query);
  80. $result = $query->execute();
  81. $rows = array();
  82. foreach ($result as $page) {
  83. $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)));
  84. }
  85. drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
  86. $build['statistics_top_pages_table'] = array(
  87. '#theme' => 'table',
  88. '#header' => $header,
  89. '#rows' => $rows,
  90. '#empty' => t('No statistics available.'),
  91. );
  92. $build['statistics_top_pages_pager'] = array('#theme' => 'pager');
  93. return $build;
  94. }
  95. /**
  96. * Page callback: Displays the "top visitors" page.
  97. *
  98. * This displays the pages with the top number of visitors in a given time
  99. * interval that haven't been flushed yet. The flush interval is set on the
  100. * statistics settings form, but is dependent on cron running.
  101. *
  102. * @return
  103. * A render array containing the top visitors information.
  104. */
  105. function statistics_top_visitors() {
  106. $header = array(
  107. array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
  108. array('data' => t('Visitor'), 'field' => 'u.name'),
  109. array('data' => t('Total page generation time'), 'field' => 'total'),
  110. array('data' => user_access('block IP addresses') ? t('Operations') : '', 'colspan' => 2),
  111. );
  112. $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
  113. $query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip');
  114. $query->leftJoin('users', 'u', 'a.uid = u.uid');
  115. $query->addExpression('COUNT(a.uid)', 'hits');
  116. $query->addExpression('SUM(a.timer)', 'total');
  117. $query
  118. ->fields('a', array('uid', 'hostname'))
  119. ->fields('u', array('name'))
  120. ->fields('bl', array('iid'))
  121. ->groupBy('a.hostname')
  122. ->groupBy('a.uid')
  123. ->groupBy('u.name')
  124. ->groupBy('bl.iid')
  125. ->limit(30)
  126. ->orderByHeader($header);
  127. $uniques_query = db_select('accesslog')->distinct();
  128. $uniques_query->fields('accesslog', array('uid', 'hostname'));
  129. $count_query = db_select($uniques_query);
  130. $count_query->addExpression('COUNT(*)');
  131. $query->setCountQuery($count_query);
  132. $result = $query->execute();
  133. $rows = array();
  134. $destination = drupal_get_destination();
  135. foreach ($result as $account) {
  136. $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));
  137. $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 : '');
  138. }
  139. drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
  140. $build['statistics_top_visitors_table'] = array(
  141. '#theme' => 'table',
  142. '#header' => $header,
  143. '#rows' => $rows,
  144. '#empty' => t('No statistics available.'),
  145. );
  146. $build['statistics_top_visitors_pager'] = array('#theme' => 'pager');
  147. return $build;
  148. }
  149. /**
  150. * Page callback: Displays the "top referrers" in the access logs.
  151. *
  152. * This displays the pages with the top referrers in a given time interval that
  153. * haven't been flushed yet. The flush interval is set on the statistics
  154. * settings form, but is dependent on cron running.
  155. *
  156. * @return
  157. * A render array containing the top referrers information.
  158. */
  159. function statistics_top_referrers() {
  160. drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
  161. $header = array(
  162. array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
  163. array('data' => t('Url'), 'field' => 'url'),
  164. array('data' => t('Last visit'), 'field' => 'last'),
  165. );
  166. $query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');
  167. $query->addExpression('COUNT(url)', 'hits');
  168. $query->addExpression('MAX(timestamp)', 'last');
  169. $query
  170. ->fields('a', array('url'))
  171. ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
  172. ->condition('url', '', '<>')
  173. ->groupBy('url')
  174. ->limit(30)
  175. ->orderByHeader($header);
  176. $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
  177. $count_query->addExpression('COUNT(DISTINCT url)');
  178. $count_query
  179. ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
  180. ->condition('url', '', '<>');
  181. $query->setCountQuery($count_query);
  182. $result = $query->execute();
  183. $rows = array();
  184. foreach ($result as $referrer) {
  185. $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(REQUEST_TIME - $referrer->last))));
  186. }
  187. $build['statistics_top_referrers_table'] = array(
  188. '#theme' => 'table',
  189. '#header' => $header,
  190. '#rows' => $rows,
  191. '#empty' => t('No statistics available.'),
  192. );
  193. $build['statistics_top_referrers_pager'] = array('#theme' => 'pager');
  194. return $build;
  195. }
  196. /**
  197. * Page callback: Gathers page access statistics suitable for rendering.
  198. *
  199. * @param $aid
  200. * The unique accesslog ID.
  201. *
  202. * @return
  203. * A render array containing page access statistics. If information for the
  204. * page was not found, drupal_not_found() is called.
  205. */
  206. function statistics_access_log($aid) {
  207. $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();
  208. if ($access) {
  209. $rows[] = array(
  210. array('data' => t('URL'), 'header' => TRUE),
  211. l(url($access->path, array('absolute' => TRUE)), $access->path)
  212. );
  213. // It is safe to avoid filtering $access->title through check_plain because
  214. // it comes from drupal_get_title().
  215. $rows[] = array(
  216. array('data' => t('Title'), 'header' => TRUE),
  217. $access->title
  218. );
  219. $rows[] = array(
  220. array('data' => t('Referrer'), 'header' => TRUE),
  221. ($access->url ? l($access->url, $access->url) : '')
  222. );
  223. $rows[] = array(
  224. array('data' => t('Date'), 'header' => TRUE),
  225. format_date($access->timestamp, 'long')
  226. );
  227. $rows[] = array(
  228. array('data' => t('User'), 'header' => TRUE),
  229. theme('username', array('account' => $access))
  230. );
  231. $rows[] = array(
  232. array('data' => t('Hostname'), 'header' => TRUE),
  233. check_plain($access->hostname)
  234. );
  235. $build['statistics_table'] = array(
  236. '#theme' => 'table',
  237. '#rows' => $rows,
  238. );
  239. return $build;
  240. }
  241. return MENU_NOT_FOUND;
  242. }
  243. /**
  244. * Form constructor for the statistics administration form.
  245. *
  246. * @ingroup forms
  247. * @see system_settings_form()
  248. */
  249. function statistics_settings_form() {
  250. // Access log settings.
  251. $form['access'] = array(
  252. '#type' => 'fieldset',
  253. '#title' => t('Access log settings'),
  254. );
  255. $form['access']['statistics_enable_access_log'] = array(
  256. '#type' => 'checkbox',
  257. '#title' => t('Enable access log'),
  258. '#default_value' => variable_get('statistics_enable_access_log', 0),
  259. '#description' => t('Log each page access. Required for referrer statistics.'),
  260. );
  261. $form['access']['statistics_flush_accesslog_timer'] = array(
  262. '#type' => 'select',
  263. '#title' => t('Discard access logs older than'),
  264. '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
  265. '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
  266. '#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'))),
  267. );
  268. // Content counter settings.
  269. $form['content'] = array(
  270. '#type' => 'fieldset',
  271. '#title' => t('Content viewing counter settings'),
  272. );
  273. $form['content']['statistics_count_content_views'] = array(
  274. '#type' => 'checkbox',
  275. '#title' => t('Count content views'),
  276. '#default_value' => variable_get('statistics_count_content_views', 0),
  277. '#description' => t('Increment a counter each time content is viewed.'),
  278. );
  279. $form['content']['statistics_count_content_views_ajax'] = array(
  280. '#type' => 'checkbox',
  281. '#title' => t('Use Ajax to increment the counter'),
  282. '#default_value' => variable_get('statistics_count_content_views_ajax', 0),
  283. '#description' => t('Perform the count asynchronously after page load rather than during page generation.'),
  284. '#states' => array(
  285. 'disabled' => array(
  286. ':input[name="statistics_count_content_views"]' => array('checked' => FALSE),
  287. ),
  288. ),
  289. );
  290. return system_settings_form($form);
  291. }