You are here

function advagg_get_filename in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg.module \advagg_get_filename()

Given a list of files; return back the aggregated filename.

Parameters

$files: List of files in the proposed bundle.

$filetype: css or js.

$counter: (optional) Counter value.

$bundle_md5: (optional) Bundle's machine name.

Return value

Aggregated filename.

2 calls to advagg_get_filename()
advagg_css_js_file_builder in ./advagg.module
Aggregate CSS/JS files, putting them in the files directory.
advagg_missing_remove_cache in ./advagg.missing.inc
Set cache value to FALSE.

File

./advagg.module, line 912
Advanced CSS/JS aggregation module

Code

function advagg_get_filename($files, $filetype, $counter = FALSE, $bundle_md5 = '') {
  if (empty($files) || empty($filetype)) {
    return FALSE;
  }
  global $_advagg;
  $filenames = array();
  $run_alter = FALSE;
  if (empty($bundle_md5)) {

    // Create bundle md5
    $schema = advagg_get_server_schema();
    $bundle_md5 = md5($schema . implode('', $files));
    $run_alter = TRUE;

    // Record root request in db.
    // Get counter if there.
    if (empty($counter)) {
      $counter = db_result(db_query("SELECT counter FROM {advagg_bundles} WHERE bundle_md5 = '%s'", $bundle_md5));
    }

    // If this is a brand new bundle then insert file/bundle info into database.
    if ($counter === FALSE) {
      $counter = 0;
      advagg_insert_bundle_db($files, $filetype, $bundle_md5, TRUE);
    }

    // If bundle should be root and is not, then make it root.
    // Refresh timestamp if older then 12 hours.
    $row = db_fetch_array(db_query("SELECT root, timestamp FROM {advagg_bundles} WHERE bundle_md5 = '%s'", $bundle_md5));
    if ($row['root'] === 0 || time() - $row['timestamp'] > variable_get('advagg_file_last_used_interval', ADVAGG_FILE_LAST_USED_INTERVAL)) {
      db_query("UPDATE {advagg_bundles} SET root = '1', timestamp = %d WHERE bundle_md5 = '%s'", time(), $bundle_md5);
    }
  }

  // Set original array.
  $filenames[] = array(
    'filetype' => $filetype,
    'files' => $files,
    'counter' => $counter,
    'bundle_md5' => $bundle_md5,
  );

  // Invoke hook_advagg_filenames_alter() to give installed modules a chance to
  // alter filenames. One to many relationships come to mind.
  // Do not run alter if MD5 was given, we want to generate that file only in
  // this special case.
  if ($run_alter) {

    // Force counter to be looked up later.
    $filenames[0]['counter'] = FALSE;
    drupal_alter('advagg_filenames', $filenames);
  }

  // Write to DB if needed and create filenames.
  $output = array();
  $used_md5 = array();
  if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
    $_advagg['debug']['get_filename_post_alter'][] = array(
      'key' => $bundle_md5,
      'filenames' => $filenames,
    );
  }

  // Get all counters at once
  $counters = array();
  foreach ($filenames as $key => $values) {
    if (empty($values['counter'])) {
      $counters[$key] = $values['bundle_md5'];
    }
  }
  $result = advagg_db_multi_select_in('advagg_bundles', 'bundle_md5', "'%s'", $counters, array(
    'counter',
    'bundle_md5',
  ), 'GROUP BY bundle_md5, counter');
  while ($row = db_fetch_array($result)) {
    $key = array_search($row['bundle_md5'], $counters);
    if (empty($filenames[$key]['counter']) && $filenames[$key]['counter'] !== 0) {
      $filenames[$key]['counter'] = intval($row['counter']);
    }
  }
  foreach ($filenames as $values) {

    // Get info from array.
    $filetype = $values['filetype'];
    $files = $values['files'];
    $counter = $values['counter'];
    $bundle_md5 = $values['bundle_md5'];

    // See if a JS bundle exists that already has the same files in it, just in a
    // different order.
    //     if ($filetype == 'js' && $run_alter) {
    //       advagg_find_existing_bundle($files, $bundle_md5);
    //     }
    // Do not add the same bundle twice.
    if (isset($used_md5[$bundle_md5])) {
      continue;
    }
    $used_md5[$bundle_md5] = TRUE;

    // If this is a brand new bundle then insert file/bundle info into database.
    if (empty($counter) && $counter !== 0) {
      $counter = 0;
      advagg_insert_bundle_db($files, $filetype, $bundle_md5, FALSE);
    }

    // Prefix filename to prevent blocking by firewalls which reject files
    // starting with "ad*".
    $output[] = array(
      'filename' => advagg_build_filename($filetype, $bundle_md5, $counter),
      'files' => $files,
      'bundle_md5' => $bundle_md5,
    );

    // Refresh timestamp if older then 12 hours.
    $row = db_fetch_array(db_query("SELECT timestamp FROM {advagg_bundles} WHERE bundle_md5 = '%s'", $bundle_md5));
    if (time() - $row['timestamp'] > variable_get('advagg_file_last_used_interval', ADVAGG_FILE_LAST_USED_INTERVAL)) {
      db_query("UPDATE {advagg_bundles} SET timestamp = %d WHERE bundle_md5 = '%s'", time(), $bundle_md5);
    }
  }
  return $output;
}