MediaWiki  1.34.0
FormSpecialPage.php
Go to the documentation of this file.
1 <?php
31 abstract class FormSpecialPage extends SpecialPage {
36  protected $par = null;
37 
42  protected $reauthPostData = null;
43 
48  abstract protected function getFormFields();
49 
54  protected function preText() {
55  return '';
56  }
57 
62  protected function postText() {
63  return '';
64  }
65 
70  protected function alterForm( HTMLForm $form ) {
71  }
72 
79  protected function getMessagePrefix() {
80  return strtolower( $this->getName() );
81  }
82 
89  protected function getDisplayFormat() {
90  return 'table';
91  }
92 
97  protected function getForm() {
98  $context = $this->getContext();
99  $onSubmit = [ $this, 'onSubmit' ];
100 
101  if ( $this->reauthPostData ) {
102  // Restore POST data
104  $oldRequest = $this->getRequest();
105  $context->setRequest( new DerivativeRequest(
106  $oldRequest, $this->reauthPostData + $oldRequest->getQueryValues(), true
107  ) );
108 
109  // But don't treat it as a "real" submission just in case of some
110  // crazy kind of CSRF.
111  $onSubmit = function () {
112  return false;
113  };
114  }
115 
116  $form = HTMLForm::factory(
117  $this->getDisplayFormat(),
118  $this->getFormFields(),
119  $context,
120  $this->getMessagePrefix()
121  );
122  $form->setSubmitCallback( $onSubmit );
123  if ( $this->getDisplayFormat() !== 'ooui' ) {
124  // No legend and wrapper by default in OOUI forms, but can be set manually
125  // from alterForm()
126  $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
127  }
128 
129  $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
130  if ( !$headerMsg->isDisabled() ) {
131  $form->addHeaderText( $headerMsg->parseAsBlock() );
132  }
133 
134  $form->addPreText( $this->preText() );
135  $form->addPostText( $this->postText() );
136  $this->alterForm( $form );
137  if ( $form->getMethod() == 'post' ) {
138  // Retain query parameters (uselang etc) on POST requests
139  $params = array_diff_key(
140  $this->getRequest()->getQueryValues(), [ 'title' => null ] );
141  $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
142  }
143 
144  // Give hooks a chance to alter the form, adding extra fields or text etc
145  Hooks::run( 'SpecialPageBeforeFormDisplay', [ $this->getName(), &$form ] );
146 
147  return $form;
148  }
149 
157  abstract public function onSubmit( array $data /* HTMLForm $form = null */ );
158 
164  public function onSuccess() {
165  }
166 
172  public function execute( $par ) {
173  $this->setParameter( $par );
174  $this->setHeaders();
175 
176  // This will throw exceptions if there's a problem
177  $this->checkExecutePermissions( $this->getUser() );
178 
179  $securityLevel = $this->getLoginSecurityLevel();
180  if ( $securityLevel !== false && !$this->checkLoginSecurityLevel( $securityLevel ) ) {
181  return;
182  }
183 
184  $form = $this->getForm();
185  if ( $form->show() ) {
186  $this->onSuccess();
187  }
188  }
189 
194  protected function setParameter( $par ) {
195  $this->par = $par;
196  }
197 
204  protected function checkExecutePermissions( User $user ) {
205  $this->checkPermissions();
206 
207  if ( $this->requiresUnblock() ) {
208  $block = $user->getBlock();
209  if ( $block && $block->isSitewide() ) {
210  throw new UserBlockedError( $block );
211  }
212  }
213 
214  if ( $this->requiresWrite() ) {
215  $this->checkReadOnly();
216  }
217  }
218 
223  public function requiresWrite() {
224  return true;
225  }
226 
231  public function requiresUnblock() {
232  return true;
233  }
234 
241  protected function setReauthPostData( array $data ) {
242  $this->reauthPostData = $data;
243  }
244 }
FormSpecialPage\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: FormSpecialPage.php:164
DerivativeRequest
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...
Definition: DerivativeRequest.php:34
FormSpecialPage\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
FormSpecialPage\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: FormSpecialPage.php:70
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
FormSpecialPage\requiresUnblock
requiresUnblock()
Whether this action cannot be executed by a blocked user.
Definition: FormSpecialPage.php:231
FormSpecialPage\onSubmit
onSubmit(array $data)
Process the form on POST submission.
FormSpecialPage\getForm
getForm()
Get the HTMLForm to control behavior.
Definition: FormSpecialPage.php:97
UserBlockedError
Show an error when the user tries to do something whilst blocked.
Definition: UserBlockedError.php:29
true
return true
Definition: router.php:92
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:315
FormSpecialPage\requiresWrite
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: FormSpecialPage.php:223
FormSpecialPage\setParameter
setParameter( $par)
Maybe do something interesting with the subpage parameter.
Definition: FormSpecialPage.php:194
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
FormSpecialPage\preText
preText()
Add pre-text to the form.
Definition: FormSpecialPage.php:54
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:153
FormSpecialPage\postText
postText()
Add post-text to the form.
Definition: FormSpecialPage.php:62
FormSpecialPage\getMessagePrefix
getMessagePrefix()
Get message prefix for HTMLForm.
Definition: FormSpecialPage.php:79
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
FormSpecialPage\$reauthPostData
array null $reauthPostData
POST data preserved across re-authentication.
Definition: FormSpecialPage.php:42
SpecialPage\checkLoginSecurityLevel
checkLoginSecurityLevel( $level=null)
Verifies that the user meets the security level, possibly reauthenticating them in the process.
Definition: SpecialPage.php:406
FormSpecialPage\checkExecutePermissions
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
Definition: FormSpecialPage.php:204
FormSpecialPage\execute
execute( $par)
Basic SpecialPage workflow: get a form, send it to the user; get some data back,.
Definition: FormSpecialPage.php:172
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:537
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:729
User\getBlock
getBlock( $fromReplica=true)
Get the block affecting the user, or null if the user is not blocked.
Definition: User.php:2068
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
FormSpecialPage\$par
string null $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
FormSpecialPage\getDisplayFormat
getDisplayFormat()
Get display format for the form.
Definition: FormSpecialPage.php:89
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:37
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
SpecialPage\getLoginSecurityLevel
getLoginSecurityLevel()
Tells if the special page does something security-sensitive and needs extra defense against a stolen ...
Definition: SpecialPage.php:360
$context
$context
Definition: load.php:45
FormSpecialPage\setReauthPostData
setReauthPostData(array $data)
Preserve POST data across reauthentication.
Definition: FormSpecialPage.php:241
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:328
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:303
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131
wfArrayToCgi
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Definition: GlobalFunctions.php:347