bug 21672: Add Accept-Language to Vary and XVO headers to help Squid to determine...
authorPhilip Tzou <philip@users.mediawiki.org>
Sat, 28 Nov 2009 19:13:23 +0000 (19:13 +0000)
committerPhilip Tzou <philip@users.mediawiki.org>
Sat, 28 Nov 2009 19:13:23 +0000 (19:13 +0000)
RELEASE-NOTES
includes/OutputPage.php
languages/LanguageConverter.php

index b1caaeb..98e56ae 100644 (file)
@@ -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 ==
 
index 6c93062..503466d 100644 (file)
@@ -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
index 6209540..1999447 100644 (file)
@@ -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