Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.1214 diff -u -p -r1.1214 common.inc --- includes/common.inc 5 Sep 2010 15:38:16 -0000 1.1214 +++ includes/common.inc 10 Sep 2010 03:39:24 -0000 @@ -3221,42 +3221,64 @@ function drupal_build_css_cache($css) { $uri = $map[$key]; } - if (empty($uri) || !file_exists($uri)) { - // Build aggregate CSS file. - foreach ($css as $stylesheet) { - // Only 'file' stylesheets can be aggregated. - if ($stylesheet['type'] == 'file') { - $contents = drupal_load_stylesheet($stylesheet['data'], TRUE); - // Return the path to where this CSS file originated from. - $base = base_path() . dirname($stylesheet['data']) . '/'; - _drupal_build_css_path(NULL, $base); - // Prefix all paths within this CSS file, ignoring external and absolute paths. - $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents); - } - } + $lock_acquire_attempts = 0; + $mtime = $uri ? filemtime($uri) : 0; + do { + if (empty($uri) || !$mtime || (isset($map['clear']) && $mtime < $map['clear'])) { + if ($lock_acquired = lock_acquire('css_cache')) { + // Build aggregate CSS file. + foreach ($css as $stylesheet) { + // Only 'file' stylesheets can be aggregated. + if ($stylesheet['type'] == 'file') { + $contents = drupal_load_stylesheet($stylesheet['data'], TRUE); + // Return the path to where this CSS file originated from. + $base = base_path() . dirname($stylesheet['data']) . '/'; + _drupal_build_css_path(NULL, $base); + // Prefix all paths within this CSS file, ignoring external and absolute paths. + $data .= preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $contents); + } + } - // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, - // @import rules must proceed any other style, so we move those to the top. - $regexp = '/@import[^;]+;/i'; - preg_match_all($regexp, $data, $matches); - $data = preg_replace($regexp, '', $data); - $data = implode('', $matches[0]) . $data; - - // Prefix filename to prevent blocking by firewalls which reject files - // starting with "ad*". - $filename = 'css_' . drupal_hash_base64($data) . '.css'; - // Create the css/ within the files folder. - $csspath = 'public://css'; - $uri = $csspath . '/' . $filename; - // Create the CSS file. - file_prepare_directory($csspath, FILE_CREATE_DIRECTORY); - if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) { - return FALSE; + // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, + // @import rules must proceed any other style, so we move those to the top. + $regexp = '/@import[^;]+;/i'; + preg_match_all($regexp, $data, $matches); + $data = preg_replace($regexp, '', $data); + $data = implode('', $matches[0]) . $data; + + // Prefix filename to prevent blocking by firewalls which reject files + // starting with "ad*". + $filename = 'css_' . drupal_hash_base64($data) . '.css'; + // Create the css/ within the files folder. + $csspath = 'public://css'; + $uri = $csspath . '/' . $filename; + // Create the CSS file. + file_prepare_directory($csspath, FILE_CREATE_DIRECTORY); + if (!file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) { + lock_release('css_cache'); + return FALSE; + } + // If this took more than one attempt, get the map again, because it has likely changed. + if ($lock_acquire_attempts) { + $vars = variable_initialize(array()); + $map = $vars['drupal_css_cache_files']; + } + // Save the updated map. + $map = variable_get('drupal_css_cache_files', array()); + $map[$key] = $uri; + variable_set('drupal_css_cache_files', $map); + lock_release('css_cache'); + } + else { + if ($mtime) { + break; + } + lock_wait('css_cache'); + } + $mtime = $uri ? filemtime($uri) : 0; } - // Save the updated map. - $map[$key] = $uri; - variable_set('drupal_css_cache_files', $map); } + while (!$mtime && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5); return $uri; } @@ -3394,8 +3416,7 @@ function _drupal_load_stylesheet($matche * Deletes old cached CSS files. */ function drupal_clear_css_cache() { - variable_del('drupal_css_cache_files'); - file_scan_directory('public://css', '/.*/', array('callback' => 'drupal_delete_file_if_stale')); + drupal_clear_js_css_cache('css'); } /** @@ -4432,28 +4453,50 @@ function drupal_build_js_cache($files) { $uri = $map[$key]; } - if (empty($uri) || !file_exists($uri)) { - // Build aggregate JS file. - foreach ($files as $path => $info) { - if ($info['preprocess']) { - // Append a ';' and a newline after each JS file to prevent them from running together. - $contents .= file_get_contents($path) . ";\n"; - } - } - // Prefix filename to prevent blocking by firewalls which reject files - // starting with "ad*". - $filename = 'js_' . drupal_hash_base64($contents) . '.js'; - // Create the js/ within the files folder. - $jspath = 'public://js'; - $uri = $jspath . '/' . $filename; - // Create the JS file. - file_prepare_directory($jspath, FILE_CREATE_DIRECTORY); - if (!file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) { - return FALSE; + $lock_acquire_attempts = 0; + $mtime = $uri ? filemtime($uri) : 0; + do { + if (empty($uri) || !$mtime || (isset($map['clear']) && $mtime < $map['clear'])) { + if ($lock_acquired = lock_acquire('js_cache')) { + // Build aggregate JS file. + foreach ($files as $path => $info) { + if ($info['preprocess']) { + // Append a ';' and a newline after each JS file to prevent them from running together. + $contents .= file_get_contents($path) . ";\n"; + } + } + // Prefix filename to prevent blocking by firewalls which reject files + // starting with "ad*". + $filename = 'js_' . drupal_hash_base64($contents) . '.js'; + // Create the js/ within the files folder. + $jspath = 'public://js'; + $uri = $jspath . '/' . $filename; + // Create the JS file. + file_prepare_directory($jspath, FILE_CREATE_DIRECTORY); + if (!file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) { + lock_release('js_cache'); + return FALSE; + } + // If this took more than one attempt, get the map again, because it has likely changed. + if ($lock_acquire_attempts) { + $vars = variable_initialize(array()); + $map = $vars['drupal_js_cache_files']; + } + // Save the updated map. + $map[$key] = $uri; + variable_set('drupal_js_cache_files', $map); + lock_release('js_cache'); + } + else { + if ($mtime) { + break; + } + lock_wait('js_cache'); + } + $mtime = $uri ? filemtime($uri) : 0; } - $map[$key] = $uri; - variable_set('drupal_js_cache_files', $map); } + while (!$mtime && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5); return $uri; } @@ -4462,8 +4505,22 @@ function drupal_build_js_cache($files) { */ function drupal_clear_js_cache() { variable_del('javascript_parsed'); - variable_del('drupal_js_cache_files'); - file_scan_directory('public://js', '/.*/', array('callback' => 'drupal_delete_file_if_stale')); + drupal_clear_js_css_cache('js'); +} + +/** + * Deletes old cached JavaScript or CSS files and variables. + */ +function drupal_clear_js_css_cache($type) { + // Set the cache clear timestamp. + $var = 'drupal_' . $type . '_cache_files'; + if ($map = variable_get($var, array())) { + $map['clear'] = REQUEST_TIME; + variable_set($var, $map); + } + + // Delete old stale files. + file_scan_directory('public://' . $type, '/.*/', array('callback' => 'drupal_delete_file_if_stale')); } /** @@ -6154,7 +6211,7 @@ function drupal_write_record($table, &$r } if (!property_exists($object, $field)) { - // Skip fields that are not provided, default values are already known + // Skip fields that are not provided, default values are already known // by the database. continue; } Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.959 diff -u -p -r1.959 system.module --- modules/system/system.module 5 Sep 2010 02:21:38 -0000 1.959 +++ modules/system/system.module 10 Sep 2010 03:39:25 -0000 @@ -2905,6 +2905,8 @@ function system_cron() { foreach ($cache_tables as $table) { cache_clear_all(NULL, $table); } + system_cleanup_cache_map('css'); + system_cleanup_cache_map('js'); // Cleanup the batch table and the queue for failed batches. db_delete('batch') @@ -2936,6 +2938,34 @@ function system_flush_caches() { } /** + * Helper function to remove stale css and js from the cache maps. + * + * @param $type + * 'css' or 'js' + */ +function system_cleanup_cache_map($type) { + $var = 'drupal_' . $type . '_cache_files'; + $map = variable_get($var, array()); + if (!empty($map['clear'])) { + $clear = $map['clear']; + foreach ($map as $key => $uri) { + if ($key == 'clear') { + continue; + } + $mtime = filemtime($uri); + if ($mtime && $mtime < $clear && $mtime < REQUEST_TIME - variable_get('drupal_stale_map_threshold', 3600)) { + unset($map[$key]); + $changed = TRUE; + } + } + if (!isset($changed)) { + unset($map['clear']); + } + variable_set($var, $map); + } +} + +/** * Implements hook_action_info(). */ function system_action_info() {