You are here

function advagg_cron in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 8.4 advagg.module \advagg_cron()
  2. 8.2 advagg.module \advagg_cron()
  3. 8.3 advagg.module \advagg_cron()
  4. 7.2 advagg.module \advagg_cron()
  5. 7 advagg.module \advagg_cron()

Implementation of hook_cron().

File

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

Code

function advagg_cron() {
  if (!variable_get('advagg_prune_on_cron', ADVAGG_PRUNE_ON_CRON)) {
    return;
  }

  // Set the oldest file/bundle to keep at 2 weeks.
  $max_time = module_exists('advagg_bundler') ? variable_get('advagg_bundler_outdated', ADVAGG_BUNDLER_OUTDATED) : 1209600;
  $max_file_time = time() - $max_time;
  $max_bundle_time = time() - $max_time * 3;
  $bundles_removed = 0;
  $files_removed = array();

  // Prune old files
  $results = db_query("SELECT filename, filename_md5 FROM {advagg_files}");
  while ($row = db_fetch_array($results)) {

    // If the file exists, do nothing
    if (file_exists($row['filename'])) {
      continue;
    }

    // Remove bundles referencing missing files, if they are older than 2 weeks.
    $bundles = db_query("SELECT bundle_md5 FROM {advagg_bundles} WHERE filename_md5 = '%s' AND timestamp < %d", $row['filename_md5'], $max_file_time);
    while ($bundle_md5 = db_result($bundles)) {
      $bundles_removed++;
      db_query("DELETE FROM {advagg_bundles} WHERE bundle_md5 = '%s'", $bundle_md5);
    }
    $count = db_result(db_query("SELECT COUNT(*) FROM {advagg_bundles} WHERE filename_md5 = '%s'", $row['filename_md5']));

    // If no more bundles reference the missing file then remove the file.
    if (empty($count)) {
      db_query("DELETE FROM {advagg_files} WHERE filename_md5 = '%s'", $row['filename_md5']);
      $files_removed[] = $row['filename'];
    }
  }

  // Prune old bundles
  $bundles_removed += db_result(db_query("\n    SELECT COUNT(DISTINCT bundle_md5) AS advagg_count\n    FROM {advagg_bundles}\n    WHERE timestamp < %d\n  ", $max_bundle_time));
  $results = db_query("DELETE FROM {advagg_bundles} WHERE timestamp < %d", $max_bundle_time);

  // Report to watchdog if anything was done.
  if (!empty($bundles_removed) || !empty($files_removed)) {
    watchdog('advagg', 'Cron ran and the following files where removed from the database: %files <br /> %count old bundles where also removed from the database.', array(
      '%files' => implode(', ', $files_removed),
      '%count' => $bundles_removed,
    ));
  }
}