Index: includes/bootstrap.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v retrieving revision 1.264 diff -u -p -r1.264 bootstrap.inc --- includes/bootstrap.inc 14 Jan 2009 12:15:38 -0000 1.264 +++ includes/bootstrap.inc 15 Jan 2009 18:36:13 -0000 @@ -130,15 +130,15 @@ define('DRUPAL_BOOTSTRAP_SESSION', 4); define('DRUPAL_BOOTSTRAP_VARIABLES', 5); /** - * Seventh bootstrap phase: load bootstrap.inc and module.inc, start - * the variable system and try to serve a page from the cache. + * Seventh bootstrap phase: find out the language of the page. */ -define('DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE', 6); +define('DRUPAL_BOOTSTRAP_LANGUAGE', 6); /** - * Eighth bootstrap phase: find out language of the page. + * Eighth bootstrap phase: load bootstrap.inc and module.inc, start + * the variable system and try to serve a page from the cache. */ -define('DRUPAL_BOOTSTRAP_LANGUAGE', 7); +define('DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE', 7); /** * Nineth bootstrap phase: set $_GET['q'] to Drupal path of request. @@ -1062,14 +1062,14 @@ function drupal_anonymous_user($session * DRUPAL_BOOTSTRAP_ACCESS: identify and reject banned hosts. * DRUPAL_BOOTSTRAP_SESSION: initialize session handling. * DRUPAL_BOOTSTRAP_VARIABLES: initialize variable handling. + * DRUPAL_BOOTSRAP_LANGUAGE: identify the language used on the page. * DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: load bootstrap.inc and module.inc, start * the variable system and try to serve a page from the cache. - * DRUPAL_BOOTSTRAP_LANGUAGE: identify the language used on the page. * DRUPAL_BOOTSTRAP_PATH: set $_GET['q'] to Drupal path of request. * DRUPAL_BOOTSTRAP_FULL: Drupal is fully loaded, validate and fix input data. */ function drupal_bootstrap($phase = NULL) { - static $phases = array(DRUPAL_BOOTSTRAP_CONFIGURATION, DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE, DRUPAL_BOOTSTRAP_DATABASE, DRUPAL_BOOTSTRAP_ACCESS, DRUPAL_BOOTSTRAP_SESSION, DRUPAL_BOOTSTRAP_VARIABLES, DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE, DRUPAL_BOOTSTRAP_LANGUAGE, DRUPAL_BOOTSTRAP_PATH, DRUPAL_BOOTSTRAP_FULL), $completed_phase = -1; + static $phases = array(DRUPAL_BOOTSTRAP_CONFIGURATION, DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE, DRUPAL_BOOTSTRAP_DATABASE, DRUPAL_BOOTSTRAP_ACCESS, DRUPAL_BOOTSTRAP_SESSION, DRUPAL_BOOTSTRAP_VARIABLES, DRUPAL_BOOTSTRAP_LANGUAGE, DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE, DRUPAL_BOOTSTRAP_PATH, DRUPAL_BOOTSTRAP_FULL), $completed_phase = -1; if (isset($phase)) { while ($phases && $phase > $completed_phase) { @@ -1093,7 +1093,7 @@ function drupal_get_bootstrap_phase() { } function _drupal_bootstrap($phase) { - global $conf; + global $conf, $language, $language_redirect; switch ($phase) { @@ -1147,16 +1147,34 @@ function _drupal_bootstrap($phase) { $conf = variable_init(isset($conf) ? $conf : array()); break; + case DRUPAL_BOOTSTRAP_LANGUAGE: + drupal_init_language(); + break; + case DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: $cache_mode = variable_get('cache', CACHE_DISABLED); // Get the page from the cache. - $cache = $cache_mode == CACHE_DISABLED ? '' : page_get_cache(); // If the skipping of the bootstrap hooks is not enforced, call hook_boot. - if (!$cache || $cache_mode != CACHE_AGGRESSIVE) { + if ($cache_mode != CACHE_AGGRESSIVE) { // Load module handling. require_once DRUPAL_ROOT . '/includes/module.inc'; module_invoke_all('boot'); } + // If the language has been reset from the default, redirect + // to the new language. + if ($language_redirect && (($default_language = language_default()) != $language)) { + // Load modules required for hook_exit. + module_invoke_all('exit'); + // We need the path system. + drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH); + require_once DRUPAL_ROOT . '/includes/common.inc'; + $path = drupal_get_normal_path($_GET['q'], $default_language->language); + drupal_goto($path); + } + + // Fetch the page from cache. + $cache = $cache_mode == CACHE_DISABLED ? '' : page_get_cache(); + // If there is a cached page, display it. if ($cache) { drupal_page_cache_header($cache); @@ -1167,14 +1185,16 @@ function _drupal_bootstrap($phase) { // We are done. exit; } + // If not skipped above, call hook_boot now. + if ($cache_mode == CACHE_AGGRESSIVE) { + require_once DRUPAL_ROOT . '/includes/module.inc'; + module_invoke_all('boot'); + } + // Prepare for non-cached page workflow. drupal_page_header(); break; - case DRUPAL_BOOTSTRAP_LANGUAGE: - drupal_init_language(); - break; - case DRUPAL_BOOTSTRAP_PATH: require_once DRUPAL_ROOT . '/includes/path.inc'; // Initialize $_GET['q'] prior to loading modules and invoking hook_init(). Index: includes/language.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/language.inc,v retrieving revision 1.18 diff -u -p -r1.18 language.inc --- includes/language.inc 2 Jan 2009 23:37:32 -0000 1.18 +++ includes/language.inc 15 Jan 2009 18:36:13 -0000 @@ -7,10 +7,14 @@ */ /** - * Choose a language for the page, based on language negotiation settings. + * Choose a language for the page, based on language negotiation settings. + * + * @return + * Boolean indicating whether the language set is valid for the current + * path. */ function language_initialize() { - global $user; + global $user, $language_redirect; // Configured presentation language mode. $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE); @@ -52,13 +56,19 @@ function language_initialize() { break; } + // We should reach this point only if LANGUAGE_NEGOTIATION_PATH is set + // but no valid prefix was found or if some custom value has been set for + // language_negotiation. + // User language. if ($user->uid && isset($languages[$user->language])) { + $language_redirect = TRUE; return $languages[$user->language]; } // Browser accept-language parsing. if ($language = language_from_browser()) { + $language_redirect = TRUE; return $language; } Index: modules/simpletest/tests/bootstrap.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/bootstrap.test,v retrieving revision 1.10 diff -u -p -r1.10 bootstrap.test --- modules/simpletest/tests/bootstrap.test 14 Jan 2009 12:15:38 -0000 1.10 +++ modules/simpletest/tests/bootstrap.test 15 Jan 2009 18:36:14 -0000 @@ -88,6 +88,76 @@ class BootstrapIPAddressTestCase extends } } +/** + * Test page caching with language negotiation. + */ +class BootstrapPageCacheWithLanguageFallbackTestCase extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => t('Page cache test with language negotiation'), + 'description' => t('Enable the page cache and language negotiation, ensure that cached pages are served in the correct language.'), + 'group' => t('Bootstrap') + ); + } + + function setUp() { + parent::setUp('locale'); + $web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); + $this->drupalLogin($web_user); + + // Enable French language. + $edit = array(); + $edit['langcode'] = 'fr'; + + $this->drupalPost('admin/settings/language/add', $edit, t('Add language')); + $this->drupalLogout($web_user); + + // Set language negotiation to "Path prefix with fallback". + variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH); + + // Force inclusion of language.inc. + drupal_init_language(); + // Enable the page cache. + variable_set('cache', CACHE_NORMAL); + } + + /** + * Test page caching with language fallback. + */ + function testPageCacheLanguageFallback() { + + // Visit the root path with browser language preference set to French. + // Since this is in the default site language (English), the page should + // not be cached for this request. + $this->drupalGet('', array(), array('Accept-Language: fr')); + $this->assertFalse(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has not been cached.')); + $this->assertFalse($this->drupalGetHeader('Etag'), t('Page not served from cache.')); + + // The visitor should be redirected to a version of the page with their + // language prefix. + $this->assertRaw('xml:lang="fr"', t('Page returned in the correct language.')); + + // When requesting the page a second time, the browser should be + // redirected to a cached version of the page. + $this->drupalGet('', array(), array('Accept-Language: fr')); + $this->assertRaw('xml:lang="fr"', t('Page returned in the correct language.')); + $this->assertTrue($this->drupalGetHeader('Etag'), t('Page served from cache.')); + + // Visit the page again with browser language set to English, and confirm that + // it's returned in the correct language. + $this->drupalGet('', array(), array('Accept-Language: en')); + $this->assertFalse($this->drupalGetHeader('Etag'), t('Page not served from cache.')); + $this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.')); + $this->assertRaw('xml:lang="en"', t('Page returned in the correct language.')); + + // Ensure that the page is cached correctly when requested again. + $this->drupalGet('', array(), array('Accept-Language: en')); + $this->assertTrue($this->drupalGetHeader('Etag'), t('Page served from cache')); + $this->assertRaw('xml:lang="en"', t('Page returned in the correct language.')); + } +} + class BootstrapPageCacheTestCase extends DrupalWebTestCase { function getInfo() {