From 3b94d1d5727db757100f02aebf259d16cc4f00f1 Mon Sep 17 00:00:00 2001
From: mikeytown2 <mike.carper@gmail.com>
Date: Fri, 4 Mar 2011 15:02:52 -0800
Subject: [PATCH 1/3] Issue #1080986 by mikeytown2: Fix some very bad bugs & implement semi compression if compression fails on a file.

---
 advagg.admin.inc                             |    4 +-
 advagg.missing.inc                           |   24 +++++++-------
 advagg.module                                |   23 +++++++++++---
 advagg_js_compress/advagg_js_compress.module |   43 ++++++++++++++++++++++---
 4 files changed, 69 insertions(+), 25 deletions(-)

diff --git a/advagg.admin.inc b/advagg.admin.inc
index 8667f2c..e771d9e 100644
--- a/advagg.admin.inc
+++ b/advagg.admin.inc
@@ -36,7 +36,7 @@ function advagg_admin_info_form() {
       '#type'          => 'fieldset',
       '#collapsible'   => TRUE,
       '#collapsed'     => TRUE,
-      '#title'         => t('%type files', array('%type' => strtoupper($type))),
+      '#title'         => t('@type files', array('@type' => strtoupper($type))),
     );
     // filename  filename_md5  checksum  filetype  counter   data
     $results = db_query("SELECT * FROM {advagg_files} WHERE filetype = '%s'", $type);
@@ -56,7 +56,7 @@ function advagg_admin_info_form() {
       else {
         $form[$type][$row['filename_md5']] = array(
           '#type'   => 'markup',
-          '#value'  => format_plural($row['counter'], 'changed 1 time - %file<br />', 'changed %counter times - %file<br />',  array(
+          '#value'  => '<div>' . format_plural($row['counter'], 'changed 1 time - %file<br />', 'changed %counter times - %file<br /></div>',  array(
             '%counter'  => $row['counter'],
             '%file'     => $row['filename'],
           )),
diff --git a/advagg.missing.inc b/advagg.missing.inc
index 3477835..6f04342 100644
--- a/advagg.missing.inc
+++ b/advagg.missing.inc
@@ -18,10 +18,10 @@ function advagg_missing_css() {
   ignore_user_abort();
 
   // Try to regenerate missing file
-  advagg_missing_regenerate();
+  $msg = advagg_missing_regenerate();
 
   // If here send out fast 404.
-  advagg_missing_fast404();
+  advagg_missing_fast404($msg);
 }
 
 /**
@@ -31,10 +31,10 @@ function advagg_missing_js() {
   ignore_user_abort();
 
   // Try to regenerate missing file
-  advagg_missing_regenerate();
+  $msg = advagg_missing_regenerate();
 
   // If here send out fast 404.
-  advagg_missing_fast404();
+  advagg_missing_fast404($msg);
 }
 
 /**
@@ -54,9 +54,9 @@ function advagg_missing_regenerate() {
   $arg = arg();
   $filename = array_pop($arg);
 
-  // Verfy requested filename has the correct pattern.
+  // Verify requested filename has the correct pattern.
   if (preg_match('/^(j|cs)s_[0-9a-f]{32}_\d+\.(j|cs)s$/', $filename) == FALSE) {
-    return FALSE;
+    return t('Wrong Pattern.');
   }
 
   // Get type
@@ -67,7 +67,7 @@ function advagg_missing_regenerate() {
 
   // Make sure extension is the same as the type.
   if ($ext != $type) {
-    return FALSE;
+    return t('Type does not match extension.');
   }
 
   // Extract info from wanted filename.
@@ -80,13 +80,13 @@ function advagg_missing_regenerate() {
     $counter = substr($filename, 36, strpos($filename, '.', 37)-36);
   }
   else {
-    return FALSE;
+    return t('Wrong file type.');
   }
 
   $_GET['redirect_counter'] = isset($_GET['redirect_counter']) ? intval($_GET['redirect_counter']) : 0;
   if ($_GET['redirect_counter'] > 5) {
     watchdog('advagg', 'This request could not generate correctly. Loop detected. Request data: %info', array('%info' => $_GET['q']));
-    return FALSE;
+    return t('In a Loop.');
   }
 
   // Remove file from the cache.
@@ -95,12 +95,12 @@ function advagg_missing_regenerate() {
   // Counter in database.
   $counter_in_db = db_result(db_query("SELECT counter FROM {advagg_bundles} WHERE bundle_md5 = '%s'", $md5));
   if ($counter_in_db === FALSE) {
-    return FALSE;
+    return t('Not a valid bundle.');
   }
 
   // Only process if we got an older counter.
   if ($counter > $counter_in_db || $counter < 0) {
-    return FALSE;
+    return t('Counter out of range.');
   }
 
   // Rebuild file.
@@ -108,7 +108,7 @@ function advagg_missing_regenerate() {
   $good = advagg_rebuild_bundle($md5, $counter);
   if (!$good) {
     watchdog('advagg', 'This request could not generate correctly. Aggregate not generated. Request data: %info', array('%info' => $_GET['q']));
-    return FALSE;
+    return t('Rebuild Failed.');
   }
 
   // Redirect to file.
diff --git a/advagg.module b/advagg.module
index ad42b87..3c22db4 100644
--- a/advagg.module
+++ b/advagg.module
@@ -81,6 +81,7 @@ function advagg_faster_404() {
     $js = strpos($_SERVER['REQUEST_URI'], $js_path);
     if ($css !== FALSE || $js !== FALSE) {
       $_GET['q'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], file_directory_path()));
+      $_GET['q'] = trim($_GET['q'], '/');
       menu_execute_active_handler();
     }
   }
@@ -90,6 +91,7 @@ function advagg_faster_404() {
     $css = strpos($_GET['q'], $css_path);
     $js = strpos($_GET['q'], $js_path);
     if ($css !== FALSE || $js !== FALSE) {
+      $_GET['q'] = trim($_GET['q'], '/');
       menu_execute_active_handler();
     }
   }
@@ -1117,7 +1119,7 @@ function advagg_process_js($scope = 'header', $javascript = NULL, $noagg = FALSE
  */
 function advagg_css_js_file_builder($type, $files, $counter = '', $force = FALSE) {
   $data = '';
-  $filepath = file_create_path('advagg_' . $type);
+  $file_type_path = file_create_path('advagg_' . $type);
 
   // Send $files, get filename back
   $filenames = advagg_get_filename($files, $type, $counter);
@@ -1129,7 +1131,7 @@ function advagg_css_js_file_builder($type, $files, $counter = '', $force = FALSE
     $bundle_md5 = $info['bundle_md5'];
     $prefix = '';
     $suffix = '';
-    $filepath = $filepath .'/'. $filename;
+    $filepath = $file_type_path .'/'. $filename;
 
     // Check that the file exists & filesize is not zero
     $built = advagg_bundle_built($filepath);
@@ -1182,7 +1184,7 @@ function advagg_css_js_file_builder($type, $files, $counter = '', $force = FALSE
       drupal_alter('advagg_' . $type, $data, $files, $bundle_md5);
 
       // Create the advagg_$type/ within the files folder.
-      file_check_directory($filepath, FILE_CREATE_DIRECTORY);
+      file_check_directory($file_type_path, FILE_CREATE_DIRECTORY);
 
       $good = advagg_file_saver($data, $filepath);
       if (!$good) {
@@ -1234,7 +1236,18 @@ function advagg_build_css_bundle($files) {
   return $data;
 }
 
+/**
+ * Given a list of files, grab their contents and glue it into one big string.
+ *
+ * @param $files
+ *   array of filenames.
+ * @return
+ *   string containing all the files.
+ */
 function advagg_build_js_bundle($files) {
+  if (empty($files)) {
+    return '';
+  }
   // Build aggregate JS file.
   foreach ($files as $file) {
     // Append a ';' and a newline after each JS file to prevent them from running together.
@@ -1248,13 +1261,13 @@ function advagg_build_js_bundle($files) {
 /**
  * Send out a fast 404 and exit.
  */
-function advagg_missing_fast404() {
+function advagg_missing_fast404($msg = '') {
 //   watchdog('x', str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($_SERVER, TRUE)))));
 
   global $base_path;
   if (!headers_sent()) {
     header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
-    header('X-AdvAgg: Failed Validation');
+    header('X-AdvAgg: Failed Validation. ' . $msg);
   }
   print '<html>';
   print '<head><title>404 Not Found</title></head>';
diff --git a/advagg_js_compress/advagg_js_compress.module b/advagg_js_compress/advagg_js_compress.module
index 3f8ff43..9cb77df 100644
--- a/advagg_js_compress/advagg_js_compress.module
+++ b/advagg_js_compress/advagg_js_compress.module
@@ -85,7 +85,7 @@ function advagg_js_compress_advagg_js_alter(&$contents, $files, $bundle_md5) {
   // Make sure every file in this aggregate is compressible.
   $files_to_test = array();
   $all_ok = TRUE;
-  $advagg_js_compress_callback = varible_get('advagg_js_compress_callback', ADVAGG_JS_COMPRESS_CALLBACK);
+  $advagg_js_compress_callback = variable_get('advagg_js_compress_callback', ADVAGG_JS_COMPRESS_CALLBACK);
   if ($advagg_js_compress_callback) {
     foreach ($files as $filename) {
       $filename_md5 = md5($filename);
@@ -108,6 +108,33 @@ function advagg_js_compress_advagg_js_alter(&$contents, $files, $bundle_md5) {
       $compressible = advagg_js_compress_test_compression($files_to_test);
       // If an array then it is a list of files that can not be compressed.
       if (is_array($compressible)) {
+        $contents = '';
+        // Place filename in an array key.
+        $list_bad = array();
+        foreach ($compressible as $file) {
+          $file = $file['filename'];
+          $list_bad[$file] = $file;
+        }
+
+        // Do not compress the file that it bombs on.
+        $list_good = array();
+        foreach ($files as $file) {
+          if (!empty($list_bad[$file])) {
+            $data = advagg_build_js_bundle($list_good);
+            advagg_js_compress_jsminplus($data);
+            $data .= ";\n" . advagg_build_js_bundle(array($file));
+            $contents .= $data;
+            $list_good = array();
+          }
+          else {
+            $list_good[] = $file;
+          }
+        }
+        if (!empty($list_good)) {
+          $data = advagg_build_js_bundle($list_good);
+          advagg_js_compress_jsminplus($data);
+          $contents .= $data;
+        }
         $all_ok = FALSE;
       }
     }
@@ -115,14 +142,18 @@ function advagg_js_compress_advagg_js_alter(&$contents, $files, $bundle_md5) {
 
   // Only compress if all files are compressible.
   if ($all_ok) {
-    // JSMin+ the contents of the aggregated file.
-    require_once(drupal_get_path('module', 'advagg_js_compress') .'/jsminplus.php');
-    // Strip Byte Order Marks (BOM's) from the file, JSMin+ cannot parse these.
-    $contents = str_replace(pack("CCC", 0xef, 0xbb, 0xbf), "", $contents);
-    $contents = JSMinPlus::minify($contents);
+    advagg_js_compress_jsminplus($contents);
   }
 }
 
+function advagg_js_compress_jsminplus(&$contents) {
+  // JSMin+ the contents of the aggregated file.
+  require_once(drupal_get_path('module', 'advagg_js_compress') .'/jsminplus.php');
+  // Strip Byte Order Marks (BOM's) from the file, JSMin+ cannot parse these.
+  $contents = str_replace(pack("CCC", 0xef, 0xbb, 0xbf), "", $contents);
+  $contents = JSMinPlus::minify($contents);
+}
+
 /**
  * Run various theme functions so the cache is primed.
  *
-- 
1.7.4.1


From 40204feadc4288827f3f9e17b91a46b6aebd9e99 Mon Sep 17 00:00:00 2001
From: mikeytown2 <mike.carper@gmail.com>
Date: Fri, 4 Mar 2011 15:04:02 -0800
Subject: [PATCH 2/3] Issue #1080986 by mikeytown2: Fix some very bad bugs & implement semi compression if compression fails on a file.

---
 advagg.module |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)

diff --git a/advagg.module b/advagg.module
index 3c22db4..586ce5d 100644
--- a/advagg.module
+++ b/advagg.module
@@ -81,7 +81,6 @@ function advagg_faster_404() {
     $js = strpos($_SERVER['REQUEST_URI'], $js_path);
     if ($css !== FALSE || $js !== FALSE) {
       $_GET['q'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], file_directory_path()));
-      $_GET['q'] = trim($_GET['q'], '/');
       menu_execute_active_handler();
     }
   }
@@ -91,7 +90,6 @@ function advagg_faster_404() {
     $css = strpos($_GET['q'], $css_path);
     $js = strpos($_GET['q'], $js_path);
     if ($css !== FALSE || $js !== FALSE) {
-      $_GET['q'] = trim($_GET['q'], '/');
       menu_execute_active_handler();
     }
   }
-- 
1.7.4.1


From 375ab69fc7ec45187129304f5cb54d69de5c35dc Mon Sep 17 00:00:00 2001
From: mikeytown2 <mike.carper@gmail.com>
Date: Fri, 4 Mar 2011 15:06:01 -0800
Subject: [PATCH 3/3] Issue #1080986 by mikeytown2: Fix some very bad bugs & implement semi compression if compression fails on a file.

---
 advagg_js_compress/advagg_js_compress.module |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/advagg_js_compress/advagg_js_compress.module b/advagg_js_compress/advagg_js_compress.module
index 9cb77df..adabbaf 100644
--- a/advagg_js_compress/advagg_js_compress.module
+++ b/advagg_js_compress/advagg_js_compress.module
@@ -146,6 +146,12 @@ function advagg_js_compress_advagg_js_alter(&$contents, $files, $bundle_md5) {
   }
 }
 
+/**
+ * Compress a JS string using jsmin+
+ *
+ * @param $contents
+ *   Javascript string.
+ */
 function advagg_js_compress_jsminplus(&$contents) {
   // JSMin+ the contents of the aggregated file.
   require_once(drupal_get_path('module', 'advagg_js_compress') .'/jsminplus.php');
-- 
1.7.4.1

