You are here

function scheduler_cron in Scheduler 5

Same name and namespace in other branches
  1. 8 scheduler.module \scheduler_cron()
  2. 6 scheduler.module \scheduler_cron()
  3. 7 scheduler.module \scheduler_cron()
  4. 2.x scheduler.module \scheduler_cron()

Implementation of hook_cron().

1 call to scheduler_cron()
_scheduler_run_cron in ./scheduler.module

File

./scheduler.module, line 420

Code

function scheduler_cron() {
  $clear_cache = FALSE;

  //if the time now is greater than the time to publish a node, publish it
  $nodes = db_query('SELECT * FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid WHERE n.status = 0 AND s.publish_on > 0 AND s.publish_on < %d', time());
  while ($node = db_fetch_object($nodes)) {
    $n = node_load($node->nid);
    $n->changed = $node->publish_on;
    if (variable_get('scheduler_touch_' . $n->type, 0) == 1) {
      $n->created = $node->publish_on;
    }
    $n->status = 1;
    node_save($n);

    //if this node is not to be unpublished, then we can delete the record
    if ($n->unpublish_on == 0) {
      db_query('DELETE FROM {scheduler} WHERE nid = %d', $n->nid);
    }
    else {
      db_query('UPDATE {scheduler} SET publish_on = 0 WHERE nid = %d', $n->nid);
    }

    //invoke scheduler API
    _scheduler_scheduler_api($n, 'publish');
    watchdog('content', t('@type: scheduled publishing of %title.', array(
      '@type' => $n->type,
      '%title' => $n->title,
    )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $n->nid));
    $clear_cache = TRUE;
  }

  //if the time is greater than the time to unpublish a node, unpublish it
  $nodes = db_query('SELECT * FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid WHERE n.status = 1 AND s.unpublish_on > 0 AND s.unpublish_on < %d', time());
  while ($node = db_fetch_object($nodes)) {

    //if this node is to be unpublished, we can update the node and remove the record since it can't be republished
    $n = node_load($node->nid);
    $n->changed = $node->unpublish_on;
    $n->status = 0;
    node_save($n);
    db_query('DELETE FROM {scheduler} WHERE nid = %d', $n->nid);

    //invoke scheduler API
    _scheduler_scheduler_api($n, 'unpublish');
    watchdog('content', t('@type: scheduled unpublishing of %title.', array(
      '@type' => $n->type,
      '%title' => $n->title,
    )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $n->nid));
    $clear_cache = TRUE;
  }
  if ($clear_cache) {

    // clear the cache so an anonymous poster can see the node being published or unpublished
    cache_clear_all();
  }
}