MediaWiki master
FormAction.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Actions;
11
14
22abstract class FormAction extends Action {
23
29 protected function getFormFields() {
30 // Default to an empty form with just a submit button
31 return [];
32 }
33
39 protected function preText() {
40 return '';
41 }
42
47 protected function postText() {
48 return '';
49 }
50
56 protected function alterForm( HTMLForm $form ) {
57 }
58
64 protected function usesOOUI() {
65 return false;
66 }
67
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->addPreHtml( $this->preText() );
102 $form->addPostHtml( $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
145 public function show() {
146 $this->setHeaders();
147
148 // This will throw exceptions if there's a problem
149 $this->checkCanExecute( $this->getUser() );
150
151 $form = $this->getForm();
152 if ( $form->show() ) {
153 $this->onSuccess();
154 }
155 }
156
161 public function doesWrites() {
162 return true;
163 }
164}
165
167class_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:53
getContext()
Get the IContextSource in use here.
Definition Action.php:119
setHeaders()
Set output headers for noindexing etc.
Definition Action.php:407
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:153
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition Action.php:327
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:213
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:133
getArticle()
Get a Article object.
Definition Action.php:203
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:195
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44