function advagg_delete_files_if_orphaned in Advanced CSS/JS Aggregation 7.2
Given an array of files remove that file if there is no associated db record.
Parameters
array $files: Array of files returned by file_scan_directory.
Return value
array Array of files that got removed.
1 call to advagg_delete_files_if_orphaned()
- advagg_delete_orphaned_aggregates in ./
advagg.cache.inc - Scan CSS/JS advagg dir and remove file if there is no associated db record.
File
- ./
advagg.cache.inc, line 553 - Advanced CSS/JS aggregation module.
Code
function advagg_delete_files_if_orphaned(array $files) {
// Get the uri for the advagg_css/parts directory.
list($css_path) = advagg_get_root_files_dir();
$parts_uri = $css_path[0] . '/parts/';
// Array used to record what files were deleted.
$kill_list = $keyed_file_list = array();
// Create a listing of all file names and associated hashes.
foreach ($files as $uri => $file) {
// Get info on file.
$data = advagg_get_hashes_from_filename($file->filename, TRUE);
if (is_array($data)) {
list(, $aggregate_filenames_hash) = $data;
// Check to see if the file is in the database.
$keyed_file_list[$aggregate_filenames_hash] = $uri;
}
else {
// Check to see if this is a parts css file.
$start = strpos($file->uri, $parts_uri);
if ($start !== FALSE) {
// Get the original filename.
$original_file = substr($file->uri, $start + strlen($parts_uri));
$original_file = preg_replace('/(.\\d+\\.css)$/i', '.css', $original_file);
if (file_exists($original_file)) {
// Original file exists, do not delete.
continue;
}
}
// Can not get data on file, remove it.
$kill_list[] = $uri;
continue;
}
}
if (!empty($keyed_file_list)) {
$filenames_hash = array_keys($keyed_file_list);
$aggregates_in_database = array();
// Process in chunks when a large array is passed.
do {
// Check if the aggregate_filenames_hash exists in the database.
$aggregates_in_database += db_select('advagg_aggregates_versions', 'av')
->fields('av', array(
'aggregate_filenames_hash',
))
->condition('av.aggregate_filenames_hash', array_splice($filenames_hash, 0, 1000), 'IN')
->distinct()
->execute()
->fetchAllAssoc('aggregate_filenames_hash');
} while (count($filenames_hash));
// Get values not found in the database.
$to_delete = array_values(array_diff_key($keyed_file_list, $aggregates_in_database));
// Add the file uri to the kill list.
$kill_list = array_merge($kill_list, $to_delete);
}
if (!empty($kill_list)) {
foreach ($kill_list as $uri) {
advagg_delete_file_by_uri($uri);
}
}
// Let other modules know about the removed files.
// Call hook_advagg_removed_aggregates().
module_invoke_all('advagg_removed_aggregates', $kill_list);
return $kill_list;
}