MediaWiki master
FormSpecialPage.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\SpecialPage;
11
18
26abstract class FormSpecialPage extends SpecialPage {
31 protected $par = null;
32
37 protected $reauthPostData = null;
38
43 abstract protected function getFormFields();
44
50 protected function preHtml() {
51 return '';
52 }
53
59 protected function postHtml() {
60 return '';
61 }
62
66 protected function alterForm( HTMLForm $form ) {
67 }
68
75 protected function getMessagePrefix() {
76 return strtolower( $this->getName() );
77 }
78
85 protected function getDisplayFormat() {
86 return 'table';
87 }
88
93 protected function getForm() {
94 $context = $this->getContext();
95 $onSubmit = $this->onSubmit( ... );
96
97 if ( $this->reauthPostData ) {
98 // Restore POST data
99 $context = new DerivativeContext( $context );
100 $oldRequest = $this->getRequest();
101 $context->setRequest( new DerivativeRequest(
102 $oldRequest, $this->reauthPostData + $oldRequest->getQueryValues(), true
103 ) );
104
105 // But don't treat it as a "real" submission just in case of some
106 // crazy kind of CSRF.
107 $onSubmit = static function () {
108 return false;
109 };
110 }
111
112 $form = HTMLForm::factory(
113 $this->getDisplayFormat(),
114 $this->getFormFields(),
115 $context,
116 $this->getMessagePrefix()
117 );
118 if ( !$this->requiresPost() ) {
119 $form->setMethod( 'get' );
120 }
121 $form->setSubmitCallback( $onSubmit );
122 if ( $this->getDisplayFormat() !== 'ooui' ) {
123 // No legend and wrapper by default in OOUI forms, but can be set manually
124 // from alterForm()
125 $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
126 }
127
128 $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
129 if ( !$headerMsg->isDisabled() ) {
130 $form->addHeaderHtml( $headerMsg->parseAsBlock() );
131 }
132
133 $form->addPreHtml( $this->preHtml() );
134 $form->addPostHtml( $this->postHtml() );
135
136 // Give precedence to subpage syntax
137 $field = $this->getSubpageField();
138 // cast to string so that "0" is not thrown away
139 if ( strval( $this->par ) !== '' && $field ) {
140 $this->getRequest()->setVal( $form->getField( $field )->getName(), $this->par );
141 $form->setTitle( $this->getPageTitle() );
142 }
143 $this->alterForm( $form );
144 if ( $form->getMethod() == 'post' ) {
145 // Retain query parameters (uselang etc) on POST requests
146 $params = array_diff_key(
147 $this->getRequest()->getQueryValues(), [ 'title' => null ] );
148 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
149 }
150
151 // Give hooks a chance to alter the form, adding extra fields or text etc
152 $this->getHookRunner()->onSpecialPageBeforeFormDisplay( $this->getName(), $form );
153
154 return $form;
155 }
156
166 abstract public function onSubmit( array $data /* HTMLForm $form = null */ );
167
173 public function onSuccess() {
174 }
175
181 public function execute( $par ) {
182 $this->setParameter( $par );
183 $this->setHeaders();
184 $this->outputHeader();
185
186 // This will throw exceptions if there's a problem
187 $this->checkExecutePermissions( $this->getUser() );
188
189 $securityLevel = $this->getLoginSecurityLevel();
190 if ( $securityLevel !== false && !$this->checkLoginSecurityLevel( $securityLevel ) ) {
191 return;
192 }
193
194 $form = $this->getForm();
195 // GET forms can be set as includable
196 if ( !$this->including() ) {
197 $result = $this->getShowAlways() ? $form->showAlways() : $form->show();
198 } else {
199 $result = $form->prepareForm()->tryAuthorizedSubmit();
200 }
201 if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
202 $this->onSuccess();
203 }
204 }
205
211 protected function getShowAlways() {
212 return false;
213 }
214
219 protected function setParameter( $par ) {
220 $this->par = $par;
221 }
222
228 protected function getSubpageField() {
229 return false;
230 }
231
238 protected function checkExecutePermissions( User $user ) {
239 $this->checkPermissions();
240
241 if ( $this->requiresUnblock() ) {
242 $block = $user->getBlock();
243 if ( $block && $block->isSitewide() ) {
244 throw new UserBlockedError(
245 $block,
246 $user,
247 $this->getLanguage(),
248 $this->getRequest()->getIP()
249 );
250 }
251 }
252
253 if ( $this->requiresWrite() ) {
254 $this->checkReadOnly();
255 }
256 }
257
263 public function requiresPost() {
264 return true;
265 }
266
271 public function requiresWrite() {
272 return $this->requiresPost();
273 }
274
279 public function requiresUnblock() {
280 return $this->requiresPost();
281 }
282
289 protected function setReauthPostData( array $data ) {
290 $this->reauthPostData = $data;
291 }
292}
293
295class_alias( FormSpecialPage::class, 'FormSpecialPage' );
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
An IContextSource implementation which will inherit context from another source but allow individual ...
Show an error when the user tries to do something whilst blocked.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
Similar to MediaWiki\Request\FauxRequest, but only fakes URL parameters and method (POST or GET) and ...
Special page which uses an HTMLForm to handle processing.
getMessagePrefix()
Get message prefix for HTMLForm.
requiresUnblock()
Whether this action cannot be executed by a blocked user, default to requiresPost()
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
requiresPost()
Whether this action should using POST method to submit, default to true.
getShowAlways()
Whether the form should always be shown despite the success of submission.
getForm()
Get the HTMLForm to control behavior.
setParameter( $par)
Maybe do something interesting with the subpage parameter.
execute( $par)
Basic SpecialPage workflow: get a form, send it to the user; get some data back,.
requiresWrite()
Whether this action requires the wiki not to be locked, default to requiresPost()
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
setReauthPostData(array $data)
Preserve POST data across reauthentication.
string null $par
The subpage of the special page.
array null $reauthPostData
POST data preserved across re-authentication.
getSubpageField()
Override this function to set the field name used in the subpage syntax.
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
getFormFields()
Get an HTMLForm descriptor array.
preHtml()
Add pre-HTML to the form.
onSubmit(array $data)
Process the form on submission.
getDisplayFormat()
Get display format for the form.
postHtml()
Add post-HTML to the form.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getUser()
Shortcut to get the User executing this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getLoginSecurityLevel()
Tells if the special page does something security-sensitive and needs extra defense against a stolen ...
including( $x=null)
Whether the special page is being evaluated via transclusion.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
getName()
Get the canonical, unlocalized name of this special page without namespace.
checkLoginSecurityLevel( $level=null)
Verifies that the user meets the security level, possibly reauthenticating them in the process.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
User class for the MediaWiki software.
Definition User.php:130
getBlock( $freshness=IDBAccessObject::READ_NORMAL, $disableIpBlockExemptChecking=false)
Get the block affecting the user, or null if the user is not blocked.
Definition User.php:1425
isGood()
Returns whether the operation completed and didn't have any error or warnings.