MediaWiki master
FormAction.php
Go to the documentation of this file.
1<?php
25
33abstract class FormAction extends Action {
34
40 protected function getFormFields() {
41 // Default to an empty form with just a submit button
42 return [];
43 }
44
50 protected function preText() {
51 return '';
52 }
53
58 protected function postText() {
59 return '';
60 }
61
67 protected function alterForm( HTMLForm $form ) {
68 }
69
75 protected function usesOOUI() {
76 return false;
77 }
78
84 protected function getForm() {
85 $this->fields = $this->getFormFields();
86
87 // Give hooks a chance to alter the form, adding extra fields or text etc
88 $this->getHookRunner()->onActionModifyFormFields(
89 $this->getName(),
90 $this->fields,
91 $this->getArticle()
92 );
93
94 if ( $this->usesOOUI() ) {
95 $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
96 } else {
97 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
98 }
99 $form->setSubmitCallback( [ $this, 'onSubmit' ] );
100
101 $title = $this->getTitle();
102 $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
103 // Retain query parameters (uselang etc)
104 $params = array_diff_key(
105 $this->getRequest()->getQueryValues(),
106 [ 'action' => null, 'title' => null ]
107 );
108 if ( $params ) {
109 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
110 }
111
112 $form->addPreHtml( $this->preText() );
113 $form->addPostHtml( $this->postText() );
114 $this->alterForm( $form );
115
116 // Give hooks a chance to alter the form, adding extra fields or text etc
117 $this->getHookRunner()->onActionBeforeFormDisplay(
118 $this->getName(),
119 $form,
120 $this->getArticle()
121 );
122
123 return $form;
124 }
125
139 abstract public function onSubmit( $data );
140
146 abstract public function onSuccess();
147
156 public function show() {
157 $this->setHeaders();
158
159 // This will throw exceptions if there's a problem
160 $this->checkCanExecute( $this->getUser() );
161
162 $form = $this->getForm();
163 if ( $form->show() ) {
164 $this->onSuccess();
165 }
166 }
167
172 public function doesWrites() {
173 return true;
174 }
175}
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
array $params
The job parameters.
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition Action.php:51
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition Action.php:323
getHookRunner()
Definition Action.php:255
getName()
Return the name of the action this object responds to.
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:211
getContext()
Get the IContextSource in use here.
Definition Action.php:117
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:151
setHeaders()
Set output headers for noindexing etc.
Definition Action.php:394
getArticle()
Get a Article object.
Definition Action.php:201
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:131
An action which shows a form and does something based on the input from the form.
onSuccess()
Do something exciting on successful processing of the form.
getFormFields()
Get an HTMLForm descriptor array.
usesOOUI()
Whether the form should use OOUI.
show()
The basic pattern for actions is to display some sort of HTMLForm UI, maybe with some stuff underneat...
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
preText()
Add pre- or post-text to the form.
onSubmit( $data)
Process the form on POST submission.
getForm()
Get the HTMLForm to control behavior.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:208
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54