You are here

function _auditfiles_used_not_managed_batch_display_get_files in Audit Files 7.3

The batch process operation for getting the files.

Parameters

array $context: Used by the Batch API to keep track of and pass data from one operation to the next.

1 string reference to '_auditfiles_used_not_managed_batch_display_get_files'
_auditfiles_used_not_managed_batch_display_get_operations in ./auditfiles.usednotmanaged.inc
Configures the operations for the batch process.

File

./auditfiles.usednotmanaged.inc, line 261
Generates a report showing files in file_usage, but not in file_managed.

Code

function _auditfiles_used_not_managed_batch_display_get_files(array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox'] = array();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_file'] = 0;
    $query = 'SELECT COUNT(DISTINCT fid) FROM {file_usage} fu WHERE fid NOT IN (SELECT fid FROM {file_managed})';
    $record_count = db_query($query)
      ->fetchField();
    $batch_size = variable_get('auditfiles_report_options_batch_size', 1000);
    if ($batch_size > 0 && $record_count > $batch_size) {
      $context['sandbox']['max'] = $batch_size;
    }
    else {
      $context['sandbox']['max'] = $record_count;
    }
  }
  if (empty($context['results']['file_list'])) {
    $context['results']['file_list'] = array();
  }

  // Get the file data from the database.
  // This cannot be sorted any other way here, or the results are not complete.
  $query = 'SELECT fid
    FROM {file_usage}
    WHERE fid NOT IN (SELECT fid FROM {file_managed})
      AND fid > :fid
    ORDER BY fid ASC
    LIMIT 20';
  $files = db_query($query, array(
    ':fid' => $context['sandbox']['current_file'],
  ))
    ->fetchAll();
  foreach ($files as $file) {
    $context['results']['file_list'][$file->fid] = $file->fid;

    // Update the progress information.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_file'] = $file->fid;
    $context['message'] = t('Getting the list of files. Processed file @num1 of @num2. Last file ID processed: !file_id.', array(
      '@num1' => $context['sandbox']['progress'],
      '@num2' => $context['sandbox']['max'],
      '!file_id' => $file->fid,
    ));
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] >= $context['sandbox']['max'];
  }
}