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
23 * @defgroup Actions Action done on pages
27 * An action which shows a form and does something based on the input from the form
29 abstract class FormAction
extends Action
{
32 * Get an HTMLForm descriptor array
35 abstract protected function getFormFields();
38 * Add pre- or post-text to the form
39 * @return string HTML which will be sent to $form->addPreText()
41 protected function preText() {
48 protected function postText() {
53 * Play with the HTMLForm if you need to more substantially
54 * @param HTMLForm $form
56 protected function alterForm( HTMLForm
$form ) {
60 * Get the HTMLForm to control behavior
61 * @return HTMLForm|null
63 protected function getForm() {
64 $this->fields
= $this->getFormFields();
66 // Give hooks a chance to alter the form, adding extra fields or text etc
67 wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields
, $this->page
) );
69 $form = new HTMLForm( $this->fields
, $this->getContext(), $this->getName() );
70 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
72 // Retain query parameters (uselang etc)
73 $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
74 $params = array_diff_key(
75 $this->getRequest()->getQueryValues(),
76 array( 'action' => null, 'title' => null )
78 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
80 $form->addPreText( $this->preText() );
81 $form->addPostText( $this->postText() );
82 $this->alterForm( $form );
84 // Give hooks a chance to alter the form, adding extra fields or text etc
85 wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page
) );
91 * Process the form on POST submission. If you return false from getFormFields(),
92 * this will obviously never be reached. If you don't want to do anything with the
93 * form, just return false here
95 * @return bool|array True for success, false for didn't-try, array of errors on failure
97 abstract public function onSubmit( $data );
100 * Do something exciting on successful processing of the form. This might be to show
101 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
104 abstract public function onSuccess();
107 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
108 * some stuff underneath (history etc); to do some processing on submission of that
109 * form (delete, protect, etc) and to do something exciting on 'success', be that
110 * display something new or redirect to somewhere. Some actions have more exotic
111 * behavior, but that's what subclassing is for :D
113 public function show() {
116 // This will throw exceptions if there's a problem
117 $this->checkCanExecute( $this->getUser() );
119 $form = $this->getForm();
120 if ( $form->show() ) {
126 * @see Action::execute()
128 * @param array|null $data
129 * @param bool $captureErrors
130 * @throws ErrorPageError|Exception
133 public function execute( array $data = null, $captureErrors = true ) {
135 // Set a new context so output doesn't leak.
136 $this->context
= clone $this->getContext();
138 // This will throw exceptions if there's a problem
139 $this->checkCanExecute( $this->getUser() );
142 foreach ( $this->fields
as $key => $params ) {
143 if ( isset( $data[$key] ) ) {
144 $fields[$key] = $data[$key];
145 } elseif ( isset( $params['default'] ) ) {
146 $fields[$key] = $params['default'];
148 $fields[$key] = null;
151 $status = $this->onSubmit( $fields );
152 if ( $status === true ) {
153 // This might do permanent stuff
160 catch ( ErrorPageError
$e ) {
161 if ( $captureErrors ) {