3 * Actions are things which can be done to pages (edit, delete, rollback, etc). They
4 * are distinct from Special Pages because an action must apply to exactly one page.
6 * To add an action in an extension, create a subclass of Action, and add the key to
7 * $wgActions. There is also the deprecated UnknownAction hook
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 abstract class Action
{
28 // Page on which we're performing the action
32 // RequestContext if specified; otherwise we'll use the Context from the Page
33 // @var RequestContext
36 // The fields used to create the HTMLForm
41 * Get the Action subclass which should be used to handle this action, false if
42 * the action is disabled, or null if it's not recognised
43 * @param $action String
44 * @return bool|null|string
46 private final static function getClass( $action ){
48 $action = strtolower( $action );
50 if( !isset( $wgActions[$action] ) ){
54 if( $wgActions[$action] === false ){
58 elseif( $wgActions[$action] === true ){
59 return ucfirst( $action ) . 'Action';
63 return $wgActions[$action];
68 * Get an appropriate Action subclass for the given action
69 * @param $action String
70 * @param $page Article
71 * @return Action|false|null false if the action is disabled, null
72 * if it is not recognised
74 public final static function factory( $action, Article
$page ){
75 $class = self
::getClass( $action );
77 $obj = new $class( $page );
84 * Check if a given action is recognised, even if it's disabled
86 * @param $name String: name of an action
89 public final static function exists( $name ) {
90 return self
::getClass( $name ) !== null;
94 * Get the RequestContext in use here
95 * @return RequestContext
97 protected final function getContext(){
98 if( $this->context
instanceof RequestContext
){
99 return $this->context
;
101 return $this->page
->getContext();
105 * Get the WebRequest being used for this instance
109 protected final function getRequest() {
110 return $this->getContext()->request
;
114 * Get the OutputPage being used for this instance
118 protected final function getOutput() {
119 return $this->getContext()->output
;
123 * Shortcut to get the User being used for this instance
127 protected final function getUser() {
128 return $this->getContext()->user
;
132 * Shortcut to get the Skin being used for this instance
136 protected final function getSkin() {
137 return $this->getContext()->skin
;
141 * Shortcut to get the Title object from the page
144 protected final function getTitle(){
145 return $this->page
->getTitle();
149 * Protected constructor: use Action::factory( $action, $page ) to actually build
150 * these things in the real world
151 * @param Article $page
153 protected function __construct( Article
$page ){
158 * Return the name of the action this object responds to
159 * @return String lowercase
161 public abstract function getName();
164 * Get the permission required to perform this action. Often, but not always,
165 * the same as the action name
167 public abstract function getRestriction();
170 * Checks if the given user (identified by an object) can perform this action. Can be
171 * overridden by sub-classes with more complicated permissions schemes. Failures here
172 * must throw subclasses of ErrorPageError
174 * @param $user User: the user to check, or null to use the context user
175 * @throws ErrorPageError
177 protected function checkCanExecute( User
$user ) {
178 if( $this->requiresWrite() && wfReadOnly() ){
179 throw new ReadOnlyError();
182 if( $this->getRestriction() !== null && !$user->isAllowed( $this->getRestriction() ) ){
183 throw new PermissionsError( $this->getRestriction() );
186 if( $this->requiresUnblock() && $user->isBlocked() ){
187 $block = $user->mBlock
;
188 throw new UserBlockedError( $block );
193 * Whether this action requires the wiki not to be locked
196 public function requiresWrite(){
201 * Whether this action can still be executed by a blocked user
204 public function requiresUnblock(){
209 * Set output headers for noindexing etc. This function will not be called through
210 * the execute() entry point, so only put UI-related stuff in here.
212 protected function setHeaders() {
213 $out = $this->getOutput();
214 $out->setRobotPolicy( "noindex,nofollow" );
215 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
216 $this->getOutput()->setSubtitle( $this->getDescription() );
217 $out->setArticleRelated( true );
221 * Returns the name that goes in the \<h1\> page title
223 * Derived classes can override this, but usually it is easier to keep the
224 * default behaviour. Messages can be added at run-time, see
229 protected function getDescription() {
230 return wfMsg( strtolower( $this->getName() ) );
234 * The main action entry point. Do all output for display and send it to the context
235 * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
236 * $this->getOutput(), etc.
237 * @throws ErrorPageError
239 public abstract function show();
242 * Execute the action in a silent fashion: do not display anything or release any errors.
243 * @param $data Array values that would normally be in the POST request
244 * @param $captureErrors Bool whether to catch exceptions and just return false
245 * @return Bool whether execution was successful
247 public abstract function execute();
250 abstract class FormAction
extends Action
{
253 * Get an HTMLForm descriptor array
256 protected abstract function getFormFields();
259 * Add pre- or post-text to the form
260 * @return String HTML which will be sent to $form->addPreText()
262 protected function preText(){ return ''; }
263 protected function postText(){ return ''; }
266 * Play with the HTMLForm if you need to more substantially
267 * @param &$form HTMLForm
269 protected function alterForm( HTMLForm
&$form ){}
272 * Get the HTMLForm to control behaviour
273 * @return HTMLForm|null
275 protected function getForm(){
276 $this->fields
= $this->getFormFields();
278 // Give hooks a chance to alter the form, adding extra fields or text etc
279 wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields
, $this->page
) );
281 $form = new HTMLForm( $this->fields
, $this->getContext() );
282 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
283 $form->addHiddenField( 'action', $this->getName() );
285 $form->addPreText( $this->preText() );
286 $form->addPostText( $this->postText() );
287 $this->alterForm( $form );
289 // Give hooks a chance to alter the form, adding extra fields or text etc
290 wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page
) );
296 * Process the form on POST submission. If you return false from getFormFields(),
297 * this will obviously never be reached. If you don't want to do anything with the
298 * form, just return false here
300 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
302 public abstract function onSubmit( $data );
305 * Do something exciting on successful processing of the form. This might be to show
306 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
309 public abstract function onSuccess();
312 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
313 * some stuff underneath (history etc); to do some processing on submission of that
314 * form (delete, protect, etc) and to do something exciting on 'success', be that
315 * display something new or redirect to somewhere. Some actions have more exotic
316 * behaviour, but that's what subclassing is for :D
318 public function show(){
321 // This will throw exceptions if there's a problem
322 $this->checkCanExecute( $this->getUser() );
324 $form = $this->getForm();
331 * @see Action::execute()
332 * @throws ErrorPageError
333 * @param array|null $data
334 * @param bool $captureErrors
337 public function execute( array $data = null, $captureErrors = true ){
339 // Set a new context so output doesn't leak.
340 $this->context
= clone $this->page
->getContext();
342 // This will throw exceptions if there's a problem
343 $this->checkCanExecute( $this->getUser() );
346 foreach( $this->fields
as $key => $params ){
347 if( isset( $data[$key] ) ){
348 $fields[$key] = $data[$key];
349 } elseif( isset( $params['default'] ) ) {
350 $fields[$key] = $params['default'];
352 $fields[$key] = null;
355 $status = $this->onSubmit( $fields );
356 if( $status === true ){
357 // This might do permanent stuff
364 catch ( ErrorPageError
$e ){
365 if( $captureErrors ){
375 * Actions generally fall into two groups: the show-a-form-then-do-something-with-the-input
376 * format (protect, delete, move, etc), and the just-do-something format (watch, rollback,
379 abstract class FormlessAction
extends Action
{
382 * Show something on GET request.
383 * @return String|null will be added to the HTMLForm if present, or just added to the
384 * output if not. Return null to not add anything
386 public abstract function onView();
389 * We don't want an HTMLForm
391 protected function getFormFields(){
395 public function onSubmit( $data ){
399 public function onSuccess(){
403 public function show(){
406 // This will throw exceptions if there's a problem
407 $this->checkCanExecute( $this->getUser() );
409 $this->getOutput()->addHTML( $this->onView() );
413 * Execute the action silently, not giving any output. Since these actions don't have
414 * forms, they probably won't have any data, but some (eg rollback) may do
415 * @param $data Array values that would normally be in the GET request
416 * @param $captureErrors Bool whether to catch exceptions and just return false
417 * @return Bool whether execution was successful
419 public function execute( array $data = null, $captureErrors = true){
421 // Set a new context so output doesn't leak.
422 $this->context
= clone $this->page
->getContext();
423 if( is_array( $data ) ){
424 $this->context
->setRequest( new FauxRequest( $data, false ) );
427 // This will throw exceptions if there's a problem
428 $this->checkCanExecute( $this->getUser() );
433 catch ( ErrorPageError
$e ){
434 if( $captureErrors ){