You are here

function _classified_scheduled_build_expire in Classified Ads 6.3

Same name and namespace in other branches
  1. 7.3 classified.scheduled.inc \_classified_scheduled_build_expire()

Unpublish nodes past their expiration date.

The test on n.type is not required, since the inner join will only return such nodes anyway, but allows the query to take advantage of the core index on node.type

Reset notify time to 0 because this field is only used for interim notifications, not expire/purge.

No db_rewrite_sql: this query can be run at any time by anyone.

@TODO check whether the update/join syntax works on non-MySQL DB engines

Parameters

int $time: A UNIX timestamp. Normally not set: this was added for testing purposes.

Return value

array A per-user array of per-nid expired nodes titles.

3 calls to _classified_scheduled_build_expire()
classified_cron in ./classified.module
Implements hook_cron().
drush_classified_expire in ./classified.drush.inc
Command callback for classified-expire
_classified_scheduled_page_expire in ./classified.scheduled.inc
Page callback for expirations

File

./classified.scheduled.inc, line 51
Scheduled operations for classified.module

Code

function _classified_scheduled_build_expire($time = NULL) {

  // Obtain the list of ads to expire.
  $expires = _classified_get_time($time);
  $sq = <<<EOT
    SELECT n.nid, n.title, n.uid
    FROM {node} n
      INNER JOIN {classified_node} cn ON n.nid = cn.nid
    WHERE
      n.type = 'classified' AND n.status = 1
      AND cn.expires < %d
EOT;
  $q = db_query($sq, $expires);
  $count1 = 0;
  $expired = array();
  while ($o = db_fetch_object($q)) {
    $expired[$o->uid][$o->nid] = $o->title;
    $count1++;
  }

  // Now perform the expiration.
  $sq = <<<EOT
    UPDATE {node} n
      INNER JOIN {classified_node} cn ON n.nid = cn.nid
    SET n.status = 0, cn.notify = 0
    WHERE
      n.type = 'classified' AND n.status = 1
      AND cn.expires < %d
EOT;
  $sts = db_query($sq, $expires);
  $count2 = db_affected_rows();

  // should match $count1
  if ($count1 || $count2) {
    watchdog('classified', 'Expiration unpublished @count2 ads (@count1 scheduled): @expired', array(
      '@count2' => $count2,
      '@count1' => $count1,
      '@expired' => var_export($expired, TRUE),
    ), $count1 == $count2 ? WATCHDOG_INFO : WATCHDOG_WARNING);
  }
  else {
    watchdog('classified', 'Expiration check did not find any ad to expire.', NULL, WATCHDOG_INFO);
  }
  drupal_alter('classified_expire', $expired);
  return $expired;
}