Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.1210 diff -u -p -r1.1210 common.inc --- includes/common.inc 22 Aug 2010 12:46:21 -0000 1.1210 +++ includes/common.inc 24 Aug 2010 02:05:09 -0000 @@ -3210,42 +3210,56 @@ 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; + } + // Save the updated map. + $map[$key] = $uri; + variable_set('drupal_css_cache_files', $map); + } + else { + if ($mtime) { + break; + } + lock_wait(); } } - - // 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; - } - // Save the updated map. - $map[$key] = $uri; - variable_set('drupal_css_cache_files', $map); + $mtime = $uri ? filemtime($uri) : 0; } + while (!$mtime && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5); return $uri; } @@ -3383,8 +3397,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'); } /** @@ -4421,29 +4434,45 @@ 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)) { + return FALSE; + } + $map[$key] = $uri; + variable_set('drupal_js_cache_files', $map); + lock_release($key); + } + else { + if ($mtime) { + break; + } + lock_wait(); + } } - $map[$key] = $uri; - variable_set('drupal_js_cache_files', $map); + $mtime = $uri ? filemtime($uri) : 0; } + while (!$mtime && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5); return $uri; + } /** @@ -4451,8 +4480,21 @@ 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'; + $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')); } /**