You are here

function _auditfiles_managed_not_used_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_managed_not_used_batch_display_get_files'
_auditfiles_managed_not_used_batch_display_get_operations in ./auditfiles.managednotused.inc
Configures the operations for the batch process.

File

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

Code

function _auditfiles_managed_not_used_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_managed} WHERE fid NOT IN (SELECT fid FROM {file_usage})';
    $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, filename
    FROM {file_managed}
    WHERE fid NOT IN (SELECT fid FROM {file_usage})
      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 processed: !file_name.', array(
      '@num1' => $context['sandbox']['progress'],
      '@num2' => $context['sandbox']['max'],
      '!file_name' => $file->filename,
    ));
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] >= $context['sandbox']['max'];
  }
}