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