(bug 31420) Fix weird tablesorter bug where headers spanning multiple rows would...
[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 Article
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 Article
76 * @return Action|false|null false if the action is disabled, null
77 * if it is not recognised
78 */
79 public final static function factory( $action, Page $page ) {
80 $class = self::getClass( $action, $page->getActionOverrides() );
81 if ( $class ) {
82 $obj = new $class( $page );
83 return $obj;
84 }
85 return $class;
86 }
87
88 /**
89 * Check if a given action is recognised, even if it's disabled
90 *
91 * @param $name String: name of an action
92 * @return Bool
93 */
94 public final static function exists( $name ) {
95 return self::getClass( $name, array() ) !== null;
96 }
97
98 /**
99 * Get the IContextSource in use here
100 * @return IContextSource
101 */
102 protected final function getContext() {
103 if ( $this->context instanceof IContextSource ) {
104 return $this->context;
105 }
106 return $this->page->getContext();
107 }
108
109 /**
110 * Get the WebRequest being used for this instance
111 *
112 * @return WebRequest
113 */
114 protected final function getRequest() {
115 return $this->getContext()->getRequest();
116 }
117
118 /**
119 * Get the OutputPage being used for this instance
120 *
121 * @return OutputPage
122 */
123 protected final function getOutput() {
124 return $this->getContext()->getOutput();
125 }
126
127 /**
128 * Shortcut to get the User being used for this instance
129 *
130 * @return User
131 */
132 protected final function getUser() {
133 return $this->getContext()->getUser();
134 }
135
136 /**
137 * Shortcut to get the Skin being used for this instance
138 *
139 * @return Skin
140 */
141 protected final function getSkin() {
142 return $this->getContext()->getSkin();
143 }
144
145 /**
146 * Shortcut to get the user Language being used for this instance
147 *
148 * @return Skin
149 */
150 protected final function getLang() {
151 return $this->getContext()->getLang();
152 }
153
154 /**
155 * Shortcut to get the Title object from the page
156 * @return Title
157 */
158 protected final function getTitle() {
159 return $this->page->getTitle();
160 }
161
162 /**
163 * Protected constructor: use Action::factory( $action, $page ) to actually build
164 * these things in the real world
165 * @param Page $page
166 */
167 protected function __construct( Page $page ) {
168 $this->page = $page;
169 }
170
171 /**
172 * Return the name of the action this object responds to
173 * @return String lowercase
174 */
175 public abstract function getName();
176
177 /**
178 * Get the permission required to perform this action. Often, but not always,
179 * the same as the action name
180 */
181 public abstract function getRestriction();
182
183 /**
184 * Checks if the given user (identified by an object) can perform this action. Can be
185 * overridden by sub-classes with more complicated permissions schemes. Failures here
186 * must throw subclasses of ErrorPageError
187 *
188 * @param $user User: the user to check, or null to use the context user
189 * @throws ErrorPageError
190 */
191 protected function checkCanExecute( User $user ) {
192 if ( $this->requiresWrite() && wfReadOnly() ) {
193 throw new ReadOnlyError();
194 }
195
196 if ( $this->getRestriction() !== null && !$user->isAllowed( $this->getRestriction() ) ) {
197 throw new PermissionsError( $this->getRestriction() );
198 }
199
200 if ( $this->requiresUnblock() && $user->isBlocked() ) {
201 $block = $user->mBlock;
202 throw new UserBlockedError( $block );
203 }
204 }
205
206 /**
207 * Whether this action requires the wiki not to be locked
208 * @return Bool
209 */
210 public function requiresWrite() {
211 return true;
212 }
213
214 /**
215 * Whether this action can still be executed by a blocked user
216 * @return Bool
217 */
218 public function requiresUnblock() {
219 return true;
220 }
221
222 /**
223 * Set output headers for noindexing etc. This function will not be called through
224 * the execute() entry point, so only put UI-related stuff in here.
225 */
226 protected function setHeaders() {
227 $out = $this->getOutput();
228 $out->setRobotPolicy( "noindex,nofollow" );
229 $out->setPageTitle( $this->getPageTitle() );
230 $this->getOutput()->setSubtitle( $this->getDescription() );
231 $out->setArticleRelated( true );
232 }
233
234 /**
235 * Returns the name that goes in the \<h1\> page title
236 *
237 * @return String
238 */
239 protected function getPageTitle() {
240 return $this->getTitle()->getPrefixedText();
241 }
242
243 /**
244 * Returns the description that goes below the \<h1\> tag
245 *
246 * @return String
247 */
248 protected function getDescription() {
249 return wfMsg( strtolower( $this->getName() ) );
250 }
251
252 /**
253 * The main action entry point. Do all output for display and send it to the context
254 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
255 * $this->getOutput(), etc.
256 * @throws ErrorPageError
257 */
258 public abstract function show();
259
260 /**
261 * Execute the action in a silent fashion: do not display anything or release any errors.
262 * @param $data Array values that would normally be in the POST request
263 * @param $captureErrors Bool whether to catch exceptions and just return false
264 * @return Bool whether execution was successful
265 */
266 public abstract function execute();
267 }
268
269 abstract class FormAction extends Action {
270
271 /**
272 * Get an HTMLForm descriptor array
273 * @return Array
274 */
275 protected abstract function getFormFields();
276
277 /**
278 * Add pre- or post-text to the form
279 * @return String HTML which will be sent to $form->addPreText()
280 */
281 protected function preText() { return ''; }
282 protected function postText() { return ''; }
283
284 /**
285 * Play with the HTMLForm if you need to more substantially
286 * @param $form HTMLForm
287 */
288 protected function alterForm( HTMLForm $form ) {}
289
290 /**
291 * Get the HTMLForm to control behaviour
292 * @return HTMLForm|null
293 */
294 protected function getForm() {
295 $this->fields = $this->getFormFields();
296
297 // Give hooks a chance to alter the form, adding extra fields or text etc
298 wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields, $this->page ) );
299
300 $form = new HTMLForm( $this->fields, $this->getContext() );
301 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
302
303 // Retain query parameters (uselang etc)
304 $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
305 $params = array_diff_key(
306 $this->getRequest()->getQueryValues(),
307 array( 'action' => null, 'title' => null )
308 );
309 $form->addHiddenField( 'redirectparams', wfArrayToCGI( $params ) );
310
311 $form->addPreText( $this->preText() );
312 $form->addPostText( $this->postText() );
313 $this->alterForm( $form );
314
315 // Give hooks a chance to alter the form, adding extra fields or text etc
316 wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page ) );
317
318 return $form;
319 }
320
321 /**
322 * Process the form on POST submission. If you return false from getFormFields(),
323 * this will obviously never be reached. If you don't want to do anything with the
324 * form, just return false here
325 * @param $data Array
326 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
327 */
328 public abstract function onSubmit( $data );
329
330 /**
331 * Do something exciting on successful processing of the form. This might be to show
332 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
333 * protect, etc).
334 */
335 public abstract function onSuccess();
336
337 /**
338 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
339 * some stuff underneath (history etc); to do some processing on submission of that
340 * form (delete, protect, etc) and to do something exciting on 'success', be that
341 * display something new or redirect to somewhere. Some actions have more exotic
342 * behaviour, but that's what subclassing is for :D
343 */
344 public function show() {
345 $this->setHeaders();
346
347 // This will throw exceptions if there's a problem
348 $this->checkCanExecute( $this->getUser() );
349
350 $form = $this->getForm();
351 if ( $form->show() ) {
352 $this->onSuccess();
353 }
354 }
355
356 /**
357 * @see Action::execute()
358 * @throws ErrorPageError
359 * @param array|null $data
360 * @param bool $captureErrors
361 * @return bool
362 */
363 public function execute( array $data = null, $captureErrors = true ) {
364 try {
365 // Set a new context so output doesn't leak.
366 $this->context = clone $this->page->getContext();
367
368 // This will throw exceptions if there's a problem
369 $this->checkCanExecute( $this->getUser() );
370
371 $fields = array();
372 foreach ( $this->fields as $key => $params ) {
373 if ( isset( $data[$key] ) ) {
374 $fields[$key] = $data[$key];
375 } elseif ( isset( $params['default'] ) ) {
376 $fields[$key] = $params['default'];
377 } else {
378 $fields[$key] = null;
379 }
380 }
381 $status = $this->onSubmit( $fields );
382 if ( $status === true ) {
383 // This might do permanent stuff
384 $this->onSuccess();
385 return true;
386 } else {
387 return false;
388 }
389 }
390 catch ( ErrorPageError $e ) {
391 if ( $captureErrors ) {
392 return false;
393 } else {
394 throw $e;
395 }
396 }
397 }
398 }
399
400 /**
401 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
402 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
403 * patrol, etc).
404 */
405 abstract class FormlessAction extends Action {
406
407 /**
408 * Show something on GET request.
409 * @return String|null will be added to the HTMLForm if present, or just added to the
410 * output if not. Return null to not add anything
411 */
412 public abstract function onView();
413
414 /**
415 * We don't want an HTMLForm
416 */
417 protected function getFormFields() {
418 return false;
419 }
420
421 public function onSubmit( $data ) {
422 return false;
423 }
424
425 public function onSuccess() {
426 return false;
427 }
428
429 public function show() {
430 $this->setHeaders();
431
432 // This will throw exceptions if there's a problem
433 $this->checkCanExecute( $this->getUser() );
434
435 $this->getOutput()->addHTML( $this->onView() );
436 }
437
438 /**
439 * Execute the action silently, not giving any output. Since these actions don't have
440 * forms, they probably won't have any data, but some (eg rollback) may do
441 * @param $data Array values that would normally be in the GET request
442 * @param $captureErrors Bool whether to catch exceptions and just return false
443 * @return Bool whether execution was successful
444 */
445 public function execute( array $data = null, $captureErrors = true ) {
446 try {
447 // Set a new context so output doesn't leak.
448 $this->context = clone $this->page->getContext();
449 if ( is_array( $data ) ) {
450 $this->context->setRequest( new FauxRequest( $data, false ) );
451 }
452
453 // This will throw exceptions if there's a problem
454 $this->checkCanExecute( $this->getUser() );
455
456 $this->onView();
457 return true;
458 }
459 catch ( ErrorPageError $e ) {
460 if ( $captureErrors ) {
461 return false;
462 } else {
463 throw $e;
464 }
465 }
466 }
467 }