function ServiceAuditFilesMergeFileReferences::_auditfiles_merge_file_references_batch_merge_process_file in Audit Files 8
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.
File
- src/
ServiceAuditFilesMergeFileReferences.php, line 163 - providing the service that used in 'managed not used' functionality.
Class
Namespace
Drupal\auditfilesCode
function _auditfiles_merge_file_references_batch_merge_process_file($file_being_kept, $file_being_merged) {
$connection = Database::getConnection();
$file_being_kept_results = $connection
->select('file_usage', 'fu')
->fields('fu', [
'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);
$file_being_kept_name_results = $connection
->select('file_managed', 'fm')
->fields('fm', [
'filename',
])
->condition('fid', $file_being_kept)
->execute()
->fetchAll();
$file_being_kept_name = reset($file_being_kept_name_results);
$file_being_merged_results = $connection
->select('file_usage', 'fu')
->fields('fu', [
'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.', [
'%fid' => $file_being_merged,
]);
drupal_set_message($message, 'warning');
return;
}
$file_being_merged_data = reset($file_being_merged_results);
$file_being_merged_uri_results = $connection
->select('file_managed', 'fm')
->fields('fm', [
'uri',
])
->condition('fid', $file_being_merged)
->execute()
->fetchAll();
$file_being_merged_uri = reset($file_being_merged_uri_results);
if ($file_being_kept_data->id == $file_being_merged_data->id) {
$file_being_kept_data->count += $file_being_merged_data->count;
// Delete the unnecessary entry from the file_usage table.
$connection
->delete('file_usage')
->condition('fid', $file_being_merged)
->execute();
// Update the entry for the file being kept.
$connection
->update('file_usage')
->fields([
'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 {
$connection
->update('file_usage')
->fields([
'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.
$this
->_auditfiles_merge_file_references_update_file_fields($file_being_kept, $file_being_merged);
}
// Delete the unnecessary entries from the file_managed table.
$connection
->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);
}
}