[Actions] Update usage of wgDisabledActions to check wgActions instead
[lhc/web/wiklou.git] / includes / Action.php
1 <?php
2 /**
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.
5 *
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
8 *
9 *
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.
14 *
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.
19 *
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
23 *
24 * @file
25 */
26 abstract class Action {
27
28 /**
29 * Page on which we're performing the action
30 * @var Page
31 */
32 protected $page;
33
34 /**
35 * IContextSource if specified; otherwise we'll use the Context from the Page
36 * @var IContextSource
37 */
38 protected $context;
39
40 /**
41 * The fields used to create the HTMLForm
42 * @var Array
43 */
44 protected $fields;
45
46 /**
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
52 */
53 private final static function getClass( $action, array $overrides ) {
54 global $wgActions;
55 $action = strtolower( $action );
56
57 if ( !isset( $wgActions[$action] ) ) {
58 return null;
59 }
60
61 if ( $wgActions[$action] === false ) {
62 return false;
63 } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
64 return $overrides[$action];
65 } elseif ( $wgActions[$action] === true ) {
66 return ucfirst( $action ) . 'Action';
67 } else {
68 return $wgActions[$action];
69 }
70 }
71
72 /**
73 * Get an appropriate Action subclass for the given action
74 * @param $action String
75 * @param $page Page
76 * @param $context IContextSource
77 * @return Action|false|null false if the action is disabled, null
78 * if it is not recognised
79 */
80 public final static function factory( $action, Page $page, IContextSource $context = null ) {
81 $class = self::getClass( $action, $page->getActionOverrides() );
82 if ( $class ) {
83 $obj = new $class( $page, $context );
84 return $obj;
85 }
86 return $class;
87 }
88
89 /**
90 * Get the action that will be executed, not necessarily the one passed
91 * passed through the "action" request parameter. Actions disabled in
92 * $wgActions will be replaced by "nosuchaction".
93 *
94 * @since 1.19
95 * @param $context IContextSource
96 * @return string: action name
97 */
98 public final static function getActionName( IContextSource $context ) {
99 global $wgActions;
100
101 $request = $context->getRequest();
102 $actionName = $request->getVal( 'action', 'view' );
103
104 // Check for disabled actions
105 if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
106 $actionName = 'nosuchaction';
107 }
108
109 // Workaround for bug #20966: inability of IE to provide an action dependent
110 // on which submit button is clicked.
111 if ( $actionName === 'historysubmit' ) {
112 if ( $request->getBool( 'revisiondelete' ) ) {
113 $actionName = 'revisiondelete';
114 } else {
115 $actionName = 'view';
116 }
117 } elseif ( $actionName == 'editredlink' ) {
118 $actionName = 'edit';
119 }
120
121 // Trying to get a WikiPage for NS_SPECIAL etc. will result
122 // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
123 // For SpecialPages et al, default to action=view.
124 if ( !$context->canUseWikiPage() ) {
125 return 'view';
126 }
127
128 $action = Action::factory( $actionName, $context->getWikiPage() );
129 if ( $action instanceof Action ) {
130 return $action->getName();
131 }
132
133 return 'nosuchaction';
134 }
135
136 /**
137 * Check if a given action is recognised, even if it's disabled
138 *
139 * @param $name String: name of an action
140 * @return Bool
141 */
142 public final static function exists( $name ) {
143 return self::getClass( $name, array() ) !== null;
144 }
145
146 /**
147 * Get the IContextSource in use here
148 * @return IContextSource
149 */
150 public final function getContext() {
151 if ( $this->context instanceof IContextSource ) {
152 return $this->context;
153 }
154 return $this->page->getContext();
155 }
156
157 /**
158 * Get the WebRequest being used for this instance
159 *
160 * @return WebRequest
161 */
162 public final function getRequest() {
163 return $this->getContext()->getRequest();
164 }
165
166 /**
167 * Get the OutputPage being used for this instance
168 *
169 * @return OutputPage
170 */
171 public final function getOutput() {
172 return $this->getContext()->getOutput();
173 }
174
175 /**
176 * Shortcut to get the User being used for this instance
177 *
178 * @return User
179 */
180 public final function getUser() {
181 return $this->getContext()->getUser();
182 }
183
184 /**
185 * Shortcut to get the Skin being used for this instance
186 *
187 * @return Skin
188 */
189 public final function getSkin() {
190 return $this->getContext()->getSkin();
191 }
192
193 /**
194 * Shortcut to get the user Language being used for this instance
195 *
196 * @return Language
197 */
198 public final function getLanguage() {
199 return $this->getContext()->getLanguage();
200 }
201
202 /**
203 * Shortcut to get the user Language being used for this instance
204 *
205 * @deprecated 1.19 Use getLanguage instead
206 * @return Language
207 */
208 public final function getLang() {
209 wfDeprecated( __METHOD__, '1.19' );
210 return $this->getLanguage();
211 }
212
213 /**
214 * Shortcut to get the Title object from the page
215 * @return Title
216 */
217 public final function getTitle() {
218 return $this->page->getTitle();
219 }
220
221 /**
222 * Get a Message object with context set
223 * Parameters are the same as wfMessage()
224 *
225 * @return Message object
226 */
227 public final function msg() {
228 $params = func_get_args();
229 return call_user_func_array( array( $this->getContext(), 'msg' ), $params );
230 }
231
232 /**
233 * Protected constructor: use Action::factory( $action, $page ) to actually build
234 * these things in the real world
235 * @param $page Page
236 * @param $context IContextSource
237 */
238 protected function __construct( Page $page, IContextSource $context = null ) {
239 $this->page = $page;
240 $this->context = $context;
241 }
242
243 /**
244 * Return the name of the action this object responds to
245 * @return String lowercase
246 */
247 public abstract function getName();
248
249 /**
250 * Get the permission required to perform this action. Often, but not always,
251 * the same as the action name
252 * @return String|null
253 */
254 public function getRestriction() {
255 return null;
256 }
257
258 /**
259 * Checks if the given user (identified by an object) can perform this action. Can be
260 * overridden by sub-classes with more complicated permissions schemes. Failures here
261 * must throw subclasses of ErrorPageError
262 *
263 * @param $user User: the user to check, or null to use the context user
264 * @throws ErrorPageError
265 */
266 protected function checkCanExecute( User $user ) {
267 $right = $this->getRestriction();
268 if ( $right !== null ) {
269 $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
270 if ( count( $errors ) ) {
271 throw new PermissionsError( $right, $errors );
272 }
273 }
274
275 if ( $this->requiresUnblock() && $user->isBlocked() ) {
276 $block = $user->mBlock;
277 throw new UserBlockedError( $block );
278 }
279
280 // This should be checked at the end so that the user won't think the
281 // error is only temporary when he also don't have the rights to execute
282 // this action
283 if ( $this->requiresWrite() && wfReadOnly() ) {
284 throw new ReadOnlyError();
285 }
286 }
287
288 /**
289 * Whether this action requires the wiki not to be locked
290 * @return Bool
291 */
292 public function requiresWrite() {
293 return true;
294 }
295
296 /**
297 * Whether this action can still be executed by a blocked user
298 * @return Bool
299 */
300 public function requiresUnblock() {
301 return true;
302 }
303
304 /**
305 * Set output headers for noindexing etc. This function will not be called through
306 * the execute() entry point, so only put UI-related stuff in here.
307 */
308 protected function setHeaders() {
309 $out = $this->getOutput();
310 $out->setRobotPolicy( "noindex,nofollow" );
311 $out->setPageTitle( $this->getPageTitle() );
312 $this->getOutput()->setSubtitle( $this->getDescription() );
313 $out->setArticleRelated( true );
314 }
315
316 /**
317 * Returns the name that goes in the \<h1\> page title
318 *
319 * @return String
320 */
321 protected function getPageTitle() {
322 return $this->getTitle()->getPrefixedText();
323 }
324
325 /**
326 * Returns the description that goes below the \<h1\> tag
327 *
328 * @return String
329 */
330 protected function getDescription() {
331 return wfMsgHtml( strtolower( $this->getName() ) );
332 }
333
334 /**
335 * The main action entry point. Do all output for display and send it to the context
336 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
337 * $this->getOutput(), etc.
338 * @throws ErrorPageError
339 */
340 public abstract function show();
341
342 /**
343 * Execute the action in a silent fashion: do not display anything or release any errors.
344 * @return Bool whether execution was successful
345 */
346 public abstract function execute();
347 }
348
349 abstract class FormAction extends Action {
350
351 /**
352 * Get an HTMLForm descriptor array
353 * @return Array
354 */
355 protected abstract function getFormFields();
356
357 /**
358 * Add pre- or post-text to the form
359 * @return String HTML which will be sent to $form->addPreText()
360 */
361 protected function preText() { return ''; }
362
363 /**
364 * @return string
365 */
366 protected function postText() { return ''; }
367
368 /**
369 * Play with the HTMLForm if you need to more substantially
370 * @param $form HTMLForm
371 */
372 protected function alterForm( HTMLForm $form ) {}
373
374 /**
375 * Get the HTMLForm to control behaviour
376 * @return HTMLForm|null
377 */
378 protected function getForm() {
379 $this->fields = $this->getFormFields();
380
381 // Give hooks a chance to alter the form, adding extra fields or text etc
382 wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields, $this->page ) );
383
384 $form = new HTMLForm( $this->fields, $this->getContext() );
385 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
386
387 // Retain query parameters (uselang etc)
388 $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
389 $params = array_diff_key(
390 $this->getRequest()->getQueryValues(),
391 array( 'action' => null, 'title' => null )
392 );
393 $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
394
395 $form->addPreText( $this->preText() );
396 $form->addPostText( $this->postText() );
397 $this->alterForm( $form );
398
399 // Give hooks a chance to alter the form, adding extra fields or text etc
400 wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page ) );
401
402 return $form;
403 }
404
405 /**
406 * Process the form on POST submission. If you return false from getFormFields(),
407 * this will obviously never be reached. If you don't want to do anything with the
408 * form, just return false here
409 * @param $data Array
410 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
411 */
412 public abstract function onSubmit( $data );
413
414 /**
415 * Do something exciting on successful processing of the form. This might be to show
416 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
417 * protect, etc).
418 */
419 public abstract function onSuccess();
420
421 /**
422 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
423 * some stuff underneath (history etc); to do some processing on submission of that
424 * form (delete, protect, etc) and to do something exciting on 'success', be that
425 * display something new or redirect to somewhere. Some actions have more exotic
426 * behaviour, but that's what subclassing is for :D
427 */
428 public function show() {
429 $this->setHeaders();
430
431 // This will throw exceptions if there's a problem
432 $this->checkCanExecute( $this->getUser() );
433
434 $form = $this->getForm();
435 if ( $form->show() ) {
436 $this->onSuccess();
437 }
438 }
439
440 /**
441 * @see Action::execute()
442 * @throws ErrorPageError
443 * @param array|null $data
444 * @param bool $captureErrors
445 * @return bool
446 */
447 public function execute( array $data = null, $captureErrors = true ) {
448 try {
449 // Set a new context so output doesn't leak.
450 $this->context = clone $this->page->getContext();
451
452 // This will throw exceptions if there's a problem
453 $this->checkCanExecute( $this->getUser() );
454
455 $fields = array();
456 foreach ( $this->fields as $key => $params ) {
457 if ( isset( $data[$key] ) ) {
458 $fields[$key] = $data[$key];
459 } elseif ( isset( $params['default'] ) ) {
460 $fields[$key] = $params['default'];
461 } else {
462 $fields[$key] = null;
463 }
464 }
465 $status = $this->onSubmit( $fields );
466 if ( $status === true ) {
467 // This might do permanent stuff
468 $this->onSuccess();
469 return true;
470 } else {
471 return false;
472 }
473 }
474 catch ( ErrorPageError $e ) {
475 if ( $captureErrors ) {
476 return false;
477 } else {
478 throw $e;
479 }
480 }
481 }
482 }
483
484 /**
485 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
486 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
487 * patrol, etc).
488 */
489 abstract class FormlessAction extends Action {
490
491 /**
492 * Show something on GET request.
493 * @return String|null will be added to the HTMLForm if present, or just added to the
494 * output if not. Return null to not add anything
495 */
496 public abstract function onView();
497
498 /**
499 * We don't want an HTMLForm
500 */
501 protected function getFormFields() {
502 return false;
503 }
504
505 public function onSubmit( $data ) {
506 return false;
507 }
508
509 public function onSuccess() {
510 return false;
511 }
512
513 public function show() {
514 $this->setHeaders();
515
516 // This will throw exceptions if there's a problem
517 $this->checkCanExecute( $this->getUser() );
518
519 $this->getOutput()->addHTML( $this->onView() );
520 }
521
522 /**
523 * Execute the action silently, not giving any output. Since these actions don't have
524 * forms, they probably won't have any data, but some (eg rollback) may do
525 * @param $data Array values that would normally be in the GET request
526 * @param $captureErrors Bool whether to catch exceptions and just return false
527 * @return Bool whether execution was successful
528 */
529 public function execute( array $data = null, $captureErrors = true ) {
530 try {
531 // Set a new context so output doesn't leak.
532 $this->context = clone $this->page->getContext();
533 if ( is_array( $data ) ) {
534 $this->context->setRequest( new FauxRequest( $data, false ) );
535 }
536
537 // This will throw exceptions if there's a problem
538 $this->checkCanExecute( $this->getUser() );
539
540 $this->onView();
541 return true;
542 }
543 catch ( ErrorPageError $e ) {
544 if ( $captureErrors ) {
545 return false;
546 } else {
547 throw $e;
548 }
549 }
550 }
551 }