statistics.module 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @file
  4. * Logs and displays content statistics for a site.
  5. */
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  8. use Drupal\Core\Routing\RouteMatchInterface;
  9. use Drupal\Core\Url;
  10. use Drupal\node\NodeInterface;
  11. /**
  12. * Implements hook_help().
  13. */
  14. function statistics_help($route_name, RouteMatchInterface $route_match) {
  15. switch ($route_name) {
  16. case 'help.page.statistics':
  17. $output = '';
  18. $output .= '<h3>' . t('About') . '</h3>';
  19. $output .= '<p>' . t('The Statistics module shows you how often content is viewed. This is useful in determining which pages of your site are most popular. For more information, see the <a href=":statistics_do">online documentation for the Statistics module</a>.', array(':statistics_do' => 'https://www.drupal.org/documentation/modules/statistics/')) . '</p>';
  20. $output .= '<h3>' . t('Uses') . '</h3>';
  21. $output .= '<dl>';
  22. $output .= '<dt>' . t('Displaying popular content') . '</dt>';
  23. $output .= '<dd>' . t('The module includes a <em>Popular content</em> block that displays the most viewed pages today and for all time, and the last content viewed. To use the block, enable <em>Count content views</em> on the <a href=":statistics-settings">Statistics page</a>, and then you can enable and configure the block on the <a href=":blocks">Block layout page</a>.', array(':statistics-settings' => \Drupal::url('statistics.settings'), ':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#')) . '</dd>';
  24. $output .= '<dt>' . t('Page view counter') . '</dt>';
  25. $output .= '<dd>' . t('The Statistics module includes a counter for each page that increases whenever the page is viewed. To use the counter, enable <em>Count content views</em> on the <a href=":statistics-settings">Statistics page</a>, and set the necessary <a href=":permissions">permissions</a> (<em>View content hits</em>) so that the counter is visible to the users.', array(':statistics-settings' => \Drupal::url('statistics.settings'), ':permissions' => \Drupal::url('user.admin_permissions', array(), array('fragment' => 'module-statistics')))) . '</dd>';
  26. $output .= '</dl>';
  27. return $output;
  28. case 'statistics.settings':
  29. return '<p>' . t('Settings for the statistical information that Drupal will keep about the site.') . '</p>';
  30. }
  31. }
  32. /**
  33. * Implements hook_ENTITY_TYPE_view() for node entities.
  34. */
  35. function statistics_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  36. if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
  37. $build['#attached']['library'][] = 'statistics/drupal.statistics';
  38. $settings = array('data' => array('nid' => $node->id()), 'url' => Url::fromUri('base:' . drupal_get_path('module', 'statistics') . '/statistics.php')->toString());
  39. $build['#attached']['drupalSettings']['statistics'] = $settings;
  40. }
  41. }
  42. /**
  43. * Implements hook_node_links_alter().
  44. */
  45. function statistics_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
  46. if ($context['view_mode'] != 'rss') {
  47. $links['#cache']['contexts'][] = 'user.permissions';
  48. if (\Drupal::currentUser()->hasPermission('view post access counter')) {
  49. $statistics = statistics_get($entity->id());
  50. if ($statistics) {
  51. $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics['totalcount'], '1 view', '@count views');
  52. $links['statistics'] = array(
  53. '#theme' => 'links__node__statistics',
  54. '#links' => $statistics_links,
  55. '#attributes' => array('class' => array('links', 'inline')),
  56. );
  57. }
  58. $links['#cache']['max-age'] = \Drupal::config('statistics.settings')->get('display_max_age');
  59. }
  60. }
  61. }
  62. /**
  63. * Implements hook_cron().
  64. */
  65. function statistics_cron() {
  66. $statistics_timestamp = \Drupal::state()->get('statistics.day_timestamp') ?: 0;
  67. if ((REQUEST_TIME - $statistics_timestamp) >= 86400) {
  68. // Reset day counts.
  69. db_update('node_counter')
  70. ->fields(array('daycount' => 0))
  71. ->execute();
  72. \Drupal::state()->set('statistics.day_timestamp', REQUEST_TIME);
  73. }
  74. // Calculate the maximum of node views, for node search ranking.
  75. \Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1.0, db_query('SELECT MAX(totalcount) FROM {node_counter}')->fetchField()));
  76. }
  77. /**
  78. * Returns the most viewed content of all time, today, or the last-viewed node.
  79. *
  80. * @param string $dbfield
  81. * The database field to use, one of:
  82. * - 'totalcount': Integer that shows the top viewed content of all time.
  83. * - 'daycount': Integer that shows the top viewed content for today.
  84. * - 'timestamp': Integer that shows only the last viewed node.
  85. * @param int $dbrows
  86. * The number of rows to be returned.
  87. *
  88. * @return SelectQuery|FALSE
  89. * A query result containing the node ID, title, user ID that owns the node,
  90. * and the username for the selected node(s), or FALSE if the query could not
  91. * be executed correctly.
  92. */
  93. function statistics_title_list($dbfield, $dbrows) {
  94. if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) {
  95. $query = db_select('node_field_data', 'n');
  96. $query->addTag('node_access');
  97. $query->join('node_counter', 's', 'n.nid = s.nid');
  98. $query->join('users_field_data', 'u', 'n.uid = u.uid');
  99. return $query
  100. ->fields('n', array('nid', 'title'))
  101. ->fields('u', array('uid', 'name'))
  102. ->condition($dbfield, 0, '<>')
  103. ->condition('n.status', 1)
  104. // @todo This should be actually filtering on the desired node status
  105. // field language and just fall back to the default language.
  106. ->condition('n.default_langcode', 1)
  107. ->condition('u.default_langcode', 1)
  108. ->orderBy($dbfield, 'DESC')
  109. ->range(0, $dbrows)
  110. ->execute();
  111. }
  112. return FALSE;
  113. }
  114. /**
  115. * Retrieves a node's "view statistics".
  116. *
  117. * @param int $nid
  118. * The node ID.
  119. *
  120. * @return array
  121. * An associative array containing:
  122. * - totalcount: Integer for the total number of times the node has been
  123. * viewed.
  124. * - daycount: Integer for the total number of times the node has been viewed
  125. * "today". For the daycount to be reset, cron must be enabled.
  126. * - timestamp: Integer for the timestamp of when the node was last viewed.
  127. */
  128. function statistics_get($nid) {
  129. if ($nid > 0) {
  130. // Retrieve an array with both totalcount and daycount.
  131. return db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'replica'))->fetchAssoc();
  132. }
  133. }
  134. /**
  135. * Implements hook_ENTITY_TYPE_predelete() for node entities.
  136. */
  137. function statistics_node_predelete(EntityInterface $node) {
  138. // Clean up statistics table when node is deleted.
  139. db_delete('node_counter')
  140. ->condition('nid', $node->id())
  141. ->execute();
  142. }
  143. /**
  144. * Implements hook_ranking().
  145. */
  146. function statistics_ranking() {
  147. if (\Drupal::config('statistics.settings')->get('count_content_views')) {
  148. return array(
  149. 'views' => array(
  150. 'title' => t('Number of views'),
  151. 'join' => array(
  152. 'type' => 'LEFT',
  153. 'table' => 'node_counter',
  154. 'alias' => 'node_counter',
  155. 'on' => 'node_counter.nid = i.sid',
  156. ),
  157. // Inverse law that maps the highest view count on the site to 1 and 0
  158. // to 0. Note that the ROUND here is necessary for PostgreSQL and SQLite
  159. // in order to ensure that the :statistics_scale argument is treated as
  160. // a numeric type, because the PostgreSQL PDO driver sometimes puts
  161. // values in as strings instead of numbers in complex expressions like
  162. // this.
  163. 'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * (ROUND(:statistics_scale, 4)))',
  164. 'arguments' => array(':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0),
  165. ),
  166. );
  167. }
  168. }
  169. /**
  170. * Implements hook_preprocess_HOOK() for block templates.
  171. */
  172. function statistics_preprocess_block(&$variables) {
  173. if ($variables['configuration']['provider'] == 'statistics') {
  174. $variables['attributes']['role'] = 'navigation';
  175. }
  176. }
  177. /**
  178. * Implements hook_block_alter().
  179. *
  180. * Removes the "popular" block from display if the module is not configured
  181. * to count content views.
  182. */
  183. function statistics_block_alter(&$definitions) {
  184. $statistics_count_content_views = \Drupal::config('statistics.settings')->get('count_content_views');
  185. if (empty($statistics_count_content_views)) {
  186. unset($definitions['statistics_popular_block']);
  187. }
  188. }