You are here

function _classified_scheduled_build_purge in Classified Ads 7.3

Same name and namespace in other branches
  1. 6.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.

There is no addTag('node_access') because this is an administrative operation, that can be triggered by any user and needs to access all matching nodes, not only those for which access is granted.

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.

Throws

\Exception

4 calls to _classified_scheduled_build_purge()
ClassifiedTestTestBasicTest::test1733594 in tests/classified_test_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. No output: nothing to return.
_classified_scheduled_page_purge in ./classified.scheduled.inc
Page callback for purges.

File

./classified.scheduled.inc, line 250
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;
  $q = db_select('node', 'n')
    ->comment(__FUNCTION__);
  $cn = $q
    ->innerJoin('classified_node', 'cn', 'n.nid = cn.nid');
  $results = $q
    ->fields('n', array(
    'nid',
    'title',
    'uid',
  ))
    ->condition('n.type', 'classified')
    ->condition("{$cn}.expires", $limit, '<')
    ->execute()
    ->fetchAll();
  $count = count($results);
  if ($count) {
    $ads = array();
    foreach ($results as $result) {
      $ads[$result->uid][$result->nid] = $result->title;
      $deleted[] = $result->nid;
    }

    // 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.
    //
    // About coder false positive:
    // http://drupal.org/node/224333#drupal_set_session
    $messages = isset($_SESSION['messages']) ? $_SESSION['messages'] : array();
    global $user;
    $saved_account = $user;
    drupal_save_session(FALSE);
    $user = user_load(1);

    // May invoke drupal_set_message(), hence the hiding code.
    node_delete_multiple($deleted);
    $user = $saved_account;
    drupal_save_session(TRUE);

    // Coder: false positive.
    $_SESSION['messages'] = $messages;
    watchdog('classified', "Deleted @count nodes: @deleted", array(
      '@count' => $count,
      '@deleted' => var_export($deleted, 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;
}