You are here

function advagg_missing_regenerate in Advanced CSS/JS Aggregation 7

Same name and namespace in other branches
  1. 6 advagg.missing.inc \advagg_missing_regenerate()

regenerates a missing css file.

Parameters

$filename: filename

$type: css or js

Return value

false if bundle couldn't be generated.

2 calls to advagg_missing_regenerate()
advagg_missing_css in includes/missing.inc
Menu Callback; regenerates a missing css file.
advagg_missing_js in includes/missing.inc
Menu Callback; regenerates a missing js file.

File

includes/missing.inc, line 45
Advanced aggregation module; 404 handler.

Code

function advagg_missing_regenerate() {
  global $base_path, $conf;

  // Get filename from request.
  $arg = arg();
  $filename = array_pop($arg);
  $filename = explode('?', $filename);
  $filename = array_shift($filename);
  $data = advagg_get_bundle_from_filename($filename);
  if (is_array($data)) {
    list($type, $md5, $counter) = $data;
  }
  else {
    return $data;
  }
  $_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 t('In a Loop.');
  }

  // Counter in database.
  $counter_in_db = db_query("SELECT counter FROM {advagg_bundles} WHERE bundle_md5 = :bundle_md5", array(
    ':bundle_md5' => $md5,
  ))
    ->fetchField();
  if ($counter_in_db === FALSE) {
    return t('Not a valid bundle.');
  }

  // Cast counter as int
  $counter_in_db = intval($counter_in_db);
  $counter = intval($counter);

  // Set file(s) in cache to FALSE.
  $arg[] = $filename;
  cache_set(implode('/', $arg), FALSE, 'cache_advagg', TRUE);
  advagg_missing_remove_cache($md5);

  // Build filepath.
  list($css_path, $js_path) = advagg_get_root_files_dir();
  if ($type == 'js') {
    $file_type_path = $js_path;
  }
  if ($type == 'css') {
    $file_type_path = $css_path;
  }
  $new_filename = advagg_build_filename($type, $md5, $counter);
  $filepath = $file_type_path . '/' . $new_filename;

  // Only process if we got an older counter.
  // If we have an out of range counter see if a simlar file exists and serve
  // that up.
  if ($counter > $counter_in_db || $counter < 0) {
    $new_filename = advagg_build_filename($type, $md5, $counter_in_db);
    $filepath = $file_type_path . '/' . $new_filename;
    advagg_missing_send_file($filepath, advagg_build_uri($filepath), $type);
    exit;
  }

  // Break connection and do generation in the background.
  if (!empty($_GET['generator'])) {
    advagg_missing_async_opp($md5 . ' ' . $counter);
  }

  // Rebuild file.
  $conf['advagg_async_generation'] = FALSE;
  $good = advagg_rebuild_bundle($md5, $counter, TRUE);
  if (!$good) {
    watchdog('advagg', 'This request could not generate correctly. Aggregate not generated. Request data: %info', array(
      '%info' => $_GET['q'],
    ));
    return t('Rebuild Failed.');
  }

  // Serve direct or redirect to file.
  $_GET['redirect_counter']++;
  $uri = $base_path . $_GET['q'] . '?redirect_counter=' . $_GET['redirect_counter'];
  advagg_missing_send_file($filepath, $uri, $type);
  exit;
}