Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.1264 diff -u -p -r1.1264 common.inc --- includes/common.inc 22 Nov 2010 05:22:43 -0000 1.1264 +++ includes/common.inc 28 Nov 2010 04:39:13 -0000 @@ -3062,7 +3062,12 @@ function drupal_aggregate_css(&$css_grou // the group's data property to the file path of the aggregate file. case 'file': if ($group['preprocess'] && $preprocess_css) { - $css_groups[$key]['data'] = drupal_build_css_cache($group['items']); + $uri = drupal_build_css_cache($group['items']); + // Only include the file if was written successfully. Errors are + // logged using watchdog. + if ($uri) { + $css_groups[$key]['data'] = $uri; + } } break; // Aggregate all inline CSS content into the group's data property. @@ -3326,68 +3331,115 @@ function drupal_pre_render_styles($eleme */ function drupal_build_css_cache($css) { $data = ''; - $uri = ''; + $new_file_uri = ''; $map = variable_get('drupal_css_cache_files', array()); $key = hash('sha256', serialize($css)); - if (isset($map[$key])) { - $uri = $map[$key]; - } + $old_file_uri = (isset($map[$key]) && file_exists($map[$key])) ? $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); - - // Build the base URL of this CSS file: start with the full URL. - $css_base_url = file_create_url($stylesheet['data']); - // Move to the parent. - $css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/')); - // Simplify to a relative URL if the stylesheet URL starts with the - // base URL of the website. - if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) { - $css_base_url = substr($css_base_url, strlen($GLOBALS['base_root'])); - } + $lock_name = 'css_cache:' . $key; + $lock_acquired = FALSE; + while (++$lock_acquire_attempts < 5 && !$lock_acquired) { + $lock_acquired = lock_acquire($lock_name); + + // Reload the CSS map directly from the database. + $vars = variable_initialize(); + $map = isset($vars['drupal_css_cache_files']) ? $vars['drupal_css_cache_files'] : array(); + + if ($lock_acquired) { + // We got the lock, but the CSS file has changed since we started trying + // to rebuild it, so another process did the work, just return. + if (isset($map[$key]) && $map[$key] != $old_file_uri) { + return $map[$key]; + } + } + else { + // We didn't get the lock, so check if there's a file we can serve. + if (isset($map[$key]) && file_exists($map[$key])) { + return $map[$key]; + } + else if ($old_file_uri) { + return $old_file_uri; + } - _drupal_build_css_path(NULL, $css_base_url . '/'); - // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths. - $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents); + // No file and no lock, so try again. + lock_wait($lock_name); + } + } + + // We got the lock, or there's no aggregated CSS file for this $key. + foreach ($css as $stylesheet) { + // Only 'file' stylesheets can be aggregated. + if ($stylesheet['type'] == 'file') { + $contents = drupal_load_stylesheet($stylesheet['data'], TRUE); + + // Build the base URL of this CSS file: start with the full URL. + $css_base_url = file_create_url($stylesheet['data']); + // Move to the parent. + $css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/')); + // Simplify to a relative URL if the stylesheet URL starts with the + // base URL of the website. + if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) { + $css_base_url = substr($css_base_url, strlen($GLOBALS['base_root'])); } + + _drupal_build_css_path(NULL, $css_base_url . '/'); + // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths. + $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/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; - } - // If CSS gzip compression is enabled, clean URLs are enabled (which means - // that rewrite rules are working) and the zlib extension is available then - // create a gzipped version of this file. This file is served conditionally - // to browsers that accept gzip using .htaccess rules. - if (variable_get('css_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) { - if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { - return FALSE; - } - } - // Save the updated map. - $map[$key] = $uri; - variable_set('drupal_css_cache_files', $map); + // 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; + + // Create the css/ directory within the files folder. + $csspath = 'public://css'; + file_prepare_directory($csspath, FILE_CREATE_DIRECTORY); + + // Prefix filename to prevent blocking by firewalls which reject files + // starting with "ad*", and create the CSS file. + $filename = 'css_' . drupal_hash_base64($data) . '.css'; + $new_file_uri = $csspath . '/' . $filename; + if (!file_exists($new_file_uri) && !file_unmanaged_save_data($data, $new_file_uri, FILE_EXISTS_REPLACE)) { + lock_release($lock_name); + // If there's an existing file $uri, we should return it. + return $old_file_uri; + } + + // Save the updated map atomically. + $map_lock_acquire_attempts = 0; + while (!lock_acquire('css_cache') && ++$map_lock_acquire_attempts < 5) { + lock_wait('css_cache'); + } + + // Be sure we have the latest version of the css map. + $vars = variable_initialize(); + $map = isset($vars['drupal_css_cache_files']) ? $vars['drupal_css_cache_files'] : array(); + $map[$key] = $new_file_uri; + variable_set('drupal_css_cache_files', $map); + lock_release('css_cache'); + + // Release the lock now, because the file is written to disc and map + // updated, so we've stopped the stampede. + lock_release($lock_name); + + // If CSS gzip compression is enabled, clean URLs are enabled (which + // means that rewrite rules are working) and the zlib extension is + // available then create a gzipped version of this file. This file is + // served conditionally to browsers that accept gzip using .htaccess + // rules. + if (variable_get('css_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) { + if (!file_exists($uri . '.gz') { + // Even if we error out, we've successfully written the uncompressed + // aggregate file to disc, so there's no need to check the return value. + file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { + } } - return $uri; + + return $new_file_uri; } /** @@ -3558,7 +3610,12 @@ function _drupal_load_stylesheet($matche * Deletes old cached CSS files. */ function drupal_clear_css_cache() { + $map_lock_acquire_attempts = 0; + while (!lock_acquire('css_cache') && ++$map_lock_acquire_attempts < 5) { + lock_wait('css_cache'); + } variable_del('drupal_css_cache_files'); + lock_release('css_cache'); file_scan_directory('public://css', '/.*/', array('callback' => 'drupal_delete_file_if_stale')); } @@ -4688,36 +4745,77 @@ 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_exists($uri) && !file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) { - return FALSE; - } - // If JS gzip compression is enabled, clean URLs are enabled (which means - // that rewrite rules are working) and the zlib extension is available then - // create a gzipped version of this file. This file is served conditionally - // to browsers that accept gzip using .htaccess rules. - if (variable_get('js_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) { - if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($contents, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { - return FALSE; + $lock_acquired = FALSE; + $lock_acquire_attempts = 0; + $lock_name = 'js_cache:' . $key; + $mtime = $uri ? filemtime($uri) : 0; + + while (!$mtime && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5) { + if (empty($uri) || // Cachefile does exist in the map + !$mtime) { // Cachefile doesn't exist on the filesystem. + if ($lock_acquired = lock_acquire($lock_name)) { + // During the time it took to acquire the lock the map may have been + // updated. We call variable initialize as if a variable has been set + // while waiting the variable cache is invalid and we'll need to reload + // the variables out of the database. If no variable has been set, the + // variables will be quickly loaded out of the cache. + $vars = variable_initialize(); + $map = isset($vars['drupal_js_cache_files']) ? $vars['drupal_js_cache_files'] : array(); + if (isset($map[$key])) { + $uri = $map[$key]; + $mtime = $uri ? filemtime($uri) : 0; + break; + } + // 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_exists($uri) && !file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) { + lock_release($lock_name); + return FALSE; + } + // If JS gzip compression is enabled, clean URLs are enabled (which + // means that rewrite rules are working) and the zlib extension is + // available then create a gzipped version of this file. This file is + // served conditionally to browsers that accept gzip using .htaccess + // rules. + if (variable_get('js_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) { + if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($contents, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { + lock_release($lock_name); + return FALSE; + } + } + // Atomically save the updated map. + $map_lock_acquire_attempts = 0; + while (!lock_acquire('js_cache') && ++$map_lock_acquire_attempts < 5) { + lock_wait('js_cache'); + } + // Be sure we have the absolutely latest version of the js cache + // before we update it. + $vars = variable_initialize(); + $map = isset($vars['drupal_js_cache_files']) ? $vars['drupal_js_cache_files'] : array(); + $map[$key] = $uri; + variable_set('drupal_js_cache_files', $map); + lock_release('js_cache'); + // Done rebuilding JS file. + lock_release($lock_name); + } + else { + lock_wait($lock_name); + $mtime = $uri ? filemtime($uri) : 0; } } - $map[$key] = $uri; - variable_set('drupal_js_cache_files', $map); } return $uri; }