You are here

function advagg_bundler_analysis in Advanced CSS/JS Aggregation 6

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

Given a filename return a bundle key.

Parameters

$filename: filename

$force: bypass the cache and get a fresh version of the analysis.

Return value

string to be used for the grouping key.

2 calls to advagg_bundler_analysis()
advagg_bundler_admin_settings_form in advagg_bundler/advagg_bundler.admin.inc
Form builder; Configure advagg settings.
advagg_bundler_advagg_filenames_alter in advagg_bundler/advagg_bundler.module
Implement hook_advagg_filenames_alter.

File

advagg_bundler/advagg_bundler.module, line 176
Advanced aggregation bundler module.

Code

function advagg_bundler_analysis($filename = '', $force = FALSE) {

  // Cache query in a static.
  static $analysis = array();
  if (empty($analysis)) {

    // See if we have a cached version of this.
    $count = db_result(db_query("SELECT COUNT(*) FROM {advagg_bundles} WHERE root=1"));
    $cache = cache_get('advagg_bundler_analysis:' . $count);
    if (empty($cache->data) || $force) {

      // "Magic Query"; only needs to run once.
      // Return a count of how many root bundles all files are used in. Count is
      // padded with eight zeros so the count can be key sorted as a string
      // without worrying about it getting put in the wrong order.
      // Return the bundle_md5's value; we need something more unique than count
      // when grouping together.
      // Return the filename. Used for lookup.
      // We join the advagg bundles and files together making sure to only use
      // root bundles that have been used in the last 2 weeks. This prevents an
      // old site structure from influencing new bundles.
      // Grouping by the filename gives us the count and makes it so we don't
      // return a lot of rows;
      $result = db_query("\n        SELECT\n          count,\n          bundle_md5,\n          filename\n        FROM (\n          SELECT\n            LPAD(COUNT(*), 8, '00000000') AS count,\n            bundle_md5,\n            filename_md5\n          FROM\n            {advagg_bundles}\n          WHERE\n            root = 1\n            AND\n            timestamp > %d\n          GROUP BY\n            filename_md5) AS ab\n        INNER JOIN {advagg_files} AS af USING ( filename_md5 )\n      ", time() - variable_get('advagg_bundler_outdated', ADVAGG_BUNDLER_OUTDATED));

      // Save query into a static array with the filename as the key.
      while ($row = db_fetch_array($result)) {
        $analysis[$row['filename']] = $row['count'] . ' ' . $row['bundle_md5'];
      }

      // Invoke hook_advagg_bundler_analysis_alter() to give installed modules a
      // chance to alter the analysis array.
      drupal_alter('advagg_bundler_analysis', $analysis);

      // Save results to the cache.
      cache_set('advagg_bundler_analysis:' . $count, $analysis, 'cache', CACHE_TEMPORARY);
    }
    else {
      $analysis = $cache->data;
    }
  }

  // If no filename is given pass back then entire query results.
  if (empty($filename)) {
    return $analysis;
  }

  // Return a key to be used in groupings.
  if (!empty($analysis[$filename])) {
    return $analysis[$filename];
  }

  // We need to return a value that can be used as an array key if the query
  // didn't give us anything.
  return 0;
}