(bug 37330) Fix wrong block being displayed due to autoblock in place.
[lhc/web/wiklou.git] / includes / Action.php
index 44fafca..a9891ec 100644 (file)
@@ -1,11 +1,6 @@
 <?php
 /**
- * Actions are things which can be done to pages (edit, delete, rollback, etc).  They
- * are distinct from Special Pages because an action must apply to exactly one page.
- *
- * To add an action in an extension, create a subclass of Action, and add the key to
- * $wgActions.  There is also the deprecated UnknownAction hook
- *
+ * Base classes for actions done on pages.
  *
  * 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
  *
  * @file
  */
+
+/**
+ * @defgroup Actions Action done on pages
+ */
+
+/**
+ * Actions are things which can be done to pages (edit, delete, rollback, etc).  They
+ * are distinct from Special Pages because an action must apply to exactly one page.
+ *
+ * To add an action in an extension, create a subclass of Action, and add the key to
+ * $wgActions.  There is also the deprecated UnknownAction hook
+ *
+ * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
+ * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
+ * patrol, etc). The FormAction and FormlessAction classes respresent these two groups.
+ */
 abstract class Action {
 
-       // Page on which we're performing the action
-       // @var Article
+       /**
+        * Page on which we're performing the action
+        * @var Page $page
+        */
        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 $context
+        */
        protected $context;
 
-       // The fields used to create the HTMLForm
-       // @var Array
+       /**
+        * The fields used to create the HTMLForm
+        * @var Array $fields
+        */
        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 );
 
-               if( !isset( $wgActions[$action] ) ){
+               if ( !isset( $wgActions[$action] ) ) {
                        return null;
                }
 
-               if( $wgActions[$action] === false ){
+               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];
                }
        }
@@ -67,17 +83,65 @@ abstract class Action {
        /**
         * Get an appropriate Action subclass for the given action
         * @param $action String
-        * @param $page Article
-        * @return Action|false|null false if the action is disabled, null
+        * @param $page Page
+        * @param $context IContextSource
+        * @return Action|bool|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 );
-               if( $class ){
-                       $obj = new $class( $page );
+       public final static function factory( $action, Page $page, IContextSource $context = null ) {
+               $class = self::getClass( $action, $page->getActionOverrides() );
+               if ( $class ) {
+                       $obj = new $class( $page, $context );
                        return $obj;
                }
-               return null;
+               return $class;
+       }
+
+       /**
+        * Get the action that will be executed, not necessarily the one passed
+        * passed through the "action" request parameter. Actions disabled in
+        * $wgActions will be replaced by "nosuchaction".
+        *
+        * @since 1.19
+        * @param $context IContextSource
+        * @return string: action name
+        */
+       public final static function getActionName( IContextSource $context ) {
+               global $wgActions;
+
+               $request = $context->getRequest();
+               $actionName = $request->getVal( 'action', 'view' );
+
+               // Check for disabled actions
+               if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
+                       $actionName = 'nosuchaction';
+               }
+
+               // Workaround for bug #20966: inability of IE to provide an action dependent
+               // on which submit button is clicked.
+               if ( $actionName === 'historysubmit' ) {
+                       if ( $request->getBool( 'revisiondelete' ) ) {
+                               $actionName = 'revisiondelete';
+                       } else {
+                               $actionName = 'view';
+                       }
+               } elseif ( $actionName == 'editredlink' ) {
+                       $actionName = 'edit';
+               }
+
+               // Trying to get a WikiPage for NS_SPECIAL etc. will result
+               // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
+               // For SpecialPages et al, default to action=view.
+               if ( !$context->canUseWikiPage() ) {
+                       return 'view';
+               }
+
+               $action = Action::factory( $actionName, $context->getWikiPage() );
+               if ( $action instanceof Action ) {
+                       return $action->getName();
+               }
+
+               return 'nosuchaction';
        }
 
        /**
@@ -87,15 +151,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 +170,8 @@ abstract class Action {
         *
         * @return WebRequest
         */
-       protected final function getRequest() {
-               return $this->getContext()->request;
+       public final function getRequest() {
+               return $this->getContext()->getRequest();
        }
 
        /**
@@ -115,43 +179,76 @@ abstract class Action {
         *
         * @return OutputPage
         */
-       protected final function getOutput() {
-               return $this->getContext()->output;
+       public final function getOutput() {
+               return $this->getContext()->getOutput();
        }
 
        /**
-        * Shortcut to get the skin being used for this instance
+        * Shortcut to get the User being used for this instance
         *
         * @return User
         */
-       protected final function getUser() {
-               return $this->getContext()->user;
+       public final function getUser() {
+               return $this->getContext()->getUser();
        }
 
        /**
-        * Shortcut to get the skin being used for this instance
+        * Shortcut to get the Skin being used for this instance
         *
         * @return Skin
         */
-       protected final function getSkin() {
-               return $this->getContext()->skin;
+       public final function getSkin() {
+               return $this->getContext()->getSkin();
+       }
+
+       /**
+        * Shortcut to get the user Language being used for this instance
+        *
+        * @return Language
+        */
+       public final function getLanguage() {
+               return $this->getContext()->getLanguage();
+       }
+
+       /**
+        * Shortcut to get the user Language being used for this instance
+        *
+        * @deprecated 1.19 Use getLanguage instead
+        * @return Language
+        */
+       public final function getLang() {
+               wfDeprecated( __METHOD__, '1.19' );
+               return $this->getLanguage();
        }
 
        /**
         * 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
+        * @param $context IContextSource
         */
-       protected function __construct( Article $page ){
+       protected function __construct( Page $page, IContextSource $context = null ) {
                $this->page = $page;
+               $this->context = $context;
        }
 
        /**
@@ -163,8 +260,11 @@ abstract class Action {
        /**
         * Get the permission required to perform this action.  Often, but not always,
         * the same as the action name
+        * @return String|null
         */
-       public abstract function getRestriction();
+       public function getRestriction() {
+               return null;
+       }
 
        /**
         * Checks if the given user (identified by an object) can perform this action.  Can be
@@ -173,27 +273,36 @@ abstract class Action {
         *
         * @param $user User: the user to check, or null to use the context user
         * @throws ErrorPageError
+        * @return bool True on success
         */
        protected function checkCanExecute( User $user ) {
-               if( $this->requiresWrite() && wfReadOnly() ){
-                       throw new ReadOnlyError();
+               $right = $this->getRestriction();
+               if ( $right !== null ) {
+                       $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
+                       if ( count( $errors ) ) {
+                               throw new PermissionsError( $right, $errors );
+                       }
                }
 
-               if( $this->getRestriction() !== null && !$user->isAllowed( $this->getRestriction() ) ){
-                       throw new PermissionsError( $this->getRestriction() );
+               if ( $this->requiresUnblock() && $user->isBlocked() ) {
+                       $block = $user->getBlock();
+                       throw new UserBlockedError( $block );
                }
 
-               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();
                }
+               return true;
        }
 
        /**
         * Whether this action requires the wiki not to be locked
         * @return Bool
         */
-       public function requiresWrite(){
+       public function requiresWrite() {
                return true;
        }
 
@@ -201,7 +310,7 @@ abstract class Action {
         * Whether this action can still be executed by a blocked user
         * @return Bool
         */
-       public function requiresUnblock(){
+       public function requiresUnblock() {
                return true;
        }
 
@@ -212,7 +321,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 );
        }
@@ -220,125 +329,84 @@ abstract class Action {
        /**
         * Returns the name that goes in the \<h1\> page title
         *
-        * Derived classes can override this, but usually it is easier to keep the
-        * default behaviour. Messages can be added at run-time, see
-        * MessageCache.php.
+        * @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() ) );
+               return wfMsgHtml( strtolower( $this->getName() ) );
        }
 
        /**
-        * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
-        * some stuff underneath (history etc); to do some processing on submission of that
-        * form (delete, protect, etc) and to do something exciting on 'success', be that
-        * display something new or redirect to somewhere.  Some actions have more exotic
-        * behaviour, but that's what subclassing is for :D
+        * The main action entry point.  Do all output for display and send it to the context
+        * output.  Do not use globals $wgOut, $wgRequest, etc, in implementations; use
+        * $this->getOutput(), etc.
+        * @throws ErrorPageError
         */
-       public function show(){
-               $this->setHeaders();
-
-               // This will throw exceptions if there's a problem
-               $this->checkCanExecute( $this->getUser() );
-
-               $form = $this->getForm();
-               if( $form instanceof HTMLForm ){
-                       if( $form->show() ){
-                               $this->onSuccess();
-                       }
-               } else {
-                       // You're using the wrong type of Action
-                       throw new MWException( "Action::getFormFields() must produce a form.  Use GetAction if you don't want one." );
-               }
-       }
+       public abstract function show();
 
        /**
         * Execute the action in a silent fashion: do not display anything or release any errors.
-        * @param $data Array values that would normally be in the POST request
-        * @param $captureErrors Bool whether to catch exceptions and just return false
         * @return Bool whether execution was successful
         */
-       public function execute( array $data = null, $captureErrors = true ){
-               try {
-                       // Set a new context so output doesn't leak.
-                       $this->context = clone $this->page->getContext();
-
-                       // This will throw exceptions if there's a problem
-                       $this->checkCanExecute( $this->getUser() );
+       public abstract function execute();
+}
 
-                       $form = $this->getForm();
-                       if( $form instanceof HTMLForm ){
-                               // Great, so there's a form.  Ignore it and go straight to the submission callback
-                               $fields = array();
-                               foreach( $this->fields as $key => $params ){
-                                       if( isset( $data[$key] ) ){
-                                               $fields[$key] = $data[$key];
-                                       } elseif( isset( $params['default'] ) ) {
-                                               $fields[$key] = $params['default'];
-                                       } else {
-                                               $fields[$key] = null;
-                                       }
-                               }
-                               $status = $this->onSubmit( $fields );
-                               if( $status === true ){
-                                       // This might do permanent stuff
-                                       $this->onSuccess();
-                                       return true;
-                               } else {
-                                       return false;
-                               }
-                       } else {
-                               // You're using the wrong type of Action
-                               throw new MWException( "Action::getFormFields() must produce a form.  Use GetAction if you don't want one." );
-                       }
-               }
-               catch ( ErrorPageError $e ){
-                       if( $captureErrors ){
-                               return false;
-                       } else {
-                               throw $e;
-                       }
-               }
-       }
+/**
+ * An action which shows a form and does something based on the input from the form
+ */
+abstract class FormAction extends Action {
 
        /**
-        * Get an HTMLForm descriptor array, or false if you don't want a form
+        * Get an HTMLForm descriptor array
         * @return Array
         */
        protected abstract function getFormFields();
 
        /**
         * Add pre- or post-text to the form
-        * @return String
+        * @return String HTML which will be sent to $form->addPreText()
         */
-       protected function preText(){ return ''; }
-       protected function postText(){ return ''; }
+       protected function preText() { return ''; }
+
+       /**
+        * @return string
+        */
+       protected function postText() { return ''; }
 
        /**
         * Play with the HTMLForm if you need to more substantially
-        * @param &$form HTMLForm
+        * @param $form HTMLForm
         */
-       protected function alterForm( HTMLForm &$form ){}
+       protected function alterForm( HTMLForm $form ) {}
 
        /**
         * Get the HTMLForm to control behaviour
         * @return HTMLForm|null
         */
-       protected function getForm(){
+       protected function getForm() {
                $this->fields = $this->getFormFields();
 
                // Give hooks a chance to alter the form, adding extra fields or text etc
                wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields, $this->page ) );
 
-               if( $this->fields === false ){
-                       return null;
-               }
-
                $form = new HTMLForm( $this->fields, $this->getContext() );
                $form->setSubmitCallback( array( $this, 'onSubmit' ) );
-               $form->addHiddenField( 'action', $this->getName() );
+
+               // Retain query parameters (uselang etc)
+               $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
+               $params = array_diff_key(
+                       $this->getRequest()->getQueryValues(),
+                       array( 'action' => null, 'title' => null )
+               );
+               $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
 
                $form->addPreText( $this->preText() );
                $form->addPostText( $this->postText() );
@@ -366,20 +434,76 @@ abstract class Action {
         */
        public abstract function onSuccess();
 
+       /**
+        * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
+        * some stuff underneath (history etc); to do some processing on submission of that
+        * form (delete, protect, etc) and to do something exciting on 'success', be that
+        * display something new or redirect to somewhere.  Some actions have more exotic
+        * behaviour, but that's what subclassing is for :D
+        */
+       public function show() {
+               $this->setHeaders();
+
+               // This will throw exceptions if there's a problem
+               $this->checkCanExecute( $this->getUser() );
+
+               $form = $this->getForm();
+               if ( $form->show() ) {
+                       $this->onSuccess();
+               }
+       }
+
+       /**
+        * @see Action::execute()
+        * @throws ErrorPageError
+        * @param $data array|null
+        * @param $captureErrors bool
+        * @return bool
+        */
+       public function execute( array $data = null, $captureErrors = true ) {
+               try {
+                       // Set a new context so output doesn't leak.
+                       $this->context = clone $this->page->getContext();
+
+                       // This will throw exceptions if there's a problem
+                       $this->checkCanExecute( $this->getUser() );
+
+                       $fields = array();
+                       foreach ( $this->fields as $key => $params ) {
+                               if ( isset( $data[$key] ) ) {
+                                       $fields[$key] = $data[$key];
+                               } elseif ( isset( $params['default'] ) ) {
+                                       $fields[$key] = $params['default'];
+                               } else {
+                                       $fields[$key] = null;
+                               }
+                       }
+                       $status = $this->onSubmit( $fields );
+                       if ( $status === true ) {
+                               // This might do permanent stuff
+                               $this->onSuccess();
+                               return true;
+                       } else {
+                               return false;
+                       }
+               }
+               catch ( ErrorPageError $e ) {
+                       if ( $captureErrors ) {
+                               return false;
+                       } else {
+                               throw $e;
+                       }
+               }
+       }
 }
 
 /**
- * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
- * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
- * patrol, etc).
+ * An action which just does something, without showing a form first.
  */
 abstract class FormlessAction extends Action {
 
        /**
-        * Show something on GET request. This is displayed as the postText() of the HTMLForm
-        * if there is one; you can always use alterForm() to add pre text if you need it.  If
-        * you call addPostText() from alterForm() as well as overriding this function, you
-        * might get strange ordering.
+        * Show something on GET request.
         * @return String|null will be added to the HTMLForm if present, or just added to the
         *     output if not.  Return null to not add anything
         */
@@ -387,24 +511,33 @@ abstract class FormlessAction extends Action {
 
        /**
         * We don't want an HTMLForm
+        * @return bool
         */
-       protected function getFormFields(){
+       protected function getFormFields() {
                return false;
        }
 
-       public function onSubmit( $data ){
+       /**
+        * @param $data Array
+        * @return bool
+        */
+       public function onSubmit( $data ) {
                return false;
        }
 
-       public function onSuccess(){
+       /**
+        * @return bool
+        */
+       public function onSuccess() {
                return false;
        }
 
-       public function show(){
+       public function show() {
                $this->setHeaders();
 
                // This will throw exceptions if there's a problem
                $this->checkCanExecute( $this->getUser() );
+
                $this->getOutput()->addHTML( $this->onView() );
        }
 
@@ -415,11 +548,11 @@ abstract class FormlessAction extends Action {
         * @param $captureErrors Bool whether to catch exceptions and just return false
         * @return Bool whether execution was successful
         */
-       public function execute( array $data = null, $captureErrors = true){
+       public function execute( array $data = null, $captureErrors = true ) {
                try {
                        // Set a new context so output doesn't leak.
                        $this->context = clone $this->page->getContext();
-                       if( is_array( $data ) ){
+                       if ( is_array( $data ) ) {
                                $this->context->setRequest( new FauxRequest( $data, false ) );
                        }
 
@@ -429,12 +562,12 @@ abstract class FormlessAction extends Action {
                        $this->onView();
                        return true;
                }
-               catch ( ErrorPageError $e ){
-                       if( $captureErrors ){
+               catch ( ErrorPageError $e ) {
+                       if ( $captureErrors ) {
                                return false;
                        } else {
                                throw $e;
                        }
                }
        }
-}
\ No newline at end of file
+}