Index: update.php =================================================================== RCS file: /cvs/drupal/drupal/update.php,v retrieving revision 1.206 diff -u -F^f -r1.206 update.php --- update.php 28 Nov 2006 20:52:51 -0000 1.206 +++ update.php 30 Nov 2006 00:46:37 -0000 @@ -785,6 +785,9 @@ function update_create_cache_tables() { update_fix_watchdog(); update_fix_sessions(); + // Clear any cached CSS files, in case any module updates its CSS as well + drupal_flush_css_cache(); + $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : ''; switch ($op) { case 'Update': Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.592 diff -u -F^f -r1.592 common.inc --- includes/common.inc 29 Nov 2006 06:03:56 -0000 1.592 +++ includes/common.inc 30 Nov 2006 00:46:39 -0000 @@ -1352,19 +1352,27 @@ function drupal_add_link($attributes) { * Adds a CSS file to the stylesheet queue. * * @param $path - * The path to the CSS file relative to the base_path(), e.g., /modules/devel/devel.css. + * (optional) The path to the CSS file relative to the base_path(), e.g., /modules/devel/devel.css. * @param $type - * The type of stylesheet that is being added. Types are: core, module, and theme. + * (optional) The type of stylesheet that is being added. Types are: core, module, and theme. * @param $media * (optional) The media type for the stylesheet, e.g., all, print, screen. + * @param $cache + * (optional) Should this CSS file be cached if cache CSS setting is turned on? * @return * An array of CSS files. */ -function drupal_add_css($path = NULL, $type = 'module', $media = 'all') { - static $css = array('core' => array(), 'module' => array(), 'theme' => array()); +function drupal_add_css($path = NULL, $type = 'module', $media = 'all', $cache = TRUE) { + static $css = array(); - if (!is_null($path)) { - $css[$type][$path] = array('path' => $path, 'media' => $media); + // Create an array of CSS files for each media type first, since each type needs to be served + // to the browser differently. + if (isset($path)) { + // This check is necessary to ensure proper cascading of styles and is faster than an asort() + if (!isset($css[$media])) { + $css[$media] = array('core' => array(), 'module' => array(), 'theme' => array()); + } + $css[$media][$type][$path] = $cache; } return $css; @@ -1382,13 +1390,34 @@ function drupal_add_css($path = NULL, $t */ function drupal_get_css($css = NULL) { $output = ''; - if (is_null($css)) { + if (!isset($css)) { $css = drupal_add_css(); } - foreach ($css as $type) { - foreach ($type as $file) { - $output .= '\n"; + $cache_css = variable_get('cache_css', FALSE); + $directory_path = file_directory_path(); + $is_writable = is_writable($directory_path); + + foreach ($css as $media => $types) { + if ($is_writable && $cache_css) { + $filename = md5(serialize($types)) .'.css'; + $path = $directory_path .'/css/'; + + if (!file_exists($path . $filename)) { + $filename = drupal_build_css_cache($types); + } + + $output .= ''. "\n"; + } + + // If CSS caching is off, we still need to ouput the styles + // Additionally, go through any remaining styles if CSS caching is on and output the non-cached ones + foreach ($types as $type) { + foreach ($type as $file => $cache) { + if (!$cache || !$cache_css) { + $output .= '' ."\n"; + } + } } } @@ -1396,6 +1425,71 @@ function drupal_get_css($css = NULL) { } /** + * Aggregate and optimize CSS files, putting them in the files directory. + * + * @param $css + * An array of CSS files to aggregate and compress into one file. + * @return + * The name of the CSS file. + */ +function drupal_build_css_cache($css) { + $data = ''; + $filename = md5(serialize($css)) .'.css'; + + // Create the css/ within the files folder + file_check_directory(file_create_path('css'), FILE_CREATE_DIRECTORY); + + // Build aggregate CSS file + foreach ($css as $type) { + foreach ($type as $file => $cache) { + if ($cache) { + $contents = file_get_contents($file); + // Return the path to where this CSS file originated from, stripping off the name of the file at the end of the path + $path = base_path() . substr($file, 0, strrpos($file, '/')) .'/'; + // Fix all paths within this CSS file, ignoring absolute paths + $contents = preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. $path . '\2', $contents); + // Fix any @import that don't use url() and is not absoslute + $data .= preg_replace('/@import\s*([\'"]?)(?![a-z]+:)/i', '@import \1'. $path . '\2', $contents); + } + } + } + + // @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; + + // If any modules implement hook_compress_css(), let them handle compressing the CSS + if (module_invoke_all('hook_compress_css')) { + foreach (module_implements('compress_css') as $module) { + $function = $module .'_compress_css'; + $function($contents); + } + } + else { + // Perform some safe CSS optimizations + $data = preg_replace('< + \s*([@{}:;\)])\s* | # Remove whitespace around separators. + /\*([^*\\\\]|\*(?!/))+\*/ | # Remove comments that are not CSS hacks + [\n\r] # Remove line breaks + >x', '\1', $data); + } + + // Create the CSS file + file_save_data($data, file_create_path('css') .'/'. $filename, FILE_EXISTS_REPLACE); + + return $filename; +} + +/** + * Delete all cached CSS files + */ +function drupal_flush_css_cache() { + file_scan_directory(file_create_path('css'), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE); +} + +/** * Add a JavaScript file, setting or inline code to the page. * * The behavior of this function depends on the parameters it is called with. Index: modules/color/color.module =================================================================== RCS file: /cvs/drupal/drupal/modules/color/color.module,v retrieving revision 1.8 diff -u -F^f -r1.8 color.module --- modules/color/color.module 25 Nov 2006 08:58:35 -0000 1.8 +++ modules/color/color.module 30 Nov 2006 00:46:39 -0000 @@ -41,7 +41,7 @@ function _color_page_alter(&$vars) { // Override stylesheet $path = variable_get('color_'. $theme_key .'_stylesheet', NULL); if ($path) { - $vars['css']['theme'] = array($path => array('path' => $path, 'media' => 'all')); + $vars['css']['all']['theme'][$path] = TRUE; $vars['styles'] = drupal_get_css($vars['css']); } Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.415 diff -u -F^f -r1.415 system.module --- modules/system/system.module 29 Nov 2006 20:36:30 -0000 1.415 +++ modules/system/system.module 30 Nov 2006 00:46:42 -0000 @@ -675,6 +675,14 @@ function system_page_caching_settings() '#description' => t('On high-traffic sites it can become necessary to enforce a minimum cache lifetime. The minimum cache lifetime is the minimum amount of time that will go by before the cache is emptied and recreated. A larger minimum cache lifetime offers better performance, but users will not see new content for a longer period of time.') ); + $form['cache_css'] = array( + '#type' => 'select', + '#title' => t('Cache and compress CSS files'), + '#default_value' => variable_get('cache_css', FALSE), + '#options' => array(t('Disabled'), t('Enabled')), + '#description' => t("A lot of Drupal modules include their own CSS files. When these modules are enabled, each module's CSS file adds an additional HTTP request to the page, which can slow down the loading time of each page. These HTTP requests can increase the server load. It is recommended to only turn this option on when your site is in production, as leaving it on can interfere with theming development."), + ); + return system_settings_form($form); } @@ -1102,6 +1110,12 @@ function system_settings_form_submit($fo else { drupal_set_message(t('The configuration options have been saved.')); } + + // Only clear the CSS cache if the settings on the caching paged are saved + if ($form_id == 'system_page_caching_settings') { + drupal_flush_css_cache(); + } + menu_rebuild(); } @@ -1109,6 +1123,8 @@ function system_settings_form_submit($fo * Menu callback; displays a listing of all themes. */ function system_themes() { + // Clear cached CSS files + drupal_flush_css_cache(); $themes = system_theme_data(); ksort($themes); @@ -1485,6 +1501,10 @@ function system_modules_submit($form_id, if ($dependencies && !isset($form_values['confirm'])) { return FALSE; } + + // Clear the CSS cache + drupal_flush_css_cache(); + return 'admin/build/modules'; }