You are here

protected static function LogFilter::_deleteLogs in Log Filter 7

Using a LIMIT for delete queryies is kind of hard since db_delete doesnt support LIMIT (because PostgresSQL doesnt support that), and MySQL doesnt support LIMIT for sub queries.

And MySQL doesnt support using the same table in a sub query and in an outer CRUD query.

So we have to do a separate select getting the relevant ids, and then do a delete using that - potentially too long - list of ids. Does it in chunks of 40000 items; that should work even when db query max length is only half a megabyte.

Parameters

array $conditions:

array $order_by:

integer $offset:

integer $max:

Return value

integer

1 call to LogFilter::_deleteLogs()
LogFilter::ajaxCallback in ./LogFilter.inc
Access permission: 'access site reports'.

File

./LogFilter.inc, line 1722
Drupal Log Filter module

Class

LogFilter
@file Drupal Log Filter module

Code

protected static function _deleteLogs($conditions, $order_by, $offset, $max) {
  global $user;

  // Deleting all logs unconditionally is not an option, because deleting logs of type 'log_filter delete logs' is illegal.

  //if (!$conditions && !$offset && !$max) {

  //  $deleted = db_delete('watchdog')
  //    ->execute();

  //}

  //else {
  $query = self::_logListQuery($conditions, $order_by, $offset, $max, array(
    'wid',
  ), FALSE, TRUE);
  if (($offset || $max) && !self::DB_SUBQUERY_LIMIT) {

    // A range must have a max length, otherwise it's ignored.
    if (!$max) {
      $max = PHP_INT_MAX - $offset;
    }
    $query
      ->range($offset, $max);
  }
  $ids = $query
    ->execute()
    ->fetchCol();
  if (!($le = count($ids))) {
    return 0;
  }

  //  This may fail because the list of ids may exceed query limit (query max length).
  if ($le > 40000) {
    $le = count($ids = array_chunk($ids, 40000));
    $deleted = 0;
    for ($i = 0; $i < $le; $i++) {
      $deleted += db_delete('watchdog')
        ->condition('wid', $ids[$i], 'IN')
        ->execute();
    }
  }
  else {
    $deleted = db_delete('watchdog')
      ->condition('wid', $ids, 'IN')
      ->execute();
  }

  //}
  watchdog('log_filter delete logs', 'User (%uid) %name deleted !deleted log events.', array(
    '%uid' => $user->uid,
    '%name' => $user->name,
    '!deleted' => $deleted,
  ), WATCHDOG_NOTICE);
  return $deleted;
}