You are here

function advagg_js_compress_test_file in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7.2 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_test_file()
  2. 7 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_test_file()

Run various theme functions so the cache is primed.

Parameters

$values: object File info

1 string reference to 'advagg_js_compress_test_file'
advagg_js_compress_menu in advagg_js_compress/advagg_js_compress.module
Implementation of hook_menu

File

advagg_js_compress/advagg_js_compress.module, line 423
Advanced CSS/JS aggregation js compression module.

Code

function advagg_js_compress_test_file($values = NULL) {

  //   watchdog('debug', str_replace('    ', '    ', nl2br(htmlentities(print_r($values, TRUE) . print_r($_REQUEST, TRUE)))));
  // Exit if key does not match & called with $file not set.
  if (is_null($values)) {
    if (empty($_POST['key']) || empty($_POST['values'])) {
      return;
    }
    $key = variable_get('advagg_js_compress_url_key', FALSE);
    if ($key != $_POST['key']) {
      return;
    }
    $values = array();
    $values['values'] = $_POST['values'];
  }
  $filename = $values['values']['filename'];
  $md5 = $values['values']['md5'];

  // Compression test file if it exists.
  advagg_clearstatcache(TRUE, $filename);
  if (file_exists($filename)) {
    $contents = file_get_contents($filename);
    $filesize = filesize($filename);
    $lock_name = 'advagg_set_file_data_' . $md5;
    if (!lock_acquire($lock_name, 45)) {
      lock_wait($lock_name);
      echo $md5;
      exit;
    }
    $data = advagg_get_file_data($md5);

    // Set to "-1" so if php bombs out, the file will be marked as bad.
    $data['advagg_js_compress']['tested']['jsminplus'] = -1;
    advagg_set_file_data($md5, $data);

    // Compress the data.
    list($before, $after) = advagg_js_compress_jsminplus($contents);

    // Set to "-2" if compression ratio sucks.
    $ratio = 0;
    if ($before != 0) {
      $ratio = ($before - $after) / $before;
    }
    if ($ratio < variable_get('advagg_js_compress_ratio', ADVAGG_JS_COMPRESS_RATIO)) {
      $data['advagg_js_compress']['tested']['jsminplus'] = -2;
      advagg_set_file_data($md5, $data);
      lock_release($lock_name);
      echo $md5;
      exit;
    }

    // Set to "-3" if the compression ratio is way too good.
    if ($ratio > variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO)) {
      $data['advagg_js_compress']['tested']['jsminplus'] = -3;
      advagg_set_file_data($md5, $data);
      lock_release($lock_name);
      echo $md5;
      exit;
    }

    // Everything worked, mark this file as compressible.
    $data['advagg_js_compress']['tested']['jsminplus'] = 1;
    advagg_set_file_data($md5, $data);

    // Set the file cache.
    if (variable_get('advagg_js_compress_file_cache', ADVAGG_JS_COMPRESS_FILE_CACHE)) {
      $key = $filename;
      $table = 'cache_advagg_js_compress_file';
      cache_set($key, $contents, $table);
    }
  }
  if (isset($lock_name)) {
    lock_release($lock_name);
  }
  echo $md5;
  exit;
}