Merge "Add deprecation messages to static methods in Article.php"
[mediawiki.git] / includes / actions / Action.php
blobd4b08b2e70c7ac3d301afdfe1a6d4eab1103f24f
1 <?php
2 /**
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
19 * @file
22 /**
23 * @defgroup Actions Action done on pages
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.
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
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.
37 abstract class Action {
39 /**
40 * Page on which we're performing the action
41 * @var WikiPage|Article|ImagePage|CategoryPage|Page $page
43 protected $page;
45 /**
46 * IContextSource if specified; otherwise we'll use the Context from the Page
47 * @var IContextSource $context
49 protected $context;
51 /**
52 * The fields used to create the HTMLForm
53 * @var array $fields
55 protected $fields;
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
64 final private static function getClass( $action, array $overrides ) {
65 global $wgActions;
66 $action = strtolower( $action );
68 if ( !isset( $wgActions[$action] ) ) {
69 return null;
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];
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
91 final public static function factory( $action, Page $page, IContextSource $context = null ) {
92 $classOrCallable = self::getClass( $action, $page->getActionOverrides() );
94 if ( is_string( $classOrCallable ) ) {
95 $obj = new $classOrCallable( $page, $context );
96 return $obj;
99 if ( is_callable( $classOrCallable ) ) {
100 return call_user_func_array( $classOrCallable, array( $page, $context ) );
103 return $classOrCallable;
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".
111 * @since 1.19
112 * @param IContextSource $context
113 * @return string Action name
115 final public static function getActionName( IContextSource $context ) {
116 global $wgActions;
118 $request = $context->getRequest();
119 $actionName = $request->getVal( 'action', 'view' );
121 // Check for disabled actions
122 if ( isset( $wgActions[$actionName] ) && $wgActions[$actionName] === false ) {
123 $actionName = 'nosuchaction';
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';
134 } elseif ( $actionName == 'editredlink' ) {
135 $actionName = 'edit';
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';
145 $action = Action::factory( $actionName, $context->getWikiPage(), $context );
146 if ( $action instanceof Action ) {
147 return $action->getName();
150 return 'nosuchaction';
154 * Check if a given action is recognised, even if it's disabled
156 * @param string $name Name of an action
157 * @return bool
159 final public static function exists( $name ) {
160 return self::getClass( $name, array() ) !== null;
164 * Get the IContextSource in use here
165 * @return IContextSource
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();
176 wfWarn( __METHOD__ . ': no context known, falling back to RequestContext::getMain().' );
177 return RequestContext::getMain();
181 * Get the WebRequest being used for this instance
183 * @return WebRequest
185 final public function getRequest() {
186 return $this->getContext()->getRequest();
190 * Get the OutputPage being used for this instance
192 * @return OutputPage
194 final public function getOutput() {
195 return $this->getContext()->getOutput();
199 * Shortcut to get the User being used for this instance
201 * @return User
203 final public function getUser() {
204 return $this->getContext()->getUser();
208 * Shortcut to get the Skin being used for this instance
210 * @return Skin
212 final public function getSkin() {
213 return $this->getContext()->getSkin();
217 * Shortcut to get the user Language being used for this instance
219 * @return Language
221 final public function getLanguage() {
222 return $this->getContext()->getLanguage();
226 * Shortcut to get the Title object from the page
227 * @return Title
229 final public function getTitle() {
230 return $this->page->getTitle();
234 * Get a Message object with context set
235 * Parameters are the same as wfMessage()
237 * @return Message
239 final public function msg() {
240 $params = func_get_args();
241 return call_user_func_array( array( $this->getContext(), 'msg' ), $params );
245 * Constructor.
247 * Only public since 1.21
249 * @param Page $page
250 * @param IContextSource $context
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.
259 $this->page = $page;
260 $this->context = $context;
264 * Return the name of the action this object responds to
265 * @return string Lowercase name
267 abstract public function getName();
270 * Get the permission required to perform this action. Often, but not always,
271 * the same as the action name
272 * @return string|null
274 public function getRestriction() {
275 return null;
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
283 * @param User $user The user to check, or null to use the context user
284 * @throws UserBlockedError|ReadOnlyError|PermissionsError
285 * @return bool True on success
287 protected function checkCanExecute( User $user ) {
288 $right = $this->getRestriction();
289 if ( $right !== null ) {
290 $errors = $this->getTitle()->getUserPermissionsErrors( $right, $user );
291 if ( count( $errors ) ) {
292 throw new PermissionsError( $right, $errors );
296 if ( $this->requiresUnblock() && $user->isBlocked() ) {
297 $block = $user->getBlock();
298 throw new UserBlockedError( $block );
301 // This should be checked at the end so that the user won't think the
302 // error is only temporary when he also don't have the rights to execute
303 // this action
304 if ( $this->requiresWrite() && wfReadOnly() ) {
305 throw new ReadOnlyError();
307 return true;
311 * Whether this action requires the wiki not to be locked
312 * @return bool
314 public function requiresWrite() {
315 return true;
319 * Whether this action can still be executed by a blocked user
320 * @return bool
322 public function requiresUnblock() {
323 return true;
327 * Set output headers for noindexing etc. This function will not be called through
328 * the execute() entry point, so only put UI-related stuff in here.
330 protected function setHeaders() {
331 $out = $this->getOutput();
332 $out->setRobotPolicy( "noindex,nofollow" );
333 $out->setPageTitle( $this->getPageTitle() );
334 $this->getOutput()->setSubtitle( $this->getDescription() );
335 $out->setArticleRelated( true );
339 * Returns the name that goes in the \<h1\> page title
341 * @return string
343 protected function getPageTitle() {
344 return $this->getTitle()->getPrefixedText();
348 * Returns the description that goes below the \<h1\> tag
350 * @return string
352 protected function getDescription() {
353 return $this->msg( strtolower( $this->getName() ) )->escaped();
357 * The main action entry point. Do all output for display and send it to the context
358 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
359 * $this->getOutput(), etc.
360 * @throws ErrorPageError
362 abstract public function show();
365 * Execute the action in a silent fashion: do not display anything or release any errors.
366 * @return bool whether execution was successful
368 abstract public function execute();