Display the file sha1 value in the file info page
[lhc/web/wiklou.git] / includes / actions / FormAction.php
1 <?php
2 /**
3 * Base classes for actions done on pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 * @file
20 * @ingroup Actions
21 */
22
23 /**
24 * An action which shows a form and does something based on the input from the form
25 *
26 * @ingroup Actions
27 */
28 abstract class FormAction extends Action {
29
30 /**
31 * Get an HTMLForm descriptor array
32 * @return array
33 */
34 protected function getFormFields() {
35 // Default to an empty form with just a submit button
36 return [];
37 }
38
39 /**
40 * Add pre- or post-text to the form
41 * @return string HTML which will be sent to $form->addPreText()
42 */
43 protected function preText() {
44 return '';
45 }
46
47 /**
48 * @return string
49 */
50 protected function postText() {
51 return '';
52 }
53
54 /**
55 * Play with the HTMLForm if you need to more substantially
56 * @param HTMLForm $form
57 */
58 protected function alterForm( HTMLForm $form ) {
59 }
60
61 /**
62 * Whether the form should use OOUI
63 * @return bool
64 */
65 protected function usesOOUI() {
66 return false;
67 }
68
69 /**
70 * Get the HTMLForm to control behavior
71 * @return HTMLForm|null
72 */
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 Hooks::run( 'ActionModifyFormFields', [ $this->getName(), &$this->fields, $this->page ] );
78
79 if ( $this->usesOOUI() ) {
80 $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
81 } else {
82 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
83 }
84 $form->setSubmitCallback( [ $this, 'onSubmit' ] );
85
86 $title = $this->getTitle();
87 $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
88 // Retain query parameters (uselang etc)
89 $params = array_diff_key(
90 $this->getRequest()->getQueryValues(),
91 [ 'action' => null, 'title' => null ]
92 );
93 if ( $params ) {
94 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
95 }
96
97 $form->addPreText( $this->preText() );
98 $form->addPostText( $this->postText() );
99 $this->alterForm( $form );
100
101 // Give hooks a chance to alter the form, adding extra fields or text etc
102 Hooks::run( 'ActionBeforeFormDisplay', [ $this->getName(), &$form, $this->page ] );
103
104 return $form;
105 }
106
107 /**
108 * Process the form on POST submission.
109 *
110 * If you don't want to do anything with the form, just return false here.
111 *
112 * @param array $data
113 * @return bool|array True for success, false for didn't-try, array of errors on failure
114 */
115 abstract public function onSubmit( $data );
116
117 /**
118 * Do something exciting on successful processing of the form. This might be to show
119 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
120 * protect, etc).
121 */
122 abstract public function onSuccess();
123
124 /**
125 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
126 * some stuff underneath (history etc); to do some processing on submission of that
127 * form (delete, protect, etc) and to do something exciting on 'success', be that
128 * display something new or redirect to somewhere. Some actions have more exotic
129 * behavior, but that's what subclassing is for :D
130 */
131 public function show() {
132 $this->setHeaders();
133
134 // This will throw exceptions if there's a problem
135 $this->checkCanExecute( $this->getUser() );
136
137 $form = $this->getForm();
138 if ( $form->show() ) {
139 $this->onSuccess();
140 }
141 }
142
143 public function doesWrites() {
144 return true;
145 }
146 }