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\MediaWikiServices
;
25 * @defgroup Actions Action done on pages
29 * Actions are things which can be done to pages (edit, delete, rollback, etc). They
30 * are distinct from Special Pages because an action must apply to exactly one page.
32 * To add an action in an extension, create a subclass of Action, and add the key to
35 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
36 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
37 * patrol, etc). The FormAction and FormlessAction classes represent these two groups.
39 abstract class Action
implements MessageLocalizer
{
42 * Page on which we're performing the action
44 * @var WikiPage|Article|ImagePage|CategoryPage|Page $page
49 * IContextSource if specified; otherwise we'll use the Context from the Page
51 * @var IContextSource $context
56 * The fields used to create the HTMLForm
63 * Get the Action subclass which should be used to handle this action, false if
64 * the action is disabled, or null if it's not recognised
65 * @param string $action
66 * @param array $overrides
67 * @return bool|null|string|callable|Action
69 final private static function getClass( $action, array $overrides ) {
71 $action = strtolower( $action );
73 if ( !isset( $wgActions[$action] ) ) {
77 if ( $wgActions[$action] === false ) {
79 } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
80 return $overrides[$action];
81 } elseif ( $wgActions[$action] === true ) {
82 return ucfirst( $action ) . 'Action';
84 return $wgActions[$action];
89 * Get an appropriate Action subclass for the given action
91 * @param string $action
93 * @param IContextSource|null $context
94 * @return Action|bool|null False if the action is disabled, null
95 * if it is not recognised
97 final public static function factory( $action, Page
$page, IContextSource
$context = null ) {
98 $classOrCallable = self
::getClass( $action, $page->getActionOverrides() );
100 if ( is_string( $classOrCallable ) ) {
101 if ( !class_exists( $classOrCallable ) ) {
104 return new $classOrCallable( $page, $context );
107 if ( is_callable( $classOrCallable ) ) {
108 return $classOrCallable( $page, $context );
111 return $classOrCallable;
115 * Get the action that will be executed, not necessarily the one passed
116 * passed through the "action" request parameter. Actions disabled in
117 * $wgActions will be replaced by "nosuchaction".
120 * @param IContextSource $context
121 * @return string Action name
123 final public static function getActionName( IContextSource
$context ) {
126 $request = $context->getRequest();
127 $actionName = $request->getVal( 'action', 'view' );
129 // Check for disabled actions
130 if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
131 $actionName = 'nosuchaction';
134 // Workaround for T22966: inability of IE to provide an action dependent
135 // on which submit button is clicked.
136 if ( $actionName === 'historysubmit' ) {
137 if ( $request->getBool( 'revisiondelete' ) ) {
138 $actionName = 'revisiondelete';
139 } elseif ( $request->getBool( 'editchangetags' ) ) {
140 $actionName = 'editchangetags';
142 $actionName = 'view';
144 } elseif ( $actionName === 'editredlink' ) {
145 $actionName = 'edit';
148 // Trying to get a WikiPage for NS_SPECIAL etc. will result
149 // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
150 // For SpecialPages et al, default to action=view.
151 if ( !$context->canUseWikiPage() ) {
155 $action = self
::factory( $actionName, $context->getWikiPage(), $context );
156 if ( $action instanceof Action
) {
157 return $action->getName();
160 return 'nosuchaction';
164 * Check if a given action is recognised, even if it's disabled
167 * @param string $name Name of an action
170 final public static function exists( $name ) {
171 return self
::getClass( $name, [] ) !== null;
175 * Get the IContextSource in use here
177 * @return IContextSource
179 final public function getContext() {
180 if ( $this->context
instanceof IContextSource
) {
181 return $this->context
;
182 } elseif ( $this->page
instanceof Article
) {
183 // NOTE: $this->page can be a WikiPage, which does not have a context.
184 wfDebug( __METHOD__
. ": no context known, falling back to Article's context.\n" );
185 return $this->page
->getContext();
188 wfWarn( __METHOD__
. ': no context known, falling back to RequestContext::getMain().' );
189 return RequestContext
::getMain();
193 * Get the WebRequest being used for this instance
198 final public function getRequest() {
199 return $this->getContext()->getRequest();
203 * Get the OutputPage being used for this instance
208 final public function getOutput() {
209 return $this->getContext()->getOutput();
213 * Shortcut to get the User being used for this instance
218 final public function getUser() {
219 return $this->getContext()->getUser();
223 * Shortcut to get the Skin being used for this instance
228 final public function getSkin() {
229 return $this->getContext()->getSkin();
233 * Shortcut to get the user Language being used for this instance
237 final public function getLanguage() {
238 return $this->getContext()->getLanguage();
242 * Shortcut to get the Title object from the page
247 final public function getTitle() {
248 return $this->page
->getTitle();
252 * Get a Message object with context set
253 * Parameters are the same as wfMessage()
257 final public function msg( $key ) {
258 $params = func_get_args();
259 return $this->getContext()->msg( ...$params );
263 * Only public since 1.21
266 * @param IContextSource|null $context
268 public function __construct( Page
$page, IContextSource
$context = null ) {
269 if ( $context === null ) {
270 wfWarn( __METHOD__
. ' called without providing a Context object.' );
271 // NOTE: We could try to initialize $context using $page->getContext(),
272 // if $page is an Article. That however seems to not work seamlessly.
276 $this->context
= $context;
280 * Return the name of the action this object responds to
283 * @return string Lowercase name
285 abstract public function getName();
288 * Get the permission required to perform this action. Often, but not always,
289 * the same as the action name
292 * @return string|null
294 public function getRestriction() {
299 * Checks if the given user (identified by an object) can perform this action. Can be
300 * overridden by sub-classes with more complicated permissions schemes. Failures here
301 * must throw subclasses of ErrorPageError
304 * @param User $user The user to check, or null to use the context user
305 * @throws UserBlockedError|ReadOnlyError|PermissionsError
307 protected function checkCanExecute( User
$user ) {
308 $right = $this->getRestriction();
309 if ( $right !== null ) {
310 $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
311 if ( count( $errors ) ) {
312 throw new PermissionsError( $right, $errors );
316 // If the action requires an unblock, explicitly check the user's block.
317 if ( $this->requiresUnblock() && $user->isBlockedFrom( $this->getTitle() ) ) {
318 $block = $user->getBlock();
320 throw new UserBlockedError( $block );
323 throw new PermissionsError( $this->getName(), [ 'badaccess-group0' ] );
326 // This should be checked at the end so that the user won't think the
327 // error is only temporary when he also don't have the rights to execute
329 if ( $this->requiresWrite() && wfReadOnly() ) {
330 throw new ReadOnlyError();
335 * Whether this action requires the wiki not to be locked
340 public function requiresWrite() {
345 * Whether this action can still be executed by a blocked user
350 public function requiresUnblock() {
355 * Set output headers for noindexing etc. This function will not be called through
356 * the execute() entry point, so only put UI-related stuff in here.
359 protected function setHeaders() {
360 $out = $this->getOutput();
361 $out->setRobotPolicy( 'noindex,nofollow' );
362 $out->setPageTitle( $this->getPageTitle() );
363 $out->setSubtitle( $this->getDescription() );
364 $out->setArticleRelated( true );
368 * Returns the name that goes in the \<h1\> page title
372 protected function getPageTitle() {
373 return $this->getTitle()->getPrefixedText();
377 * Returns the description that goes below the \<h1\> tag
380 * @return string HTML
382 protected function getDescription() {
383 return $this->msg( strtolower( $this->getName() ) )->escaped();
387 * Adds help link with an icon via page indicators.
388 * Link target can be overridden by a local message containing a wikilink:
389 * the message key is: lowercase action name + '-helppage'.
390 * @param string $to Target MediaWiki.org page title or encoded URL.
391 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
394 public function addHelpLink( $to, $overrideBaseUrl = false ) {
395 $msg = wfMessage( MediaWikiServices
::getInstance()->getContentLanguage()->lc(
396 self
::getActionName( $this->getContext() )
399 if ( !$msg->isDisabled() ) {
400 $helpUrl = Skin
::makeUrl( $msg->plain() );
401 $this->getOutput()->addHelpLink( $helpUrl, true );
403 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
408 * The main action entry point. Do all output for display and send it to the context
409 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
410 * $this->getOutput(), etc.
413 * @throws ErrorPageError
415 abstract public function show();
418 * Call wfTransactionalTimeLimit() if this request was POSTed
421 protected function useTransactionalTimeLimit() {
422 if ( $this->getRequest()->wasPosted() ) {
423 wfTransactionalTimeLimit();
428 * Indicates whether this action may perform database writes
432 public function doesWrites() {