Index: modules/color/color.module =================================================================== RCS file: /cvs/drupal/drupal/modules/color/color.module,v retrieving revision 1.10 diff -u -r1.10 color.module --- modules/color/color.module 4 Dec 2006 22:52:27 -0000 1.10 +++ modules/color/color.module 5 Dec 2006 15:33:47 -0000 @@ -41,7 +41,7 @@ // 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']); } @@ -88,11 +88,11 @@ $info = color_get_info($theme); // Add Farbtastic color picker - drupal_add_css('misc/farbtastic/farbtastic.css'); + drupal_add_css('misc/farbtastic/farbtastic.css', 'module', 'all', FALSE); drupal_add_js('misc/farbtastic/farbtastic.js'); // Add custom CSS/JS - drupal_add_css($base .'/color.css', 'module'); + drupal_add_css($base .'/color.css', 'module', 'all', FALSE); drupal_add_js($base .'/color.js'); drupal_add_js(array('color' => array( 'reference' => color_get_palette($theme, true) Index: update.php =================================================================== RCS file: /cvs/drupal/drupal/update.php,v retrieving revision 1.206 diff -u -r1.206 update.php --- update.php 28 Nov 2006 20:52:51 -0000 1.206 +++ update.php 5 Dec 2006 15:33:43 -0000 @@ -785,6 +785,9 @@ update_fix_watchdog(); update_fix_sessions(); + // Clear any cached CSS files, in case any module updates its CSS as well. + drupal_clear_css_cache(); + $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : ''; switch ($op) { case 'Update': Index: modules/watchdog/watchdog.module =================================================================== RCS file: /cvs/drupal/drupal/modules/watchdog/watchdog.module,v retrieving revision 1.163 diff -u -r1.163 watchdog.module --- modules/watchdog/watchdog.module 1 Dec 2006 23:17:26 -0000 1.163 +++ modules/watchdog/watchdog.module 5 Dec 2006 15:33:51 -0000 @@ -53,7 +53,7 @@ else { if (arg(0) == 'admin' && arg(1) == 'logs') { // Add the CSS for this module - drupal_add_css(drupal_get_path('module', 'watchdog') .'/watchdog.css'); + drupal_add_css(drupal_get_path('module', 'watchdog') .'/watchdog.css', 'module', 'all', FALSE); } } Index: modules/profile/profile.module =================================================================== RCS file: /cvs/drupal/drupal/modules/profile/profile.module,v retrieving revision 1.182 diff -u -r1.182 profile.module --- modules/profile/profile.module 1 Dec 2006 23:17:26 -0000 1.182 +++ modules/profile/profile.module 5 Dec 2006 15:33:48 -0000 @@ -410,7 +410,7 @@ */ function profile_browse() { // Add CSS - drupal_add_css(drupal_get_path('module', 'profile') .'/profile.css'); + drupal_add_css(drupal_get_path('module', 'profile') .'/profile.css', 'module', 'all', FALSE); $name = arg(1); list(,,$value) = explode('/', $_GET['q'], 3); Index: modules/search/search.module =================================================================== RCS file: /cvs/drupal/drupal/modules/search/search.module,v retrieving revision 1.204 diff -u -r1.204 search.module --- modules/search/search.module 1 Dec 2006 22:47:53 -0000 1.204 +++ modules/search/search.module 5 Dec 2006 15:33:49 -0000 @@ -1076,7 +1076,7 @@ */ function search_data($keys = NULL, $type = 'node') { // Add CSS - drupal_add_css(drupal_get_path('module', 'search') .'/search.css'); + drupal_add_css(drupal_get_path('module', 'search') .'/search.css', 'module', 'all', FALSE); if (isset($keys)) { if (module_hook($type, 'search')) { Index: includes/theme.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/theme.inc,v retrieving revision 1.328 diff -u -r1.328 theme.inc --- includes/theme.inc 29 Nov 2006 20:39:37 -0000 1.328 +++ includes/theme.inc 5 Dec 2006 15:33:46 -0000 @@ -406,7 +406,7 @@ function theme_maintenance_page($content, $messages = TRUE, $partial = FALSE) { drupal_set_header('Content-Type: text/html; charset=utf-8'); - drupal_add_css('misc/maintenance.css', 'core'); + drupal_add_css('misc/maintenance.css', 'core', 'all', FALSE); drupal_set_html_head(''); $output = "\n"; Index: includes/locale.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/locale.inc,v retrieving revision 1.99 diff -u -r1.99 locale.inc --- includes/locale.inc 5 Dec 2006 05:47:37 -0000 1.99 +++ includes/locale.inc 5 Dec 2006 15:33:46 -0000 @@ -1284,7 +1284,7 @@ */ function _locale_string_language_list($translation) { // Add CSS - drupal_add_css(drupal_get_path('module', 'locale') .'/locale.css'); + drupal_add_css(drupal_get_path('module', 'locale') .'/locale.css', 'module', 'all', FALSE); $languages = locale_supported_languages(FALSE, TRUE); unset($languages['name']['en']); Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.595 diff -u -r1.595 common.inc --- includes/common.inc 5 Dec 2006 05:47:37 -0000 1.595 +++ includes/common.inc 5 Dec 2006 15:33:44 -0000 @@ -1362,19 +1362,27 @@ * 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; @@ -1392,13 +1400,34 @@ */ 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); + $is_writable = is_writable(file_directory_path()) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC); + + foreach ($css as $media => $types) { + if ($is_writable && $cache_css) { + $filename = md5(serialize($types)) .'.css'; + if (function_exists('custom_build_css_cache')) { + // Allow a contrib module to override the cache building. + $cache_file = custom_build_css_cache($types, $filename); + } + else { + $cache_file = drupal_build_css_cache($types, $filename); + } + $output .= ''. "\n"; + } + + // If CSS caching is off, we still need to output 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 || !($is_writable && $cache_css)) { + $output .= '' ."\n"; + } + } } } @@ -1406,6 +1435,65 @@ } /** + * Aggregate and optimize CSS files, putting them in the files directory. + * + * @param $types + * An array of types of CSS files (e.g., screen, print) to aggregate and compress into one file. + * @param $filename + * The name of the aggregate CSS file. + * @return + * The name of the CSS file. + */ +function drupal_build_css_cache($types, $filename) { + $data = ''; + + // Create the css/ within the files folder. + $csspath = file_create_path('css'); + file_check_directory($csspath, FILE_CREATE_DIRECTORY); + + if (!file_exists($csspath .'/'. $filename)) { + // Build aggregate CSS file. + foreach ($types 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; + + // 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, $csspath .'/'. $filename, FILE_EXISTS_REPLACE); + } + return $csspath .'/'. $filename; +} + +/** + * Delete all cached CSS files. + */ +function drupal_clear_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/help/help.module =================================================================== RCS file: /cvs/drupal/drupal/modules/help/help.module,v retrieving revision 1.66 diff -u -r1.66 help.module --- modules/help/help.module 1 Dec 2006 22:47:53 -0000 1.66 +++ modules/help/help.module 5 Dec 2006 15:33:47 -0000 @@ -37,7 +37,7 @@ */ function help_main() { // Add CSS - drupal_add_css(drupal_get_path('module', 'help') .'/help.css'); + drupal_add_css(drupal_get_path('module', 'help') .'/help.css', 'module', 'all', FALSE); $output = t('
'. t('Welcome to the administration section. Here you may control how your site functions.') .'
'; case 'admin/by-module': return ''. t('This page shows you all available administration tasks for each module.') .'
'; - case 'admin/settings/page-caching': - return ''. t('Enabling the cache will offer a sufficient performance boost. Drupal can store and send compressed cached pages requested by "anonymous" users. By caching a web page, Drupal does not have to create the page each time someone wants to view it.') .'
'; case 'admin/build/themes': return ''. t('Select which themes are available to your users and specify the default theme. To configure site-wide display settings, click the "configure" task above. Alternately, to override these settings in a specific theme, click the "configure" link for the corresponding theme. Note that different themes may have different regions available for rendering content like blocks. If you want consistency in what your users see, you may wish to enable only one theme.') .'
'; case 'admin/build/themes/settings': @@ -229,11 +227,11 @@ 'callback' => 'drupal_get_form', 'callback arguments' => array('system_error_reporting_settings')); $items[] = array( - 'path' => 'admin/settings/page-caching', - 'title' => t('Page caching'), - 'description' => t('Enable or disable page caching for anonymous users.'), + 'path' => 'admin/settings/caching', + 'title' => t('Caching'), + 'description' => t('Enable or disable page caching for anonymous users, and enable or disable CSS caching.'), 'callback' => 'drupal_get_form', - 'callback arguments' => array('system_page_caching_settings')); + 'callback arguments' => array('system_caching_settings')); $items[] = array( 'path' => 'admin/settings/file-system', 'title' => t('File system'), @@ -643,7 +641,7 @@ return system_settings_form($form); } -function system_page_caching_settings() { +function system_caching_settings() { $description = ''. t("The normal cache mode is suitable for most sites and does not cause any side effects. The aggressive cache mode causes Drupal to skip the loading (init) and unloading (exit) of enabled modules when serving a cached page. This results in an additional performance boost but can cause unwanted side effects.") .'
'; @@ -656,8 +654,13 @@ else { $description .= ''. t('Currently, all enabled modules are compatible with the aggressive caching policy. Please note, if you use aggressive caching and enable new modules, you will need to check this page again to ensure compatibility.') .'
'; } - - $form['cache'] = array( + $form['page_cache'] = array( + '#type' => 'fieldset', + '#title' => t('Page cache'), + '#description' => t('Enabling the cache will offer a significant performance boost. Drupal can store and send compressed cached pages requested by anonymous users. By caching a web page, Drupal does not have to construct the page each time someone wants to view it.'), + ); + + $form['page_cache']['cache'] = array( '#type' => 'radios', '#title' => t('Caching mode'), '#default_value' => variable_get('cache', CACHE_DISABLED), @@ -667,13 +670,26 @@ $period = drupal_map_assoc(array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400), 'format_interval'); $period[0] = t('none'); - $form['cache_lifetime'] = array( + $form['page_cache']['cache_lifetime'] = array( '#type' => 'select', '#title' => t('Minimum cache lifetime'), '#default_value' => variable_get('cache_lifetime', 0), '#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.') ); + $public_downloads = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC; + $is_writable = is_writable(file_directory_path()); + $form['cache_css'] = array( + '#type' => 'radios', + '#title' => t('Cache and compress CSS files'), + '#default_value' => variable_get('cache_css', FALSE) && $public_downloads && $is_writable, + '#disabled' => !($public_downloads && $is_writable), + '#options' => array(t('Disabled'), t('Enabled')), + '#description' => t("Some 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 increase the load time of each page. These HTTP requests can also slightly increase server load. It is recommended to only turn this option on when your site is in production, as it can interfere with theme development. This option is disabled if you have not set up your files directory, or if your download method is set to private."), + ); + + $form['#submit']['system_settings_form_submit'] = array(); + $form['#submit']['drupal_clear_css_cache'] = array(); return system_settings_form($form); } @@ -1102,6 +1118,7 @@ else { drupal_set_message(t('The configuration options have been saved.')); } + menu_rebuild(); } @@ -1109,6 +1126,8 @@ * Menu callback; displays a listing of all themes. */ function system_themes() { + // Clear cached CSS files. + drupal_clear_css_cache(); $themes = system_theme_data(); ksort($themes); @@ -1485,6 +1504,10 @@ if ($dependencies && !isset($form_values['confirm'])) { return FALSE; } + + // Clear the CSS cache. + drupal_clear_css_cache(); + return 'admin/build/modules'; }