Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Tracks recent content posted by a user or users.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@18
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
10 use Drupal\comment\CommentInterface;
|
Chris@0
|
11 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
12 use Drupal\node\Entity\Node;
|
Chris@0
|
13 use Drupal\node\NodeInterface;
|
Chris@0
|
14 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Implements hook_help().
|
Chris@0
|
18 */
|
Chris@0
|
19 function tracker_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
20 switch ($route_name) {
|
Chris@0
|
21 case 'help.page.tracker':
|
Chris@0
|
22 $output = '<h3>' . t('About') . '</h3>';
|
Chris@0
|
23 $output .= '<p>' . t('The Activity Tracker module displays the most recently added and updated content on your site, and allows you to follow new content created by each user. This module has no configuration options. For more information, see the <a href=":tracker">online documentation for the Activity Tracker module</a>.', [':tracker' => 'https://www.drupal.org/documentation/modules/tracker']) . '</p>';
|
Chris@0
|
24 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
25 $output .= '<dl>';
|
Chris@0
|
26 $output .= '<dt>' . t('Tracking new and updated site content') . '</dt>';
|
Chris@18
|
27 $output .= '<dd>' . t('The <a href=":recent">Recent content</a> page shows new and updated content in reverse chronological order, listing the content type, title, author\'s name, number of comments, and time of last update. Content is considered updated when changes occur in the text, or when new comments are added. The <em>My recent content</em> tab limits the list to the currently logged-in user.', [':recent' => Url::fromRoute('tracker.page')->toString()]) . '</dd>';
|
Chris@0
|
28 $output .= '<dt>' . t('Tracking user-specific content') . '</dt>';
|
Chris@0
|
29 $output .= '<dd>' . t("To follow a specific user's new and updated content, select the <em>Activity</em> tab from the user's profile page.") . '</dd>';
|
Chris@0
|
30 $output .= '</dl>';
|
Chris@0
|
31 return $output;
|
Chris@0
|
32 }
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Implements hook_cron().
|
Chris@0
|
37 *
|
Chris@0
|
38 * Updates tracking information for any items still to be tracked. The state
|
Chris@0
|
39 * 'tracker.index_nid' is set to ((the last node ID that was indexed) - 1) and
|
Chris@0
|
40 * used to select the nodes to be processed. If there are no remaining nodes to
|
Chris@0
|
41 * process, 'tracker.index_nid' will be 0.
|
Chris@0
|
42 * This process does not run regularly on live sites, rather it updates tracking
|
Chris@0
|
43 * info once on an existing site just after the tracker module was installed.
|
Chris@0
|
44 */
|
Chris@0
|
45 function tracker_cron() {
|
Chris@0
|
46 $state = \Drupal::state();
|
Chris@0
|
47 $max_nid = $state->get('tracker.index_nid') ?: 0;
|
Chris@0
|
48 if ($max_nid > 0) {
|
Chris@0
|
49 $last_nid = FALSE;
|
Chris@0
|
50 $count = 0;
|
Chris@0
|
51
|
Chris@0
|
52 $nids = \Drupal::entityQuery('node')
|
Chris@0
|
53 ->condition('nid', $max_nid, '<=')
|
Chris@0
|
54 ->sort('nid', 'DESC')
|
Chris@0
|
55 ->range(0, \Drupal::config('tracker.settings')->get('cron_index_limit'))
|
Chris@0
|
56 ->execute();
|
Chris@0
|
57
|
Chris@0
|
58 $nodes = Node::loadMultiple($nids);
|
Chris@18
|
59 $connection = \Drupal::database();
|
Chris@0
|
60 foreach ($nodes as $nid => $node) {
|
Chris@0
|
61
|
Chris@0
|
62 // Calculate the changed timestamp for this node.
|
Chris@0
|
63 $changed = _tracker_calculate_changed($node);
|
Chris@0
|
64
|
Chris@0
|
65 // Remove existing data for this node.
|
Chris@18
|
66 $connection->delete('tracker_node')
|
Chris@0
|
67 ->condition('nid', $nid)
|
Chris@0
|
68 ->execute();
|
Chris@18
|
69 $connection->delete('tracker_user')
|
Chris@0
|
70 ->condition('nid', $nid)
|
Chris@0
|
71 ->execute();
|
Chris@0
|
72
|
Chris@0
|
73 // Insert the node-level data.
|
Chris@18
|
74 $connection->insert('tracker_node')
|
Chris@0
|
75 ->fields([
|
Chris@0
|
76 'nid' => $nid,
|
Chris@16
|
77 'published' => (int) $node->isPublished(),
|
Chris@0
|
78 'changed' => $changed,
|
Chris@0
|
79 ])
|
Chris@0
|
80 ->execute();
|
Chris@0
|
81
|
Chris@0
|
82 // Insert the user-level data for the node's author.
|
Chris@18
|
83 $connection->insert('tracker_user')
|
Chris@0
|
84 ->fields([
|
Chris@0
|
85 'nid' => $nid,
|
Chris@16
|
86 'published' => (int) $node->isPublished(),
|
Chris@0
|
87 'changed' => $changed,
|
Chris@0
|
88 'uid' => $node->getOwnerId(),
|
Chris@0
|
89 ])
|
Chris@0
|
90 ->execute();
|
Chris@0
|
91
|
Chris@0
|
92 // Insert the user-level data for the commenters (except if a commenter
|
Chris@0
|
93 // is the node's author).
|
Chris@0
|
94
|
Chris@0
|
95 // Get unique user IDs via entityQueryAggregate because it's the easiest
|
Chris@0
|
96 // database agnostic way. We don't actually care about the comments here
|
Chris@0
|
97 // so don't add an aggregate field.
|
Chris@0
|
98 $result = \Drupal::entityQueryAggregate('comment')
|
Chris@0
|
99 ->condition('entity_type', 'node')
|
Chris@0
|
100 ->condition('entity_id', $node->id())
|
Chris@0
|
101 ->condition('uid', $node->getOwnerId(), '<>')
|
Chris@0
|
102 ->condition('status', CommentInterface::PUBLISHED)
|
Chris@0
|
103 ->groupBy('uid')
|
Chris@0
|
104 ->execute();
|
Chris@0
|
105 if ($result) {
|
Chris@18
|
106 $query = $connection->insert('tracker_user');
|
Chris@0
|
107 foreach ($result as $row) {
|
Chris@0
|
108 $query->fields([
|
Chris@0
|
109 'uid' => $row['uid'],
|
Chris@0
|
110 'nid' => $nid,
|
Chris@0
|
111 'published' => CommentInterface::PUBLISHED,
|
Chris@0
|
112 'changed' => $changed,
|
Chris@0
|
113 ]);
|
Chris@0
|
114 }
|
Chris@0
|
115 $query->execute();
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 // Note that we have indexed at least one node.
|
Chris@0
|
119 $last_nid = $nid;
|
Chris@0
|
120
|
Chris@0
|
121 $count++;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 if ($last_nid !== FALSE) {
|
Chris@0
|
125 // Prepare a starting point for the next run.
|
Chris@0
|
126 $state->set('tracker.index_nid', $last_nid - 1);
|
Chris@0
|
127
|
Chris@0
|
128 \Drupal::logger('tracker')->notice('Indexed %count content items for tracking.', ['%count' => $count]);
|
Chris@0
|
129 }
|
Chris@0
|
130 else {
|
Chris@0
|
131 // If all nodes have been indexed, set to zero to skip future cron runs.
|
Chris@0
|
132 $state->set('tracker.index_nid', 0);
|
Chris@0
|
133 }
|
Chris@0
|
134 }
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 /**
|
Chris@0
|
138 * Access callback: Determines access permission for a user's own account.
|
Chris@0
|
139 *
|
Chris@0
|
140 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. As
|
Chris@0
|
141 * internal API, _tracker_user_access() may also be removed in a minor
|
Chris@0
|
142 * release.
|
Chris@0
|
143 *
|
Chris@0
|
144 * @internal
|
Chris@0
|
145 *
|
Chris@0
|
146 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
147 * The user account to track.
|
Chris@0
|
148 *
|
Chris@0
|
149 * @return bool
|
Chris@0
|
150 * TRUE if a user is accessing tracking info for their own account and
|
Chris@0
|
151 * has permission to access the content.
|
Chris@0
|
152 */
|
Chris@0
|
153 function _tracker_myrecent_access(AccountInterface $account) {
|
Chris@0
|
154 @trigger_error('_tracker_myrecent_access() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.', E_USER_DEPRECATED);
|
Chris@0
|
155 // This path is only allowed for authenticated users looking at their own content.
|
Chris@0
|
156 return $account->id() && (\Drupal::currentUser()->id() == $account->id()) && $account->hasPermission('access content');
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Access callback: Determines access permission for an account.
|
Chris@0
|
161 *
|
Chris@0
|
162 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. As
|
Chris@0
|
163 * internal API, _tracker_user_access() may also be removed in a minor
|
Chris@0
|
164 * release.
|
Chris@0
|
165 *
|
Chris@0
|
166 * @internal
|
Chris@0
|
167 *
|
Chris@0
|
168 * @param int $account
|
Chris@0
|
169 * The user account ID to track.
|
Chris@0
|
170 *
|
Chris@0
|
171 * @return bool
|
Chris@0
|
172 * TRUE if a user has permission to access the account for $account and
|
Chris@0
|
173 * has permission to access the content.
|
Chris@0
|
174 */
|
Chris@0
|
175 function _tracker_user_access($account) {
|
Chris@0
|
176 @trigger_error('_tracker_user_access() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.', E_USER_DEPRECATED);
|
Chris@0
|
177 return $account->access('view') && $account->hasPermission('access content');
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * Implements hook_ENTITY_TYPE_insert() for node entities.
|
Chris@0
|
182 *
|
Chris@0
|
183 * Adds new tracking information for this node since it's new.
|
Chris@0
|
184 */
|
Chris@0
|
185 function tracker_node_insert(NodeInterface $node, $arg = 0) {
|
Chris@0
|
186 _tracker_add($node->id(), $node->getOwnerId(), $node->getChangedTime());
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * Implements hook_ENTITY_TYPE_update() for node entities.
|
Chris@0
|
191 *
|
Chris@0
|
192 * Adds tracking information for this node since it's been updated.
|
Chris@0
|
193 */
|
Chris@0
|
194 function tracker_node_update(NodeInterface $node, $arg = 0) {
|
Chris@0
|
195 _tracker_add($node->id(), $node->getOwnerId(), $node->getChangedTime());
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 /**
|
Chris@0
|
199 * Implements hook_ENTITY_TYPE_predelete() for node entities.
|
Chris@0
|
200 *
|
Chris@0
|
201 * Deletes tracking information for a node.
|
Chris@0
|
202 */
|
Chris@0
|
203 function tracker_node_predelete(EntityInterface $node, $arg = 0) {
|
Chris@18
|
204 $connection = \Drupal::database();
|
Chris@18
|
205 $connection->delete('tracker_node')
|
Chris@0
|
206 ->condition('nid', $node->id())
|
Chris@0
|
207 ->execute();
|
Chris@18
|
208 $connection->delete('tracker_user')
|
Chris@0
|
209 ->condition('nid', $node->id())
|
Chris@0
|
210 ->execute();
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 /**
|
Chris@0
|
214 * Implements hook_ENTITY_TYPE_update() for comment entities.
|
Chris@0
|
215 */
|
Chris@0
|
216 function tracker_comment_update(CommentInterface $comment) {
|
Chris@0
|
217 if ($comment->getCommentedEntityTypeId() == 'node') {
|
Chris@0
|
218 if ($comment->isPublished()) {
|
Chris@0
|
219 _tracker_add($comment->getCommentedEntityId(), $comment->getOwnerId(), $comment->getChangedTime());
|
Chris@0
|
220 }
|
Chris@0
|
221 else {
|
Chris@0
|
222 _tracker_remove($comment->getCommentedEntityId(), $comment->getOwnerId(), $comment->getChangedTime());
|
Chris@0
|
223 }
|
Chris@0
|
224 }
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * Implements hook_ENTITY_TYPE_insert() for comment entities.
|
Chris@0
|
229 */
|
Chris@0
|
230 function tracker_comment_insert(CommentInterface $comment) {
|
Chris@0
|
231 if ($comment->getCommentedEntityTypeId() == 'node' && $comment->isPublished()) {
|
Chris@0
|
232 _tracker_add($comment->getCommentedEntityId(), $comment->getOwnerId(), $comment->getChangedTime());
|
Chris@0
|
233 }
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * Implements hook_ENTITY_TYPE_delete() for comment entities.
|
Chris@0
|
238 */
|
Chris@0
|
239 function tracker_comment_delete(CommentInterface $comment) {
|
Chris@0
|
240 if ($comment->getCommentedEntityTypeId() == 'node') {
|
Chris@0
|
241 _tracker_remove($comment->getCommentedEntityId(), $comment->getOwnerId(), $comment->getChangedTime());
|
Chris@0
|
242 }
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@0
|
246 * Updates indexing tables when a node is added, updated, or commented on.
|
Chris@0
|
247 *
|
Chris@0
|
248 * @param int $nid
|
Chris@0
|
249 * A node ID.
|
Chris@0
|
250 * @param int $uid
|
Chris@0
|
251 * The node or comment author.
|
Chris@0
|
252 * @param int $changed
|
Chris@0
|
253 * The node updated timestamp or comment timestamp.
|
Chris@0
|
254 */
|
Chris@0
|
255 function _tracker_add($nid, $uid, $changed) {
|
Chris@18
|
256 $connection = \Drupal::database();
|
Chris@0
|
257 // @todo This should be actually filtering on the desired language and just
|
Chris@0
|
258 // fall back to the default language.
|
Chris@0
|
259 $node = db_query('SELECT nid, status, uid, changed FROM {node_field_data} WHERE nid = :nid AND default_langcode = 1 ORDER BY changed DESC, status DESC', [':nid' => $nid])->fetchObject();
|
Chris@0
|
260
|
Chris@0
|
261 // Adding a comment can only increase the changed timestamp, so our
|
Chris@0
|
262 // calculation here is simple.
|
Chris@0
|
263 $changed = max($node->changed, $changed);
|
Chris@0
|
264
|
Chris@0
|
265 // Update the node-level data.
|
Chris@18
|
266 $connection->merge('tracker_node')
|
Chris@0
|
267 ->key('nid', $nid)
|
Chris@0
|
268 ->fields([
|
Chris@0
|
269 'changed' => $changed,
|
Chris@0
|
270 'published' => $node->status,
|
Chris@0
|
271 ])
|
Chris@0
|
272 ->execute();
|
Chris@0
|
273
|
Chris@0
|
274 // Create or update the user-level data, first for the user posting.
|
Chris@18
|
275 $connection->merge('tracker_user')
|
Chris@0
|
276 ->keys([
|
Chris@0
|
277 'nid' => $nid,
|
Chris@0
|
278 'uid' => $uid,
|
Chris@0
|
279 ])
|
Chris@0
|
280 ->fields([
|
Chris@0
|
281 'changed' => $changed,
|
Chris@0
|
282 'published' => $node->status,
|
Chris@0
|
283 ])
|
Chris@0
|
284 ->execute();
|
Chris@0
|
285 // Update the times for all the other users tracking the post.
|
Chris@18
|
286 $connection->update('tracker_user')
|
Chris@0
|
287 ->condition('nid', $nid)
|
Chris@0
|
288 ->fields([
|
Chris@0
|
289 'changed' => $changed,
|
Chris@0
|
290 'published' => $node->status,
|
Chris@0
|
291 ])
|
Chris@0
|
292 ->execute();
|
Chris@0
|
293 }
|
Chris@0
|
294
|
Chris@0
|
295 /**
|
Chris@0
|
296 * Picks the most recent timestamp between node changed and the last comment.
|
Chris@0
|
297 *
|
Chris@0
|
298 * @param \Drupal\node\NodeInterface $node
|
Chris@0
|
299 * The node entity.
|
Chris@0
|
300 *
|
Chris@0
|
301 * @return int
|
Chris@0
|
302 * The node changed timestamp, or most recent comment timestamp, whichever is
|
Chris@0
|
303 * the greatest.
|
Chris@0
|
304 *
|
Chris@0
|
305 * @todo Check if we should introduce 'language context' here, because the
|
Chris@0
|
306 * callers may need different timestamps depending on the users' language?
|
Chris@0
|
307 */
|
Chris@0
|
308 function _tracker_calculate_changed($node) {
|
Chris@0
|
309 $changed = $node->getChangedTime();
|
Chris@0
|
310 $latest_comment = \Drupal::service('comment.statistics')->read([$node], 'node', FALSE);
|
Chris@0
|
311 if ($latest_comment && $latest_comment->last_comment_timestamp > $changed) {
|
Chris@0
|
312 $changed = $latest_comment->last_comment_timestamp;
|
Chris@0
|
313 }
|
Chris@0
|
314 return $changed;
|
Chris@0
|
315 }
|
Chris@0
|
316
|
Chris@0
|
317 /**
|
Chris@0
|
318 * Cleans up indexed data when nodes or comments are removed.
|
Chris@0
|
319 *
|
Chris@0
|
320 * @param int $nid
|
Chris@0
|
321 * The node ID.
|
Chris@0
|
322 * @param int $uid
|
Chris@0
|
323 * The author of the node or comment.
|
Chris@0
|
324 * @param int $changed
|
Chris@0
|
325 * The last changed timestamp of the node.
|
Chris@0
|
326 */
|
Chris@0
|
327 function _tracker_remove($nid, $uid = NULL, $changed = NULL) {
|
Chris@0
|
328 $node = Node::load($nid);
|
Chris@18
|
329 $connection = \Drupal::database();
|
Chris@0
|
330
|
Chris@0
|
331 // The user only keeps their subscription if the node exists.
|
Chris@0
|
332 if ($node) {
|
Chris@0
|
333 // And they are the author of the node.
|
Chris@0
|
334 $keep_subscription = ($node->getOwnerId() == $uid);
|
Chris@0
|
335
|
Chris@0
|
336 // Or if they have commented on the node.
|
Chris@0
|
337 if (!$keep_subscription) {
|
Chris@0
|
338 // Check if the user has commented at least once on the given nid.
|
Chris@0
|
339 $keep_subscription = \Drupal::entityQuery('comment')
|
Chris@0
|
340 ->condition('entity_type', 'node')
|
Chris@0
|
341 ->condition('entity_id', $nid)
|
Chris@0
|
342 ->condition('uid', $uid)
|
Chris@0
|
343 ->condition('status', CommentInterface::PUBLISHED)
|
Chris@0
|
344 ->range(0, 1)
|
Chris@0
|
345 ->count()
|
Chris@0
|
346 ->execute();
|
Chris@0
|
347 }
|
Chris@0
|
348
|
Chris@0
|
349 // If we haven't found a reason to keep the user's subscription, delete it.
|
Chris@0
|
350 if (!$keep_subscription) {
|
Chris@18
|
351 $connection->delete('tracker_user')
|
Chris@0
|
352 ->condition('nid', $nid)
|
Chris@0
|
353 ->condition('uid', $uid)
|
Chris@0
|
354 ->execute();
|
Chris@0
|
355 }
|
Chris@0
|
356
|
Chris@0
|
357 // Now we need to update the (possibly) changed timestamps for other users
|
Chris@0
|
358 // and the node itself.
|
Chris@0
|
359 // We only need to do this if the removed item has a timestamp that equals
|
Chris@0
|
360 // or exceeds the listed changed timestamp for the node.
|
Chris@0
|
361 $tracker_node = db_query('SELECT nid, changed FROM {tracker_node} WHERE nid = :nid', [':nid' => $nid])->fetchObject();
|
Chris@0
|
362 if ($tracker_node && $changed >= $tracker_node->changed) {
|
Chris@0
|
363 // If we're here, the item being removed is *possibly* the item that
|
Chris@0
|
364 // established the node's changed timestamp.
|
Chris@0
|
365
|
Chris@0
|
366 // We just have to recalculate things from scratch.
|
Chris@0
|
367 $changed = _tracker_calculate_changed($node);
|
Chris@0
|
368
|
Chris@0
|
369 // And then we push the out the new changed timestamp to our denormalized
|
Chris@0
|
370 // tables.
|
Chris@18
|
371 $connection->update('tracker_node')
|
Chris@0
|
372 ->fields([
|
Chris@0
|
373 'changed' => $changed,
|
Chris@0
|
374 'published' => $node->isPublished(),
|
Chris@0
|
375 ])
|
Chris@0
|
376 ->condition('nid', $nid)
|
Chris@0
|
377 ->execute();
|
Chris@18
|
378 $connection->update('tracker_node')
|
Chris@0
|
379 ->fields([
|
Chris@0
|
380 'changed' => $changed,
|
Chris@0
|
381 'published' => $node->isPublished(),
|
Chris@0
|
382 ])
|
Chris@0
|
383 ->condition('nid', $nid)
|
Chris@0
|
384 ->execute();
|
Chris@0
|
385 }
|
Chris@0
|
386 }
|
Chris@0
|
387 else {
|
Chris@0
|
388 // If the node doesn't exist, remove everything.
|
Chris@18
|
389 $connection->delete('tracker_node')
|
Chris@0
|
390 ->condition('nid', $nid)
|
Chris@0
|
391 ->execute();
|
Chris@18
|
392 $connection->delete('tracker_user')
|
Chris@0
|
393 ->condition('nid', $nid)
|
Chris@0
|
394 ->execute();
|
Chris@0
|
395 }
|
Chris@0
|
396 }
|