Make mediawiki.special.pageLanguage work again
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderContext.php
index c797fd6..78bf69d 100644 (file)
@@ -29,28 +29,30 @@ use MediaWiki\Logger\LoggerFactory;
  * of a specific loader request
  */
 class ResourceLoaderContext {
-       /* Protected Members */
-
        protected $resourceLoader;
        protected $request;
-       protected $modules;
-       protected $language;
-       protected $direction;
+       protected $logger;
+
+       // Module content vary
        protected $skin;
-       protected $user;
+       protected $language;
        protected $debug;
+       protected $user;
+
+       // Request vary (in addition to cache vary)
+       protected $modules;
        protected $only;
        protected $version;
-       protected $hash;
        protected $raw;
        protected $image;
        protected $variant;
        protected $format;
+
+       protected $direction;
+       protected $hash;
        protected $userObj;
        protected $imageObj;
 
-       /* Methods */
-
        /**
         * @param ResourceLoader $resourceLoader
         * @param WebRequest $request
@@ -58,6 +60,7 @@ class ResourceLoaderContext {
        public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
                $this->resourceLoader = $resourceLoader;
                $this->request = $request;
+               $this->logger = $resourceLoader->getLogger();
 
                // List of modules
                $modules = $request->getVal( 'modules' );
@@ -146,6 +149,14 @@ class ResourceLoaderContext {
                return $this->request;
        }
 
+       /**
+        * @since 1.27
+        * @return \Psr\Log\LoggerInterface
+        */
+       public function getLogger() {
+               return $this->logger;
+       }
+
        /**
         * @return array
         */
@@ -158,8 +169,16 @@ class ResourceLoaderContext {
         */
        public function getLanguage() {
                if ( $this->language === null ) {
-                       // Must be a valid language code after this point (bug 62849)
-                       $this->language = RequestContext::sanitizeLangCode( $this->getRequest()->getVal( 'lang' ) );
+                       // Must be a valid language code after this point (T64849)
+                       // Only support uselang values that follow built-in conventions (T102058)
+                       $lang = $this->getRequest()->getVal( 'lang', '' );
+                       // Stricter version of RequestContext::sanitizeLangCode()
+                       if ( !Language::isValidBuiltInCode( $lang ) ) {
+                               wfDebug( "Invalid user language code\n" );
+                               global $wgLanguageCode;
+                               $lang = $wgLanguageCode;
+                       }
+                       $this->language = $lang;
                }
                return $this->language;
        }
@@ -325,14 +344,31 @@ class ResourceLoaderContext {
        }
 
        /**
+        * All factors that uniquely identify this request, except 'modules'.
+        *
+        * The list of modules is excluded here for legacy reasons as most callers already
+        * split up handling of individual modules. Including it here would massively fragment
+        * the cache and decrease its usefulness.
+        *
+        * E.g. Used by RequestFileCache to form a cache key for storing the reponse output.
+        *
         * @return string
         */
        public function getHash() {
                if ( !isset( $this->hash ) ) {
                        $this->hash = implode( '|', array(
-                               $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
-                               $this->getImage(), $this->getVariant(), $this->getFormat(),
-                               $this->getDebug(), $this->getOnly(), $this->getVersion()
+                               // Module content vary
+                               $this->getLanguage(),
+                               $this->getSkin(),
+                               $this->getDebug(),
+                               $this->getUser(),
+                               // Request vary
+                               $this->getOnly(),
+                               $this->getVersion(),
+                               $this->getRaw(),
+                               $this->getImage(),
+                               $this->getVariant(),
+                               $this->getFormat(),
                        ) );
                }
                return $this->hash;