MediaWiki REL1_41
FormAction.php
Go to the documentation of this file.
1<?php
24
32abstract class FormAction extends Action {
33
39 protected function getFormFields() {
40 // Default to an empty form with just a submit button
41 return [];
42 }
43
49 protected function preText() {
50 return '';
51 }
52
57 protected function postText() {
58 return '';
59 }
60
66 protected function alterForm( HTMLForm $form ) {
67 }
68
74 protected function usesOOUI() {
75 return false;
76 }
77
83 protected function getForm() {
84 $this->fields = $this->getFormFields();
85
86 // Give hooks a chance to alter the form, adding extra fields or text etc
87 $this->getHookRunner()->onActionModifyFormFields(
88 $this->getName(),
89 $this->fields,
90 $this->getArticle()
91 );
92
93 if ( $this->usesOOUI() ) {
94 $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
95 } else {
96 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
97 }
98 $form->setSubmitCallback( [ $this, 'onSubmit' ] );
99
100 $title = $this->getTitle();
101 $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
102 // Retain query parameters (uselang etc)
103 $params = array_diff_key(
104 $this->getRequest()->getQueryValues(),
105 [ 'action' => null, 'title' => null ]
106 );
107 if ( $params ) {
108 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
109 }
110
111 $form->addPreText( $this->preText() );
112 $form->addPostText( $this->postText() );
113 $this->alterForm( $form );
114
115 // Give hooks a chance to alter the form, adding extra fields or text etc
116 $this->getHookRunner()->onActionBeforeFormDisplay(
117 $this->getName(),
118 $form,
119 $this->getArticle()
120 );
121
122 return $form;
123 }
124
138 abstract public function onSubmit( $data );
139
145 abstract public function onSuccess();
146
155 public function show() {
156 $this->setHeaders();
157
158 // This will throw exceptions if there's a problem
159 $this->checkCanExecute( $this->getUser() );
160
161 $form = $this->getForm();
162 if ( $form->show() ) {
163 $this->onSuccess();
164 }
165 }
166
171 public function doesWrites() {
172 return true;
173 }
174}
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition Action.php:49
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition Action.php:321
getHookRunner()
Definition Action.php:253
getName()
Return the name of the action this object responds to.
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:209
getContext()
Get the IContextSource in use here.
Definition Action.php:115
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:149
setHeaders()
Set output headers for noindexing etc.
Definition Action.php:395
getArticle()
Get a Article object.
Definition Action.php:199
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:129
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:158
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:58