function advagg_find_existing_bundle in Advanced CSS/JS Aggregation 7
Same name and namespace in other branches
- 6 advagg.module \advagg_find_existing_bundle()
Given a list of files, see if a bundle already exists containing all of those files. If in strict mode then the file count has to be the same.
Parameters
$files: List of files in the proposed bundle.
$bundle_md5: Bundle's machine name.
File
- ./
advagg.module, line 1021 - Advanced CSS/JS aggregation module
Code
function advagg_find_existing_bundle(&$files, &$bundle_md5) {
// Sort files for better cache hits.
$temp_files = $files;
sort($temp_files);
$schema = advagg_get_server_schema();
$cached_data_key = 'advagg_existing_' . md5($schema . implode('', $temp_files));
// Try cache first; cache table is cache_advagg_bundle_reuse. string is debug name.
$cached_data = advagg_cached_bundle_get($cached_data_key, 'advagg_find_existing_bundle');
if (!empty($cached_data)) {
$files = $cached_data[0]['files'];
$bundle_md5 = $cached_data[0]['bundle_md5'];
return;
}
// Build union query.
$query = 'SELECT root.bundle_md5 FROM {advagg_bundles} AS root';
$joins = array();
$wheres = array();
$args = array();
$counter = 0;
foreach ($files as $filename) {
// Use alpha for table aliases; numerics do not work.
$key = strtr($counter, '01234567890', 'abcdefghij');
$joins[$key] = "\nINNER JOIN {advagg_bundles} AS {$key} USING(bundle_md5)\n";
if ($counter == 0) {
$wheres[$key] = "WHERE {$key}.filename_md5 = '%s'";
}
else {
$wheres[$key] = "AND {$key}.filename_md5 = '%s'";
}
$args[$key] = md5($filename);
$counter++;
}
$query .= implode("\n", $joins);
$query .= implode("\n", $wheres);
$query .= ' GROUP BY bundle_md5';
// Find matching bundles and select first good one.
$files_count = count($files);
$results = db_query($query, $args);
while ($new_md5 = $results
->fetchField()) {
$count = db_query("SELECT count(*) FROM {advagg_bundles} WHERE bundle_md5 = :bundle_md5", array(
':bundle_md5' => $new_md5,
))
->fetchField();
// Make sure bundle has the same number of files if using strict matching.
if (!empty($count) && $count == $files_count) {
$bundle_md5 = $new_md5;
$data = array(
array(
'files' => $files,
'bundle_md5' => $bundle_md5,
),
);
cache_set($cached_data_key, $data, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
return;
}
}
}