MediaWiki  1.29.2
FormSpecialPage.php
Go to the documentation of this file.
1 <?php
31 abstract class FormSpecialPage extends SpecialPage {
36  protected $par = null;
37 
42  abstract protected function getFormFields();
43 
48  protected function preText() {
49  return '';
50  }
51 
56  protected function postText() {
57  return '';
58  }
59 
64  protected function alterForm( HTMLForm $form ) {
65  }
66 
73  protected function getMessagePrefix() {
74  return strtolower( $this->getName() );
75  }
76 
83  protected function getDisplayFormat() {
84  return 'table';
85  }
86 
91  protected function getForm() {
92  $form = HTMLForm::factory(
93  $this->getDisplayFormat(),
94  $this->getFormFields(),
95  $this->getContext(),
96  $this->getMessagePrefix()
97  );
98  $form->setSubmitCallback( [ $this, 'onSubmit' ] );
99  if ( $this->getDisplayFormat() !== 'ooui' ) {
100  // No legend and wrapper by default in OOUI forms, but can be set manually
101  // from alterForm()
102  $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
103  }
104 
105  $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
106  if ( !$headerMsg->isDisabled() ) {
107  $form->addHeaderText( $headerMsg->parseAsBlock() );
108  }
109 
110  $form->addPreText( $this->preText() );
111  $form->addPostText( $this->postText() );
112  $this->alterForm( $form );
113  if ( $form->getMethod() == 'post' ) {
114  // Retain query parameters (uselang etc) on POST requests
115  $params = array_diff_key(
116  $this->getRequest()->getQueryValues(), [ 'title' => null ] );
117  $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
118  }
119 
120  // Give hooks a chance to alter the form, adding extra fields or text etc
121  Hooks::run( 'SpecialPageBeforeFormDisplay', [ $this->getName(), &$form ] );
122 
123  return $form;
124  }
125 
132  abstract public function onSubmit( array $data /* $form = null */ );
133 
139  public function onSuccess() {
140  }
141 
147  public function execute( $par ) {
148  $this->setParameter( $par );
149  $this->setHeaders();
150 
151  // This will throw exceptions if there's a problem
152  $this->checkExecutePermissions( $this->getUser() );
153 
154  $form = $this->getForm();
155  if ( $form->show() ) {
156  $this->onSuccess();
157  }
158  }
159 
164  protected function setParameter( $par ) {
165  $this->par = $par;
166  }
167 
174  protected function checkExecutePermissions( User $user ) {
175  $this->checkPermissions();
176 
177  if ( $this->requiresUnblock() && $user->isBlocked() ) {
178  $block = $user->getBlock();
179  throw new UserBlockedError( $block );
180  }
181 
182  if ( $this->requiresWrite() ) {
183  $this->checkReadOnly();
184  }
185  }
186 
191  public function requiresWrite() {
192  return true;
193  }
194 
199  public function requiresUnblock() {
200  return true;
201  }
202 }
FormSpecialPage\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: FormSpecialPage.php:139
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:64
FormSpecialPage\requiresUnblock
requiresUnblock()
Whether this action cannot be executed by a blocked user.
Definition: FormSpecialPage.php:199
FormSpecialPage\onSubmit
onSubmit(array $data)
Process the form on POST submission.
FormSpecialPage\getForm
getForm()
Get the HTMLForm to control behavior.
Definition: FormSpecialPage.php:91
UserBlockedError
Show an error when the user tries to do something whilst blocked.
Definition: UserBlockedError.php:27
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:306
FormSpecialPage\requiresWrite
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: FormSpecialPage.php:191
FormSpecialPage\setParameter
setParameter( $par)
Maybe do something interesting with the subpage parameter.
Definition: FormSpecialPage.php:164
$params
$params
Definition: styleTest.css.php:40
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:48
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:150
FormSpecialPage\postText
postText()
Add post-text to the form.
Definition: FormSpecialPage.php:56
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
FormSpecialPage\getMessagePrefix
getMessagePrefix()
Get message prefix for HTMLForm.
Definition: FormSpecialPage.php:73
FormSpecialPage\checkExecutePermissions
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
Definition: FormSpecialPage.php:174
FormSpecialPage\execute
execute( $par)
Basic SpecialPage workflow: get a form, send it to the user; get some data back,.
Definition: FormSpecialPage.php:147
HTMLForm\factory
static factory( $displayFormat)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:277
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:484
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:685
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:648
FormSpecialPage\getDisplayFormat
getDisplayFormat()
Get display format for the form.
Definition: FormSpecialPage.php:83
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:746
FormSpecialPage\$par
string $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:36
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:665
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:319
User\isBlocked
isBlocked( $bFromSlave=true)
Check if user is blocked.
Definition: User.php:2043
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
array
the array() calling protocol came about after MediaWiki 1.4rc1.
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:128
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:408