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@18
|
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' => Url::fromRoute('statistics.settings')->toString(), ':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? Url::fromRoute('block.admin_display')->toString() : '#']) . '</dd>';
|
Chris@0
|
28 $output .= '<dt>' . t('Page view counter') . '</dt>';
|
Chris@18
|
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' => Url::fromRoute('statistics.settings')->toString(), ':permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-statistics'])->toString()]) . '</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@16
|
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@18
|
95 *
|
Chris@18
|
96 * @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0.
|
Chris@18
|
97 * Use \Drupal\statistics\NodeStatisticsDatabaseStorage::fetchAll() instead.
|
Chris@0
|
98 */
|
Chris@0
|
99 function statistics_title_list($dbfield, $dbrows) {
|
Chris@18
|
100 @trigger_error('statistics_title_list() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use \Drupal\statistics\NodeStatisticsDatabaseStorage::fetchAll() instead.', E_USER_DEPRECATED);
|
Chris@0
|
101 if (in_array($dbfield, ['totalcount', 'daycount', 'timestamp'])) {
|
Chris@18
|
102 $query = \Drupal::database()->select('node_field_data', 'n');
|
Chris@0
|
103 $query->addTag('node_access');
|
Chris@0
|
104 $query->join('node_counter', 's', 'n.nid = s.nid');
|
Chris@0
|
105 $query->join('users_field_data', 'u', 'n.uid = u.uid');
|
Chris@0
|
106
|
Chris@0
|
107 return $query
|
Chris@0
|
108 ->fields('n', ['nid', 'title'])
|
Chris@0
|
109 ->fields('u', ['uid', 'name'])
|
Chris@0
|
110 ->condition($dbfield, 0, '<>')
|
Chris@0
|
111 ->condition('n.status', 1)
|
Chris@0
|
112 // @todo This should be actually filtering on the desired node status
|
Chris@0
|
113 // field language and just fall back to the default language.
|
Chris@0
|
114 ->condition('n.default_langcode', 1)
|
Chris@0
|
115 ->condition('u.default_langcode', 1)
|
Chris@0
|
116 ->orderBy($dbfield, 'DESC')
|
Chris@0
|
117 ->range(0, $dbrows)
|
Chris@0
|
118 ->execute();
|
Chris@0
|
119 }
|
Chris@0
|
120 return FALSE;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * Retrieves a node's "view statistics".
|
Chris@0
|
125 *
|
Chris@0
|
126 * @deprecated in Drupal 8.2.x, will be removed before Drupal 9.0.0.
|
Chris@0
|
127 * Use \Drupal::service('statistics.storage.node')->fetchView($id).
|
Chris@0
|
128 */
|
Chris@0
|
129 function statistics_get($id) {
|
Chris@0
|
130 if ($id > 0) {
|
Chris@0
|
131 /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
|
Chris@0
|
132 $statistics = \Drupal::service('statistics.storage.node')->fetchView($id);
|
Chris@0
|
133
|
Chris@0
|
134 // For backwards compatibility, return FALSE if an invalid node ID was
|
Chris@0
|
135 // passed in.
|
Chris@0
|
136 if (!($statistics instanceof StatisticsViewsResult)) {
|
Chris@0
|
137 return FALSE;
|
Chris@0
|
138 }
|
Chris@0
|
139 return [
|
Chris@0
|
140 'totalcount' => $statistics->getTotalCount(),
|
Chris@0
|
141 'daycount' => $statistics->getDayCount(),
|
Chris@0
|
142 'timestamp' => $statistics->getTimestamp(),
|
Chris@0
|
143 ];
|
Chris@0
|
144 }
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Implements hook_ENTITY_TYPE_predelete() for node entities.
|
Chris@0
|
149 */
|
Chris@0
|
150 function statistics_node_predelete(EntityInterface $node) {
|
Chris@0
|
151 // Clean up statistics table when node is deleted.
|
Chris@0
|
152 $id = $node->id();
|
Chris@0
|
153 return \Drupal::service('statistics.storage.node')->deleteViews($id);
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * Implements hook_ranking().
|
Chris@0
|
158 */
|
Chris@0
|
159 function statistics_ranking() {
|
Chris@0
|
160 if (\Drupal::config('statistics.settings')->get('count_content_views')) {
|
Chris@0
|
161 return [
|
Chris@0
|
162 'views' => [
|
Chris@0
|
163 'title' => t('Number of views'),
|
Chris@0
|
164 'join' => [
|
Chris@0
|
165 'type' => 'LEFT',
|
Chris@0
|
166 'table' => 'node_counter',
|
Chris@0
|
167 'alias' => 'node_counter',
|
Chris@0
|
168 'on' => 'node_counter.nid = i.sid',
|
Chris@0
|
169 ],
|
Chris@0
|
170 // Inverse law that maps the highest view count on the site to 1 and 0
|
Chris@0
|
171 // to 0. Note that the ROUND here is necessary for PostgreSQL and SQLite
|
Chris@0
|
172 // in order to ensure that the :statistics_scale argument is treated as
|
Chris@0
|
173 // a numeric type, because the PostgreSQL PDO driver sometimes puts
|
Chris@0
|
174 // values in as strings instead of numbers in complex expressions like
|
Chris@0
|
175 // this.
|
Chris@0
|
176 'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * (ROUND(:statistics_scale, 4)))',
|
Chris@0
|
177 'arguments' => [':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0],
|
Chris@0
|
178 ],
|
Chris@0
|
179 ];
|
Chris@0
|
180 }
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * Implements hook_preprocess_HOOK() for block templates.
|
Chris@0
|
185 */
|
Chris@0
|
186 function statistics_preprocess_block(&$variables) {
|
Chris@0
|
187 if ($variables['configuration']['provider'] == 'statistics') {
|
Chris@0
|
188 $variables['attributes']['role'] = 'navigation';
|
Chris@0
|
189 }
|
Chris@0
|
190 }
|
Chris@0
|
191
|
Chris@0
|
192 /**
|
Chris@0
|
193 * Implements hook_block_alter().
|
Chris@0
|
194 *
|
Chris@0
|
195 * Removes the "popular" block from display if the module is not configured
|
Chris@0
|
196 * to count content views.
|
Chris@0
|
197 */
|
Chris@0
|
198 function statistics_block_alter(&$definitions) {
|
Chris@0
|
199 $statistics_count_content_views = \Drupal::config('statistics.settings')->get('count_content_views');
|
Chris@0
|
200 if (empty($statistics_count_content_views)) {
|
Chris@0
|
201 unset($definitions['statistics_popular_block']);
|
Chris@0
|
202 }
|
Chris@0
|
203 }
|