Followup r102785: fix typo in index name. Thanks to Platonides and his special page...
[lhc/web/wiklou.git] / includes / Action.php
index dcdd4b3..fd4a0a6 100644 (file)
@@ -23,7 +23,7 @@
  *
  * @file
  */
-abstract class Action {
+abstract class Action extends ContextSource {
 
        /**
         * Page on which we're performing the action
@@ -32,8 +32,8 @@ abstract class Action {
        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;
 
@@ -47,9 +47,10 @@ abstract class Action {
         * 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 );
 
@@ -59,6 +60,8 @@ abstract class Action {
 
                if ( $wgActions[$action] === false ) {
                        return false;
+               } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
+                       return $overrides[$action];
                } elseif ( $wgActions[$action] === true ) {
                        return ucfirst( $action ) . 'Action';
                } else {
@@ -74,7 +77,7 @@ abstract class Action {
         *     if it is not recognised
         */
        public final static function factory( $action, Page $page ) {
-               $class = self::getClass( $action );
+               $class = self::getClass( $action, $page->getActionOverrides() );
                if ( $class ) {
                        $obj = new $class( $page );
                        return $obj;
@@ -89,71 +92,7 @@ abstract class Action {
         * @return Bool
         */
        public final static function exists( $name ) {
-               return self::getClass( $name ) !== null;
-       }
-
-       /**
-        * Get the RequestContext in use here
-        * @return RequestContext
-        */
-       protected final function getContext() {
-               if ( $this->context instanceof RequestContext ) {
-                       return $this->context;
-               }
-               return $this->page->getContext();
-       }
-
-       /**
-        * Get the WebRequest being used for this instance
-        *
-        * @return WebRequest
-        */
-       protected final function getRequest() {
-               return $this->getContext()->getRequest();
-       }
-
-       /**
-        * Get the OutputPage being used for this instance
-        *
-        * @return OutputPage
-        */
-       protected final function getOutput() {
-               return $this->getContext()->getOutput();
-       }
-
-       /**
-        * Shortcut to get the User being used for this instance
-        *
-        * @return User
-        */
-       protected final function getUser() {
-               return $this->getContext()->getUser();
-       }
-
-       /**
-        * Shortcut to get the Skin being used for this instance
-        *
-        * @return Skin
-        */
-       protected final function getSkin() {
-               return $this->getContext()->getSkin();
-       }
-
-       /**
-        * Shortcut to get the user Language being used for this instance
-        *
-        * @return Skin
-        */
-       protected final function getLang() {
-               return $this->getContext()->getLang();
-       }
-
-       /**
-        * Shortcut to get the Title object from the page
-        * @return Title
-        */
-       protected final function getTitle() {
-               return $this->page->getTitle();
+               return self::getClass( $name, array() ) !== null;
        }
 
        /**
@@ -186,18 +125,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();
+               }
        }
 
        /**
@@ -223,7 +169,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 );
        }
@@ -233,6 +179,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() ) );
        }
@@ -267,6 +222,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 ''; }
 
        /**