statistics.admin.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. else {
  242. drupal_not_found();
  243. }
  244. }
  245. /**
  246. * Form constructor for the statistics administration form.
  247. *
  248. * @ingroup forms
  249. * @see system_settings_form()
  250. */
  251. function statistics_settings_form() {
  252. // Access log settings.
  253. $form['access'] = array(
  254. '#type' => 'fieldset',
  255. '#title' => t('Access log settings'),
  256. );
  257. $form['access']['statistics_enable_access_log'] = array(
  258. '#type' => 'checkbox',
  259. '#title' => t('Enable access log'),
  260. '#default_value' => variable_get('statistics_enable_access_log', 0),
  261. '#description' => t('Log each page access. Required for referrer statistics.'),
  262. );
  263. $form['access']['statistics_flush_accesslog_timer'] = array(
  264. '#type' => 'select',
  265. '#title' => t('Discard access logs older than'),
  266. '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
  267. '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
  268. '#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'))),
  269. );
  270. // Content counter settings.
  271. $form['content'] = array(
  272. '#type' => 'fieldset',
  273. '#title' => t('Content viewing counter settings'),
  274. );
  275. $form['content']['statistics_count_content_views'] = array(
  276. '#type' => 'checkbox',
  277. '#title' => t('Count content views'),
  278. '#default_value' => variable_get('statistics_count_content_views', 0),
  279. '#description' => t('Increment a counter each time content is viewed.'),
  280. );
  281. return system_settings_form($form);
  282. }