Use varargs for MessageLocalizer::msg and similar
[lhc/web/wiklou.git] / includes / actions / Action.php
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\MediaWikiServices;
23
24 /**
25 * @defgroup Actions Actions
26 */
27
28 /**
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.
31 *
32 * To add an action in an extension, create a subclass of Action, and add the key to
33 * $wgActions.
34 *
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.
38 */
39 abstract class Action implements MessageLocalizer {
40
41 /**
42 * Page on which we're performing the action
43 * @since 1.17
44 * @var WikiPage|Article|ImagePage|CategoryPage|Page $page
45 */
46 protected $page;
47
48 /**
49 * IContextSource if specified; otherwise we'll use the Context from the Page
50 * @since 1.17
51 * @var IContextSource $context
52 */
53 protected $context;
54
55 /**
56 * The fields used to create the HTMLForm
57 * @since 1.17
58 * @var array $fields
59 */
60 protected $fields;
61
62 /**
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
68 */
69 final private static function getClass( $action, array $overrides ) {
70 global $wgActions;
71 $action = strtolower( $action );
72
73 if ( !isset( $wgActions[$action] ) ) {
74 return null;
75 }
76
77 if ( $wgActions[$action] === false ) {
78 return false;
79 } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
80 return $overrides[$action];
81 } elseif ( $wgActions[$action] === true ) {
82 return ucfirst( $action ) . 'Action';
83 } else {
84 return $wgActions[$action];
85 }
86 }
87
88 /**
89 * Get an appropriate Action subclass for the given action
90 * @since 1.17
91 * @param string $action
92 * @param Page $page
93 * @param IContextSource|null $context
94 * @return Action|bool|null False if the action is disabled, null
95 * if it is not recognised
96 */
97 final public static function factory( $action, Page $page, IContextSource $context = null ) {
98 $classOrCallable = self::getClass( $action, $page->getActionOverrides() );
99
100 if ( is_string( $classOrCallable ) ) {
101 if ( !class_exists( $classOrCallable ) ) {
102 return false;
103 }
104 return new $classOrCallable( $page, $context );
105 }
106
107 if ( is_callable( $classOrCallable ) ) {
108 return $classOrCallable( $page, $context );
109 }
110
111 return $classOrCallable;
112 }
113
114 /**
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".
118 *
119 * @since 1.19
120 * @param IContextSource $context
121 * @return string Action name
122 */
123 final public static function getActionName( IContextSource $context ) {
124 global $wgActions;
125
126 $request = $context->getRequest();
127 $actionName = $request->getVal( 'action', 'view' );
128
129 // Check for disabled actions
130 if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
131 $actionName = 'nosuchaction';
132 }
133
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';
141 } else {
142 $actionName = 'view';
143 }
144 } elseif ( $actionName === 'editredlink' ) {
145 $actionName = 'edit';
146 }
147
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() ) {
152 return 'view';
153 }
154
155 $action = self::factory( $actionName, $context->getWikiPage(), $context );
156 if ( $action instanceof Action ) {
157 return $action->getName();
158 }
159
160 return 'nosuchaction';
161 }
162
163 /**
164 * Check if a given action is recognised, even if it's disabled
165 * @since 1.17
166 *
167 * @param string $name Name of an action
168 * @return bool
169 */
170 final public static function exists( $name ) {
171 return self::getClass( $name, [] ) !== null;
172 }
173
174 /**
175 * Get the IContextSource in use here
176 * @since 1.17
177 * @return IContextSource
178 */
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();
186 }
187
188 wfWarn( __METHOD__ . ': no context known, falling back to RequestContext::getMain().' );
189 return RequestContext::getMain();
190 }
191
192 /**
193 * Get the WebRequest being used for this instance
194 * @since 1.17
195 *
196 * @return WebRequest
197 */
198 final public function getRequest() {
199 return $this->getContext()->getRequest();
200 }
201
202 /**
203 * Get the OutputPage being used for this instance
204 * @since 1.17
205 *
206 * @return OutputPage
207 */
208 final public function getOutput() {
209 return $this->getContext()->getOutput();
210 }
211
212 /**
213 * Shortcut to get the User being used for this instance
214 * @since 1.17
215 *
216 * @return User
217 */
218 final public function getUser() {
219 return $this->getContext()->getUser();
220 }
221
222 /**
223 * Shortcut to get the Skin being used for this instance
224 * @since 1.17
225 *
226 * @return Skin
227 */
228 final public function getSkin() {
229 return $this->getContext()->getSkin();
230 }
231
232 /**
233 * Shortcut to get the user Language being used for this instance
234 *
235 * @return Language
236 */
237 final public function getLanguage() {
238 return $this->getContext()->getLanguage();
239 }
240
241 /**
242 * Shortcut to get the Title object from the page
243 * @since 1.17
244 *
245 * @return Title
246 */
247 final public function getTitle() {
248 return $this->page->getTitle();
249 }
250
251 /**
252 * Get a Message object with context set
253 * Parameters are the same as wfMessage()
254 *
255 * @param string|string[]|MessageSpecifier $key
256 * @param mixed ...$params
257 * @return Message
258 */
259 final public function msg( $key, ...$params ) {
260 return $this->getContext()->msg( $key, ...$params );
261 }
262
263 /**
264 * Only public since 1.21
265 *
266 * @param Page $page
267 * @param IContextSource|null $context
268 */
269 public function __construct( Page $page, IContextSource $context = null ) {
270 if ( $context === null ) {
271 wfWarn( __METHOD__ . ' called without providing a Context object.' );
272 // NOTE: We could try to initialize $context using $page->getContext(),
273 // if $page is an Article. That however seems to not work seamlessly.
274 }
275
276 $this->page = $page;
277 $this->context = $context;
278 }
279
280 /**
281 * Return the name of the action this object responds to
282 * @since 1.17
283 *
284 * @return string Lowercase name
285 */
286 abstract public function getName();
287
288 /**
289 * Get the permission required to perform this action. Often, but not always,
290 * the same as the action name
291 * @since 1.17
292 *
293 * @return string|null
294 */
295 public function getRestriction() {
296 return null;
297 }
298
299 /**
300 * Checks if the given user (identified by an object) can perform this action. Can be
301 * overridden by sub-classes with more complicated permissions schemes. Failures here
302 * must throw subclasses of ErrorPageError
303 * @since 1.17
304 *
305 * @param User $user The user to check, or null to use the context user
306 * @throws UserBlockedError|ReadOnlyError|PermissionsError
307 */
308 protected function checkCanExecute( User $user ) {
309 $right = $this->getRestriction();
310 if ( $right !== null ) {
311 $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
312 if ( count( $errors ) ) {
313 throw new PermissionsError( $right, $errors );
314 }
315 }
316
317 // If the action requires an unblock, explicitly check the user's block.
318 if ( $this->requiresUnblock() && $user->isBlockedFrom( $this->getTitle() ) ) {
319 $block = $user->getBlock();
320 if ( $block ) {
321 throw new UserBlockedError( $block );
322 }
323
324 throw new PermissionsError( $this->getName(), [ 'badaccess-group0' ] );
325 }
326
327 // This should be checked at the end so that the user won't think the
328 // error is only temporary when he also don't have the rights to execute
329 // this action
330 if ( $this->requiresWrite() && wfReadOnly() ) {
331 throw new ReadOnlyError();
332 }
333 }
334
335 /**
336 * Whether this action requires the wiki not to be locked
337 * @since 1.17
338 *
339 * @return bool
340 */
341 public function requiresWrite() {
342 return true;
343 }
344
345 /**
346 * Whether this action can still be executed by a blocked user
347 * @since 1.17
348 *
349 * @return bool
350 */
351 public function requiresUnblock() {
352 return true;
353 }
354
355 /**
356 * Set output headers for noindexing etc. This function will not be called through
357 * the execute() entry point, so only put UI-related stuff in here.
358 * @since 1.17
359 */
360 protected function setHeaders() {
361 $out = $this->getOutput();
362 $out->setRobotPolicy( 'noindex,nofollow' );
363 $out->setPageTitle( $this->getPageTitle() );
364 $out->setSubtitle( $this->getDescription() );
365 $out->setArticleRelated( true );
366 }
367
368 /**
369 * Returns the name that goes in the \<h1\> page title
370 *
371 * @return string
372 */
373 protected function getPageTitle() {
374 return $this->getTitle()->getPrefixedText();
375 }
376
377 /**
378 * Returns the description that goes below the \<h1\> tag
379 * @since 1.17
380 *
381 * @return string HTML
382 */
383 protected function getDescription() {
384 return $this->msg( strtolower( $this->getName() ) )->escaped();
385 }
386
387 /**
388 * Adds help link with an icon via page indicators.
389 * Link target can be overridden by a local message containing a wikilink:
390 * the message key is: lowercase action name + '-helppage'.
391 * @param string $to Target MediaWiki.org page title or encoded URL.
392 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
393 * @since 1.25
394 */
395 public function addHelpLink( $to, $overrideBaseUrl = false ) {
396 $msg = wfMessage( MediaWikiServices::getInstance()->getContentLanguage()->lc(
397 self::getActionName( $this->getContext() )
398 ) . '-helppage' );
399
400 if ( !$msg->isDisabled() ) {
401 $helpUrl = Skin::makeUrl( $msg->plain() );
402 $this->getOutput()->addHelpLink( $helpUrl, true );
403 } else {
404 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
405 }
406 }
407
408 /**
409 * The main action entry point. Do all output for display and send it to the context
410 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
411 * $this->getOutput(), etc.
412 * @since 1.17
413 *
414 * @throws ErrorPageError
415 */
416 abstract public function show();
417
418 /**
419 * Call wfTransactionalTimeLimit() if this request was POSTed
420 * @since 1.26
421 */
422 protected function useTransactionalTimeLimit() {
423 if ( $this->getRequest()->wasPosted() ) {
424 wfTransactionalTimeLimit();
425 }
426 }
427
428 /**
429 * Indicates whether this action may perform database writes
430 * @return bool
431 * @since 1.27
432 */
433 public function doesWrites() {
434 return false;
435 }
436 }