MediaWiki master
FormAction.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Actions;
24
27
35abstract class FormAction extends Action {
36
42 protected function getFormFields() {
43 // Default to an empty form with just a submit button
44 return [];
45 }
46
52 protected function preText() {
53 return '';
54 }
55
60 protected function postText() {
61 return '';
62 }
63
69 protected function alterForm( HTMLForm $form ) {
70 }
71
77 protected function usesOOUI() {
78 return false;
79 }
80
86 protected function getForm() {
87 $this->fields = $this->getFormFields();
88
89 // Give hooks a chance to alter the form, adding extra fields or text etc
90 $this->getHookRunner()->onActionModifyFormFields(
91 $this->getName(),
92 $this->fields,
93 $this->getArticle()
94 );
95
96 if ( $this->usesOOUI() ) {
97 $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
98 } else {
99 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
100 }
101 $form->setSubmitCallback( [ $this, 'onSubmit' ] );
102
103 $title = $this->getTitle();
104 $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
105 // Retain query parameters (uselang etc)
106 $params = array_diff_key(
107 $this->getRequest()->getQueryValues(),
108 [ 'action' => null, 'title' => null ]
109 );
110 if ( $params ) {
111 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
112 }
113
114 $form->addPreHtml( $this->preText() );
115 $form->addPostHtml( $this->postText() );
116 $this->alterForm( $form );
117
118 // Give hooks a chance to alter the form, adding extra fields or text etc
119 $this->getHookRunner()->onActionBeforeFormDisplay(
120 $this->getName(),
121 $form,
122 $this->getArticle()
123 );
124
125 return $form;
126 }
127
141 abstract public function onSubmit( $data );
142
148 abstract public function onSuccess();
149
158 public function show() {
159 $this->setHeaders();
160
161 // This will throw exceptions if there's a problem
162 $this->checkCanExecute( $this->getUser() );
163
164 $form = $this->getForm();
165 if ( $form->show() ) {
166 $this->onSuccess();
167 }
168 }
169
174 public function doesWrites() {
175 return true;
176 }
177}
178
180class_alias( FormAction::class, 'FormAction' );
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:66
getContext()
Get the IContextSource in use here.
Definition Action.php:132
setHeaders()
Set output headers for noindexing etc.
Definition Action.php:420
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:166
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition Action.php:340
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:226
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:146
getArticle()
Get a Article object.
Definition Action.php:216
getName()
Return the name of the action this object responds to.
An action which shows a form and does something based on the input from the form.
usesOOUI()
Whether the form should use OOUI.
getFormFields()
Get an HTMLForm descriptor array.
getForm()
Get the HTMLForm to control behavior.
onSubmit( $data)
Process the form on POST submission.
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.
onSuccess()
Do something exciting on successful processing of the form.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54