3 * Actions are things which can be done to pages (edit, delete, rollback, etc). They
4 * are distinct from Special Pages because an action must apply to exactly one page.
6 * To add an action in an extension, create a subclass of Action, and add the key to
7 * $wgActions. There is also the deprecated UnknownAction hook
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 abstract class Action
{
29 * Page on which we're performing the action
35 * IContextSource if specified; otherwise we'll use the Context from the Page
41 * The fields used to create the HTMLForm
47 * Get the Action subclass which should be used to handle this action, false if
48 * the action is disabled, or null if it's not recognised
49 * @param $action String
50 * @param $overrides Array
51 * @return bool|null|string
53 private final static function getClass( $action, array $overrides ) {
55 $action = strtolower( $action );
57 if ( !isset( $wgActions[$action] ) ) {
61 if ( $wgActions[$action] === false ) {
63 } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
64 return $overrides[$action];
65 } elseif ( $wgActions[$action] === true ) {
66 return ucfirst( $action ) . 'Action';
68 return $wgActions[$action];
73 * Get an appropriate Action subclass for the given action
74 * @param $action String
76 * @param $context IContextSource
77 * @return Action|false|null false if the action is disabled, null
78 * if it is not recognised
80 public final static function factory( $action, Page
$page, IContextSource
$context = null ) {
81 $class = self
::getClass( $action, $page->getActionOverrides() );
83 $obj = new $class( $page, $context );
90 * Check if a given action is recognised, even if it's disabled
92 * @param $name String: name of an action
95 public final static function exists( $name ) {
96 return self
::getClass( $name, array() ) !== null;
100 * Get the IContextSource in use here
101 * @return IContextSource
103 public final function getContext() {
104 if ( $this->context
instanceof IContextSource
) {
105 return $this->context
;
107 return $this->page
->getContext();
111 * Get the WebRequest being used for this instance
115 public final function getRequest() {
116 return $this->getContext()->getRequest();
120 * Get the OutputPage being used for this instance
124 public final function getOutput() {
125 return $this->getContext()->getOutput();
129 * Shortcut to get the User being used for this instance
133 public final function getUser() {
134 return $this->getContext()->getUser();
138 * Shortcut to get the Skin being used for this instance
142 public final function getSkin() {
143 return $this->getContext()->getSkin();
147 * Shortcut to get the user Language being used for this instance
151 public final function getLanguage() {
152 return $this->getContext()->getLanguage();
156 * Shortcut to get the user Language being used for this instance
158 * @deprecated 1.19 Use getLanguage instead
161 public final function getLang() {
162 return $this->getLanguage();
166 * Shortcut to get the Title object from the page
169 public final function getTitle() {
170 return $this->page
->getTitle();
174 * Get a Message object with context set
175 * Parameters are the same as wfMessage()
177 * @return Message object
179 public final function msg() {
180 $params = func_get_args();
181 return call_user_func_array( array( $this->getContext(), 'msg' ), $params );
185 * Protected constructor: use Action::factory( $action, $page ) to actually build
186 * these things in the real world
188 * @param $context IContextSource
190 protected function __construct( Page
$page, IContextSource
$context = null ) {
192 $this->context
= $context;
196 * Return the name of the action this object responds to
197 * @return String lowercase
199 public abstract function getName();
202 * Get the permission required to perform this action. Often, but not always,
203 * the same as the action name
205 public abstract function getRestriction();
208 * Checks if the given user (identified by an object) can perform this action. Can be
209 * overridden by sub-classes with more complicated permissions schemes. Failures here
210 * must throw subclasses of ErrorPageError
212 * @param $user User: the user to check, or null to use the context user
213 * @throws ErrorPageError
215 protected function checkCanExecute( User
$user ) {
216 $right = $this->getRestriction();
217 if ( $right !== null ) {
218 $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
219 if ( count( $errors ) ) {
220 throw new PermissionsError( $right, $errors );
224 if ( $this->requiresUnblock() && $user->isBlocked() ) {
225 $block = $user->mBlock
;
226 throw new UserBlockedError( $block );
229 // This should be checked at the end so that the user won't think the
230 // error is only temporary when he also don't have the rights to execute
232 if ( $this->requiresWrite() && wfReadOnly() ) {
233 throw new ReadOnlyError();
238 * Whether this action requires the wiki not to be locked
241 public function requiresWrite() {
246 * Whether this action can still be executed by a blocked user
249 public function requiresUnblock() {
254 * Set output headers for noindexing etc. This function will not be called through
255 * the execute() entry point, so only put UI-related stuff in here.
257 protected function setHeaders() {
258 $out = $this->getOutput();
259 $out->setRobotPolicy( "noindex,nofollow" );
260 $out->setPageTitle( $this->getPageTitle() );
261 $this->getOutput()->setSubtitle( $this->getDescription() );
262 $out->setArticleRelated( true );
266 * Returns the name that goes in the \<h1\> page title
270 protected function getPageTitle() {
271 return $this->getTitle()->getPrefixedText();
275 * Returns the description that goes below the \<h1\> tag
279 protected function getDescription() {
280 return wfMsg( strtolower( $this->getName() ) );
284 * The main action entry point. Do all output for display and send it to the context
285 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
286 * $this->getOutput(), etc.
287 * @throws ErrorPageError
289 public abstract function show();
292 * Execute the action in a silent fashion: do not display anything or release any errors.
293 * @param $data Array values that would normally be in the POST request
294 * @param $captureErrors Bool whether to catch exceptions and just return false
295 * @return Bool whether execution was successful
297 public abstract function execute();
300 abstract class FormAction
extends Action
{
303 * Get an HTMLForm descriptor array
306 protected abstract function getFormFields();
309 * Add pre- or post-text to the form
310 * @return String HTML which will be sent to $form->addPreText()
312 protected function preText() { return ''; }
317 protected function postText() { return ''; }
320 * Play with the HTMLForm if you need to more substantially
321 * @param $form HTMLForm
323 protected function alterForm( HTMLForm
$form ) {}
326 * Get the HTMLForm to control behaviour
327 * @return HTMLForm|null
329 protected function getForm() {
330 $this->fields
= $this->getFormFields();
332 // Give hooks a chance to alter the form, adding extra fields or text etc
333 wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields
, $this->page
) );
335 $form = new HTMLForm( $this->fields
, $this->getContext() );
336 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
338 // Retain query parameters (uselang etc)
339 $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
340 $params = array_diff_key(
341 $this->getRequest()->getQueryValues(),
342 array( 'action' => null, 'title' => null )
344 $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
346 $form->addPreText( $this->preText() );
347 $form->addPostText( $this->postText() );
348 $this->alterForm( $form );
350 // Give hooks a chance to alter the form, adding extra fields or text etc
351 wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page
) );
357 * Process the form on POST submission. If you return false from getFormFields(),
358 * this will obviously never be reached. If you don't want to do anything with the
359 * form, just return false here
361 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
363 public abstract function onSubmit( $data );
366 * Do something exciting on successful processing of the form. This might be to show
367 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
370 public abstract function onSuccess();
373 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
374 * some stuff underneath (history etc); to do some processing on submission of that
375 * form (delete, protect, etc) and to do something exciting on 'success', be that
376 * display something new or redirect to somewhere. Some actions have more exotic
377 * behaviour, but that's what subclassing is for :D
379 public function show() {
382 // This will throw exceptions if there's a problem
383 $this->checkCanExecute( $this->getUser() );
385 $form = $this->getForm();
386 if ( $form->show() ) {
392 * @see Action::execute()
393 * @throws ErrorPageError
394 * @param array|null $data
395 * @param bool $captureErrors
398 public function execute( array $data = null, $captureErrors = true ) {
400 // Set a new context so output doesn't leak.
401 $this->context
= clone $this->page
->getContext();
403 // This will throw exceptions if there's a problem
404 $this->checkCanExecute( $this->getUser() );
407 foreach ( $this->fields
as $key => $params ) {
408 if ( isset( $data[$key] ) ) {
409 $fields[$key] = $data[$key];
410 } elseif ( isset( $params['default'] ) ) {
411 $fields[$key] = $params['default'];
413 $fields[$key] = null;
416 $status = $this->onSubmit( $fields );
417 if ( $status === true ) {
418 // This might do permanent stuff
425 catch ( ErrorPageError
$e ) {
426 if ( $captureErrors ) {
436 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
437 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
440 abstract class FormlessAction
extends Action
{
443 * Show something on GET request.
444 * @return String|null will be added to the HTMLForm if present, or just added to the
445 * output if not. Return null to not add anything
447 public abstract function onView();
450 * We don't want an HTMLForm
452 protected function getFormFields() {
456 public function onSubmit( $data ) {
460 public function onSuccess() {
464 public function show() {
467 // This will throw exceptions if there's a problem
468 $this->checkCanExecute( $this->getUser() );
470 $this->getOutput()->addHTML( $this->onView() );
474 * Execute the action silently, not giving any output. Since these actions don't have
475 * forms, they probably won't have any data, but some (eg rollback) may do
476 * @param $data Array values that would normally be in the GET request
477 * @param $captureErrors Bool whether to catch exceptions and just return false
478 * @return Bool whether execution was successful
480 public function execute( array $data = null, $captureErrors = true ) {
482 // Set a new context so output doesn't leak.
483 $this->context
= clone $this->page
->getContext();
484 if ( is_array( $data ) ) {
485 $this->context
->setRequest( new FauxRequest( $data, false ) );
488 // This will throw exceptions if there's a problem
489 $this->checkCanExecute( $this->getUser() );
494 catch ( ErrorPageError
$e ) {
495 if ( $captureErrors ) {