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