FauxRequest: don’t override getValues()
[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 Action done on pages
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 * @return Message
256 */
257 final public function msg( $key ) {
258 $params = func_get_args();
259 return $this->getContext()->msg( ...$params );
260 }
261
262 /**
263 * Only public since 1.21
264 *
265 * @param Page $page
266 * @param IContextSource|null $context
267 */
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.
273 }
274
275 $this->page = $page;
276 $this->context = $context;
277 }
278
279 /**
280 * Return the name of the action this object responds to
281 * @since 1.17
282 *
283 * @return string Lowercase name
284 */
285 abstract public function getName();
286
287 /**
288 * Get the permission required to perform this action. Often, but not always,
289 * the same as the action name
290 * @since 1.17
291 *
292 * @return string|null
293 */
294 public function getRestriction() {
295 return null;
296 }
297
298 /**
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
302 * @since 1.17
303 *
304 * @param User $user The user to check, or null to use the context user
305 * @throws UserBlockedError|ReadOnlyError|PermissionsError
306 */
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 );
313 }
314 }
315
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();
319 if ( $block ) {
320 throw new UserBlockedError( $block );
321 }
322
323 throw new PermissionsError( $this->getName(), [ 'badaccess-group0' ] );
324 }
325
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
328 // this action
329 if ( $this->requiresWrite() && wfReadOnly() ) {
330 throw new ReadOnlyError();
331 }
332 }
333
334 /**
335 * Whether this action requires the wiki not to be locked
336 * @since 1.17
337 *
338 * @return bool
339 */
340 public function requiresWrite() {
341 return true;
342 }
343
344 /**
345 * Whether this action can still be executed by a blocked user
346 * @since 1.17
347 *
348 * @return bool
349 */
350 public function requiresUnblock() {
351 return true;
352 }
353
354 /**
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.
357 * @since 1.17
358 */
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 );
365 }
366
367 /**
368 * Returns the name that goes in the \<h1\> page title
369 *
370 * @return string
371 */
372 protected function getPageTitle() {
373 return $this->getTitle()->getPrefixedText();
374 }
375
376 /**
377 * Returns the description that goes below the \<h1\> tag
378 * @since 1.17
379 *
380 * @return string HTML
381 */
382 protected function getDescription() {
383 return $this->msg( strtolower( $this->getName() ) )->escaped();
384 }
385
386 /**
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.
392 * @since 1.25
393 */
394 public function addHelpLink( $to, $overrideBaseUrl = false ) {
395 $msg = wfMessage( MediaWikiServices::getInstance()->getContentLanguage()->lc(
396 self::getActionName( $this->getContext() )
397 ) . '-helppage' );
398
399 if ( !$msg->isDisabled() ) {
400 $helpUrl = Skin::makeUrl( $msg->plain() );
401 $this->getOutput()->addHelpLink( $helpUrl, true );
402 } else {
403 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
404 }
405 }
406
407 /**
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.
411 * @since 1.17
412 *
413 * @throws ErrorPageError
414 */
415 abstract public function show();
416
417 /**
418 * Call wfTransactionalTimeLimit() if this request was POSTed
419 * @since 1.26
420 */
421 protected function useTransactionalTimeLimit() {
422 if ( $this->getRequest()->wasPosted() ) {
423 wfTransactionalTimeLimit();
424 }
425 }
426
427 /**
428 * Indicates whether this action may perform database writes
429 * @return bool
430 * @since 1.27
431 */
432 public function doesWrites() {
433 return false;
434 }
435 }