Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormAction
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 8
132
0.00% covered (danger)
0.00%
0 / 1
 getFormFields
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 preText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 postText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 alterForm
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 usesOOUI
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getForm
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
12
 onSubmit
n/a
0 / 0
n/a
0 / 0
0
 onSuccess
n/a
0 / 0
n/a
0 / 0
0
 show
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Base classes for actions done on pages.
4 *
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.
9 *
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.
14 *
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
18 *
19 * @file
20 * @ingroup Actions
21 */
22
23use MediaWiki\Status\Status;
24
25/**
26 * An action which shows a form and does something based on the input from the form
27 *
28 * @stable to extend
29 *
30 * @ingroup Actions
31 */
32abstract class FormAction extends Action {
33
34    /**
35     * Get an HTMLForm descriptor array
36     * @stable to override
37     * @return array
38     */
39    protected function getFormFields() {
40        // Default to an empty form with just a submit button
41        return [];
42    }
43
44    /**
45     * Add pre- or post-text to the form
46     * @stable to override
47     * @return string HTML which will be sent to $form->addPreText()
48     */
49    protected function preText() {
50        return '';
51    }
52
53    /**
54     * @stable to override
55     * @return string
56     */
57    protected function postText() {
58        return '';
59    }
60
61    /**
62     * Play with the HTMLForm if you need to more substantially
63     * @stable to override
64     * @param HTMLForm $form
65     */
66    protected function alterForm( HTMLForm $form ) {
67    }
68
69    /**
70     * Whether the form should use OOUI
71     * @stable to override
72     * @return bool
73     */
74    protected function usesOOUI() {
75        return false;
76    }
77
78    /**
79     * Get the HTMLForm to control behavior
80     * @stable to override
81     * @return HTMLForm
82     */
83    protected function getForm() {
84        $this->fields = $this->getFormFields();
85
86        // Give hooks a chance to alter the form, adding extra fields or text etc
87        $this->getHookRunner()->onActionModifyFormFields(
88            $this->getName(),
89            $this->fields,
90            $this->getArticle()
91        );
92
93        if ( $this->usesOOUI() ) {
94            $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
95        } else {
96            $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
97        }
98        $form->setSubmitCallback( [ $this, 'onSubmit' ] );
99
100        $title = $this->getTitle();
101        $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
102        // Retain query parameters (uselang etc)
103        $params = array_diff_key(
104            $this->getRequest()->getQueryValues(),
105            [ 'action' => null, 'title' => null ]
106        );
107        if ( $params ) {
108            $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
109        }
110
111        $form->addPreText( $this->preText() );
112        $form->addPostText( $this->postText() );
113        $this->alterForm( $form );
114
115        // Give hooks a chance to alter the form, adding extra fields or text etc
116        $this->getHookRunner()->onActionBeforeFormDisplay(
117            $this->getName(),
118            $form,
119            $this->getArticle()
120        );
121
122        return $form;
123    }
124
125    /**
126     * Process the form on POST submission.
127     *
128     * If you don't want to do anything with the form, just return false here.
129     *
130     * This method will be passed to the HTMLForm as a submit callback (see
131     * HTMLForm::setSubmitCallback) and must return as documented for HTMLForm::trySubmit.
132     *
133     * @see HTMLForm::setSubmitCallback()
134     * @see HTMLForm::trySubmit()
135     * @param array $data
136     * @return bool|string|array|Status Must return as documented for HTMLForm::trySubmit
137     */
138    abstract public function onSubmit( $data );
139
140    /**
141     * Do something exciting on successful processing of the form.  This might be to show
142     * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
143     * protect, etc).
144     */
145    abstract public function onSuccess();
146
147    /**
148     * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
149     * some stuff underneath (history etc); to do some processing on submission of that
150     * form (delete, protect, etc) and to do something exciting on 'success', be that
151     * display something new or redirect to somewhere.  Some actions have more exotic
152     * behavior, but that's what subclassing is for :D
153     * @stable to override
154     */
155    public function show() {
156        $this->setHeaders();
157
158        // This will throw exceptions if there's a problem
159        $this->checkCanExecute( $this->getUser() );
160
161        $form = $this->getForm();
162        if ( $form->show() ) {
163            $this->onSuccess();
164        }
165    }
166
167    /**
168     * @stable to override
169     * @return bool
170     */
171    public function doesWrites() {
172        return true;
173    }
174}