tracker_cron()
Implements hook_cron().
Updates tracking information for any items still to be tracked. The state 'tracker.index_nid' is set to ((the last node ID that was indexed) - 1) and used to select the nodes to be processed. If there are no remaining nodes to process, 'tracker.index_nid' will be 0. This process does not run regularly on live sites, rather it updates tracking info once on an existing site just after the tracker module was installed.
File
- core/modules/tracker/tracker.module, line 44
- Tracks recent content posted by a user or users.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | function tracker_cron() { $state = \Drupal::state(); $max_nid = $state ->get( 'tracker.index_nid' ) ? : 0; if ( $max_nid > 0) { $last_nid = FALSE; $count = 0; $nids = \Drupal::entityQuery( 'node' ) ->condition( 'nid' , $max_nid , '<=' ) ->sort( 'nid' , 'DESC' ) ->range(0, \Drupal::config( 'tracker.settings' )->get( 'cron_index_limit' )) ->execute(); $nodes = Node::loadMultiple( $nids ); foreach ( $nodes as $nid => $node ) { // Calculate the changed timestamp for this node. $changed = _tracker_calculate_changed( $node ); // Remove existing data for this node. db_delete( 'tracker_node' ) ->condition( 'nid' , $nid ) ->execute(); db_delete( 'tracker_user' ) ->condition( 'nid' , $nid ) ->execute(); // Insert the node-level data. db_insert( 'tracker_node' ) ->fields( array ( 'nid' => $nid , 'published' => $node ->isPublished(), 'changed' => $changed , )) ->execute(); // Insert the user-level data for the node's author. db_insert( 'tracker_user' ) ->fields( array ( 'nid' => $nid , 'published' => $node ->isPublished(), 'changed' => $changed , 'uid' => $node ->getOwnerId(), )) ->execute(); // Insert the user-level data for the commenters (except if a commenter // is the node's author). // Get unique user IDs via entityQueryAggregate because it's the easiest // database agnostic way. We don't actually care about the comments here // so don't add an aggregate field. $result = \Drupal::entityQueryAggregate( 'comment' ) ->condition( 'entity_type' , 'node' ) ->condition( 'entity_id' , $node ->id()) ->condition( 'uid' , $node ->getOwnerId(), '<>' ) ->condition( 'status' , CommentInterface::PUBLISHED) ->groupBy( 'uid' ) ->execute(); if ( $result ) { $query = db_insert( 'tracker_user' ); foreach ( $result as $row ) { $query ->fields( array ( 'uid' => $row [ 'uid' ], 'nid' => $nid , 'published' => CommentInterface::PUBLISHED, 'changed' => $changed , )); } $query ->execute(); } // Note that we have indexed at least one node. $last_nid = $nid ; $count ++; } if ( $last_nid !== FALSE) { // Prepare a starting point for the next run. $state ->set( 'tracker.index_nid' , $last_nid - 1); \Drupal::logger( 'tracker' )->notice( 'Indexed %count content items for tracking.' , array ( '%count' => $count )); } else { // If all nodes have been indexed, set to zero to skip future cron runs. $state ->set( 'tracker.index_nid' , 0); } } } |
Please login to continue.