annotate core/modules/statistics/statistics.module @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents c75dbcec494b
children 12f9dff5fda9
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Logs and displays content statistics for a site.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Core\Entity\EntityInterface;
Chris@0 9 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
Chris@0 10 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 11 use Drupal\Core\Url;
Chris@0 12 use Drupal\node\NodeInterface;
Chris@0 13 use Drupal\statistics\StatisticsViewsResult;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Implements hook_help().
Chris@0 17 */
Chris@0 18 function statistics_help($route_name, RouteMatchInterface $route_match) {
Chris@0 19 switch ($route_name) {
Chris@0 20 case 'help.page.statistics':
Chris@0 21 $output = '';
Chris@0 22 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 23 $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>.', [':statistics_do' => 'https://www.drupal.org/documentation/modules/statistics/']) . '</p>';
Chris@0 24 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 25 $output .= '<dl>';
Chris@0 26 $output .= '<dt>' . t('Displaying popular content') . '</dt>';
Chris@0 27 $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>.', [':statistics-settings' => \Drupal::url('statistics.settings'), ':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#']) . '</dd>';
Chris@0 28 $output .= '<dt>' . t('Page view counter') . '</dt>';
Chris@0 29 $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.', [':statistics-settings' => \Drupal::url('statistics.settings'), ':permissions' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-statistics'])]) . '</dd>';
Chris@0 30 $output .= '</dl>';
Chris@0 31 return $output;
Chris@0 32
Chris@0 33 case 'statistics.settings':
Chris@0 34 return '<p>' . t('Settings for the statistical information that Drupal will keep about the site.') . '</p>';
Chris@0 35 }
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Implements hook_ENTITY_TYPE_view() for node entities.
Chris@0 40 */
Chris@0 41 function statistics_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
Chris@0 42 if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
Chris@0 43 $build['#attached']['library'][] = 'statistics/drupal.statistics';
Chris@0 44 $settings = ['data' => ['nid' => $node->id()], 'url' => \Drupal::request()->getBasePath() . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'];
Chris@0 45 $build['#attached']['drupalSettings']['statistics'] = $settings;
Chris@0 46 }
Chris@0 47 }
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Implements hook_node_links_alter().
Chris@0 51 */
Chris@0 52 function statistics_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
Chris@0 53 if ($context['view_mode'] != 'rss') {
Chris@0 54 $links['#cache']['contexts'][] = 'user.permissions';
Chris@0 55 if (\Drupal::currentUser()->hasPermission('view post access counter')) {
Chris@0 56 $statistics = \Drupal::service('statistics.storage.node')->fetchView($entity->id());
Chris@0 57 if ($statistics) {
Chris@0 58 $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views');
Chris@0 59 $links['statistics'] = [
Chris@0 60 '#theme' => 'links__node__statistics',
Chris@0 61 '#links' => $statistics_links,
Chris@0 62 '#attributes' => ['class' => ['links', 'inline']],
Chris@0 63 ];
Chris@0 64 }
Chris@0 65 $links['#cache']['max-age'] = \Drupal::config('statistics.settings')->get('display_max_age');
Chris@0 66 }
Chris@0 67 }
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Implements hook_cron().
Chris@0 72 */
Chris@0 73 function statistics_cron() {
Chris@0 74 $storage = \Drupal::service('statistics.storage.node');
Chris@0 75 $storage->resetDayCount();
Chris@0 76 $max_total_count = $storage->maxTotalCount();
Chris@0 77 \Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1.0, $max_total_count));
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Returns the most viewed content of all time, today, or the last-viewed node.
Chris@0 82 *
Chris@0 83 * @param string $dbfield
Chris@0 84 * The database field to use, one of:
Chris@0 85 * - 'totalcount': Integer that shows the top viewed content of all time.
Chris@0 86 * - 'daycount': Integer that shows the top viewed content for today.
Chris@0 87 * - 'timestamp': Integer that shows only the last viewed node.
Chris@0 88 * @param int $dbrows
Chris@0 89 * The number of rows to be returned.
Chris@0 90 *
Chris@0 91 * @return SelectQuery|false
Chris@0 92 * A query result containing the node ID, title, user ID that owns the node,
Chris@0 93 * and the username for the selected node(s), or FALSE if the query could not
Chris@0 94 * be executed correctly.
Chris@0 95 */
Chris@0 96 function statistics_title_list($dbfield, $dbrows) {
Chris@0 97 if (in_array($dbfield, ['totalcount', 'daycount', 'timestamp'])) {
Chris@0 98 $query = db_select('node_field_data', 'n');
Chris@0 99 $query->addTag('node_access');
Chris@0 100 $query->join('node_counter', 's', 'n.nid = s.nid');
Chris@0 101 $query->join('users_field_data', 'u', 'n.uid = u.uid');
Chris@0 102
Chris@0 103 return $query
Chris@0 104 ->fields('n', ['nid', 'title'])
Chris@0 105 ->fields('u', ['uid', 'name'])
Chris@0 106 ->condition($dbfield, 0, '<>')
Chris@0 107 ->condition('n.status', 1)
Chris@0 108 // @todo This should be actually filtering on the desired node status
Chris@0 109 // field language and just fall back to the default language.
Chris@0 110 ->condition('n.default_langcode', 1)
Chris@0 111 ->condition('u.default_langcode', 1)
Chris@0 112 ->orderBy($dbfield, 'DESC')
Chris@0 113 ->range(0, $dbrows)
Chris@0 114 ->execute();
Chris@0 115 }
Chris@0 116 return FALSE;
Chris@0 117 }
Chris@0 118
Chris@0 119 /**
Chris@0 120 * Retrieves a node's "view statistics".
Chris@0 121 *
Chris@0 122 * @deprecated in Drupal 8.2.x, will be removed before Drupal 9.0.0.
Chris@0 123 * Use \Drupal::service('statistics.storage.node')->fetchView($id).
Chris@0 124 */
Chris@0 125 function statistics_get($id) {
Chris@0 126 if ($id > 0) {
Chris@0 127 /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
Chris@0 128 $statistics = \Drupal::service('statistics.storage.node')->fetchView($id);
Chris@0 129
Chris@0 130 // For backwards compatibility, return FALSE if an invalid node ID was
Chris@0 131 // passed in.
Chris@0 132 if (!($statistics instanceof StatisticsViewsResult)) {
Chris@0 133 return FALSE;
Chris@0 134 }
Chris@0 135 return [
Chris@0 136 'totalcount' => $statistics->getTotalCount(),
Chris@0 137 'daycount' => $statistics->getDayCount(),
Chris@0 138 'timestamp' => $statistics->getTimestamp(),
Chris@0 139 ];
Chris@0 140 }
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Implements hook_ENTITY_TYPE_predelete() for node entities.
Chris@0 145 */
Chris@0 146 function statistics_node_predelete(EntityInterface $node) {
Chris@0 147 // Clean up statistics table when node is deleted.
Chris@0 148 $id = $node->id();
Chris@0 149 return \Drupal::service('statistics.storage.node')->deleteViews($id);
Chris@0 150 }
Chris@0 151
Chris@0 152 /**
Chris@0 153 * Implements hook_ranking().
Chris@0 154 */
Chris@0 155 function statistics_ranking() {
Chris@0 156 if (\Drupal::config('statistics.settings')->get('count_content_views')) {
Chris@0 157 return [
Chris@0 158 'views' => [
Chris@0 159 'title' => t('Number of views'),
Chris@0 160 'join' => [
Chris@0 161 'type' => 'LEFT',
Chris@0 162 'table' => 'node_counter',
Chris@0 163 'alias' => 'node_counter',
Chris@0 164 'on' => 'node_counter.nid = i.sid',
Chris@0 165 ],
Chris@0 166 // Inverse law that maps the highest view count on the site to 1 and 0
Chris@0 167 // to 0. Note that the ROUND here is necessary for PostgreSQL and SQLite
Chris@0 168 // in order to ensure that the :statistics_scale argument is treated as
Chris@0 169 // a numeric type, because the PostgreSQL PDO driver sometimes puts
Chris@0 170 // values in as strings instead of numbers in complex expressions like
Chris@0 171 // this.
Chris@0 172 'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * (ROUND(:statistics_scale, 4)))',
Chris@0 173 'arguments' => [':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0],
Chris@0 174 ],
Chris@0 175 ];
Chris@0 176 }
Chris@0 177 }
Chris@0 178
Chris@0 179 /**
Chris@0 180 * Implements hook_preprocess_HOOK() for block templates.
Chris@0 181 */
Chris@0 182 function statistics_preprocess_block(&$variables) {
Chris@0 183 if ($variables['configuration']['provider'] == 'statistics') {
Chris@0 184 $variables['attributes']['role'] = 'navigation';
Chris@0 185 }
Chris@0 186 }
Chris@0 187
Chris@0 188 /**
Chris@0 189 * Implements hook_block_alter().
Chris@0 190 *
Chris@0 191 * Removes the "popular" block from display if the module is not configured
Chris@0 192 * to count content views.
Chris@0 193 */
Chris@0 194 function statistics_block_alter(&$definitions) {
Chris@0 195 $statistics_count_content_views = \Drupal::config('statistics.settings')->get('count_content_views');
Chris@0 196 if (empty($statistics_count_content_views)) {
Chris@0 197 unset($definitions['statistics_popular_block']);
Chris@0 198 }
Chris@0 199 }