From: Philip Tzou Date: Sat, 28 Nov 2009 19:13:23 +0000 (+0000) Subject: bug 21672: Add Accept-Language to Vary and XVO headers to help Squid to determine... X-Git-Tag: 1.31.0-rc.0~38687 X-Git-Url: http://git.cyclocoop.org/fichier?a=commitdiff_plain;h=46ee7b263927dc9b9c4dd402309b3fafcd63c34b;p=lhc%2Fweb%2Fwiklou.git bug 21672: Add Accept-Language to Vary and XVO headers to help Squid to determine user's perferred local language which has been eagerly wanted in Chinese Wikipedia. --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index b1caaebb4c..98e56ae241 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -649,6 +649,7 @@ Hopefully we will remove this configuration var soon) * (bug 21403) memcached class renamed to MWMemecached to avoid conflict with PHP's memcached extension * (bug 21650) Both calls to SkinTemplateTabs hook are now compatible +* (bug 21672) Add missing Accept-Language to both Vary and XVO headers == API changes in 1.16 == diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 6c93062de0..503466d4c4 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -50,6 +50,8 @@ class OutputPage { private $mIndexPolicy = 'index'; private $mFollowPolicy = 'follow'; + private $mVaryHeader = array( 'Accept-Encoding', 'Cookie' ); + private $mXVOHeader = array( 'Accept-Encoding' => array('list-contains=gzip') ); /** * Constructor @@ -805,19 +807,45 @@ class OutputPage { return false; } + public function addXVOHeader( $header, $option = null ) { + if ( !array_key_exists( $header, $this->mXVOHeader ) ) { + $this->mXVOHeader[$header] = $option; + } + elseif( is_array( $option ) ) { + if( is_array( $this->mXVOHeader[$header] ) ) { + $this->mXVOHeader[$header] = array_merge( $this->mXVOHeader[$header], $option ); + } + else { + $this->mXVOHeader[$header] = $option; + } + } + } + + public function addVaryHeader( $header ) { + if ( !in_array( $header, $this->mVaryHeader ) ) { + $this->mVaryHeader[] = $header; + } + } + /** Get a complete X-Vary-Options header */ public function getXVO() { $cvCookies = $this->getCacheVaryCookies(); - $xvo = 'X-Vary-Options: Accept-Encoding;list-contains=gzip,Cookie;'; - $first = true; + + $cookiesOption = array(); foreach ( $cvCookies as $cookieName ) { - if ( $first ) { - $first = false; - } else { - $xvo .= ';'; - } - $xvo .= 'string-contains=' . $cookieName; - } + $cookiesOption[] = 'string-contains=' . $cookieName; + } + $this->addXVOHeader( 'Cookie', $cookiesOption ); + + $headers = array(); + foreach( $this->mXVOHeader as $header => $option ) { + $newheader = $header; + if( is_array( $option ) ) + $newheader .= ';' . implode( ';', $option ); + $headers[] = $newheader; + } + $xvo = 'X-Vary-Options: ' . implode( ',', $headers ); + return $xvo; } @@ -830,7 +858,7 @@ class OutputPage { # 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: Accept-Encoding, Cookie' ); + $response->header( 'Vary: ' . join( ', ', $this->mVaryHeader ) ); if ( $wgUseXVO ) { # Add an X-Vary-Options header for Squid with Wikimedia patches diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php index 62095405d4..1999447b3d 100644 --- a/languages/LanguageConverter.php +++ b/languages/LanguageConverter.php @@ -185,6 +185,12 @@ class LanguageConverter { // variable in case this is called before the user's // preference is loaded if( 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 + global $wgOut, $wgUseXVO; + $wgOut->addVaryHeader( 'Accept-Language' ); + if( $wgUseXVO ) + $wgOut->addXVOHeader( 'Accept-Language' ); $acceptLanguage = strtolower( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ); // explode by comma