MediaWiki  master
FormAction.php
Go to the documentation of this file.
1 <?php
28 abstract class FormAction extends Action {
29 
34  protected function getFormFields() {
35  // Default to an empty form with just a submit button
36  return [];
37  }
38 
43  protected function preText() {
44  return '';
45  }
46 
50  protected function postText() {
51  return '';
52  }
53 
58  protected function alterForm( HTMLForm $form ) {
59  }
60 
65  protected function usesOOUI() {
66  return false;
67  }
68 
73  protected function getForm() {
74  $this->fields = $this->getFormFields();
75 
76  // Give hooks a chance to alter the form, adding extra fields or text etc
77  $this->getHookRunner()->onActionModifyFormFields(
78  $this->getName(),
79  $this->fields,
80  $this->getArticle()
81  );
82 
83  if ( $this->usesOOUI() ) {
84  $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
85  } else {
86  $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
87  }
88  $form->setSubmitCallback( [ $this, 'onSubmit' ] );
89 
90  $title = $this->getTitle();
91  $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
92  // Retain query parameters (uselang etc)
93  $params = array_diff_key(
94  $this->getRequest()->getQueryValues(),
95  [ 'action' => null, 'title' => null ]
96  );
97  if ( $params ) {
98  $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
99  }
100 
101  $form->addPreText( $this->preText() );
102  $form->addPostText( $this->postText() );
103  $this->alterForm( $form );
104 
105  // Give hooks a chance to alter the form, adding extra fields or text etc
106  $this->getHookRunner()->onActionBeforeFormDisplay(
107  $this->getName(),
108  $form,
109  $this->getArticle()
110  );
111 
112  return $form;
113  }
114 
128  abstract public function onSubmit( $data );
129 
135  abstract public function onSuccess();
136 
144  public function show() {
145  $this->setHeaders();
146 
147  // This will throw exceptions if there's a problem
148  $this->checkCanExecute( $this->getUser() );
149 
150  $form = $this->getForm();
151  if ( $form->show() ) {
152  $this->onSuccess();
153  }
154  }
155 
156  public function doesWrites() {
157  return true;
158  }
159 }
Action\setHeaders
setHeaders()
Set output headers for noindexing etc.
Definition: Action.php:463
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:227
FormAction\usesOOUI
usesOOUI()
Whether the form should use OOUI.
Definition: FormAction.php:65
FormAction
An action which shows a form and does something based on the input from the form.
Definition: FormAction.php:28
Action\getName
getName()
Return the name of the action this object responds to.
FormAction\postText
postText()
Definition: FormAction.php:50
FormAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
FormAction\preText
preText()
Add pre- or post-text to the form.
Definition: FormAction.php:43
Action
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition: Action.php:41
FormAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Action\getContext
getContext()
Get the IContextSource in use here.
Definition: Action.php:213
Action\getArticle
getArticle()
Get a Article object.
Definition: Action.php:287
Action\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: Action.php:405
$title
$title
Definition: testCompression.php:38
FormAction\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: FormAction.php:34
FormAction\getForm
getForm()
Get the HTMLForm to control behavior.
Definition: FormAction.php:73
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:247
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:297
Action\getHookRunner
getHookRunner()
Definition: Action.php:327
FormAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: FormAction.php:58
FormAction\doesWrites
doesWrites()
Indicates whether this action may perform database writes.
Definition: FormAction.php:156
FormAction\show
show()
The basic pattern for actions is to display some sort of HTMLForm UI, maybe with some stuff underneat...
Definition: FormAction.php:144
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:311
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:133
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