View source
<?php
define('ADVAGG_BUNDLER_MAX_CSS', 2);
define('ADVAGG_BUNDLER_MAX_JS', 5);
define('ADVAGG_BUNDLER_OUTDATED', 1209600);
define('ADVAGG_BUNDLER_ACTIVE', TRUE);
define('ADVAGG_BUNDLER_GROUPING_LOGIC', 1);
define('ADVAGG_BUNDLER_ADMIN_MODE', 4);
function advagg_bundler_menu() {
$file_path = drupal_get_path('module', 'advagg_bundler');
$config_path = advagg_admin_config_root_path();
$items[$config_path . '/advagg/bundler'] = array(
'title' => 'Bundler',
'description' => 'Adjust Bundler settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'advagg_bundler_admin_settings_form',
),
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer site configuration',
),
'file path' => $file_path,
'file' => 'advagg_bundler.admin.inc',
'weight' => 10,
);
return $items;
}
function advagg_bundler_advagg_hooks_implemented_alter(&$hooks, $all) {
if ($all) {
$hooks['advagg_bundler_analysis_alter'] = array();
}
}
function advagg_bundler_init() {
if (advagg_bundler_enabled()) {
$GLOBALS['conf']['advagg_core_groups'] = FALSE;
}
}
function advagg_bundler_form_advagg_admin_settings_form_alter(&$form, $form_state) {
if (advagg_bundler_enabled()) {
$form['global']['advagg_core_groups']['#disabled'] = TRUE;
$form['global']['advagg_core_groups']['#description'] = t('The bundler submodule disables core grouping logic.');
$form['global']['advagg_core_groups']['#states'] = array();
}
}
function advagg_bundler_enabled() {
if (variable_get('advagg_bundler_active', ADVAGG_BUNDLER_ACTIVE) && (variable_get('advagg_bundler_max_css', ADVAGG_BUNDLER_MAX_CSS) || variable_get('advagg_bundler_max_js', ADVAGG_BUNDLER_MAX_JS))) {
return TRUE;
}
}
function advagg_bundler_analysis($filename = '', $force = FALSE, $safesql = FALSE, $depth = 0) {
static $analysis = array();
if (empty($analysis)) {
$query = db_select('advagg_aggregates_versions', 'aav')
->condition('aav.root', 1)
->condition('aav.atime', REQUEST_TIME - max(172800, variable_get('advagg_bundler_outdated', ADVAGG_BUNDLER_OUTDATED), '>'), '>');
$query
->addExpression('COUNT(aggregate_filenames_hash)', 'counter');
$count = $query
->execute()
->fetchField();
$ideal_cid = 'advagg:bundler_analysis:' . $count;
if (!$force) {
$counts = range(max(0, $count - 3), $count + 3);
foreach ($counts as $count) {
$cache_ids[] = 'advagg:bundler_analysis:' . $count;
}
$cache_hits = cache_get_multiple($cache_ids, 'cache_advagg_aggregates');
if (!empty($cache_hits)) {
if (isset($cache_hits[$ideal_cid])) {
$cache = $cache_hits[$ideal_cid];
}
elseif (!$force && module_exists('httprl') && httprl_is_background_callback_capable()) {
$callback_options = array(
array(
'function' => 'advagg_bundler_analysis',
),
$filename,
TRUE,
);
httprl_queue_background_callback($callback_options);
httprl_send_request();
$max = 0;
foreach ($cache_hits as $data) {
if ($data->created > $max) {
$max = $data->created;
$cache = $data;
}
}
}
}
}
if ($force || empty($cache->data)) {
try {
$analysis = advagg_bundler_analyisis_query($safesql);
cache_set($ideal_cid, $analysis, 'cache_advagg_aggregates', CACHE_TEMPORARY);
} catch (PDOException $e) {
if ($depth > 2) {
throw $e;
}
$depth++;
return advagg_bundler_analysis($filename, TRUE, TRUE, $depth);
}
}
else {
$analysis = $cache->data;
}
}
if (empty($filename)) {
return $analysis;
}
if (!empty($analysis[$filename])) {
return $analysis[$filename];
}
return 0;
}
function advagg_bundler_analyisis_query($safesql) {
$db_type = Database::getConnection()
->databaseType();
$schema = Database::getConnection()
->schema();
if ($safesql) {
$mssql_group_concat = FALSE;
$mssql_lpad = FALSE;
$mssql_md5 = FALSE;
$pg9 = FALSE;
}
else {
$mssql_group_concat = method_exists($schema, 'functionExists') && $schema
->functionExists('GROUP_CONCAT');
$mssql_lpad = method_exists($schema, 'functionExists') && $schema
->functionExists('LPAD');
$mssql_md5 = method_exists($schema, 'functionExists') && $schema
->functionExists('MD5');
if ($db_type === 'pgsql') {
$database_connection = Database::getConnection();
$pg9 = FALSE;
if (version_compare($database_connection
->version(), '9') >= 0) {
$pg9 = TRUE;
}
}
}
$subquery_aggregates = db_select('advagg_aggregates', 'aa');
$fields = array(
'counter',
);
if ($db_type === 'sqlsrv' && !$mssql_lpad) {
$subquery_aggregates
->addExpression("RIGHT(REPLICATE('0',8) + CAST(COUNT(aav.aggregate_filenames_hash) AS char(8)),8)", 'counter');
}
elseif ($db_type === 'sqlite') {
$subquery_aggregates
->addExpression("substr('00000000' || CAST(COUNT(aav.aggregate_filenames_hash) AS char(8)), -8, 8)", 'counter');
}
else {
$subquery_aggregates
->addExpression("LPAD(CAST(COUNT(aav.aggregate_filenames_hash) AS char(8)), 8, '0')", 'counter');
}
if ($db_type === 'mysql') {
$fields[] = 'hashlist';
db_query('SET SESSION group_concat_max_len = 65535');
$subquery_aggregates
->addExpression('HEX(SHA1(GROUP_CONCAT(DISTINCT aa.aggregate_filenames_hash ORDER BY aa.aggregate_filenames_hash ASC)))', 'hashlist');
}
elseif ($db_type === 'pgsql') {
if ($pg9) {
$fields[] = 'hashlist';
$subquery_aggregates
->addExpression("MD5(STRING_AGG(DISTINCT(aa.aggregate_filenames_hash), ',' ORDER BY aa.aggregate_filenames_hash ASC))", 'hashlist');
}
}
elseif ($db_type === 'sqlite') {
$fields[] = 'hashlist';
$subquery_aggregates
->addExpression('GROUP_CONCAT(DISTINCT aa.aggregate_filenames_hash)', 'hashlist');
$subquery_aggregates
->orderBy("aa.aggregate_filenames_hash", "ASC");
}
elseif ($db_type === 'sqlsrv' && $mssql_group_concat) {
$fields[] = 'hashlist';
if ($mssql_md5) {
$subquery_aggregates
->addExpression('MD5(GROUP_CONCAT(DISTINCT aa.aggregate_filenames_hash))', 'hashlist');
}
else {
$subquery_aggregates
->addExpression('GROUP_CONCAT(DISTINCT aa.aggregate_filenames_hash)', 'hashlist');
}
}
$time = REQUEST_TIME - max(172800, variable_get('advagg_bundler_outdated', ADVAGG_BUNDLER_OUTDATED), '>');
$subquery_aggregates
->join('advagg_aggregates_versions', 'aav', "aav.aggregate_filenames_hash=aa.aggregate_filenames_hash AND aav.root=1 AND aav.atime > {$time}");
$subquery_aggregates = $subquery_aggregates
->fields('aa', array(
'filename_hash',
))
->groupBy('aa.filename_hash');
$af_fields = array(
'filename',
'filesize',
'mtime',
'changes',
'linecount',
'filename_hash',
);
include_once DRUPAL_ROOT . '/includes/install.inc';
if (drupal_get_installed_schema_version('advagg') >= 7211) {
$af_fields[] = 'filesize_processed';
}
$query = db_select('advagg_files', 'af');
$query
->join($subquery_aggregates, 'aa', 'af.filename_hash=aa.filename_hash');
$query = $query
->fields('af', $af_fields)
->fields('aa', $fields);
$query
->comment('Query called from ' . __FUNCTION__ . '()');
$results = $query
->execute();
$analysis = array();
foreach ($results as $row) {
if (empty($row->hashlist)) {
$subquery_aggregates_versions = db_select('advagg_aggregates_versions', 'aav')
->fields('aav')
->condition('aav.root', 1)
->condition('aav.atime', REQUEST_TIME - max(172800, variable_get('advagg_bundler_outdated', ADVAGG_BUNDLER_OUTDATED), '>'), '>');
$subquery_aggregates = db_select('advagg_aggregates', 'aa');
$subquery_aggregates
->join($subquery_aggregates_versions, 'aav', 'aav.aggregate_filenames_hash=aa.aggregate_filenames_hash');
$subquery_aggregates = $subquery_aggregates
->fields('aa', array(
'aggregate_filenames_hash',
))
->condition('aa.filename_hash', $row->filename_hash)
->groupBy('aa.aggregate_filenames_hash')
->orderBy('aa.aggregate_filenames_hash', 'ASC');
$subquery_aggregates
->comment('Query called from ' . __FUNCTION__ . '()');
$aa_results = $subquery_aggregates
->execute();
$aa_rows = array();
foreach ($aa_results as $aa_row) {
$aa_rows[] = $aa_row->aggregate_filenames_hash;
}
$row->hashlist = implode(',', $aa_rows);
}
$row->hashlist = drupal_hash_base64($row->hashlist);
$analysis[$row->filename] = array(
'group_hash' => $row->counter . ' ' . $row->hashlist,
'mtime' => $row->mtime,
'filesize' => $row->filesize,
'filesize_processed' => empty($row->filesize_processed) ? $row->filesize : $row->filesize_processed,
'linecount' => $row->linecount,
'changes' => $row->changes,
);
}
arsort($analysis);
drupal_alter('advagg_bundler_analysis', $analysis);
return $analysis;
}