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 use MediaWiki\HTMLForm\HTMLForm
;
24 use MediaWiki\Status\Status
;
27 * An action which shows a form and does something based on the input from the form
33 abstract class FormAction
extends Action
{
36 * Get an HTMLForm descriptor array
40 protected function getFormFields() {
41 // Default to an empty form with just a submit button
46 * Add pre- or post-text to the form
48 * @return string HTML which will be sent to $form->addPreHtml()
50 protected function preText() {
58 protected function postText() {
63 * Play with the HTMLForm if you need to more substantially
65 * @param HTMLForm $form
67 protected function alterForm( HTMLForm
$form ) {
71 * Whether the form should use OOUI
75 protected function usesOOUI() {
80 * Get the HTMLForm to control behavior
84 protected function getForm() {
85 $this->fields
= $this->getFormFields();
87 // Give hooks a chance to alter the form, adding extra fields or text etc
88 $this->getHookRunner()->onActionModifyFormFields(
94 if ( $this->usesOOUI() ) {
95 $form = HTMLForm
::factory( 'ooui', $this->fields
, $this->getContext(), $this->getName() );
97 $form = new HTMLForm( $this->fields
, $this->getContext(), $this->getName() );
99 $form->setSubmitCallback( [ $this, 'onSubmit' ] );
101 $title = $this->getTitle();
102 $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
103 // Retain query parameters (uselang etc)
104 $params = array_diff_key(
105 $this->getRequest()->getQueryValues(),
106 [ 'action' => null, 'title' => null ]
109 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
112 $form->addPreHtml( $this->preText() );
113 $form->addPostHtml( $this->postText() );
114 $this->alterForm( $form );
116 // Give hooks a chance to alter the form, adding extra fields or text etc
117 $this->getHookRunner()->onActionBeforeFormDisplay(
127 * Process the form on POST submission.
129 * If you don't want to do anything with the form, just return false here.
131 * This method will be passed to the HTMLForm as a submit callback (see
132 * HTMLForm::setSubmitCallback) and must return as documented for HTMLForm::trySubmit.
134 * @see HTMLForm::setSubmitCallback()
135 * @see HTMLForm::trySubmit()
137 * @return bool|string|array|Status Must return as documented for HTMLForm::trySubmit
139 abstract public function onSubmit( $data );
142 * Do something exciting on successful processing of the form. This might be to show
143 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
146 abstract public function onSuccess();
149 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
150 * some stuff underneath (history etc); to do some processing on submission of that
151 * form (delete, protect, etc) and to do something exciting on 'success', be that
152 * display something new or redirect to somewhere. Some actions have more exotic
153 * behavior, but that's what subclassing is for :D
154 * @stable to override
156 public function show() {
159 // This will throw exceptions if there's a problem
160 $this->checkCanExecute( $this->getUser() );
162 $form = $this->getForm();
163 if ( $form->show() ) {
169 * @stable to override
172 public function doesWrites() {