function advagg_js_compress_prep in Advanced CSS/JS Aggregation 7.2
Same name and namespace in other branches
- 6 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_prep()
- 7 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_prep()
Compress a JS string.
Parameters
string $contents: Javascript string.
string $filename: Filename.
array $aggregate_settings: The aggregate_settings array.
bool $add_licensing: FALSE to remove Source and licensing information comment.
bool $log_errors: FALSE to disable logging to watchdog on failure.
bool $test_ratios: FALSE to disable compression ratio testing.
bool $force_run: TRUE to skip cache and force the compression test.
Return value
bool FALSE if there was an error.
7 calls to advagg_js_compress_prep()
- advagg_js_compress_advagg_get_js_file_contents_alter in advagg_js_compress/
advagg_js_compress.advagg.inc - Implements hook_advagg_get_js_file_contents_alter().
- advagg_js_compress_advagg_modify_js_pre_render_alter in advagg_js_compress/
advagg_js_compress.module - Implements hook_advagg_modify_js_pre_render_alter().
- advagg_js_compress_advagg_save_aggregate_alter in advagg_js_compress/
advagg_js_compress.advagg.inc - Implements hook_advagg_save_aggregate_alter().
- advagg_js_compress_test_file in advagg_js_compress/
advagg_js_compress.module - Test a file, making sure it is compressible.
- hook_advagg_get_js_aggregate_contents_alter in ./
advagg.api.php - Allow other modules to modify this aggregates contents.
File
- advagg_js_compress/
advagg_js_compress.advagg.inc, line 397
Code
function advagg_js_compress_prep(&$contents, $filename, array $aggregate_settings, $add_licensing = TRUE, $log_errors = TRUE, $test_ratios = TRUE, $force_run = FALSE) {
$no_errors = TRUE;
// Get the info on this file.
module_load_include('inc', 'advagg', 'advagg');
$compressor = $aggregate_settings['variables']['advagg_js_compressor'];
if ($compressor == 0) {
return FALSE;
}
// Do nothing if the file is already minified.
$url = file_create_url($filename);
$semicolon_count = substr_count($contents, ';');
if ($compressor != 2 && $semicolon_count > 10 && $semicolon_count > substr_count($contents, "\n", strpos($contents, ';')) * 5 && !$force_run) {
$add_license_setting = isset($aggregate_settings['variables']['advagg_js_compress_add_license']) ? $aggregate_settings['variables']['advagg_js_compress_add_license'] : variable_get('advagg_js_compress_add_license', ADVAGG_JS_COMPRESS_ADD_LICENSE);
if ($add_licensing && ($add_license_setting == 1 || $add_license_setting == 3)) {
$contents = "/* Source and licensing information for the line(s) below can be found at {$url}. */\n" . $contents . ";\n/* Source and licensing information for the above line(s) can be found at {$url}. */\n";
// Return FALSE here to not compress an already minified file.
return FALSE;
}
}
// Get the JS string length before the compression operation.
$contents_before = $contents;
$before = strlen($contents);
// Do not use jsmin() if the function can not be called.
if ($compressor == 3 && !function_exists('jsmin')) {
if (defined('PHP_VERSION_ID') && constant('PHP_VERSION_ID') >= 50300) {
$compressor = 5;
watchdog('advagg_js_compress', 'The jsmin function does not exist. Using JSqueeze.');
}
else {
$compressor = 1;
watchdog('advagg_js_compress', 'The jsmin function does not exist. Using JSmin+.');
}
}
// Jsmin doesn't handle multi-byte characters before version 2, fall back to
// different compressor if jsmin version < 2 and $contents contains multi-
// byte characters.
if ($compressor == 3 && (version_compare(phpversion('jsmin'), '2.0.0', '<') && advagg_js_compress_string_contains_multibyte_characters($contents))) {
if (defined('PHP_VERSION_ID') && constant('PHP_VERSION_ID') >= 50300) {
$compressor = 5;
watchdog('advagg_js_compress', 'The currently installed jsmin version does not handle multibyte characters, you may consider to upgrade the jsmin extension. Using JSqueeze fallback.');
}
else {
$compressor = 1;
watchdog('advagg_js_compress', 'The currently installed jsmin version does not handle multibyte characters, you may consider to upgrade the jsmin extension. Using JSmin+ fallback.');
}
}
$info = advagg_get_info_on_files(array(
$filename,
), FALSE, FALSE);
$info = $info[$filename];
$no_errors = advagg_js_compress_do_it($contents, $info, $compressor, $force_run, $aggregate_settings, $log_errors, $url);
// Make sure compression ratios are good.
$after = strlen($contents);
$ratio = 0;
if ($before != 0) {
$ratio = ($before - $after) / $before;
}
// Get ratios settings.
$aggregate_settings['variables']['advagg_js_compress_max_ratio'] = isset($aggregate_settings['variables']['advagg_js_compress_max_ratio']) ? $aggregate_settings['variables']['advagg_js_compress_max_ratio'] : variable_get('advagg_js_compress_max_ratio', ADVAGG_JS_COMPRESS_MAX_RATIO);
$aggregate_settings['variables']['advagg_js_compress_ratio'] = isset($aggregate_settings['variables']['advagg_js_compress_ratio']) ? $aggregate_settings['variables']['advagg_js_compress_ratio'] : variable_get('advagg_js_compress_ratio', ADVAGG_JS_COMPRESS_RATIO);
// Get license settings.
$add_license_setting = isset($aggregate_settings['variables']['advagg_js_compress_add_license']) ? $aggregate_settings['variables']['advagg_js_compress_add_license'] : variable_get('advagg_js_compress_add_license', ADVAGG_JS_COMPRESS_ADD_LICENSE);
// Make sure the returned string is not empty or has a VERY high
// compression ratio.
if (empty($contents) || empty($ratio) || $ratio < $aggregate_settings['variables']['advagg_js_compress_ratio'] || $ratio > $aggregate_settings['variables']['advagg_js_compress_max_ratio']) {
$contents = $contents_before;
if ($compressor !== 1) {
// Try again using jsmin+.
$no_errors = advagg_js_compress_do_it($contents, $info, 1, $force_run, $aggregate_settings, $log_errors, $url);
// Make sure compression ratios are good.
$after = strlen($contents);
$ratio = 0;
if ($before != 0) {
$ratio = ($before - $after) / $before;
}
if (empty($contents) || empty($ratio) || $ratio < $aggregate_settings['variables']['advagg_js_compress_ratio'] || $ratio > $aggregate_settings['variables']['advagg_js_compress_max_ratio']) {
$contents = $contents_before;
}
}
}
if ($add_licensing && ($add_license_setting == 1 || $add_license_setting == 3)) {
$contents = "/* Source and licensing information for the line(s) below can be found at {$url}. */\n" . $contents . ";\n/* Source and licensing information for the above line(s) can be found at {$url}. */\n";
}
// Reset if cache settings are set to Development.
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0 && !$force_run) {
$contents = $contents_before;
}
return $no_errors;
}