Debian Codegraph @

includes/actions/Action.php

raw symbols SCIP
1 <?php
2 /**
3 * Base classes for actions done on pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 * @file
20 */
21
22 use MediaWiki\Context\IContextSource;
23 use MediaWiki\HookContainer\HookContainer;
24 use MediaWiki\HookContainer\HookRunner;
25 use MediaWiki\Language\Language;
26 use MediaWiki\Language\RawMessage;
27 use MediaWiki\MediaWikiServices;
28 use MediaWiki\Message\Message;
29 use MediaWiki\Output\OutputPage;
30 use MediaWiki\Permissions\Authority;
31 use MediaWiki\Request\WebRequest;
32 use MediaWiki\Title\Title;
33 use MediaWiki\User\User;
34 use Wikimedia\Message\MessageSpecifier;
35
36 /**
37 * @defgroup Actions Actions
38 */
39
40 /**
41 * Actions are things which can be done to pages (edit, delete, rollback, etc). They
42 * are distinct from Special Pages because an action must apply to exactly one page.
43 *
44 * To add an action in an extension, create a subclass of Action, and add the key to
45 * $wgActions.
46 *
47 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
48 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
49 * patrol, etc). The FormAction and FormlessAction classes represent these two groups.
50 *
51 * @stable to extend
52 */
53 abstract class Action implements MessageLocalizer {
54
55 /**
56 * @var Article
57 * @since 1.35
58 */
59 private $article;
60
61 /**
62 * IContextSource if specified; otherwise we'll use the Context from the Page
63 * @since 1.17
64 * @var IContextSource|null
65 */
66 protected $context;
67
68 /**
69 * The fields used to create the HTMLForm
70 * @since 1.17
71 * @var array
72 */
73 protected $fields;
74
75 /** @var HookContainer|null */
76 private $hookContainer;
77 /** @var HookRunner|null */
78 private $hookRunner;
79
80 /**
81 * Get an appropriate Action subclass for the given action
82 * @since 1.17
83 *
84 * @param string $action
85 * @param Article $article
86 * @param IContextSource|null $context Falls back to article's context
87 * @return Action|false|null False if the action is disabled, null
88 * if it is not recognised
89 */
90 final public static function factory(
91 string $action,
92 Article $article,
93 ?IContextSource $context = null
94 ) {
95 return MediaWikiServices::getInstance()
96 ->getActionFactory()
97 ->getAction( $action, $article, $context ?? $article->getContext() );
98 }
99
100 /**
101 * Get the action that will be executed, not necessarily the one passed
102 * passed through the "action" request parameter. Actions disabled in
103 * $wgActions will be replaced by "nosuchaction".
104 *
105 * @since 1.19
106 * @param IContextSource $context
107 * @return string Action name
108 */
109 final public static function getActionName( IContextSource $context ) {
110 // Optimisation: Reuse/prime the cached value of RequestContext
111 return $context->getActionName();
112 }
113
114 /**
115 * Get the IContextSource in use here
116 * @since 1.17
117 * @return IContextSource
118 */
119 final public function getContext() {
120 if ( $this->context instanceof IContextSource ) {
121 return $this->context;
122 }
123 wfDebug( __METHOD__ . ": no context known, falling back to Article's context." );
124 return $this->getArticle()->getContext();
125 }
126
127 /**
128 * Get the WebRequest being used for this instance
129 * @since 1.17
130 *
131 * @return WebRequest
132 */
133 final public function getRequest() {
134 return $this->getContext()->getRequest();
135 }
136
137 /**
138 * Get the OutputPage being used for this instance
139 * @since 1.17
140 *
141 * @return OutputPage
142 */
143 final public function getOutput() {
144 return $this->getContext()->getOutput();
145 }
146
147 /**
148 * Shortcut to get the User being used for this instance
149 * @since 1.17
150 *
151 * @return User
152 */
153 final public function getUser() {
154 return $this->getContext()->getUser();
155 }
156
157 /**
158 * Shortcut to get the Authority executing this instance
159 *
160 * @return Authority
161 * @since 1.39
162 */
163 final public function getAuthority(): Authority {
164 return $this->getContext()->getAuthority();
165 }
166
167 /**
168 * Shortcut to get the Skin being used for this instance
169 * @since 1.17
170 *
171 * @return Skin
172 */
173 final public function getSkin() {
174 return $this->getContext()->getSkin();
175 }
176
177 /**
178 * Shortcut to get the user Language being used for this instance
179 *
180 * @return Language
181 */
182 final public function getLanguage() {
183 return $this->getContext()->getLanguage();
184 }
185
186 /**
187 * Get a WikiPage object
188 * @since 1.35
189 *
190 * @return WikiPage
191 */
192 final public function getWikiPage(): WikiPage {
193 return $this->getArticle()->getPage();
194 }
195
196 /**
197 * Get a Article object
198 * @since 1.35
199 * Overriding this method is deprecated since 1.35
200 *
201 * @return Article|ImagePage|CategoryPage
202 */
203 public function getArticle() {
204 return $this->article;
205 }
206
207 /**
208 * Shortcut to get the Title object from the page
209 * @since 1.17
210 *
211 * @return Title
212 */
213 final public function getTitle() {
214 return $this->getWikiPage()->getTitle();
215 }
216
217 /**
218 * Get a Message object with context set
219 * Parameters are the same as wfMessage()
220 *
221 * @param string|string[]|MessageSpecifier $key
222 * @param mixed ...$params
223 * @return Message
224 */
225 final public function msg( $key, ...$params ) {
226 return $this->getContext()->msg( $key, ...$params );
227 }
228
229 /**
230 * @since 1.40
231 * @internal For use by ActionFactory
232 * @param HookContainer $hookContainer
233 */
234 public function setHookContainer( HookContainer $hookContainer ) {
235 $this->hookContainer = $hookContainer;
236 $this->hookRunner = new HookRunner( $hookContainer );
237 }
238
239 /**
240 * @since 1.35
241 * @internal since 1.37
242 * @return HookContainer
243 */
244 protected function getHookContainer() {
245 if ( !$this->hookContainer ) {
246 $this->hookContainer = MediaWikiServices::getInstance()->getHookContainer();
247 }
248 return $this->hookContainer;
249 }
250
251 /**
252 * @since 1.35
253 * @internal This is for use by core only. Hook interfaces may be removed
254 * without notice.
255 * @return HookRunner
256 */
257 protected function getHookRunner() {
258 if ( !$this->hookRunner ) {
259 $this->hookRunner = new HookRunner( $this->getHookContainer() );
260 }
261 return $this->hookRunner;
262 }
263
264 /**
265 * Only public since 1.21
266 *
267 * @stable to call
268 *
269 * @param Article $article
270 * @param IContextSource $context
271 */
272 public function __construct( Article $article, IContextSource $context ) {
273 $this->article = $article;
274 $this->context = $context;
275 }
276
277 /**
278 * Return the name of the action this object responds to
279 * @since 1.17
280 *
281 * @return string Lowercase name
282 */
283 abstract public function getName();
284
285 /**
286 * Get the permission required to perform this action. Often, but not always,
287 * the same as the action name
288 *
289 * Implementations of this methods must always return the same value, regardless
290 * of parameters passed to the constructor or system state.
291 *
292 * @since 1.17
293 * @stable to override
294 *
295 * @return string|null
296 */
297 public function getRestriction() {
298 return null;
299 }
300
301 /**
302 * Indicates whether this action requires read rights
303 *
304 * Implementations of this methods must always return the same value, regardless
305 * of parameters passed to the constructor or system state.
306 *
307 * @since 1.38
308 * @stable to override
309 * @return bool
310 */
311 public function needsReadRights() {
312 return true;
313 }
314
315 /**
316 * Checks if the given user (identified by an object) can perform this action. Can be
317 * overridden by sub-classes with more complicated permissions schemes. Failures here
318 * must throw subclasses of ErrorPageError
319 * @since 1.17
320 * @stable to override
321 *
322 * @param User $user
323 * @throws UserBlockedError|ReadOnlyError|PermissionsError
324 */
325 protected function checkCanExecute( User $user ) {
326 $right = $this->getRestriction();
327 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
328 if ( $right !== null ) {
329 $permissionManager->throwPermissionErrors( $right, $user, $this->getTitle() );
330 }
331
332 // If the action requires an unblock, explicitly check the user's block.
333 $checkReplica = !$this->getRequest()->wasPosted();
334 if (
335 $this->requiresUnblock() &&
336 $permissionManager->isBlockedFrom( $user, $this->getTitle(), $checkReplica )
337 ) {
338 $block = $user->getBlock();
339 if ( $block ) {
340 throw new UserBlockedError(
341 $block,
342 $user,
343 $this->getLanguage(),
344 $this->getRequest()->getIP()
345 );
346 }
347
348 throw new PermissionsError( $this->getName(), [ 'badaccess-group0' ] );
349 }
350
351 // This should be checked at the end so that the user won't think the
352 // error is only temporary when he also don't have the rights to execute
353 // this action
354 $readOnlyMode = MediaWikiServices::getInstance()->getReadOnlyMode();
355 if ( $this->requiresWrite() && $readOnlyMode->isReadOnly() ) {
356 throw new ReadOnlyError();
357 }
358 }
359
360 /**
361 * Indicates whether this action page write access to the wiki.
362 *
363 * Subclasses must override this method to return true if the operation they will
364 * perform is not "safe" per RFC 7231 section 4.2.1. A subclass's operation is "safe"
365 * if it is essentially read-only, i.e. the client does not request nor expect any
366 * state change that would be observable in the responses to future requests.
367 *
368 * Implementations of this method must always return the same value, regardless of the
369 * parameters passed to the constructor or system state.
370 *
371 * When handling GET/HEAD requests, subclasses should only perform "safe" operations.
372 * Note that subclasses handling POST requests might still implement "safe" operations,
373 * particularly in the case where large input parameters are required.
374 *
375 * @since 1.17
376 * @stable to override
377 *
378 * @return bool
379 */
380 public function requiresWrite() {
381 return true;
382 }
383
384 /**
385 * Whether this action can still be executed by a blocked user.
386 *
387 * Implementations of this methods must always return the same value, regardless
388 * of parameters passed to the constructor or system state.
389 *
390 * @since 1.17
391 * @stable to override
392 *
393 * @return bool
394 */
395 public function requiresUnblock() {
396 return true;
397 }
398
399 /**
400 * Set output headers for noindexing etc. This function will not be called through
401 * the execute() entry point, so only put UI-related stuff in here.
402 * @stable to override
403 * @since 1.17
404 */
405 protected function setHeaders() {
406 $out = $this->getOutput();
407 $out->setRobotPolicy( 'noindex,nofollow' );
408 $title = $this->getPageTitle();
409 if ( is_string( $title ) ) {
410 // T343849: deprecated
411 wfDeprecated( 'string return from Action::getPageTitle()', '1.41' );
412 $title = ( new RawMessage( '$1' ) )->rawParams( $title );
413 }
414 $out->setPageTitleMsg( $title );
415 $out->setSubtitle( $this->getDescription() );
416 $out->setArticleRelated( true );
417 }
418
419 /**
420 * Returns the name that goes in the `<h1>` page title.
421 *
422 * Since 1.41, returning a string from this method has been deprecated.
423 *
424 * @stable to override
425 * @return string|Message
426 */
427 protected function getPageTitle() {
428 return ( new RawMessage( '$1' ) )->plaintextParams( $this->getTitle()->getPrefixedText() );
429 }
430
431 /**
432 * Returns the description that goes below the `<h1>` element.
433 *
434 * @since 1.17
435 * @stable to override
436 * @return string HTML
437 */
438 protected function getDescription() {
439 return $this->msg( strtolower( $this->getName() ) )->escaped();
440 }
441
442 /**
443 * Adds help link with an icon via page indicators.
444 * Link target can be overridden by a local message containing a wikilink:
445 * the message key is: lowercase action name + '-helppage'.
446 * @param string $to Target MediaWiki.org page title or encoded URL.
447 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
448 * @since 1.25
449 */
450 public function addHelpLink( $to, $overrideBaseUrl = false ) {
451 $lang = MediaWikiServices::getInstance()->getContentLanguage();
452 $target = $lang->lc( $this->getName() . '-helppage' );
453 $msg = $this->msg( $target );
454
455 if ( !$msg->isDisabled() ) {
456 $title = Title::newFromText( $msg->plain() );
457 if ( $title instanceof Title ) {
458 $this->getOutput()->addHelpLink( $title->getLocalURL(), true );
459 }
460 } else {
461 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
462 }
463 }
464
465 /**
466 * The main action entry point. Do all output for display and send it to the context
467 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
468 * $this->getOutput(), etc.
469 * @since 1.17
470 *
471 * @throws ErrorPageError
472 */
473 abstract public function show();
474
475 /**
476 * Call wfTransactionalTimeLimit() if this request was POSTed
477 * @since 1.26
478 */
479 protected function useTransactionalTimeLimit() {
480 if ( $this->getRequest()->wasPosted() ) {
481 wfTransactionalTimeLimit();
482 }
483 }
484
485 /**
486 * Indicates whether POST requests handled by this action require write access to the wiki.
487 *
488 * Subclasses must override this method to return true if any of the operations that
489 * they perform on POST requests are not "safe" per RFC 7231 section 4.2.1. A subclass's
490 * operation is "safe" if it is essentially read-only, i.e. the client does not request
491 * nor expect any state change that would be observable in the responses to future requests.
492 *
493 * Implementations of this method must always return the same value, regardless of the
494 * parameters passed to the constructor or system state.
495 *
496 * When handling GET/HEAD requests, subclasses should only perform "safe" operations.
497 * Note that some subclasses might only perform "safe" operations even for POST requests,
498 * particularly in the case where large input parameters are required.
499 *
500 * @return bool
501 * @since 1.27
502 * @stable to override
503 */
504 public function doesWrites() {
505 return false;
506 }
507 }