merging latest master
[lhc/web/wiklou.git] / includes / Action.php
index 2287941..5192225 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
+        * @var Page $page
         */
        protected $page;
 
        /**
         * IContextSource if specified; otherwise we'll use the Context from the Page
-        * @var IContextSource
+        * @var IContextSource $context
         */
        protected $context;
 
        /**
         * The fields used to create the HTMLForm
-        * @var Array
+        * @var Array $fields
         */
        protected $fields;
 
@@ -72,19 +83,67 @@ 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, Page $page ) {
+       public final static function factory( $action, Page $page, IContextSource $context = null ) {
                $class = self::getClass( $action, $page->getActionOverrides() );
                if ( $class ) {
-                       $obj = new $class( $page );
+                       $obj = new $class( $page, $context );
                        return $obj;
                }
                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';
+       }
+
        /**
         * Check if a given action is recognised, even if it's disabled
         *
@@ -145,10 +204,21 @@ abstract class Action {
        /**
         * Shortcut to get the user Language being used for this instance
         *
-        * @return Skin
+        * @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() {
-               return $this->getContext()->getLang();
+               wfDeprecated( __METHOD__, '1.19' );
+               return $this->getLanguage();
        }
 
        /**
@@ -173,10 +243,12 @@ abstract class Action {
        /**
         * Protected constructor: use Action::factory( $action, $page ) to actually build
         * these things in the real world
-        * @param Page $page
+        * @param $page Page
+        * @param $context IContextSource
         */
-       protected function __construct( Page $page ) {
+       protected function __construct( Page $page, IContextSource $context = null ) {
                $this->page = $page;
+               $this->context = $context;
        }
 
        /**
@@ -188,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
@@ -198,6 +273,7 @@ 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 ) {
                $right = $this->getRestriction();
@@ -209,7 +285,7 @@ abstract class Action {
                }
 
                if ( $this->requiresUnblock() && $user->isBlocked() ) {
-                       $block = $user->mBlock;
+                       $block = $user->getBlock();
                        throw new UserBlockedError( $block );
                }
 
@@ -219,6 +295,7 @@ abstract class Action {
                if ( $this->requiresWrite() && wfReadOnly() ) {
                        throw new ReadOnlyError();
                }
+               return true;
        }
 
        /**
@@ -264,7 +341,7 @@ abstract class Action {
         * @return String
         */
        protected function getDescription() {
-               return wfMsg( strtolower( $this->getName() ) );
+               return $this->msg( strtolower( $this->getName() ) )->escaped();
        }
 
        /**
@@ -277,13 +354,14 @@ abstract class Action {
 
        /**
         * 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 abstract function execute();
 }
 
+/**
+ * An action which shows a form and does something based on the input from the form
+ */
 abstract class FormAction extends Action {
 
        /**
@@ -319,7 +397,7 @@ abstract class FormAction extends Action {
                // Give hooks a chance to alter the form, adding extra fields or text etc
                wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields, $this->page ) );
 
-               $form = new HTMLForm( $this->fields, $this->getContext() );
+               $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
                $form->setSubmitCallback( array( $this, 'onSubmit' ) );
 
                // Retain query parameters (uselang etc)
@@ -378,8 +456,8 @@ abstract class FormAction extends Action {
        /**
         * @see Action::execute()
         * @throws ErrorPageError
-        * @param array|null $data
-        * @param bool $captureErrors
+        * @param $data array|null
+        * @param $captureErrors bool
         * @return bool
         */
        public function execute( array $data = null, $captureErrors = true ) {
@@ -420,9 +498,7 @@ abstract class FormAction extends Action {
 }
 
 /**
- * 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 {
 
@@ -435,15 +511,23 @@ abstract class FormlessAction extends Action {
 
        /**
         * We don't want an HTMLForm
+        * @return bool
         */
        protected function getFormFields() {
                return false;
        }
 
+       /**
+        * @param $data Array
+        * @return bool
+        */
        public function onSubmit( $data ) {
                return false;
        }
 
+       /**
+        * @return bool
+        */
        public function onSuccess() {
                return false;
        }