You are here

function _auditfiles_referenced_not_used_batch_display_get_files in Audit Files 7.3

Retrieves the file IDs to operate on.

Parameters

array $file_fields: The list of file fields to search for files 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_referenced_not_used_batch_display_get_files'
_auditfiles_referenced_not_used_batch_display_get_operations in ./auditfiles.referencednotused.inc
Configures the operations for the batch process.

File

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

Code

function _auditfiles_referenced_not_used_batch_display_get_files(array $file_fields, array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox'] = array();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_id'] = 0;

    // Set the number of records to operate on for this batch process.
    $context['sandbox']['max'] = 1;
    $batch_size = variable_get('auditfiles_report_options_batch_size', 1000);
    if ($batch_size > 0) {
      $context['sandbox']['max'] = $batch_size;
    }

    // Set up the field data to use to query the database with.
    $context['sandbox']['file_fields'] = array();
    $context['sandbox']['table'] = '';
    $context['sandbox']['column'] = '';
    if (!empty($file_fields)) {
      $context['sandbox']['file_fields'] = $file_fields;
      $file_field = array_shift($context['sandbox']['file_fields']);

      // Get the database table name for the field.
      $context['sandbox']['table'] = key($file_field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);

      // Get the column name in the database table for the field.
      $context['sandbox']['column'] = $file_field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$context['sandbox']['table']]['fid'];
    }
  }
  if (!isset($context['results']['file_list'])) {
    $context['results']['file_list'] = array();
  }

  // Get the next set of records.
  // This cannot be sorted any other way here, or the results are not complete.
  $query = 'SELECT entity_id, entity_type, ' . $context['sandbox']['column'] . '
    FROM {' . $context['sandbox']['table'] . '}
    WHERE entity_id > ' . $context['sandbox']['current_id'] . '
      AND ' . $context['sandbox']['column'] . ' NOT IN (SELECT DISTINCT fid FROM {file_usage})
    ORDER BY entity_id ASC
    LIMIT 20';
  $results = db_query($query)
    ->fetchAll();

  // Check the data to see what needs to be done next.
  if (empty($results)) {

    // There are no more records for the current field, so get the next field.
    if (empty($context['sandbox']['file_fields'])) {

      // There are no more fields to process, so set the exit condition.
      $context['sandbox']['progress'] = $context['sandbox']['max'];
    }
    else {
      $file_field = array_shift($context['sandbox']['file_fields']);

      // Get the database table name for the field.
      $context['sandbox']['table'] = key($file_field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);

      // Get the column name in the database table for the field.
      $context['sandbox']['column'] = $file_field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$context['sandbox']['table']]['fid'];
    }
  }
  else {

    // Save the data in the data store.
    foreach ($results as $result) {
      $reference_id = $context['sandbox']['table'] . '.' . $context['sandbox']['column'] . '.' . $result->entity_id . '.' . $result->entity_type . '.' . $result->{$context['sandbox']['column']};
      $context['results']['file_list'][$reference_id] = array(
        'table' => $context['sandbox']['table'],
        'column' => $context['sandbox']['column'],
        'entity_id' => $result->entity_id,
        'file_id' => $result->{$context['sandbox']['column']},
      );
      if ($context['sandbox']['max'] > 1 && count($context['results']['file_list']) >= $context['sandbox']['max']) {

        // The maximum number of records has been reached, so set the exit
        // condition.
        $context['sandbox']['progress'] = $context['sandbox']['max'];
        break;
      }
    }

    // Update the progress information.
    $context['sandbox']['current_id'] = $result->entity_id;
    $context['message'] = t('Getting the list of files. Last ID processed: !entity_id.', array(
      '!entity_id' => $result->entity_id,
    ));
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] >= $context['sandbox']['max'];
  }
}