MediaWiki  master
FormAction.php
Go to the documentation of this file.
1 <?php
30 abstract class FormAction extends Action {
31 
37  protected function getFormFields() {
38  // Default to an empty form with just a submit button
39  return [];
40  }
41 
47  protected function preText() {
48  return '';
49  }
50 
55  protected function postText() {
56  return '';
57  }
58 
64  protected function alterForm( HTMLForm $form ) {
65  }
66 
72  protected function usesOOUI() {
73  return false;
74  }
75 
81  protected function getForm() {
82  $this->fields = $this->getFormFields();
83 
84  // Give hooks a chance to alter the form, adding extra fields or text etc
85  $this->getHookRunner()->onActionModifyFormFields(
86  $this->getName(),
87  $this->fields,
88  $this->getArticle()
89  );
90 
91  if ( $this->usesOOUI() ) {
92  $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
93  } else {
94  $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
95  }
96  $form->setSubmitCallback( [ $this, 'onSubmit' ] );
97 
98  $title = $this->getTitle();
99  $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
100  // Retain query parameters (uselang etc)
101  $params = array_diff_key(
102  $this->getRequest()->getQueryValues(),
103  [ 'action' => null, 'title' => null ]
104  );
105  if ( $params ) {
106  $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
107  }
108 
109  $form->addPreText( $this->preText() );
110  $form->addPostText( $this->postText() );
111  $this->alterForm( $form );
112 
113  // Give hooks a chance to alter the form, adding extra fields or text etc
114  $this->getHookRunner()->onActionBeforeFormDisplay(
115  $this->getName(),
116  $form,
117  $this->getArticle()
118  );
119 
120  return $form;
121  }
122 
136  abstract public function onSubmit( $data );
137 
143  abstract public function onSuccess();
144 
153  public function show() {
154  $this->setHeaders();
155 
156  // This will throw exceptions if there's a problem
157  $this->checkCanExecute( $this->getUser() );
158 
159  $form = $this->getForm();
160  if ( $form->show() ) {
161  $this->onSuccess();
162  }
163  }
164 
169  public function doesWrites() {
170  return true;
171  }
172 }
Action\setHeaders
setHeaders()
Set output headers for noindexing etc.
Definition: Action.php:472
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:229
FormAction\usesOOUI
usesOOUI()
Whether the form should use OOUI Stable to override.
Definition: FormAction.php:72
FormAction
An action which shows a form and does something based on the input from the form.
Definition: FormAction.php:30
Action\getName
getName()
Return the name of the action this object responds to.
FormAction\postText
postText()
Stable to override.
Definition: FormAction.php:55
FormAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
FormAction\preText
preText()
Add pre- or post-text to the form Stable to override.
Definition: FormAction.php:47
Action
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition: Action.php:43
FormAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Action\getContext
getContext()
Get the IContextSource in use here.
Definition: Action.php:215
Action\getArticle
getArticle()
Get a Article object.
Definition: Action.php:289
Action\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: Action.php:411
$title
$title
Definition: testCompression.php:38
FormAction\getFormFields
getFormFields()
Get an HTMLForm descriptor array Stable to override.
Definition: FormAction.php:37
FormAction\getForm
getForm()
Get the HTMLForm to control behavior Stable to override.
Definition: FormAction.php:81
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:249
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:299
Action\getHookRunner
getHookRunner()
Definition: Action.php:329
FormAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially Stable to override.
Definition: FormAction.php:64
FormAction\doesWrites
doesWrites()
Stable to override.
Definition: FormAction.php:169
FormAction\show
show()
The basic pattern for actions is to display some sort of HTMLForm UI, maybe with some stuff underneat...
Definition: FormAction.php:153
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:315
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:135
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:346