function 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.

2 calls to tracker_cron()
TrackerTest::testTrackerCronIndexing in drupal/core/modules/tracker/lib/Drupal/tracker/Tests/TrackerTest.php
Tests that existing nodes are indexed by cron.
tracker_enable in drupal/core/modules/tracker/tracker.install
Implements hook_enable().

File

drupal/core/modules/tracker/tracker.module, line 81
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) {
    $batch_size = config('tracker.settings')
      ->get('cron_index_limit');
    $last_nid = FALSE;

    // @todo This should be actually filtering on the desired language and just
    //   fall back to the default language.
    $result = db_query_range('SELECT nid, uid, status FROM {node_field_data} WHERE nid <= :max_nid AND default_langcode = 1 ORDER BY nid DESC', 0, $batch_size, array(
      ':max_nid' => $max_nid,
    ), array(
      'target' => 'slave',
    ));
    $count = 0;
    foreach ($result as $row) {

      // Calculate the changed timestamp for this node.
      $changed = _tracker_calculate_changed($row->nid);

      // Remove existing data for this node.
      db_delete('tracker_node')
        ->condition('nid', $row->nid)
        ->execute();
      db_delete('tracker_user')
        ->condition('nid', $row->nid)
        ->execute();

      // Insert the node-level data.
      db_insert('tracker_node')
        ->fields(array(
        'nid' => $row->nid,
        'published' => $row->status,
        'changed' => $changed,
      ))
        ->execute();

      // Insert the user-level data for the node's author.
      db_insert('tracker_user')
        ->fields(array(
        'nid' => $row->nid,
        'published' => $row->status,
        'changed' => $changed,
        'uid' => $row->uid,
      ))
        ->execute();
      $query = db_select('comment', 'c', array(
        'target' => 'slave',
      ));

      // Force PostgreSQL to do an implicit cast by adding 0.
      $query
        ->addExpression('0 + :changed', 'changed', array(
        ':changed' => $changed,
      ));
      $query
        ->addField('c', 'status', 'published');
      $query
        ->distinct()
        ->fields('c', array(
        'uid',
        'nid',
      ))
        ->condition('c.nid', $row->nid)
        ->condition('c.uid', $row->uid, '<>')
        ->condition('c.status', COMMENT_PUBLISHED);

      // Insert the user-level data for the commenters (except if a commenter
      // is the node's author).
      db_insert('tracker_user')
        ->from($query)
        ->execute();

      // Note that we have indexed at least one node.
      $last_nid = $row->nid;
      $count++;
    }
    if ($last_nid !== FALSE) {

      // Prepare a starting point for the next run.
      $state
        ->set('tracker.index_nid', $last_nid - 1);
      watchdog('tracker', '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);
    }
  }
}