You are here

function _auditfiles_merge_file_references_batch_merge_process_file in Audit Files 7.3

Deletes the specified file from the file_managed table.

Parameters

int $file_being_kept: The file ID of the file to merge the other into.

int $file_being_merged: The file ID of the file to merge.

1 call to _auditfiles_merge_file_references_batch_merge_process_file()
_auditfiles_merge_file_references_batch_merge_process_batch in ./auditfiles.mergefilereferences.inc
The batch process for deleting the file.

File

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

Code

function _auditfiles_merge_file_references_batch_merge_process_file($file_being_kept, $file_being_merged) {

  // Get the usage data for the file being kept.
  $file_being_kept_results = db_select('file_usage', 'fu')
    ->fields('fu', array(
    'module',
    'type',
    'id',
    'count',
  ))
    ->condition('fid', $file_being_kept)
    ->execute()
    ->fetchAll();
  if (empty($file_being_kept_results)) {
    $message = t('There was no file usage data found for the file you choose to keep. No changes were made.');
    drupal_set_message($message, 'warning');
    return;
  }
  $file_being_kept_data = reset($file_being_kept_results);

  // Also get the filename for the file being kept.
  $file_being_kept_name_results = db_select('file_managed', 'fm')
    ->fields('fm', array(
    'filename',
  ))
    ->condition('fid', $file_being_kept)
    ->execute()
    ->fetchAll();
  $file_being_kept_name = reset($file_being_kept_name_results);

  // Get the usage data for the file being merged.
  $file_being_merged_results = db_select('file_usage', 'fu')
    ->fields('fu', array(
    'module',
    'type',
    'id',
    'count',
  ))
    ->condition('fid', $file_being_merged)
    ->execute()
    ->fetchAll();
  if (empty($file_being_merged_results)) {
    $message = t('There was an error retrieving the file usage data from the database for file ID !fid. Please check the files in one of the other reports. No changes were made for this file.', array(
      '!fid' => $file_being_merged,
    ));
    drupal_set_message($message, 'warning');
    return;
  }
  $file_being_merged_data = reset($file_being_merged_results);

  // Also get the URI for the file being merged.
  $file_being_merged_uri_results = db_select('file_managed', 'fm')
    ->fields('fm', array(
    'uri',
  ))
    ->condition('fid', $file_being_merged)
    ->execute()
    ->fetchAll();
  $file_being_merged_uri = reset($file_being_merged_uri_results);

  // Compare the content ID of the file being merged with the content ID of the
  // file being kept.
  if ($file_being_kept_data->id == $file_being_merged_data->id) {

    // The IDs match, so update the kept file and delete the one being merged.
    $file_being_kept_data->count += $file_being_merged_data->count;

    // Delete the unnecessary entry from the file_usage table.
    db_delete('file_usage')
      ->condition('fid', $file_being_merged)
      ->execute();

    // Update the entry for the file being kept.
    db_update('file_usage')
      ->fields(array(
      'count' => $file_being_kept_data->count,
    ))
      ->condition('fid', $file_being_kept)
      ->condition('module', $file_being_kept_data->module)
      ->condition('type', $file_being_kept_data->type)
      ->condition('id', $file_being_kept_data->id)
      ->execute();
  }
  else {

    // The IDs do not match, so update the file being merged, by making the file
    // usage of the file being merged point to the fid of the file being kept.
    db_update('file_usage')
      ->fields(array(
      'fid' => $file_being_kept,
    ))
      ->condition('fid', $file_being_merged)
      ->condition('module', $file_being_merged_data->module)
      ->condition('type', $file_being_merged_data->type)
      ->condition('id', $file_being_merged_data->id)
      ->execute();

    // Update any fields that might be pointing to the file being merged.
    _auditfiles_merge_file_references_update_file_fields($file_being_kept, $file_being_merged);
  }

  // Delete the unnecessary entries from the file_managed table.
  db_delete('file_managed')
    ->condition('fid', $file_being_merged)
    ->execute();

  // Delete the duplicate file.
  if (!empty($file_being_merged_uri->uri)) {
    file_unmanaged_delete($file_being_merged_uri->uri);
  }

  // Remove the deleted files from the list of files to display.
  $rows = variable_get('auditfiles_merge_file_references_files_to_display', array());
  unset($rows[$file_being_kept_name->filename]);
  variable_set('auditfiles_merge_file_references_files_to_display', $rows);
}