Merge "Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderContext.php
index 7af7b89..66b4ee2 100644 (file)
@@ -41,6 +41,11 @@ class ResourceLoaderContext {
        protected $version;
        protected $hash;
        protected $raw;
+       protected $image;
+       protected $variant;
+       protected $format;
+       protected $userObj;
+       protected $imageObj;
 
        /* Methods */
 
@@ -65,6 +70,10 @@ class ResourceLoaderContext {
                $this->only = $request->getVal( 'only' );
                $this->version = $request->getVal( 'version' );
                $this->raw = $request->getFuzzyBool( 'raw' );
+               // Image requests
+               $this->image = $request->getVal( 'image' );
+               $this->variant = $request->getVal( 'variant' );
+               $this->format = $request->getVal( 'format' );
 
                $skinnames = Skin::getSkinNames();
                // If no skin is specified, or we don't recognize the skin, use the default skin
@@ -178,6 +187,31 @@ class ResourceLoaderContext {
                return $this->user;
        }
 
+       /**
+        * Get the possibly-cached User object for the specified username
+        *
+        * @since 1.25
+        * @return User|bool false if a valid object cannot be created
+        */
+       public function getUserObj() {
+               if ( $this->userObj === null ) {
+                       $username = $this->getUser();
+                       if ( $username ) {
+                               // Optimize: Avoid loading a new User object if possible
+                               global $wgUser;
+                               if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
+                                       $this->userObj = $wgUser;
+                               } else {
+                                       $this->userObj = User::newFromName( $username );
+                               }
+                       } else {
+                               $this->userObj = new User; // Anonymous user
+                       }
+               }
+
+               return $this->userObj;
+       }
+
        /**
         * @return bool
         */
@@ -193,6 +227,8 @@ class ResourceLoaderContext {
        }
 
        /**
+        * @see ResourceLoaderModule::getVersionHash
+        * @see OutputPage::makeResourceLoaderLink
         * @return string|null
         */
        public function getVersion() {
@@ -206,6 +242,62 @@ class ResourceLoaderContext {
                return $this->raw;
        }
 
+       /**
+        * @return string|null
+        */
+       public function getImage() {
+               return $this->image;
+       }
+
+       /**
+        * @return string|null
+        */
+       public function getVariant() {
+               return $this->variant;
+       }
+
+       /**
+        * @return string|null
+        */
+       public function getFormat() {
+               return $this->format;
+       }
+
+       /**
+        * If this is a request for an image, get the ResourceLoaderImage object.
+        *
+        * @since 1.25
+        * @return ResourceLoaderImage|bool false if a valid object cannot be created
+        */
+       public function getImageObj() {
+               if ( $this->imageObj === null ) {
+                       $this->imageObj = false;
+
+                       if ( !$this->image ) {
+                               return $this->imageObj;
+                       }
+
+                       $modules = $this->getModules();
+                       if ( count( $modules ) !== 1 ) {
+                               return $this->imageObj;
+                       }
+
+                       $module = $this->getResourceLoader()->getModule( $modules[0] );
+                       if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
+                               return $this->imageObj;
+                       }
+
+                       $image = $module->getImage( $this->image );
+                       if ( !$image ) {
+                               return $this->imageObj;
+                       }
+
+                       $this->imageObj = $image;
+               }
+
+               return $this->imageObj;
+       }
+
        /**
         * @return bool
         */
@@ -224,7 +316,7 @@ class ResourceLoaderContext {
         * @return bool
         */
        public function shouldIncludeMessages() {
-               return is_null( $this->getOnly() ) || $this->getOnly() === 'messages';
+               return is_null( $this->getOnly() );
        }
 
        /**
@@ -234,6 +326,7 @@ class ResourceLoaderContext {
                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()
                        ) );
                }