You are here

function _auditfiles_merge_file_references_batch_display_get_files in Audit Files 7.3

The batch process operation for getting the files.

Parameters

bool $show_single_file_names: TRUE if the list of files should include filenames that only have one file ID associated with them.

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_merge_file_references_batch_display_get_files'
_auditfiles_merge_file_references_batch_display_get_operations in ./auditfiles.mergefilereferences.inc
Configures the operations for the batch process.

File

./auditfiles.mergefilereferences.inc, line 342
Generates a report showing & allowing for merging potential duplicate files.

Code

function _auditfiles_merge_file_references_batch_display_get_files($show_single_file_names, array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox'] = array();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_file'] = '';

    // Get the count of available records to operate on.
    if ($show_single_file_names) {
      $query = 'SELECT COUNT(DISTINCT filename) FROM {file_managed}';
      $record_count = db_query($query)
        ->fetchField();
    }
    else {
      $query = 'SELECT COUNT(filename)
        FROM (SELECT COUNT(fid) AS file_count, filename
          FROM {file_managed}
          GROUP BY filename
          HAVING file_count > 1
        ) AS filename_count';
      $record_count = db_query($query)
        ->fetchField();
    }

    // Set the number of records to operate on.
    $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();
  }
  if ($show_single_file_names) {

    // Get the file data from the database.
    $query = 'SELECT DISTINCT filename
      FROM {file_managed}
      WHERE filename > :filename';
  }
  else {
    $query = '
      SELECT COUNT(fid) AS file_count, filename
      FROM {file_managed}
      WHERE filename > :filename
      GROUP BY filename
      HAVING file_count > 1';
  }

  // This cannot be sorted any other way here, or the results are not complete.
  $query .= ' ORDER BY filename ASC';
  $query .= ' LIMIT 20';
  $files = db_query($query, array(
    ':filename' => $context['sandbox']['current_file'],
  ))
    ->fetchAll();
  foreach ($files as $file) {
    $context['results']['file_list'][$file->filename] = $file->filename;

    // Update the progress information.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_file'] = $file->filename;
    $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'];
  }
}