From 0a6a24661f19d2815a2bf2f273df87613573b5d7 Mon Sep 17 00:00:00 2001 From: Philip Tzou Date: Mon, 21 Dec 2009 18:55:42 +0000 Subject: [PATCH] follow-up r59522. follow Tim's suggestion to serve same vary and XVO headers for the same URL. Now the vary headers should be consistent. Feel free to test it. --- includes/OutputPage.php | 25 +++++++++++++++++++++++++ includes/Wiki.php | 4 ++-- languages/LanguageConverter.php | 16 ++-------------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index d6b72c73de..b7f06b3f1c 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -844,6 +844,29 @@ class OutputPage { return $xvo; } + /** bug 21672: Add Accept-Language to Vary and XVO headers + if there's no 'variant' parameter existed in $_GET. + + For example: + /w/index.php?title=Main_page should always be served; but + /w/index.php?title=Main_page&variant=zh-cn should never be served. + + patched by Liangent and Philip */ + function addAcceptLanguage() { + global $wgContLang; + if( !isset( $_GET['variant'] ) && $wgContLang->hasVariants() ) { + $variants = $wgContLang->getVariants(); + $aloption = array(); + foreach ( $variants as $variant ) { + if( $variant === $wgContLang->getCode() ) + continue; + else + $aloption[] = "string-contains=$variant"; + } + $this->addVaryHeader( 'Accept-Language', $aloption ); + } + } + public function sendCacheControl() { global $wgUseSquid, $wgUseESI, $wgUseETag, $wgSquidMaxage, $wgRequest, $wgUseXVO; @@ -851,6 +874,8 @@ class OutputPage { if ($wgUseETag && $this->mETag) $response->header("ETag: $this->mETag"); + $this->addAcceptLanguage(); + # don't serve compressed data to clients who can't handle it # maintain different caches for logged-in users and non-logged in ones $response->header( 'Vary: ' . join( ', ', array_keys( $this->mVaryHeader ) ) ); diff --git a/includes/Wiki.php b/includes/Wiki.php index 0a6c263619..bbe2cb2548 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -206,11 +206,11 @@ class MediaWiki { wfProfileOut( __METHOD__ ); throw new ErrorPageError( 'badtitle', 'badtitletext' ); } - // Redirect loops, no title in URL, $wgUsePathInfo URLs + // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant } else if( $action == 'view' && !$request->wasPosted() && ( ( !isset($this->GET['title']) || $title->getPrefixedDBKey() != $this->GET['title'] ) || // No valid variant in URL (if the main-language has multi-variants), to ensure - // the Accept-Language would only be added to XVO when a 301 redirection happened + // anonymous access would always be redirect to a URL with 'variant' parameter ( !isset($this->GET['variant']) && $wgContLang->hasVariants() && !$wgUser->isLoggedIn() ) ) && !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) ) { diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php index 3f23f3b51f..e53933abf1 100644 --- a/languages/LanguageConverter.php +++ b/languages/LanguageConverter.php @@ -186,19 +186,6 @@ class LanguageConverter { // variable in case this is called before the user's // preference is loaded if( $fromHeader && array_key_exists( 'HTTP_ACCEPT_LANGUAGE', $_SERVER ) ) { - - // bug 21672: Add Accept-Language to Vary and XVO headers - // to help Squid to determine user's perferred local language - // ONLY add Accept-Language when a variant has been found out - // patched by Liangent - $aloption = array(); - foreach ( $this->mVariants as $variant ) { - if( $variant === $this->mMainLanguageCode ) - continue; - $aloption[] = "string-contains=$variant"; - } - $wgOut->addVaryHeader( 'Accept-Language', $aloption ); - $acceptLanguage = strtolower( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ); // explode by comma $result = explode(',', $acceptLanguage); @@ -243,9 +230,10 @@ class LanguageConverter { } } } + return $this->mMainLanguageCode; } + else return $this->mPreferredVariant; - return $this->mMainLanguageCode; } /** -- 2.20.1