You are here

function advagg_flush_caches in Advanced CSS/JS Aggregation 7

Same name and namespace in other branches
  1. 6 advagg.module \advagg_flush_caches()
  2. 7.2 advagg.module \advagg_flush_caches()

Implements hook_flush_caches().

9 calls to advagg_flush_caches()
advagg_admin_flush_cache_button in includes/admin.inc
Cache clear button.
advagg_bundler_disable in advagg_bundler/advagg_bundler.install
Implements hook_disable().
advagg_bundler_enable in advagg_bundler/advagg_bundler.install
Implements hook_enable().
advagg_css_compress_disable in advagg_css_compress/advagg_css_compress.install
Implements hook_disable().
advagg_css_compress_enable in advagg_css_compress/advagg_css_compress.install
Implements hook_enable().

... See full list

1 string reference to 'advagg_flush_caches'
advagg_enable in ./advagg.install
Implements hook_enable().

File

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

Code

function advagg_flush_caches() {

  // Try to allocate enough time to flush the cache
  if (function_exists('set_time_limit')) {
    @set_time_limit(240);
  }
  global $_advagg;

  // Only one advagg cache flusher can run at a time.
  if (!lock_acquire('advagg_flush_caches')) {
    return;
  }

  // Only run code below if the advagg db tables exist.
  if (!db_table_exists('advagg_files')) {
    return array(
      'cache_advagg_bundle_reuse',
    );
  }

  // Find files that have changed.
  $needs_refreshing = array();
  $results = db_query("SELECT * FROM {advagg_files}");
  while ($row = $results
    ->fetchAssoc()) {
    $checksum = advagg_checksum($row['filename']);

    // Let other modules see if the bundles needs to be rebuilt.
    // hook_advagg_files_table
    // Return TRUE in order to increment the counter.
    $hook_results = module_invoke_all('advagg_files_table', $row, $checksum);

    // Check each return value; see if an update is needed.
    $update = FALSE;
    if (!empty($hook_results)) {
      foreach ($hook_results as $update) {
        if ($update === TRUE) {
          break;
        }
      }
    }

    // Increment the counter if needed and mark file for bundle refreshment.
    if ($checksum != $row['checksum'] || $update == TRUE) {
      $needs_refreshing[$row['filename_md5']] = $row['filename'];

      // Update checksum; increment counter.
      // TODO Please review the conversion of this statement to the D7 database API syntax.

      /* db_query("UPDATE {advagg_files} SET checksum = '%s', counter = counter + 1 WHERE filename_md5 = '%s'", $checksum, $row['filename_md5']) */
      db_update('advagg_files')
        ->fields(array(
        'checksum' => $checksum,
        'counter' => counter + 1,
      ))
        ->condition('filename_md5', $row['filename_md5'])
        ->execute();
    }
  }

  // Get the bundles.
  $bundles = array();
  foreach ($needs_refreshing as $filename_md5 => $filename) {
    $results = db_query("SELECT bundle_md5 FROM {advagg_bundles} WHERE filename_md5 = :filename_md5", array(
      ':filename_md5' => $filename_md5,
    ));
    while ($row = db_fetch_array($results)) {
      $bundles[$row['bundle_md5']] = $row['bundle_md5'];
    }
  }
  foreach ($bundles as $bundle_md5) {

    // Increment Counter
    // TODO Please review the conversion of this statement to the D7 database API syntax.

    /* db_query("UPDATE {advagg_bundles} SET counter = counter + 1, timestamp = %d WHERE bundle_md5 = '%s'", REQUEST_TIME, $bundle_md5) */
    db_update('advagg_bundles')
      ->fields(array(
      'counter' => counter + 1,
      'timestamp' => REQUEST_TIME,
    ))
      ->condition('bundle_md5', $bundle_md5)
      ->execute();
    if (variable_get('advagg_rebuild_on_flush', ADVAGG_REBUILD_ON_FLUSH)) {

      // Rebuild bundles on shutdown in the background. This is needed so that
      // the cache_advagg_bundle_reuse table has been cleared.
      register_shutdown_function('advagg_rebuild_bundle', $bundle_md5, '', TRUE);
    }
  }
  $_advagg['bundles'] = $bundles;
  $_advagg['files'] = $needs_refreshing;

  // Garbage collection
  list($css_path, $js_path) = advagg_get_root_files_dir();
  file_scan_directory($css_path[1], '/.*/', array(
    'callback' => 'advagg_delete_file_if_stale',
  ));
  file_scan_directory($js_path[1], '/.*/', array(
    'callback' => 'advagg_delete_file_if_stale',
  ));
  lock_release('advagg_flush_caches');
  return array(
    'cache_advagg_bundle_reuse',
  );
}