Fux PHP Notice: Undefined property: ContribsPager::$showSizeDiff in /www/w/includes...
[lhc/web/wiklou.git] / includes / RequestContext.php
index 3481bb5..c0b49a1 100644 (file)
@@ -1,15 +1,81 @@
 <?php
 /**
- * Group all the pieces relevant to the context of a request into one instance
- * 
+ * Request-dependant objects containers.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @since 1.18
  *
- * @author IAlex
+ * @author Alexandre Emsenhuber
  * @author Daniel Friesen
  * @file
  */
 
-class RequestContext {
+/**
+ * Interface for objects which can provide a context on request.
+ */
+interface IContextSource {
+
+       /**
+        * Get the WebRequest object
+        *
+        * @return WebRequest
+        */
+       public function getRequest();
+
+       /**
+        * Get the Title object
+        *
+        * @return Title
+        */
+       public function getTitle();
+
+       /**
+        * Get the OutputPage object
+        *
+        * @return OutputPage object
+        */
+       public function getOutput();
+
+       /**
+        * Get the User object
+        *
+        * @return User
+        */
+       public function getUser();
+
+       /**
+        * Get the Language object
+        *
+        * @return Language
+        */
+       public function getLang();
+
+       /**
+        * Get the Skin object
+        *
+        * @return Skin
+        */
+       public function getSkin();
+}
+
+/**
+ * Group all the pieces relevant to the context of a request into one instance
+ */
+class RequestContext implements IContextSource {
 
        /**
         * @var WebRequest
@@ -56,7 +122,7 @@ class RequestContext {
         * @return WebRequest
         */
        public function getRequest() {
-               if ( !isset($this->request) ) {
+               if ( $this->request === null ) {
                        global $wgRequest; # fallback to $wg till we can improve this
                        $this->request = $wgRequest;
                }
@@ -78,7 +144,7 @@ class RequestContext {
         * @return Title
         */
        public function getTitle() {
-               if ( !isset($this->title) ) {
+               if ( $this->title === null ) {
                        global $wgTitle; # fallback to $wg till we can improve this
                        $this->title = $wgTitle;
                }
@@ -98,7 +164,7 @@ class RequestContext {
         * @return OutputPage object
         */
        public function getOutput() {
-               if ( !isset( $this->output ) ) {
+               if ( $this->output === null ) {
                        $this->output = new OutputPage( $this );
                }
                return $this->output;
@@ -119,22 +185,28 @@ class RequestContext {
         * @return User
         */
        public function getUser() {
-               if ( !isset($this->user) ) {
-                       global $wgCommandLineMode;
-                       $this->user = $wgCommandLineMode
-                               ? new User
-                               : User::newFromSession( $this->getRequest() );
+               if ( $this->user === null ) {
+                       $this->user = User::newFromSession( $this->getRequest() );
                }
                return $this->user;
        }
 
+       /**
+        * Set the Language object
+        *
+        * @param $l Language
+        */
+       public function setLang( Language $l ) {
+               $this->lang = $l;
+       }
+
        /**
         * Get the Language object
         *
         * @return Language
         */
        public function getLang() {
-               if ( !isset($this->lang) ) {
+               if ( $this->lang === null ) {
                        global $wgLanguageCode, $wgContLang;
                        $code = $this->getRequest()->getVal(
                                'uselang',
@@ -161,13 +233,23 @@ class RequestContext {
                return $this->lang;
        }
 
+       /**
+        * Set the Skin object
+        *
+        * @param $s Skin
+        */
+       public function setSkin( Skin $s ) {
+               $this->skin = clone $s;
+               $this->skin->setContext( $this );
+       }
+
        /**
         * Get the Skin object
         *
         * @return Skin
         */
        public function getSkin() {
-               if ( !isset($this->skin) ) {
+               if ( $this->skin === null ) {
                        wfProfileIn( __METHOD__ . '-createskin' );
                        
                        global $wgHiddenPrefs;
@@ -198,7 +280,7 @@ class RequestContext {
         */
        public function msg() {
                $args = func_get_args();
-               return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
+               return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
        }
 
        /** Static methods **/
@@ -210,10 +292,108 @@ class RequestContext {
         */
        public static function getMain() {
                static $instance = null;
-               if ( !isset($instance) ) {
+               if ( $instance === null ) {
                        $instance = new self;
                }
                return $instance;
        }
 }
 
+/**
+ * The simplest way of implementing IContextSource is to hold a RequestContext as a
+ * member variable and provide accessors to it.
+ */
+abstract class ContextSource implements IContextSource {
+
+       /**
+        * @var RequestContext
+        */
+       private $context;
+
+       /**
+        * Get the RequestContext object
+        *
+        * @return RequestContext
+        */
+       public function getContext() {
+               if ( $this->context === null ) {
+                       $class = get_class( $this );
+                       wfDebug( __METHOD__  . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
+                       $this->context = RequestContext::getMain();
+               }
+               return $this->context;
+       }
+
+       /**
+        * Set the RequestContext object
+        *
+        * @param $context RequestContext
+        */
+       public function setContext( RequestContext $context ) {
+               $this->context = $context;
+       }
+
+       /**
+        * Get the WebRequest object
+        *
+        * @return WebRequest
+        */
+       public function getRequest() {
+               return $this->getContext()->getRequest();
+       }
+
+       /**
+        * Get the Title object
+        *
+        * @return Title
+        */
+       public function getTitle() {
+               return $this->getContext()->getTitle();
+       }
+
+       /**
+        * Get the OutputPage object
+        *
+        * @return OutputPage object
+        */
+       public function getOutput() {
+               return $this->getContext()->getOutput();
+       }
+
+       /**
+        * Get the User object
+        *
+        * @return User
+        */
+       public function getUser() {
+               return $this->getContext()->getUser();
+       }
+
+       /**
+        * Get the Language object
+        *
+        * @return Language
+        */
+       public function getLang() {
+               return $this->getContext()->getLang();
+       }
+
+       /**
+        * Get the Skin object
+        *
+        * @return Skin
+        */
+       public function getSkin() {
+               return $this->getContext()->getSkin();
+       }
+
+       /**
+        * Get a Message object with context set
+        * Parameters are the same as wfMessage()
+        *
+        * @return Message object
+        */
+       public function msg( /* $args */ ) {
+               return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );
+       }
+}