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
24 * An action which shows a form and does something based on the input from the form
28 abstract class FormAction
extends Action
{
31 * Get an HTMLForm descriptor array
34 protected function getFormFields() {
35 // Default to an empty form with just a submit button
40 * Add pre- or post-text to the form
41 * @return string HTML which will be sent to $form->addPreText()
43 protected function preText() {
50 protected function postText() {
55 * Play with the HTMLForm if you need to more substantially
56 * @param HTMLForm $form
58 protected function alterForm( HTMLForm
$form ) {
62 * Get the HTMLForm to control behavior
63 * @return HTMLForm|null
65 protected function getForm() {
66 $this->fields
= $this->getFormFields();
68 // Give hooks a chance to alter the form, adding extra fields or text etc
69 Hooks
::run( 'ActionModifyFormFields', [ $this->getName(), &$this->fields
, $this->page
] );
71 $form = new HTMLForm( $this->fields
, $this->getContext(), $this->getName() );
72 $form->setSubmitCallback( [ $this, 'onSubmit' ] );
74 $title = $this->getTitle();
75 $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
76 // Retain query parameters (uselang etc)
77 $params = array_diff_key(
78 $this->getRequest()->getQueryValues(),
79 [ 'action' => null, 'title' => null ]
82 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
85 $form->addPreText( $this->preText() );
86 $form->addPostText( $this->postText() );
87 $this->alterForm( $form );
89 // Give hooks a chance to alter the form, adding extra fields or text etc
90 Hooks
::run( 'ActionBeforeFormDisplay', [ $this->getName(), &$form, $this->page
] );
96 * Process the form on POST submission.
98 * If you don't want to do anything with the form, just return false here.
101 * @return bool|array True for success, false for didn't-try, array of errors on failure
103 abstract public function onSubmit( $data );
106 * Do something exciting on successful processing of the form. This might be to show
107 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
110 abstract public function onSuccess();
113 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
114 * some stuff underneath (history etc); to do some processing on submission of that
115 * form (delete, protect, etc) and to do something exciting on 'success', be that
116 * display something new or redirect to somewhere. Some actions have more exotic
117 * behavior, but that's what subclassing is for :D
119 public function show() {
122 // This will throw exceptions if there's a problem
123 $this->checkCanExecute( $this->getUser() );
125 $form = $this->getForm();
126 if ( $form->show() ) {
131 public function doesWrites() {