Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.22% covered (danger)
16.22%
12 / 74
6.25% covered (danger)
6.25%
1 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormSpecialPage
16.22% covered (danger)
16.22%
12 / 74
6.25% covered (danger)
6.25%
1 / 16
713.89
0.00% covered (danger)
0.00%
0 / 1
 getFormFields
n/a
0 / 0
n/a
0 / 0
0
 preHtml
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 postHtml
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
 getMessagePrefix
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDisplayFormat
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 / 36
0.00% covered (danger)
0.00%
0 / 1
72
 onSubmit
n/a
0 / 0
n/a
0 / 0
0
 onSuccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
72
 getShowAlways
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setParameter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSubpageField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 checkExecutePermissions
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 requiresPost
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 requiresWrite
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 requiresUnblock
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setReauthPostData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Special page which uses an HTMLForm to handle processing.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup SpecialPage
8 */
9
10namespace MediaWiki\SpecialPage;
11
12use MediaWiki\Context\DerivativeContext;
13use MediaWiki\Exception\UserBlockedError;
14use MediaWiki\HTMLForm\HTMLForm;
15use MediaWiki\Request\DerivativeRequest;
16use MediaWiki\Status\Status;
17use MediaWiki\User\User;
18use StatusValue;
19
20/**
21 * Special page which uses an HTMLForm to handle processing.  This is mostly a
22 * clone of FormAction.  More special pages should be built this way; maybe this could be
23 * a new structure for SpecialPages.
24 *
25 * @ingroup SpecialPage
26 */
27abstract class FormSpecialPage extends SpecialPage {
28    /**
29     * The subpage of the special page.
30     * @var string|null
31     */
32    protected $par = null;
33
34    /**
35     * @var array|null POST data preserved across re-authentication
36     * @since 1.32
37     */
38    protected $reauthPostData = null;
39
40    /**
41     * Get an HTMLForm descriptor array
42     * @return array
43     */
44    abstract protected function getFormFields();
45
46    /**
47     * Add pre-HTML to the form
48     * @return string HTML which will be sent to $form->addPreHtml()
49     * @since 1.38
50     */
51    protected function preHtml() {
52        return '';
53    }
54
55    /**
56     * Add post-HTML to the form
57     * @return string HTML which will be sent to $form->addPostHtml()
58     * @since 1.38
59     */
60    protected function postHtml() {
61        return '';
62    }
63
64    /**
65     * Play with the HTMLForm if you need to more substantially
66     */
67    protected function alterForm( HTMLForm $form ) {
68    }
69
70    /**
71     * Get message prefix for HTMLForm
72     *
73     * @since 1.21
74     * @return string
75     */
76    protected function getMessagePrefix() {
77        return strtolower( $this->getName() );
78    }
79
80    /**
81     * Get display format for the form. See HTMLForm documentation for available values.
82     *
83     * @since 1.25
84     * @return string
85     */
86    protected function getDisplayFormat() {
87        return 'table';
88    }
89
90    /**
91     * Get the HTMLForm to control behavior
92     * @return HTMLForm|null
93     */
94    protected function getForm() {
95        $context = $this->getContext();
96        $onSubmit = $this->onSubmit( ... );
97
98        if ( $this->reauthPostData ) {
99            // Restore POST data
100            $context = new DerivativeContext( $context );
101            $oldRequest = $this->getRequest();
102            $context->setRequest( new DerivativeRequest(
103                $oldRequest, $this->reauthPostData + $oldRequest->getQueryValues(), true
104            ) );
105
106            // But don't treat it as a "real" submission just in case of some
107            // crazy kind of CSRF.
108            $onSubmit = static fn () => false;
109        }
110
111        $form = HTMLForm::factory(
112            $this->getDisplayFormat(),
113            $this->getFormFields(),
114            $context,
115            $this->getMessagePrefix()
116        );
117        if ( !$this->requiresPost() ) {
118            $form->setMethod( 'get' );
119        }
120        $form->setSubmitCallback( $onSubmit );
121        if ( $this->getDisplayFormat() !== 'ooui' ) {
122            // No legend and wrapper by default in OOUI forms, but can be set manually
123            // from alterForm()
124            $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
125        }
126
127        $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
128        if ( !$headerMsg->isDisabled() ) {
129            $form->addHeaderHtml( $headerMsg->parseAsBlock() );
130        }
131
132        $form->addPreHtml( $this->preHtml() );
133        $form->addPostHtml( $this->postHtml() );
134
135        // Give precedence to subpage syntax
136        $field = $this->getSubpageField();
137        // cast to string so that "0" is not thrown away
138        if ( strval( $this->par ) !== '' && $field ) {
139            $this->getRequest()->setVal( $form->getField( $field )->getName(), $this->par );
140            $form->setTitle( $this->getPageTitle() );
141        }
142        $this->alterForm( $form );
143        if ( $form->getMethod() == 'post' ) {
144            // Retain query parameters (uselang etc) on POST requests
145            $params = array_diff_key(
146                $this->getRequest()->getQueryValues(), [ 'title' => null ] );
147            $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
148        }
149
150        // Give hooks a chance to alter the form, adding extra fields or text etc
151        $this->getHookRunner()->onSpecialPageBeforeFormDisplay( $this->getName(), $form );
152
153        return $form;
154    }
155
156    /**
157     * Process the form on submission.
158     * @phpcs:disable MediaWiki.Commenting.FunctionComment.ExtraParamComment
159     * @param array $data
160     * @param HTMLForm|null $form
161     * @suppress PhanCommentParamWithoutRealParam Many implementations don't have $form
162     * @return bool|string|array|Status As documented for HTMLForm::trySubmit.
163     * @phpcs:enable MediaWiki.Commenting.FunctionComment.ExtraParamComment
164     */
165    abstract public function onSubmit( array $data /* HTMLForm $form = null */ );
166
167    /**
168     * Do something exciting on successful processing of the form, most likely to show a
169     * confirmation message
170     * @since 1.22 Default is to do nothing
171     */
172    public function onSuccess() {
173    }
174
175    /**
176     * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
177     *
178     * @param string|null $par Subpage string if one was specified
179     */
180    public function execute( $par ) {
181        $this->setParameter( $par );
182        $this->setHeaders();
183        $this->outputHeader();
184
185        // This will throw exceptions if there's a problem
186        $this->checkExecutePermissions( $this->getUser() );
187
188        $securityLevel = $this->getLoginSecurityLevel();
189        if ( $securityLevel !== false && !$this->checkLoginSecurityLevel( $securityLevel ) ) {
190            return;
191        }
192
193        $form = $this->getForm();
194        // GET forms can be set as includable
195        if ( !$this->including() ) {
196            $result = $this->getShowAlways() ? $form->showAlways() : $form->show();
197        } else {
198            $result = $form->prepareForm()->tryAuthorizedSubmit();
199        }
200        if ( $result === true || ( $result instanceof StatusValue && $result->isGood() ) ) {
201            $this->onSuccess();
202        }
203    }
204
205    /**
206     * Whether the form should always be shown despite the success of submission.
207     * @since 1.40
208     * @return bool
209     */
210    protected function getShowAlways() {
211        return false;
212    }
213
214    /**
215     * Maybe do something interesting with the subpage parameter
216     * @param string|null $par
217     */
218    protected function setParameter( $par ) {
219        $this->par = $par;
220    }
221
222    /**
223     * Override this function to set the field name used in the subpage syntax.
224     * @since 1.40
225     * @return false|string
226     */
227    protected function getSubpageField() {
228        return false;
229    }
230
231    /**
232     * Called from execute() to check if the given user can perform this action.
233     * Failures here must throw subclasses of ErrorPageError.
234     * @param User $user
235     * @throws UserBlockedError
236     */
237    protected function checkExecutePermissions( User $user ) {
238        $this->checkPermissions();
239
240        if ( $this->requiresUnblock() ) {
241            $block = $user->getBlock();
242            if ( $block && $block->isSitewide() ) {
243                throw new UserBlockedError(
244                    $block,
245                    $user,
246                    $this->getLanguage(),
247                    $this->getRequest()->getIP()
248                );
249            }
250        }
251
252        if ( $this->requiresWrite() ) {
253            $this->checkReadOnly();
254        }
255    }
256
257    /**
258     * Whether this action should using POST method to submit, default to true
259     * @since 1.40
260     * @return bool
261     */
262    public function requiresPost() {
263        return true;
264    }
265
266    /**
267     * Whether this action requires the wiki not to be locked, default to requiresPost()
268     * @return bool
269     */
270    public function requiresWrite() {
271        return $this->requiresPost();
272    }
273
274    /**
275     * Whether this action cannot be executed by a blocked user, default to requiresPost()
276     * @return bool
277     */
278    public function requiresUnblock() {
279        return $this->requiresPost();
280    }
281
282    /**
283     * Preserve POST data across reauthentication
284     *
285     * @since 1.32
286     * @param array $data
287     */
288    protected function setReauthPostData( array $data ) {
289        $this->reauthPostData = $data;
290    }
291}
292
293// @codeCoverageIgnoreStart
294/** @deprecated class alias since 1.41 */
295class_alias( FormSpecialPage::class, 'FormSpecialPage' );
296// @codeCoverageIgnoreEnd