Index: update.php =================================================================== RCS file: /cvs/drupal/drupal/update.php,v retrieving revision 1.205 diff -u -F^f -r1.205 update.php --- update.php 2 Oct 2006 11:36:17 -0000 1.205 +++ update.php 23 Nov 2006 02:29:17 -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.589 diff -u -F^f -r1.589 common.inc --- includes/common.inc 21 Nov 2006 19:40:09 -0000 1.589 +++ includes/common.inc 23 Nov 2006 02:29:18 -0000 @@ -1286,19 +1286,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; @@ -1316,13 +1324,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"; + } + } } } @@ -1330,6 +1359,60 @@ 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 + $data .= preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. $path . '\2', $contents); + } + } + } + + // If any modules implement hook_compress_css, let them handle compressing + // the css, otherwise just strip out the whitespace, which can be done safely. + if (module_invoke_all('hook_compress_css')) { + foreach (module_implements('compress_css') as $module) { + $function = $module .'_compress_css'; + $function($contents); + } + } + else { + // Preform some safe CSS optimizations for removing white space + $data = str_replace(array(": ", "; ", "\n", "\t", "\r", ), array(':', ';', '', '', ''), $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.7 diff -u -F^f -r1.7 color.module --- modules/color/color.module 10 Nov 2006 19:59:30 -0000 1.7 +++ modules/color/color.module 23 Nov 2006 02:29:20 -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.402 diff -u -F^f -r1.402 system.module --- modules/system/system.module 21 Nov 2006 20:55:35 -0000 1.402 +++ modules/system/system.module 23 Nov 2006 02:29:24 -0000 @@ -674,6 +674,14 @@ function system_page_caching_settings() '#options' => $period, '#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('No'), t('Yes')), + '#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 initial page load for a user browsing the site. Additionally, this additional HTTP request 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); @@ -1481,6 +1497,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'; }