Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / tracker / tracker.pages.inc
1 <?php
2
3 /**
4  * @file
5  * User page callbacks for tracker.module.
6  */
7
8 use Drupal\Core\Cache\Cache;
9 use Drupal\node\Entity\Node;
10
11 /**
12  * Page callback: Generates a page of tracked nodes for the site.
13  *
14  * Queries the database for info, adds RDFa info if applicable, and generates
15  * the render array that will be used to render the page.
16  *
17  * @param \Drupal\user\UserInterface $account
18  *   (optional) The user account to track.
19  *
20  * @return array
21  *   A renderable array.
22  */
23 function tracker_page($account = NULL) {
24   if ($account) {
25     $query = db_select('tracker_user', 't')
26       ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
27       ->addMetaData('base_table', 'tracker_user')
28       ->condition('t.uid', $account->id());
29   }
30   else {
31     $query = db_select('tracker_node', 't', ['target' => 'replica'])
32       ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
33       ->addMetaData('base_table', 'tracker_node');
34   }
35
36   // This array acts as a placeholder for the data selected later
37   // while keeping the correct order.
38   $tracker_data = $query
39     ->addTag('node_access')
40     ->fields('t', ['nid', 'changed'])
41     ->condition('t.published', 1)
42     ->orderBy('t.changed', 'DESC')
43     ->limit(25)
44     ->execute()
45     ->fetchAllAssoc('nid');
46
47   $cache_tags = [];
48   $rows = [];
49   if (!empty($tracker_data)) {
50     // Load nodes into an array with the same order as $tracker_data.
51     $nodes = Node::loadMultiple(array_keys($tracker_data));
52
53     // Enrich the node data.
54     $result = \Drupal::service('comment.statistics')->read($nodes, 'node', FALSE);
55     foreach ($result as $statistics) {
56       // The node ID may not be unique; there can be multiple comment fields.
57       // Make comment_count the total of all comments.
58       $nid = $statistics->entity_id;
59       if (empty($nodes[$nid]->comment_count)
60           || !is_numeric($nodes[$nid]->comment_count)) {
61         $nodes[$nid]->comment_count = $statistics->comment_count;
62       }
63       else {
64         $nodes[$nid]->comment_count += $statistics->comment_count;
65       }
66       // Make the last comment timestamp reflect the latest comment.
67       if (!isset($nodes[$nid]->last_comment_timestamp)) {
68         $nodes[$nid]->last_comment_timestamp = $statistics->last_comment_timestamp;
69       }
70       else {
71         $nodes[$nid]->last_comment_timestamp = max($nodes[$nid]->last_comment_timestamp, $statistics->last_comment_timestamp);
72       }
73     }
74
75     // Display the data.
76     foreach ($nodes as $node) {
77       // Set the last activity time from tracker data. This also takes into
78       // account comment activity, so getChangedTime() is not used.
79       $node->last_activity = $tracker_data[$node->id()]->changed;
80
81       // Determine the number of comments.
82       $comments = 0;
83       if ($node->comment_count) {
84         $comments = $node->comment_count;
85       }
86
87       $row = [
88         'type' => node_get_type_label($node),
89         'title' => [
90           'data' => [
91             '#type' => 'link',
92             '#url' => $node->urlInfo(),
93             '#title' => $node->getTitle(),
94           ],
95           'data-history-node-id' => $node->id(),
96           'data-history-node-timestamp' => $node->getChangedTime(),
97         ],
98         'author' => [
99           'data' => [
100             '#theme' => 'username',
101             '#account' => $node->getOwner(),
102           ],
103         ],
104         'comments' => [
105           'class' => ['comments'],
106           'data' => $comments,
107           'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp,
108         ],
109         'last updated' => [
110           'data' => t('@time ago', [
111             '@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity),
112           ]),
113         ],
114       ];
115
116       $rows[] = $row;
117
118       // Add node and node owner to cache tags.
119       $cache_tags = Cache::mergeTags($cache_tags, $node->getCacheTags());
120       if ($node->getOwner()) {
121         $cache_tags = Cache::mergeTags($cache_tags, $node->getOwner()->getCacheTags());
122       }
123     }
124   }
125
126   // Add the list cache tag for nodes.
127   $cache_tags = Cache::mergeTags($cache_tags, \Drupal::entityManager()->getDefinition('node')->getListCacheTags());
128
129   $page['tracker'] = [
130     '#rows' => $rows,
131     '#header' => [t('Type'), t('Title'), t('Author'), t('Comments'), t('Last updated')],
132     '#type' => 'table',
133     '#empty' => t('No content available.'),
134   ];
135   $page['pager'] = [
136     '#type' => 'pager',
137     '#weight' => 10,
138   ];
139   $page['#sorted'] = TRUE;
140   $page['#cache']['tags'] = $cache_tags;
141   $page['#cache']['contexts'][] = 'user.node_grants:view';
142
143   // Display the reading history if that module is enabled.
144   if (\Drupal::moduleHandler()->moduleExists('history')) {
145     // Reading history is tracked for authenticated users only.
146     if (\Drupal::currentUser()->isAuthenticated()) {
147       $page['#attached']['library'][] = 'tracker/history';
148     }
149     $page['#cache']['contexts'][] = 'user.roles:authenticated';
150   }
151
152   return $page;
153 }