Merge "Don't check namespace in SpecialWantedtemplates"
[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 /**
23 * @defgroup Actions Action done on pages
24 */
25
26 /**
27 * Actions are things which can be done to pages (edit, delete, rollback, etc). They
28 * are distinct from Special Pages because an action must apply to exactly one page.
29 *
30 * To add an action in an extension, create a subclass of Action, and add the key to
31 * $wgActions. There is also the deprecated UnknownAction hook
32 *
33 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
34 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
35 * patrol, etc). The FormAction and FormlessAction classes represent these two groups.
36 */
37 abstract class Action {
38
39 /**
40 * Page on which we're performing the action
41 * @since 1.17
42 * @var WikiPage|Article|ImagePage|CategoryPage|Page $page
43 */
44 protected $page;
45
46 /**
47 * IContextSource if specified; otherwise we'll use the Context from the Page
48 * @since 1.17
49 * @var IContextSource $context
50 */
51 protected $context;
52
53 /**
54 * The fields used to create the HTMLForm
55 * @since 1.17
56 * @var array $fields
57 */
58 protected $fields;
59
60 /**
61 * Get the Action subclass which should be used to handle this action, false if
62 * the action is disabled, or null if it's not recognised
63 * @param string $action
64 * @param array $overrides
65 * @return bool|null|string|callable
66 */
67 final private static function getClass( $action, array $overrides ) {
68 global $wgActions;
69 $action = strtolower( $action );
70
71 if ( !isset( $wgActions[$action] ) ) {
72 return null;
73 }
74
75 if ( $wgActions[$action] === false ) {
76 return false;
77 } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
78 return $overrides[$action];
79 } elseif ( $wgActions[$action] === true ) {
80 return ucfirst( $action ) . 'Action';
81 } else {
82 return $wgActions[$action];
83 }
84 }
85
86 /**
87 * Get an appropriate Action subclass for the given action
88 * @since 1.17
89 * @param string $action
90 * @param Page $page
91 * @param IContextSource $context
92 * @return Action|bool|null False if the action is disabled, null
93 * if it is not recognised
94 */
95 final public static function factory( $action, Page $page, IContextSource $context = null ) {
96 $classOrCallable = self::getClass( $action, $page->getActionOverrides() );
97
98 if ( is_string( $classOrCallable ) ) {
99 $obj = new $classOrCallable( $page, $context );
100 return $obj;
101 }
102
103 if ( is_callable( $classOrCallable ) ) {
104 return call_user_func_array( $classOrCallable, array( $page, $context ) );
105 }
106
107 return $classOrCallable;
108 }
109
110 /**
111 * Get the action that will be executed, not necessarily the one passed
112 * passed through the "action" request parameter. Actions disabled in
113 * $wgActions will be replaced by "nosuchaction".
114 *
115 * @since 1.19
116 * @param IContextSource $context
117 * @return string Action name
118 */
119 final public static function getActionName( IContextSource $context ) {
120 global $wgActions;
121
122 $request = $context->getRequest();
123 $actionName = $request->getVal( 'action', 'view' );
124
125 // Check for disabled actions
126 if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
127 $actionName = 'nosuchaction';
128 }
129
130 // Workaround for bug #20966: inability of IE to provide an action dependent
131 // on which submit button is clicked.
132 if ( $actionName === 'historysubmit' ) {
133 if ( $request->getBool( 'revisiondelete' ) ) {
134 $actionName = 'revisiondelete';
135 } elseif ( $request->getBool( 'editchangetags' ) ) {
136 $actionName = 'editchangetags';
137 } else {
138 $actionName = 'view';
139 }
140 } elseif ( $actionName == 'editredlink' ) {
141 $actionName = 'edit';
142 }
143
144 // Trying to get a WikiPage for NS_SPECIAL etc. will result
145 // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
146 // For SpecialPages et al, default to action=view.
147 if ( !$context->canUseWikiPage() ) {
148 return 'view';
149 }
150
151 $action = Action::factory( $actionName, $context->getWikiPage(), $context );
152 if ( $action instanceof Action ) {
153 return $action->getName();
154 }
155
156 return 'nosuchaction';
157 }
158
159 /**
160 * Check if a given action is recognised, even if it's disabled
161 * @since 1.17
162 *
163 * @param string $name Name of an action
164 * @return bool
165 */
166 final public static function exists( $name ) {
167 return self::getClass( $name, array() ) !== null;
168 }
169
170 /**
171 * Get the IContextSource in use here
172 * @since 1.17
173 * @return IContextSource
174 */
175 final public function getContext() {
176 if ( $this->context instanceof IContextSource ) {
177 return $this->context;
178 } elseif ( $this->page instanceof Article ) {
179 // NOTE: $this->page can be a WikiPage, which does not have a context.
180 wfDebug( __METHOD__ . ": no context known, falling back to Article's context.\n" );
181 return $this->page->getContext();
182 }
183
184 wfWarn( __METHOD__ . ': no context known, falling back to RequestContext::getMain().' );
185 return RequestContext::getMain();
186 }
187
188 /**
189 * Get the WebRequest being used for this instance
190 * @since 1.17
191 *
192 * @return WebRequest
193 */
194 final public function getRequest() {
195 return $this->getContext()->getRequest();
196 }
197
198 /**
199 * Get the OutputPage being used for this instance
200 * @since 1.17
201 *
202 * @return OutputPage
203 */
204 final public function getOutput() {
205 return $this->getContext()->getOutput();
206 }
207
208 /**
209 * Shortcut to get the User being used for this instance
210 * @since 1.17
211 *
212 * @return User
213 */
214 final public function getUser() {
215 return $this->getContext()->getUser();
216 }
217
218 /**
219 * Shortcut to get the Skin being used for this instance
220 * @since 1.17
221 *
222 * @return Skin
223 */
224 final public function getSkin() {
225 return $this->getContext()->getSkin();
226 }
227
228 /**
229 * Shortcut to get the user Language being used for this instance
230 *
231 * @return Language
232 */
233 final public function getLanguage() {
234 return $this->getContext()->getLanguage();
235 }
236
237 /**
238 * Shortcut to get the Title object from the page
239 * @since 1.17
240 *
241 * @return Title
242 */
243 final public function getTitle() {
244 return $this->page->getTitle();
245 }
246
247 /**
248 * Get a Message object with context set
249 * Parameters are the same as wfMessage()
250 *
251 * @return Message
252 */
253 final public function msg() {
254 $params = func_get_args();
255 return call_user_func_array( array( $this->getContext(), 'msg' ), $params );
256 }
257
258 /**
259 * Constructor.
260 *
261 * Only public since 1.21
262 *
263 * @param Page $page
264 * @param IContextSource $context
265 */
266 public function __construct( Page $page, IContextSource $context = null ) {
267 if ( $context === null ) {
268 wfWarn( __METHOD__ . ' called without providing a Context object.' );
269 // NOTE: We could try to initialize $context using $page->getContext(),
270 // if $page is an Article. That however seems to not work seamlessly.
271 }
272
273 $this->page = $page;
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 * @since 1.17
289 *
290 * @return string|null
291 */
292 public function getRestriction() {
293 return null;
294 }
295
296 /**
297 * Checks if the given user (identified by an object) can perform this action. Can be
298 * overridden by sub-classes with more complicated permissions schemes. Failures here
299 * must throw subclasses of ErrorPageError
300 * @since 1.17
301 *
302 * @param User $user The user to check, or null to use the context user
303 * @throws UserBlockedError|ReadOnlyError|PermissionsError
304 */
305 protected function checkCanExecute( User $user ) {
306 $right = $this->getRestriction();
307 if ( $right !== null ) {
308 $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
309 if ( count( $errors ) ) {
310 throw new PermissionsError( $right, $errors );
311 }
312 }
313
314 if ( $this->requiresUnblock() && $user->isBlocked() ) {
315 $block = $user->getBlock();
316 throw new UserBlockedError( $block );
317 }
318
319 // This should be checked at the end so that the user won't think the
320 // error is only temporary when he also don't have the rights to execute
321 // this action
322 if ( $this->requiresWrite() && wfReadOnly() ) {
323 throw new ReadOnlyError();
324 }
325 }
326
327 /**
328 * Whether this action requires the wiki not to be locked
329 * @since 1.17
330 *
331 * @return bool
332 */
333 public function requiresWrite() {
334 return true;
335 }
336
337 /**
338 * Whether this action can still be executed by a blocked user
339 * @since 1.17
340 *
341 * @return bool
342 */
343 public function requiresUnblock() {
344 return true;
345 }
346
347 /**
348 * Set output headers for noindexing etc. This function will not be called through
349 * the execute() entry point, so only put UI-related stuff in here.
350 * @since 1.17
351 */
352 protected function setHeaders() {
353 $out = $this->getOutput();
354 $out->setRobotPolicy( "noindex,nofollow" );
355 $out->setPageTitle( $this->getPageTitle() );
356 $out->setSubtitle( $this->getDescription() );
357 $out->setArticleRelated( true );
358 }
359
360 /**
361 * Returns the name that goes in the \<h1\> page title
362 *
363 * @return string
364 */
365 protected function getPageTitle() {
366 return $this->getTitle()->getPrefixedText();
367 }
368
369 /**
370 * Returns the description that goes below the \<h1\> tag
371 * @since 1.17
372 *
373 * @return string
374 */
375 protected function getDescription() {
376 return $this->msg( strtolower( $this->getName() ) )->escaped();
377 }
378
379 /**
380 * Adds help link with an icon via page indicators.
381 * Link target can be overridden by a local message containing a wikilink:
382 * the message key is: lowercase action name + '-helppage'.
383 * @param string $to Target MediaWiki.org page title or encoded URL.
384 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
385 * @since 1.25
386 */
387 public function addHelpLink( $to, $overrideBaseUrl = false ) {
388 global $wgContLang;
389 $msg = wfMessage( $wgContLang->lc(
390 Action::getActionName( $this->getContext() )
391 ) . '-helppage' );
392
393 if ( !$msg->isDisabled() ) {
394 $helpUrl = Skin::makeUrl( $msg->plain() );
395 $this->getOutput()->addHelpLink( $helpUrl, true );
396 } else {
397 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
398 }
399 }
400
401 /**
402 * The main action entry point. Do all output for display and send it to the context
403 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
404 * $this->getOutput(), etc.
405 * @since 1.17
406 *
407 * @throws ErrorPageError
408 */
409 abstract public function show();
410
411 /**
412 * Call wfTransactionalTimeLimit() if this request was POSTed
413 * @since 1.26
414 */
415 protected function useTransactionalTimeLimit() {
416 if ( $this->getRequest()->wasPosted() ) {
417 wfTransactionalTimeLimit();
418 }
419 }
420 }