statistics.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 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. ->orderBy('a.hostname');
  128. $uniques_query = db_select('accesslog')->distinct();
  129. $uniques_query->fields('accesslog', array('uid', 'hostname'));
  130. $count_query = db_select($uniques_query);
  131. $count_query->addExpression('COUNT(*)');
  132. $query->setCountQuery($count_query);
  133. $result = $query->execute();
  134. $rows = array();
  135. $destination = drupal_get_destination();
  136. foreach ($result as $account) {
  137. $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));
  138. $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 : '');
  139. }
  140. drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
  141. $build['statistics_top_visitors_table'] = array(
  142. '#theme' => 'table',
  143. '#header' => $header,
  144. '#rows' => $rows,
  145. '#empty' => t('No statistics available.'),
  146. );
  147. $build['statistics_top_visitors_pager'] = array('#theme' => 'pager');
  148. return $build;
  149. }
  150. /**
  151. * Page callback: Displays the "top referrers" in the access logs.
  152. *
  153. * This displays the pages with the top referrers in a given time interval that
  154. * haven't been flushed yet. The flush interval is set on the statistics
  155. * settings form, but is dependent on cron running.
  156. *
  157. * @return
  158. * A render array containing the top referrers information.
  159. */
  160. function statistics_top_referrers() {
  161. drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
  162. $header = array(
  163. array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
  164. array('data' => t('Url'), 'field' => 'url'),
  165. array('data' => t('Last visit'), 'field' => 'last'),
  166. );
  167. $query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');
  168. $query->addExpression('COUNT(url)', 'hits');
  169. $query->addExpression('MAX(timestamp)', 'last');
  170. $query
  171. ->fields('a', array('url'))
  172. ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
  173. ->condition('url', '', '<>')
  174. ->groupBy('url')
  175. ->limit(30)
  176. ->orderByHeader($header);
  177. $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
  178. $count_query->addExpression('COUNT(DISTINCT url)');
  179. $count_query
  180. ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
  181. ->condition('url', '', '<>');
  182. $query->setCountQuery($count_query);
  183. $result = $query->execute();
  184. $rows = array();
  185. foreach ($result as $referrer) {
  186. $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(REQUEST_TIME - $referrer->last))));
  187. }
  188. $build['statistics_top_referrers_table'] = array(
  189. '#theme' => 'table',
  190. '#header' => $header,
  191. '#rows' => $rows,
  192. '#empty' => t('No statistics available.'),
  193. );
  194. $build['statistics_top_referrers_pager'] = array('#theme' => 'pager');
  195. return $build;
  196. }
  197. /**
  198. * Page callback: Gathers page access statistics suitable for rendering.
  199. *
  200. * @param $aid
  201. * The unique accesslog ID.
  202. *
  203. * @return
  204. * A render array containing page access statistics. If information for the
  205. * page was not found, drupal_not_found() is called.
  206. */
  207. function statistics_access_log($aid) {
  208. $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();
  209. if ($access) {
  210. $rows[] = array(
  211. array('data' => t('URL'), 'header' => TRUE),
  212. l(url($access->path, array('absolute' => TRUE)), $access->path)
  213. );
  214. // It is safe to avoid filtering $access->title through check_plain because
  215. // it comes from drupal_get_title().
  216. $rows[] = array(
  217. array('data' => t('Title'), 'header' => TRUE),
  218. $access->title
  219. );
  220. $rows[] = array(
  221. array('data' => t('Referrer'), 'header' => TRUE),
  222. ($access->url ? l($access->url, $access->url) : '')
  223. );
  224. $rows[] = array(
  225. array('data' => t('Date'), 'header' => TRUE),
  226. format_date($access->timestamp, 'long')
  227. );
  228. $rows[] = array(
  229. array('data' => t('User'), 'header' => TRUE),
  230. theme('username', array('account' => $access))
  231. );
  232. $rows[] = array(
  233. array('data' => t('Hostname'), 'header' => TRUE),
  234. check_plain($access->hostname)
  235. );
  236. $build['statistics_table'] = array(
  237. '#theme' => 'table',
  238. '#rows' => $rows,
  239. );
  240. return $build;
  241. }
  242. return MENU_NOT_FOUND;
  243. }
  244. /**
  245. * Form constructor for the statistics administration form.
  246. *
  247. * @ingroup forms
  248. * @see system_settings_form()
  249. */
  250. function statistics_settings_form() {
  251. // Access log settings.
  252. $form['access'] = array(
  253. '#type' => 'fieldset',
  254. '#title' => t('Access log settings'),
  255. );
  256. $form['access']['statistics_enable_access_log'] = array(
  257. '#type' => 'checkbox',
  258. '#title' => t('Enable access log'),
  259. '#default_value' => variable_get('statistics_enable_access_log', 0),
  260. '#description' => t('Log each page access. Required for referrer statistics.'),
  261. );
  262. $form['access']['statistics_flush_accesslog_timer'] = array(
  263. '#type' => 'select',
  264. '#title' => t('Discard access logs older than'),
  265. '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
  266. '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
  267. '#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'))),
  268. );
  269. // Content counter settings.
  270. $form['content'] = array(
  271. '#type' => 'fieldset',
  272. '#title' => t('Content viewing counter settings'),
  273. );
  274. $form['content']['statistics_count_content_views'] = array(
  275. '#type' => 'checkbox',
  276. '#title' => t('Count content views'),
  277. '#default_value' => variable_get('statistics_count_content_views', 0),
  278. '#description' => t('Increment a counter each time content is viewed.'),
  279. );
  280. $form['content']['statistics_count_content_views_ajax'] = array(
  281. '#type' => 'checkbox',
  282. '#title' => t('Use Ajax to increment the counter'),
  283. '#default_value' => variable_get('statistics_count_content_views_ajax', 0),
  284. '#description' => t('Perform the count asynchronously after page load rather than during page generation.'),
  285. '#states' => array(
  286. 'disabled' => array(
  287. ':input[name="statistics_count_content_views"]' => array('checked' => FALSE),
  288. ),
  289. ),
  290. );
  291. return system_settings_form($form);
  292. }