You are here

function advagg_admin_clear_file_aggregates in Advanced CSS/JS Aggregation 7.2

Remove the aggregates that contain the given filename.

Parameters

string $filename: Name of file to lookup. Can be a comma separated list.

bool $dry_run: If TRUE, return the regex search string.

Return value

array Returns an array of the parent file and what children where deleted.

Related topics

2 calls to advagg_admin_clear_file_aggregates()
advagg_admin_clear_file_aggregate_callback in ./advagg.admin.inc
Display what files where deleted via ajax callback.
advagg_admin_clear_file_aggregate_submit in ./advagg.admin.inc
Display what files where deleted in a drupal message.

File

./advagg.admin.inc, line 1929
Admin page callbacks for the advanced CSS/JS aggregation module.

Code

function advagg_admin_clear_file_aggregates($filename, $dry_run = FALSE) {
  module_load_include('inc', 'advagg', 'advagg.cache');
  list($css_path, $js_path) = advagg_get_root_files_dir();
  $space = ADVAGG_SPACE;
  $output = array();
  $options = array(
    'callback' => 'file_unmanaged_delete',
  );
  if ($dry_run) {
    $options = array();
  }

  // Strip quotes and trim.
  $filenames = array_map('trim', explode(',', trim(str_replace(array(
    '"',
    "'",
  ), '', $filename))));
  foreach ($filenames as $filename) {
    $results = db_select('advagg_files', 'af')
      ->fields('af')
      ->condition('filename', $filename)
      ->execute();
    while ($row = $results
      ->fetchAssoc()) {

      // Get aggregates that use this file.
      $row['aggregates_using_file'] = advagg_get_aggregates_using_file($row['filename_hash']);

      // Get dir and other info from file.
      if ($row['filetype'] === 'css') {
        $dirname = $css_path[0];
        $basename_prefix = "{$row['filetype']}";
      }
      if ($row['filetype'] === 'js') {
        $dirname = $js_path[0];
        $basename_prefix = "{$row['filetype']}";
      }

      // Build regex search string for file_scan_directory().
      $regex_search = array();
      foreach ($row['aggregates_using_file'] as $values) {
        $regex_search[] = preg_quote("{$basename_prefix}{$space}{$values['aggregate_filenames_hash']}{$space}") . '.*';
      }
      $regex_search = array_unique($regex_search);
      $regex_search_string = '/(' . implode('|', $regex_search) . ')/';
      $files = file_scan_directory($dirname, $regex_search_string, $options);

      // List what files were deleted.
      $row['aggregates_deleted'] = array();
      $files_deleted = array_keys($files);
      if (!empty($files_deleted)) {
        $row['aggregates_deleted'][] = $files_deleted;
      }
      $output[$filename] = $row['aggregates_deleted'];
    }
  }
  return $output;
}