function tracker_cron

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

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);
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!tracker!tracker.module/function/tracker_cron/8.1.x