FU r106752: use "media-" instead of "images-" in container names. Long live books...
[lhc/web/wiklou.git] / includes / Action.php
index e836098..655598e 100644 (file)
@@ -27,13 +27,13 @@ abstract class Action {
 
        /**
         * Page on which we're performing the action
-        * @var Article
+        * @var 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
         */
        protected $context;
 
@@ -72,14 +72,15 @@ abstract class Action {
        /**
         * Get an appropriate Action subclass for the given action
         * @param $action String
-        * @param $page Article
+        * @param $page Page
+        * @param $context IContextSource
         * @return Action|false|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;
@@ -96,11 +97,11 @@ abstract class Action {
        }
 
        /**
-        * 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();
@@ -111,7 +112,7 @@ abstract class Action {
         *
         * @return WebRequest
         */
-       protected final function getRequest() {
+       public final function getRequest() {
                return $this->getContext()->getRequest();
        }
 
@@ -120,7 +121,7 @@ abstract class Action {
         *
         * @return OutputPage
         */
-       protected final function getOutput() {
+       public final function getOutput() {
                return $this->getContext()->getOutput();
        }
 
@@ -129,7 +130,7 @@ abstract class Action {
         *
         * @return User
         */
-       protected final function getUser() {
+       public final function getUser() {
                return $this->getContext()->getUser();
        }
 
@@ -138,7 +139,7 @@ abstract class Action {
         *
         * @return Skin
         */
-       protected final function getSkin() {
+       public final function getSkin() {
                return $this->getContext()->getSkin();
        }
 
@@ -147,25 +148,49 @@ abstract class Action {
         *
         * @return Skin
         */
-       protected final function getLang() {
-               return $this->getContext()->getLang();
+       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 Skin
+        */
+       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 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;
        }
 
        /**
@@ -177,8 +202,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
@@ -189,18 +217,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();
+               }
        }
 
        /**
@@ -279,6 +314,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 ''; }
 
        /**