* Changed OutputPage's handling of subtitles to use an array and implode it with...
[lhc/web/wiklou.git] / includes / Action.php
index 5fbb5c7..2287941 100644 (file)
  */
 abstract class Action {
 
-       // Page on which we're performing the action
-       // @var Article
+       /**
+        * Page on which we're performing the action
+        * @var Article
+        */
        protected $page;
 
-       // RequestContext if specified; otherwise we'll use the Context from the Page
-       // @var RequestContext
+       /**
+        * IContextSource if specified; otherwise we'll use the Context from the Page
+        * @var IContextSource
+        */
        protected $context;
 
-       // The fields used to create the HTMLForm
-       // @var Array
+       /**
+        * The fields used to create the HTMLForm
+        * @var Array
+        */
        protected $fields;
 
        /**
         * Get the Action subclass which should be used to handle this action, false if
         * the action is disabled, or null if it's not recognised
         * @param $action String
+        * @param $overrides Array
         * @return bool|null|string
         */
-       private final static function getClass( $action ) {
+       private final static function getClass( $action, array $overrides ) {
                global $wgActions;
                $action = strtolower( $action );
 
@@ -53,13 +60,11 @@ abstract class Action {
 
                if ( $wgActions[$action] === false ) {
                        return false;
-               }
-
-               elseif ( $wgActions[$action] === true ) {
+               } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
+                       return $overrides[$action];
+               elseif ( $wgActions[$action] === true ) {
                        return ucfirst( $action ) . 'Action';
-               }
-
-               else {
+               } else {
                        return $wgActions[$action];
                }
        }
@@ -71,8 +76,8 @@ abstract class Action {
         * @return Action|false|null false if the action is disabled, null
         *     if it is not recognised
         */
-       public final static function factory( $action, Article $page ) {
-               $class = self::getClass( $action );
+       public final static function factory( $action, Page $page ) {
+               $class = self::getClass( $action, $page->getActionOverrides() );
                if ( $class ) {
                        $obj = new $class( $page );
                        return $obj;
@@ -87,15 +92,15 @@ abstract class Action {
         * @return Bool
         */
        public final static function exists( $name ) {
-               return self::getClass( $name ) !== null;
+               return self::getClass( $name, array() ) !== null;
        }
 
        /**
-        * Get the RequestContext in use here
-        * @return RequestContext
+        * Get the IContextSource in use here
+        * @return IContextSource
         */
-       protected final function getContext() {
-               if ( $this->context instanceof RequestContext ) {
+       public final function getContext() {
+               if ( $this->context instanceof IContextSource ) {
                        return $this->context;
                }
                return $this->page->getContext();
@@ -106,8 +111,8 @@ abstract class Action {
         *
         * @return WebRequest
         */
-       protected final function getRequest() {
-               return $this->getContext()->request;
+       public final function getRequest() {
+               return $this->getContext()->getRequest();
        }
 
        /**
@@ -115,8 +120,8 @@ abstract class Action {
         *
         * @return OutputPage
         */
-       protected final function getOutput() {
-               return $this->getContext()->output;
+       public final function getOutput() {
+               return $this->getContext()->getOutput();
        }
 
        /**
@@ -124,8 +129,8 @@ abstract class Action {
         *
         * @return User
         */
-       protected final function getUser() {
-               return $this->getContext()->user;
+       public final function getUser() {
+               return $this->getContext()->getUser();
        }
 
        /**
@@ -133,8 +138,8 @@ abstract class Action {
         *
         * @return Skin
         */
-       protected final function getSkin() {
-               return $this->getContext()->skin;
+       public final function getSkin() {
+               return $this->getContext()->getSkin();
        }
 
        /**
@@ -142,24 +147,35 @@ abstract class Action {
         *
         * @return Skin
         */
-       protected final function getLang() {
-               return $this->getContext()->lang;
+       public final function getLang() {
+               return $this->getContext()->getLang();
        }
 
        /**
         * Shortcut to get the Title object from the page
         * @return Title
         */
-       protected final function getTitle() {
+       public final function getTitle() {
                return $this->page->getTitle();
        }
 
+       /**
+        * Get a Message object with context set
+        * Parameters are the same as wfMessage()
+        *
+        * @return Message object
+        */
+       public final function msg() {
+               $params = func_get_args();
+               return call_user_func_array( array( $this->getContext(), 'msg' ), $params );
+       }
+
        /**
         * Protected constructor: use Action::factory( $action, $page ) to actually build
         * these things in the real world
-        * @param Article $page
+        * @param Page $page
         */
-       protected function __construct( Article $page ) {
+       protected function __construct( Page $page ) {
                $this->page = $page;
        }
 
@@ -184,18 +200,25 @@ abstract class Action {
         * @throws ErrorPageError
         */
        protected function checkCanExecute( User $user ) {
-               if ( $this->requiresWrite() && wfReadOnly() ) {
-                       throw new ReadOnlyError();
-               }
-
-               if ( $this->getRestriction() !== null && !$user->isAllowed( $this->getRestriction() ) ) {
-                       throw new PermissionsError( $this->getRestriction() );
+               $right = $this->getRestriction();
+               if ( $right !== null ) {
+                       $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
+                       if ( count( $errors ) ) {
+                               throw new PermissionsError( $right, $errors );
+                       }
                }
 
                if ( $this->requiresUnblock() && $user->isBlocked() ) {
                        $block = $user->mBlock;
                        throw new UserBlockedError( $block );
                }
+
+               // This should be checked at the end so that the user won't think the
+               // error is only temporary when he also don't have the rights to execute
+               // this action
+               if ( $this->requiresWrite() && wfReadOnly() ) {
+                       throw new ReadOnlyError();
+               }
        }
 
        /**
@@ -221,7 +244,7 @@ abstract class Action {
        protected function setHeaders() {
                $out = $this->getOutput();
                $out->setRobotPolicy( "noindex,nofollow" );
-               $out->setPageTitle( $this->getTitle()->getPrefixedText() );
+               $out->setPageTitle( $this->getPageTitle() );
                $this->getOutput()->setSubtitle( $this->getDescription() );
                $out->setArticleRelated( true );
        }
@@ -231,6 +254,15 @@ abstract class Action {
         *
         * @return String
         */
+       protected function getPageTitle() {
+               return $this->getTitle()->getPrefixedText();
+       }
+
+       /**
+        * Returns the description that goes below the \<h1\> tag
+        *
+        * @return String
+        */
        protected function getDescription() {
                return wfMsg( strtolower( $this->getName() ) );
        }
@@ -265,6 +297,10 @@ abstract class FormAction extends Action {
         * @return String HTML which will be sent to $form->addPreText()
         */
        protected function preText() { return ''; }
+
+       /**
+        * @return string
+        */
        protected function postText() { return ''; }
 
        /**
@@ -450,4 +486,4 @@ abstract class FormlessAction extends Action {
                        }
                }
        }
-}
\ No newline at end of file
+}