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