X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FAction.php;h=51922251c00c0c00b84d7a2ba64c51733ce77a74;hb=29719f846b8887e1190ddf85125387c079f9539b;hp=032d0949b5863af982aec293773dc72d0118195d;hpb=70bdc008c4d977bc6df47c516709cde23770aca2;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Action.php b/includes/Action.php index 032d0949b5..51922251c0 100644 --- a/includes/Action.php +++ b/includes/Action.php @@ -1,11 +1,6 @@ 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 * @@ -83,17 +151,104 @@ abstract class Action extends ContextSource { * @return Bool */ public final static function exists( $name ) { - return self::getClass( $name ) !== null; + return self::getClass( $name, array() ) !== null; + } + + /** + * Get the IContextSource in use here + * @return IContextSource + */ + public final function getContext() { + if ( $this->context instanceof IContextSource ) { + return $this->context; + } + return $this->page->getContext(); + } + + /** + * Get the WebRequest being used for this instance + * + * @return WebRequest + */ + public final function getRequest() { + return $this->getContext()->getRequest(); + } + + /** + * Get the OutputPage being used for this instance + * + * @return OutputPage + */ + public final function getOutput() { + return $this->getContext()->getOutput(); + } + + /** + * Shortcut to get the User being used for this instance + * + * @return User + */ + public final function getUser() { + return $this->getContext()->getUser(); + } + + /** + * Shortcut to get the Skin being used for this instance + * + * @return 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 + */ + 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->setContext( $page->getContext() ); + $this->context = $context; } /** @@ -105,8 +260,11 @@ abstract class Action extends ContextSource { /** * 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 @@ -115,20 +273,29 @@ abstract class Action extends ContextSource { * * @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(); - } - - 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; + $block = $user->getBlock(); 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; } /** @@ -154,7 +321,7 @@ abstract class Action extends ContextSource { 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 ); } @@ -164,8 +331,17 @@ abstract class Action extends ContextSource { * * @return String */ + protected function getPageTitle() { + return $this->getTitle()->getPrefixedText(); + } + + /** + * Returns the description that goes below the \ tag + * + * @return String + */ protected function getDescription() { - return wfMsg( strtolower( $this->getName() ) ); + return $this->msg( strtolower( $this->getName() ) )->escaped(); } /** @@ -178,13 +354,14 @@ abstract class Action extends ContextSource { /** * 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 { /** @@ -198,6 +375,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 ''; } /** @@ -216,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) @@ -275,14 +456,14 @@ 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 ) { try { // Set a new context so output doesn't leak. - $this->setContext( clone $this->page->getContext() ); + $this->context = clone $this->page->getContext(); // This will throw exceptions if there's a problem $this->checkCanExecute( $this->getUser() ); @@ -317,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 { @@ -332,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; } @@ -364,11 +551,10 @@ abstract class FormlessAction extends Action { public function execute( array $data = null, $captureErrors = true ) { try { // Set a new context so output doesn't leak. - $context = clone $this->page->getContext(); + $this->context = clone $this->page->getContext(); if ( is_array( $data ) ) { - $context->setRequest( new FauxRequest( $data, false ) ); + $this->context->setRequest( new FauxRequest( $data, false ) ); } - $this->setContext( $context ); // This will throw exceptions if there's a problem $this->checkCanExecute( $this->getUser() ); @@ -384,4 +570,4 @@ abstract class FormlessAction extends Action { } } } -} \ No newline at end of file +}