You are here

function _auditfiles_used_not_referenced_batch_display_get_files in Audit Files 7.3

The batch process operation for getting the files.

Parameters

array $file_fields: The relevant fields to look for file information in.

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_referenced_batch_display_get_files'
_auditfiles_used_not_referenced_batch_display_get_operations in ./auditfiles.usednotreferenced.inc
Configures the operations for the batch process.

File

./auditfiles.usednotreferenced.inc, line 289
Generates report showing files in file_usage, but not referenced by content.

Code

function _auditfiles_used_not_referenced_batch_display_get_files(array $file_fields, 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';
    $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 DISTINCT fid
    FROM {file_usage}
    WHERE fid > :fid
    ORDER BY fid ASC
    LIMIT 20';
  $files = db_query($query, array(
    ':fid' => $context['sandbox']['current_file'],
  ))
    ->fetchCol();
  $files_in_fields = array();
  foreach ($file_fields as $field) {
    foreach ($field['results'] as $fid) {
      if (in_array($fid->{$field['column']}, $files)) {
        $files_in_fields[] = $fid->{$field['column']};
      }
    }
  }
  $file_list = array_diff($files, $files_in_fields);
  $context['results']['file_list'] = array_merge($context['results']['file_list'], $file_list);
  $file_id = end($files);

  // Update the progress information.
  $context['sandbox']['progress'] += 20;
  $context['sandbox']['current_file'] = $file_id;
  $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_id,
  ));
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] >= $context['sandbox']['max'];
  }
}