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
extends ContextSource
{
28 // Page on which we're performing the action
32 // The fields used to create the HTMLForm
37 * Get the Action subclass which should be used to handle this action, false if
38 * the action is disabled, or null if it's not recognised
39 * @param $action String
40 * @return bool|null|string
42 private final static function getClass( $action ) {
44 $action = strtolower( $action );
46 if ( !isset( $wgActions[$action] ) ) {
50 if ( $wgActions[$action] === false ) {
54 elseif ( $wgActions[$action] === true ) {
55 return ucfirst( $action ) . 'Action';
59 return $wgActions[$action];
64 * Get an appropriate Action subclass for the given action
65 * @param $action String
66 * @param $page Article
67 * @return Action|false|null false if the action is disabled, null
68 * if it is not recognised
70 public final static function factory( $action, Article
$page ) {
71 $class = self
::getClass( $action );
73 $obj = new $class( $page );
80 * Check if a given action is recognised, even if it's disabled
82 * @param $name String: name of an action
85 public final static function exists( $name ) {
86 return self
::getClass( $name ) !== null;
90 * Protected constructor: use Action::factory( $action, $page ) to actually build
91 * these things in the real world
92 * @param Article $page
94 protected function __construct( Article
$page ) {
96 $this->setContext( $page->getContext() );
100 * Return the name of the action this object responds to
101 * @return String lowercase
103 public abstract function getName();
106 * Get the permission required to perform this action. Often, but not always,
107 * the same as the action name
109 public abstract function getRestriction();
112 * Checks if the given user (identified by an object) can perform this action. Can be
113 * overridden by sub-classes with more complicated permissions schemes. Failures here
114 * must throw subclasses of ErrorPageError
116 * @param $user User: the user to check, or null to use the context user
117 * @throws ErrorPageError
119 protected function checkCanExecute( User
$user ) {
120 if ( $this->requiresWrite() && wfReadOnly() ) {
121 throw new ReadOnlyError();
124 if ( $this->getRestriction() !== null && !$user->isAllowed( $this->getRestriction() ) ) {
125 throw new PermissionsError( $this->getRestriction() );
128 if ( $this->requiresUnblock() && $user->isBlocked() ) {
129 $block = $user->mBlock
;
130 throw new UserBlockedError( $block );
135 * Whether this action requires the wiki not to be locked
138 public function requiresWrite() {
143 * Whether this action can still be executed by a blocked user
146 public function requiresUnblock() {
151 * Set output headers for noindexing etc. This function will not be called through
152 * the execute() entry point, so only put UI-related stuff in here.
154 protected function setHeaders() {
155 $out = $this->getOutput();
156 $out->setRobotPolicy( "noindex,nofollow" );
157 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
158 $this->getOutput()->setSubtitle( $this->getDescription() );
159 $out->setArticleRelated( true );
163 * Returns the name that goes in the \<h1\> page title
167 protected function getDescription() {
168 return wfMsg( strtolower( $this->getName() ) );
172 * The main action entry point. Do all output for display and send it to the context
173 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
174 * $this->getOutput(), etc.
175 * @throws ErrorPageError
177 public abstract function show();
180 * Execute the action in a silent fashion: do not display anything or release any errors.
181 * @param $data Array values that would normally be in the POST request
182 * @param $captureErrors Bool whether to catch exceptions and just return false
183 * @return Bool whether execution was successful
185 public abstract function execute();
188 abstract class FormAction
extends Action
{
191 * Get an HTMLForm descriptor array
194 protected abstract function getFormFields();
197 * Add pre- or post-text to the form
198 * @return String HTML which will be sent to $form->addPreText()
200 protected function preText() { return ''; }
201 protected function postText() { return ''; }
204 * Play with the HTMLForm if you need to more substantially
205 * @param $form HTMLForm
207 protected function alterForm( HTMLForm
$form ) {}
210 * Get the HTMLForm to control behaviour
211 * @return HTMLForm|null
213 protected function getForm() {
214 $this->fields
= $this->getFormFields();
216 // Give hooks a chance to alter the form, adding extra fields or text etc
217 wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields
, $this->page
) );
219 $form = new HTMLForm( $this->fields
, $this->getContext() );
220 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
222 // Retain query parameters (uselang etc)
223 $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
224 $params = array_diff_key(
225 $this->getRequest()->getQueryValues(),
226 array( 'action' => null, 'title' => null )
228 $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
230 $form->addPreText( $this->preText() );
231 $form->addPostText( $this->postText() );
232 $this->alterForm( $form );
234 // Give hooks a chance to alter the form, adding extra fields or text etc
235 wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page
) );
241 * Process the form on POST submission. If you return false from getFormFields(),
242 * this will obviously never be reached. If you don't want to do anything with the
243 * form, just return false here
245 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
247 public abstract function onSubmit( $data );
250 * Do something exciting on successful processing of the form. This might be to show
251 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
254 public abstract function onSuccess();
257 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
258 * some stuff underneath (history etc); to do some processing on submission of that
259 * form (delete, protect, etc) and to do something exciting on 'success', be that
260 * display something new or redirect to somewhere. Some actions have more exotic
261 * behaviour, but that's what subclassing is for :D
263 public function show() {
266 // This will throw exceptions if there's a problem
267 $this->checkCanExecute( $this->getUser() );
269 $form = $this->getForm();
270 if ( $form->show() ) {
276 * @see Action::execute()
277 * @throws ErrorPageError
278 * @param array|null $data
279 * @param bool $captureErrors
282 public function execute( array $data = null, $captureErrors = true ) {
284 // Set a new context so output doesn't leak.
285 $this->setContext( clone $this->page
->getContext() );
287 // This will throw exceptions if there's a problem
288 $this->checkCanExecute( $this->getUser() );
291 foreach ( $this->fields
as $key => $params ) {
292 if ( isset( $data[$key] ) ) {
293 $fields[$key] = $data[$key];
294 } elseif ( isset( $params['default'] ) ) {
295 $fields[$key] = $params['default'];
297 $fields[$key] = null;
300 $status = $this->onSubmit( $fields );
301 if ( $status === true ) {
302 // This might do permanent stuff
309 catch ( ErrorPageError
$e ) {
310 if ( $captureErrors ) {
320 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
321 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
324 abstract class FormlessAction
extends Action
{
327 * Show something on GET request.
328 * @return String|null will be added to the HTMLForm if present, or just added to the
329 * output if not. Return null to not add anything
331 public abstract function onView();
334 * We don't want an HTMLForm
336 protected function getFormFields() {
340 public function onSubmit( $data ) {
344 public function onSuccess() {
348 public function show() {
351 // This will throw exceptions if there's a problem
352 $this->checkCanExecute( $this->getUser() );
354 $this->getOutput()->addHTML( $this->onView() );
358 * Execute the action silently, not giving any output. Since these actions don't have
359 * forms, they probably won't have any data, but some (eg rollback) may do
360 * @param $data Array values that would normally be in the GET request
361 * @param $captureErrors Bool whether to catch exceptions and just return false
362 * @return Bool whether execution was successful
364 public function execute( array $data = null, $captureErrors = true ) {
366 // Set a new context so output doesn't leak.
367 $context = clone $this->page
->getContext();
368 if ( is_array( $data ) ) {
369 $context->setRequest( new FauxRequest( $data, false ) );
371 $this->setContext( $context );
373 // This will throw exceptions if there's a problem
374 $this->checkCanExecute( $this->getUser() );
379 catch ( ErrorPageError
$e ) {
380 if ( $captureErrors ) {