You are here

function prev_next_cron in Previous/Next API 7.2

Same name and namespace in other branches
  1. 6 prev_next.module \prev_next_cron()
  2. 7 prev_next.module \prev_next_cron()

Implements hook_cron().

File

./prev_next.module, line 65
The previous next module indexes the previous and next nodes based upon user-selectable criteria and stores this index in the database for faster retrieval later.

Code

function prev_next_cron() {
  $max_nid = variable_get('prev_next_index_nid', 0);
  if ($max_nid) {
    $batch_size = variable_get('prev_next_batch_size', PREV_NEXT_BATCH_SIZE_DEFAULT);
    $last_nid = FALSE;
    $cond = _prev_next_node_types_sql();
    timer_start('prev_next_cron');
    $result = db_query_range("SELECT nid FROM {node} WHERE nid <= :nid AND status = 1 {$cond} ORDER BY nid DESC", 0, $batch_size, array(
      ':nid' => $max_nid,
    ));
    $count = 0;
    foreach ($result as $row) {

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

      //_prev_next_modify_pointing_nodes($row->nid);
      _prev_next_add($row->nid);

      // Update nodes that might point to this one.
      // Note that we have indexed at least one node.
      $last_nid = $row->nid;
      $count++;
    }
    $time = timer_read('prev_next_cron');
    if ($last_nid !== FALSE) {

      // Prepare a starting point for the next run.
      variable_set('prev_next_index_nid', $last_nid - 1);
    }
    else {

      // If all nodes have been indexed, set to zero to skip future cron runs.
      variable_set('prev_next_index_nid', 0);
    }
    if ($count) {
      watchdog('prev_next', 'Indexed %count nodes in %time milliseconds.', array(
        '%count' => $count,
        '%time' => $time,
      ));
    }
    $total = db_query("SELECT COUNT(nid) FROM {node} WHERE status = 1 {$cond}")
      ->fetchField();
    $completed = db_query("SELECT COUNT(nid) FROM {prev_next_node}")
      ->fetchField();
    $remaining = max(0, $total - $completed);
    drupal_set_message(t('Indexed %count nodes for the Prev/Next index. There are %remaining items left to index.', array(
      '%count' => $count,
      '%remaining' => $remaining,
    )));
  }
}