You are here

function _classified_scheduled_build_purge in Classified Ads 6.3

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

Purge nodes past their expiration date + grace period

Selected nodes: expires + grace < now => expires < now - grace

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

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

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 node titles to be deleted.

4 calls to _classified_scheduled_build_purge()
ClassifiedBasicTest::test1733594 in tests/classified_basic.test
Bug 1733594: Infinite grace (-1) being handled like a normal duration.
classified_cron in ./classified.module
Implements hook_cron().
drush_classified_purge in ./classified.drush.inc
Command callback for classified-purge
_classified_scheduled_page_purge in ./classified.scheduled.inc
Page callback for purges.

File

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

Code

function _classified_scheduled_build_purge($time = NULL) {
  $grace = _classified_get('grace');
  if ($grace == -1) {
    return array();
  }
  $limit = _classified_get_time($time) - $grace * 24 * 60 * 60;
  $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 cn.expires < %d
EOT;
  $q = db_query($sq, $limit);
  $ads = array();
  $count = 0;

  // Hide message information, since the page can be triggered by any user,
  // but needs to run as admin, and protect misc session content as well.
  $messages = isset($_SESSION['messages']) ? $_SESSION['messages'] : array();
  global $user;
  $saved_account = $user;
  session_save_session(FALSE);
  $user = user_load(1);
  while ($o = db_fetch_object($q)) {
    $ads[$o->uid][$o->nid] = $o->title;
    node_delete($o->nid);

    // invokes drupal_set_message(), hence the hiding code
    $count++;
  }
  $user = $saved_account;
  session_save_session(TRUE);
  $_SESSION['messages'] = $messages;
  if ($count) {
    watchdog('classified', "Deleted @count nodes: @deleted", array(
      '@count' => $count,
      '@deleted' => var_export($ads, TRUE),
    ), WATCHDOG_INFO);
  }
  else {
    watchdog('classified', 'Purge did not find any ad to delete.', NULL, WATCHDOG_INFO);
    $ads = array();
  }
  drupal_alter('classified_purge', $ads);
  return $ads;
}