3 * Base classes for actions done on pages.
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.
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.
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
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\MessageParam
;
35 use Wikimedia\Message\MessageSpecifier
;
38 * @defgroup Actions Actions
42 * Actions are things which can be done to pages (edit, delete, rollback, etc). They
43 * are distinct from Special Pages because an action must apply to exactly one page.
45 * To add an action in an extension, create a subclass of Action, and add the key to
48 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
49 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
50 * patrol, etc). The FormAction and FormlessAction classes represent these two groups.
54 abstract class Action
implements MessageLocalizer
{
63 * IContextSource if specified; otherwise we'll use the Context from the Page
65 * @var IContextSource|null
70 * The fields used to create the HTMLForm
76 /** @var HookContainer|null */
77 private $hookContainer;
78 /** @var HookRunner|null */
82 * Get an appropriate Action subclass for the given action
85 * @param string $action
86 * @param Article $article
87 * @param IContextSource|null $context Falls back to article's context
88 * @return Action|false|null False if the action is disabled, null
89 * if it is not recognised
91 final public static function factory(
94 ?IContextSource
$context = null
96 return MediaWikiServices
::getInstance()
98 ->getAction( $action, $article, $context ??
$article->getContext() );
102 * Get the action that will be executed, not necessarily the one passed
103 * passed through the "action" request parameter. Actions disabled in
104 * $wgActions will be replaced by "nosuchaction".
107 * @param IContextSource $context
108 * @return string Action name
110 final public static function getActionName( IContextSource
$context ) {
111 // Optimisation: Reuse/prime the cached value of RequestContext
112 return $context->getActionName();
116 * Get the IContextSource in use here
118 * @return IContextSource
120 final public function getContext() {
121 if ( $this->context
instanceof IContextSource
) {
122 return $this->context
;
124 wfDebug( __METHOD__
. ": no context known, falling back to Article's context." );
125 return $this->getArticle()->getContext();
129 * Get the WebRequest being used for this instance
134 final public function getRequest() {
135 return $this->getContext()->getRequest();
139 * Get the OutputPage being used for this instance
144 final public function getOutput() {
145 return $this->getContext()->getOutput();
149 * Shortcut to get the User being used for this instance
154 final public function getUser() {
155 return $this->getContext()->getUser();
159 * Shortcut to get the Authority executing this instance
164 final public function getAuthority(): Authority
{
165 return $this->getContext()->getAuthority();
169 * Shortcut to get the Skin being used for this instance
174 final public function getSkin() {
175 return $this->getContext()->getSkin();
179 * Shortcut to get the user Language being used for this instance
183 final public function getLanguage() {
184 return $this->getContext()->getLanguage();
188 * Get a WikiPage object
193 final public function getWikiPage(): WikiPage
{
194 return $this->getArticle()->getPage();
198 * Get a Article object
200 * Overriding this method is deprecated since 1.35
202 * @return Article|ImagePage|CategoryPage
204 public function getArticle() {
205 return $this->article
;
209 * Shortcut to get the Title object from the page
214 final public function getTitle() {
215 return $this->getWikiPage()->getTitle();
219 * Get a Message object with context set
220 * Parameters are the same as wfMessage()
222 * @param string|string[]|MessageSpecifier $key
223 * @phpcs:ignore Generic.Files.LineLength
224 * @param MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float> ...$params
225 * See Message::params()
228 final public function msg( $key, ...$params ) {
229 return $this->getContext()->msg( $key, ...$params );
234 * @internal For use by ActionFactory
235 * @param HookContainer $hookContainer
237 public function setHookContainer( HookContainer
$hookContainer ) {
238 $this->hookContainer
= $hookContainer;
239 $this->hookRunner
= new HookRunner( $hookContainer );
244 * @internal since 1.37
245 * @return HookContainer
247 protected function getHookContainer() {
248 if ( !$this->hookContainer
) {
249 $this->hookContainer
= MediaWikiServices
::getInstance()->getHookContainer();
251 return $this->hookContainer
;
256 * @internal This is for use by core only. Hook interfaces may be removed
260 protected function getHookRunner() {
261 if ( !$this->hookRunner
) {
262 $this->hookRunner
= new HookRunner( $this->getHookContainer() );
264 return $this->hookRunner
;
268 * Only public since 1.21
272 * @param Article $article
273 * @param IContextSource $context
275 public function __construct( Article
$article, IContextSource
$context ) {
276 $this->article
= $article;
277 $this->context
= $context;
281 * Return the name of the action this object responds to
284 * @return string Lowercase name
286 abstract public function getName();
289 * Get the permission required to perform this action. Often, but not always,
290 * the same as the action name
292 * Implementations of this methods must always return the same value, regardless
293 * of parameters passed to the constructor or system state.
296 * @stable to override
298 * @return string|null
300 public function getRestriction() {
305 * Indicates whether this action requires read rights
307 * Implementations of this methods must always return the same value, regardless
308 * of parameters passed to the constructor or system state.
311 * @stable to override
314 public function needsReadRights() {
319 * Checks if the given user (identified by an object) can perform this action. Can be
320 * overridden by sub-classes with more complicated permissions schemes. Failures here
321 * must throw subclasses of ErrorPageError
323 * @stable to override
326 * @throws UserBlockedError|ReadOnlyError|PermissionsError
328 protected function checkCanExecute( User
$user ) {
329 $right = $this->getRestriction();
330 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
331 if ( $right !== null ) {
332 $permissionManager->throwPermissionErrors( $right, $user, $this->getTitle() );
335 // If the action requires an unblock, explicitly check the user's block.
336 $checkReplica = !$this->getRequest()->wasPosted();
338 $this->requiresUnblock() &&
339 $permissionManager->isBlockedFrom( $user, $this->getTitle(), $checkReplica )
341 $block = $user->getBlock();
343 throw new UserBlockedError(
346 $this->getLanguage(),
347 $this->getRequest()->getIP()
351 throw new PermissionsError( $this->getName(), [ 'badaccess-group0' ] );
354 // This should be checked at the end so that the user won't think the
355 // error is only temporary when he also don't have the rights to execute
357 $readOnlyMode = MediaWikiServices
::getInstance()->getReadOnlyMode();
358 if ( $this->requiresWrite() && $readOnlyMode->isReadOnly() ) {
359 throw new ReadOnlyError();
364 * Indicates whether this action page write access to the wiki.
366 * Subclasses must override this method to return true if the operation they will
367 * perform is not "safe" per RFC 7231 section 4.2.1. A subclass's operation is "safe"
368 * if it is essentially read-only, i.e. the client does not request nor expect any
369 * state change that would be observable in the responses to future requests.
371 * Implementations of this method must always return the same value, regardless of the
372 * parameters passed to the constructor or system state.
374 * When handling GET/HEAD requests, subclasses should only perform "safe" operations.
375 * Note that subclasses handling POST requests might still implement "safe" operations,
376 * particularly in the case where large input parameters are required.
379 * @stable to override
383 public function requiresWrite() {
388 * Whether this action can still be executed by a blocked user.
390 * Implementations of this methods must always return the same value, regardless
391 * of parameters passed to the constructor or system state.
394 * @stable to override
398 public function requiresUnblock() {
403 * Set output headers for noindexing etc. This function will not be called through
404 * the execute() entry point, so only put UI-related stuff in here.
405 * @stable to override
408 protected function setHeaders() {
409 $out = $this->getOutput();
410 $out->setRobotPolicy( 'noindex,nofollow' );
411 $title = $this->getPageTitle();
412 if ( is_string( $title ) ) {
413 // T343849: deprecated
414 wfDeprecated( 'string return from Action::getPageTitle()', '1.41' );
415 $title = ( new RawMessage( '$1' ) )->rawParams( $title );
417 $out->setPageTitleMsg( $title );
418 $out->setSubtitle( $this->getDescription() );
419 $out->setArticleRelated( true );
423 * Returns the name that goes in the `<h1>` page title.
425 * Since 1.41, returning a string from this method has been deprecated.
427 * @stable to override
428 * @return string|Message
430 protected function getPageTitle() {
431 return ( new RawMessage( '$1' ) )->plaintextParams( $this->getTitle()->getPrefixedText() );
435 * Returns the description that goes below the `<h1>` element.
438 * @stable to override
439 * @return string HTML
441 protected function getDescription() {
442 return $this->msg( strtolower( $this->getName() ) )->escaped();
446 * Adds help link with an icon via page indicators.
447 * Link target can be overridden by a local message containing a wikilink:
448 * the message key is: lowercase action name + '-helppage'.
449 * @param string $to Target MediaWiki.org page title or encoded URL.
450 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
453 public function addHelpLink( $to, $overrideBaseUrl = false ) {
454 $lang = MediaWikiServices
::getInstance()->getContentLanguage();
455 $target = $lang->lc( $this->getName() . '-helppage' );
456 $msg = $this->msg( $target );
458 if ( !$msg->isDisabled() ) {
459 $title = Title
::newFromText( $msg->plain() );
460 if ( $title instanceof Title
) {
461 $this->getOutput()->addHelpLink( $title->getLocalURL(), true );
464 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
469 * The main action entry point. Do all output for display and send it to the context
470 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
471 * $this->getOutput(), etc.
474 * @throws ErrorPageError
476 abstract public function show();
479 * Call wfTransactionalTimeLimit() if this request was POSTed
482 protected function useTransactionalTimeLimit() {
483 if ( $this->getRequest()->wasPosted() ) {
484 wfTransactionalTimeLimit();
489 * Indicates whether POST requests handled by this action require write access to the wiki.
491 * Subclasses must override this method to return true if any of the operations that
492 * they perform on POST requests are not "safe" per RFC 7231 section 4.2.1. A subclass's
493 * operation is "safe" if it is essentially read-only, i.e. the client does not request
494 * nor expect any state change that would be observable in the responses to future requests.
496 * Implementations of this method must always return the same value, regardless of the
497 * parameters passed to the constructor or system state.
499 * When handling GET/HEAD requests, subclasses should only perform "safe" operations.
500 * Note that some subclasses might only perform "safe" operations even for POST requests,
501 * particularly in the case where large input parameters are required.
505 * @stable to override
507 public function doesWrites() {