You are here

function prev_next_cron in Previous/Next API 6

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

Implementation of hook_cron().

File

./prev_next.module, line 220

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("SELECT nid FROM {node} WHERE nid <= %d AND status = 1 {$cond} ORDER BY nid DESC LIMIT %d", $max_nid, $batch_size);
    $count = 0;
    while ($row = db_fetch_object($result)) {

      // Remove existing data for this node.
      db_query("DELETE FROM {prev_next_node} WHERE nid = %d", $row->nid);
      _prev_next_add($row->nid);

      // 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);
    }
    watchdog('prev_next', 'Indexed %count nodes in %time milliseconds.', array(
      '%count' => $count,
      '%time' => $time,
    ));
    $total = db_result(db_query("SELECT COUNT(nid) FROM {node} WHERE status = 1 {$cond}"));
    $completed = db_result(db_query("SELECT COUNT(nid) FROM {prev_next_node}"));
    $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,
    )));
  }
}