Merge "Special:Version: Link to tree instead of commit for git hashes"
[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 * @var WikiPage|Article|ImagePage|CategoryPage|Page $page
42 */
43 protected $page;
44
45 /**
46 * IContextSource if specified; otherwise we'll use the Context from the Page
47 * @var IContextSource $context
48 */
49 protected $context;
50
51 /**
52 * The fields used to create the HTMLForm
53 * @var array $fields
54 */
55 protected $fields;
56
57 /**
58 * Get the Action subclass which should be used to handle this action, false if
59 * the action is disabled, or null if it's not recognised
60 * @param string $action
61 * @param array $overrides
62 * @return bool|null|string|callable
63 */
64 final private static function getClass( $action, array $overrides ) {
65 global $wgActions;
66 $action = strtolower( $action );
67
68 if ( !isset( $wgActions[$action] ) ) {
69 return null;
70 }
71
72 if ( $wgActions[$action] === false ) {
73 return false;
74 } elseif ( $wgActions[$action] === true && isset( $overrides[$action] ) ) {
75 return $overrides[$action];
76 } elseif ( $wgActions[$action] === true ) {
77 return ucfirst( $action ) . 'Action';
78 } else {
79 return $wgActions[$action];
80 }
81 }
82
83 /**
84 * Get an appropriate Action subclass for the given action
85 * @param string $action
86 * @param Page $page
87 * @param IContextSource $context
88 * @return Action|bool|null False if the action is disabled, null
89 * if it is not recognised
90 */
91 final public static function factory( $action, Page $page, IContextSource $context = null ) {
92 $classOrCallable = self::getClass( $action, $page->getActionOverrides() );
93
94 if ( is_string( $classOrCallable ) ) {
95 $obj = new $classOrCallable( $page, $context );
96 return $obj;
97 }
98
99 if ( is_callable( $classOrCallable ) ) {
100 return call_user_func_array( $classOrCallable, array( $page, $context ) );
101 }
102
103 return $classOrCallable;
104 }
105
106 /**
107 * Get the action that will be executed, not necessarily the one passed
108 * passed through the "action" request parameter. Actions disabled in
109 * $wgActions will be replaced by "nosuchaction".
110 *
111 * @since 1.19
112 * @param IContextSource $context
113 * @return string Action name
114 */
115 final public static function getActionName( IContextSource $context ) {
116 global $wgActions;
117
118 $request = $context->getRequest();
119 $actionName = $request->getVal( 'action', 'view' );
120
121 // Check for disabled actions
122 if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
123 $actionName = 'nosuchaction';
124 }
125
126 // Workaround for bug #20966: inability of IE to provide an action dependent
127 // on which submit button is clicked.
128 if ( $actionName === 'historysubmit' ) {
129 if ( $request->getBool( 'revisiondelete' ) ) {
130 $actionName = 'revisiondelete';
131 } else {
132 $actionName = 'view';
133 }
134 } elseif ( $actionName == 'editredlink' ) {
135 $actionName = 'edit';
136 }
137
138 // Trying to get a WikiPage for NS_SPECIAL etc. will result
139 // in WikiPage::factory throwing "Invalid or virtual namespace -1 given."
140 // For SpecialPages et al, default to action=view.
141 if ( !$context->canUseWikiPage() ) {
142 return 'view';
143 }
144
145 $action = Action::factory( $actionName, $context->getWikiPage(), $context );
146 if ( $action instanceof Action ) {
147 return $action->getName();
148 }
149
150 return 'nosuchaction';
151 }
152
153 /**
154 * Check if a given action is recognised, even if it's disabled
155 *
156 * @param string $name Name of an action
157 * @return bool
158 */
159 final public static function exists( $name ) {
160 return self::getClass( $name, array() ) !== null;
161 }
162
163 /**
164 * Get the IContextSource in use here
165 * @return IContextSource
166 */
167 final public function getContext() {
168 if ( $this->context instanceof IContextSource ) {
169 return $this->context;
170 } elseif ( $this->page instanceof Article ) {
171 // NOTE: $this->page can be a WikiPage, which does not have a context.
172 wfDebug( __METHOD__ . ": no context known, falling back to Article's context.\n" );
173 return $this->page->getContext();
174 }
175
176 wfWarn( __METHOD__ . ': no context known, falling back to RequestContext::getMain().' );
177 return RequestContext::getMain();
178 }
179
180 /**
181 * Get the WebRequest being used for this instance
182 *
183 * @return WebRequest
184 */
185 final public function getRequest() {
186 return $this->getContext()->getRequest();
187 }
188
189 /**
190 * Get the OutputPage being used for this instance
191 *
192 * @return OutputPage
193 */
194 final public function getOutput() {
195 return $this->getContext()->getOutput();
196 }
197
198 /**
199 * Shortcut to get the User being used for this instance
200 *
201 * @return User
202 */
203 final public function getUser() {
204 return $this->getContext()->getUser();
205 }
206
207 /**
208 * Shortcut to get the Skin being used for this instance
209 *
210 * @return Skin
211 */
212 final public function getSkin() {
213 return $this->getContext()->getSkin();
214 }
215
216 /**
217 * Shortcut to get the user Language being used for this instance
218 *
219 * @return Language
220 */
221 final public function getLanguage() {
222 return $this->getContext()->getLanguage();
223 }
224
225 /**
226 * Shortcut to get the Title object from the page
227 * @return Title
228 */
229 final public function getTitle() {
230 return $this->page->getTitle();
231 }
232
233 /**
234 * Get a Message object with context set
235 * Parameters are the same as wfMessage()
236 *
237 * @return Message
238 */
239 final public function msg() {
240 $params = func_get_args();
241 return call_user_func_array( array( $this->getContext(), 'msg' ), $params );
242 }
243
244 /**
245 * Constructor.
246 *
247 * Only public since 1.21
248 *
249 * @param Page $page
250 * @param IContextSource $context
251 */
252 public function __construct( Page $page, IContextSource $context = null ) {
253 if ( $context === null ) {
254 wfWarn( __METHOD__ . ' called without providing a Context object.' );
255 // NOTE: We could try to initialize $context using $page->getContext(),
256 // if $page is an Article. That however seems to not work seamlessly.
257 }
258
259 $this->page = $page;
260 $this->context = $context;
261 }
262
263 /**
264 * Return the name of the action this object responds to
265 * @return string Lowercase name
266 */
267 abstract public function getName();
268
269 /**
270 * Get the permission required to perform this action. Often, but not always,
271 * the same as the action name
272 * @return string|null
273 */
274 public function getRestriction() {
275 return null;
276 }
277
278 /**
279 * Checks if the given user (identified by an object) can perform this action. Can be
280 * overridden by sub-classes with more complicated permissions schemes. Failures here
281 * must throw subclasses of ErrorPageError
282 *
283 * @param User $user The user to check, or null to use the context user
284 * @throws UserBlockedError|ReadOnlyError|PermissionsError
285 */
286 protected function checkCanExecute( User $user ) {
287 $right = $this->getRestriction();
288 if ( $right !== null ) {
289 $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
290 if ( count( $errors ) ) {
291 throw new PermissionsError( $right, $errors );
292 }
293 }
294
295 if ( $this->requiresUnblock() && $user->isBlocked() ) {
296 $block = $user->getBlock();
297 throw new UserBlockedError( $block );
298 }
299
300 // This should be checked at the end so that the user won't think the
301 // error is only temporary when he also don't have the rights to execute
302 // this action
303 if ( $this->requiresWrite() && wfReadOnly() ) {
304 throw new ReadOnlyError();
305 }
306 }
307
308 /**
309 * Whether this action requires the wiki not to be locked
310 * @return bool
311 */
312 public function requiresWrite() {
313 return true;
314 }
315
316 /**
317 * Whether this action can still be executed by a blocked user
318 * @return bool
319 */
320 public function requiresUnblock() {
321 return true;
322 }
323
324 /**
325 * Set output headers for noindexing etc. This function will not be called through
326 * the execute() entry point, so only put UI-related stuff in here.
327 */
328 protected function setHeaders() {
329 $out = $this->getOutput();
330 $out->setRobotPolicy( "noindex,nofollow" );
331 $out->setPageTitle( $this->getPageTitle() );
332 $this->getOutput()->setSubtitle( $this->getDescription() );
333 $out->setArticleRelated( true );
334 }
335
336 /**
337 * Returns the name that goes in the \<h1\> page title
338 *
339 * @return string
340 */
341 protected function getPageTitle() {
342 return $this->getTitle()->getPrefixedText();
343 }
344
345 /**
346 * Returns the description that goes below the \<h1\> tag
347 *
348 * @return string
349 */
350 protected function getDescription() {
351 return $this->msg( strtolower( $this->getName() ) )->escaped();
352 }
353
354 /**
355 * The main action entry point. Do all output for display and send it to the context
356 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
357 * $this->getOutput(), etc.
358 * @throws ErrorPageError
359 */
360 abstract public function show();
361 }