You are here

function redirect_purge_inactive_redirects in Redirect 7

Same name and namespace in other branches
  1. 7.2 redirect.module \redirect_purge_inactive_redirects()

Purge inactive redirects from the database.

Parameters

$types: An array of redirect types to remove. Default is only the self-managed 'redirect'. If not provided all redirect types will be eligible for removal.

$interval: The number of seconds to subtract from the current time and used to find the inactive redirects.

Return value

An array of redirect IDs that were deleted or FALSE if none were.

1 call to redirect_purge_inactive_redirects()
redirect_cron in ./redirect.module
Implements hook_cron().

File

./redirect.module, line 1082

Code

function redirect_purge_inactive_redirects(array $types = array(
  'redirect',
), $interval = NULL) {
  if (!isset($interval)) {
    $interval = variable_get('redirect_purge_inactive', 0);
  }
  if (!$interval) {
    return FALSE;
  }
  if (variable_get('redirect_page_cache', 0) && !variable_get('page_cache_invoke_hooks', TRUE)) {

    // If serving redirects from the page cache is enabled and hooks are not
    // executed during page caching, then we cannot track when a redirect is
    // used. Therefore, we cannot remove unused redirects.
    watchdog('redirect', 'Due to existing settings, could not track when a redirect is used, so could not remove unused redirects.');
    return FALSE;
  }
  $query = db_select('redirect');
  $query
    ->addField('redirect', 'rid');
  if (!empty($types)) {
    $query
      ->condition('type', $types);
  }
  $query
    ->condition('access', REQUEST_TIME - $interval, '<');
  $query
    ->range(0, variable_get('redirect_purge_amount', 100));
  $query
    ->addTag('redirect_purge');
  $rids = $query
    ->execute()
    ->fetchCol();
  if (count($rids)) {
    redirect_delete_multiple($rids);
    watchdog('redirect', format_plural(count($rids), 'Removed 1 inactive redirect from the database.', 'Removed @count inactive redirects from the database.'));
    return $rids;
  }
}