Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / specialpage / FormSpecialPage.php
blobf727c053815e25222fcb5967c866b085502b3d3d
1 <?php
2 /**
3 * Special page which uses an HTMLForm to handle processing.
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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special page which uses an HTMLForm to handle processing. This is mostly a
26 * clone of FormAction. More special pages should be built this way; maybe this could be
27 * a new structure for SpecialPages.
29 * @ingroup SpecialPage
31 abstract class FormSpecialPage extends SpecialPage {
32 /**
33 * The sub-page of the special page.
34 * @var string
36 protected $par = null;
38 /**
39 * Get an HTMLForm descriptor array
40 * @return array
42 abstract protected function getFormFields();
44 /**
45 * Add pre-text to the form
46 * @return string HTML which will be sent to $form->addPreText()
48 protected function preText() {
49 return '';
52 /**
53 * Add post-text to the form
54 * @return string HTML which will be sent to $form->addPostText()
56 protected function postText() {
57 return '';
60 /**
61 * Play with the HTMLForm if you need to more substantially
62 * @param HTMLForm $form
64 protected function alterForm( HTMLForm $form ) {
67 /**
68 * Get message prefix for HTMLForm
70 * @since 1.21
71 * @return string
73 protected function getMessagePrefix() {
74 return strtolower( $this->getName() );
77 /**
78 * Get display format for the form. See HTMLForm documentation for available values.
80 * @since 1.25
81 * @return string
83 protected function getDisplayFormat() {
84 return 'table';
87 /**
88 * Get the HTMLForm to control behavior
89 * @return HTMLForm|null
91 protected function getForm() {
92 $this->fields = $this->getFormFields();
94 $form = HTMLForm::factory( $this->getDisplayFormat(), $this->fields, $this->getContext(), $this->getMessagePrefix() );
95 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
96 $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
98 $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
99 if ( !$headerMsg->isDisabled() ) {
100 $form->addHeaderText( $headerMsg->parseAsBlock() );
103 // Retain query parameters (uselang etc)
104 $params = array_diff_key(
105 $this->getRequest()->getQueryValues(), array( 'title' => null ) );
106 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
108 $form->addPreText( $this->preText() );
109 $form->addPostText( $this->postText() );
110 $this->alterForm( $form );
112 // Give hooks a chance to alter the form, adding extra fields or text etc
113 Hooks::run( 'SpecialPageBeforeFormDisplay', array( $this->getName(), &$form ) );
115 return $form;
119 * Process the form on POST submission.
120 * @param array $data
121 * @param HTMLForm $form
122 * @return bool|string|array|Status As documented for HTMLForm::trySubmit.
124 abstract public function onSubmit( array $data /* $form = null */ );
127 * Do something exciting on successful processing of the form, most likely to show a
128 * confirmation message
129 * @since 1.22 Default is to do nothing
131 public function onSuccess() {
135 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
137 * @param string $par Subpage string if one was specified
139 public function execute( $par ) {
140 $this->setParameter( $par );
141 $this->setHeaders();
143 // This will throw exceptions if there's a problem
144 $this->checkExecutePermissions( $this->getUser() );
146 $form = $this->getForm();
147 if ( $form->show() ) {
148 $this->onSuccess();
153 * Maybe do something interesting with the subpage parameter
154 * @param string $par
156 protected function setParameter( $par ) {
157 $this->par = $par;
161 * Called from execute() to check if the given user can perform this action.
162 * Failures here must throw subclasses of ErrorPageError.
163 * @param User $user
164 * @throws UserBlockedError
165 * @return bool True
167 protected function checkExecutePermissions( User $user ) {
168 $this->checkPermissions();
170 if ( $this->requiresUnblock() && $user->isBlocked() ) {
171 $block = $user->getBlock();
172 throw new UserBlockedError( $block );
175 if ( $this->requiresWrite() ) {
176 $this->checkReadOnly();
179 return true;
183 * Whether this action requires the wiki not to be locked
184 * @return bool
186 public function requiresWrite() {
187 return true;
191 * Whether this action cannot be executed by a blocked user
192 * @return bool
194 public function requiresUnblock() {
195 return true;