HTML 2???
[lhc/web/wiklou.git] / includes / context / RequestContext.php
index c57399d..1ffbc08 100644 (file)
@@ -39,6 +39,11 @@ class RequestContext implements IContextSource {
         */
        private $title;
 
+       /**
+        * @var WikiPage
+        */
+       private $wikipage;
+
        /**
         * @var OutputPage
         */
@@ -103,6 +108,59 @@ class RequestContext implements IContextSource {
                return $this->title;
        }
 
+       /**
+        * Check whether a WikiPage object can be get with getWikiPage().
+        * Callers should expect that an exception is thrown from getWikiPage()
+        * if this method returns false.
+        *
+        * @since 1.19
+        * @return bool
+        */
+       public function canUseWikiPage() {
+               if ( $this->wikipage !== null ) {
+                       # If there's a WikiPage object set, we can for sure get it
+                       return true;
+               }
+               $title = $this->getTitle();
+               if ( $title === null ) {
+                       # No Title, no WikiPage
+                       return false;
+               } else {
+                       # Only namespaces whose pages are stored in the database can have WikiPage
+                       return $title->canExist();
+               }
+       }
+
+       /**
+        * Set the WikiPage object
+        *
+        * @since 1.19
+        * @param $p WikiPage object
+        */
+       public function setWikiPage( WikiPage $p ) {
+               $this->wikipage = $p;
+       }
+
+       /**
+        * Get the WikiPage object.
+        * May throw an exception if there's no Title object set or the Title object
+        * belongs to a special namespace that doesn't have WikiPage, so use first
+        * canUseWikiPage() to check whether this method can be called safely.
+        *
+        * @since 1.19
+        * @return WikiPage
+        */
+       public function getWikiPage() {
+               if ( $this->wikipage === null ) {
+                       $title = $this->getTitle();
+                       if ( $title === null ) {
+                               throw new MWException( __METHOD__ . ' called without Title object set' );
+                       }
+                       $this->wikipage = WikiPage::factory( $title );
+               }
+               return $this->wikipage;
+       }
+
        /**
         * @param $o OutputPage
         */
@@ -149,7 +207,7 @@ class RequestContext implements IContextSource {
         * @param $code string
         * @return string
         */
-       static function sanitizeLangCode( $code ) {
+       public static function sanitizeLangCode( $code ) {
                global $wgLanguageCode;
 
                // BCP 47 - letter case MUST NOT carry meaning
@@ -248,18 +306,34 @@ class RequestContext implements IContextSource {
                if ( $this->skin === null ) {
                        wfProfileIn( __METHOD__ . '-createskin' );
                        
-                       global $wgHiddenPrefs;
-                       if( !in_array( 'skin', $wgHiddenPrefs ) ) {
-                               # get the user skin
-                               $userSkin = $this->getUser()->getOption( 'skin' );
-                               $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
-                       } else {
-                               # if we're not allowing users to override, then use the default
-                               global $wgDefaultSkin;
-                               $userSkin = $wgDefaultSkin;
+                       $skin = null;
+                       wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
+
+                       // If the hook worked try to set a skin from it
+                       if ( $skin instanceof Skin ) {
+                               $this->skin = $skin;
+                       } elseif ( is_string($skin) ) {
+                               $this->skin = Skin::newFromKey( $skin );
+                       }
+
+                       // If this is still null (the hook didn't run or didn't work)
+                       // then go through the normal processing to load a skin
+                       if ( $this->skin === null ) {
+                               global $wgHiddenPrefs;
+                               if( !in_array( 'skin', $wgHiddenPrefs ) ) {
+                                       # get the user skin
+                                       $userSkin = $this->getUser()->getOption( 'skin' );
+                                       $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
+                               } else {
+                                       # if we're not allowing users to override, then use the default
+                                       global $wgDefaultSkin;
+                                       $userSkin = $wgDefaultSkin;
+                               }
+
+                               $this->skin = Skin::newFromKey( $userSkin );
                        }
 
-                       $this->skin = Skin::newFromKey( $userSkin );
+                       // After all that set a context on whatever skin got created
                        $this->skin->setContext( $this );
                        wfProfileOut( __METHOD__ . '-createskin' );
                }