Merge "resourceloader: Optimize module registry sent in the startup module"
[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 */
21
22 /**
23 * @defgroup Actions Action done on pages
24 */
25
26 /**
27 * An action which shows a form and does something based on the input from the form
28 */
29 abstract class FormAction extends Action {
30
31 /**
32 * Get an HTMLForm descriptor array
33 * @return array
34 */
35 abstract protected function getFormFields();
36
37 /**
38 * Add pre- or post-text to the form
39 * @return string HTML which will be sent to $form->addPreText()
40 */
41 protected function preText() {
42 return '';
43 }
44
45 /**
46 * @return string
47 */
48 protected function postText() {
49 return '';
50 }
51
52 /**
53 * Play with the HTMLForm if you need to more substantially
54 * @param HTMLForm $form
55 */
56 protected function alterForm( HTMLForm $form ) {
57 }
58
59 /**
60 * Get the HTMLForm to control behavior
61 * @return HTMLForm|null
62 */
63 protected function getForm() {
64 $this->fields = $this->getFormFields();
65
66 // Give hooks a chance to alter the form, adding extra fields or text etc
67 wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields, $this->page ) );
68
69 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
70 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
71
72 // Retain query parameters (uselang etc)
73 $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
74 $params = array_diff_key(
75 $this->getRequest()->getQueryValues(),
76 array( 'action' => null, 'title' => null )
77 );
78 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
79
80 $form->addPreText( $this->preText() );
81 $form->addPostText( $this->postText() );
82 $this->alterForm( $form );
83
84 // Give hooks a chance to alter the form, adding extra fields or text etc
85 wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page ) );
86
87 return $form;
88 }
89
90 /**
91 * Process the form on POST submission. If you return false from getFormFields(),
92 * this will obviously never be reached. If you don't want to do anything with the
93 * form, just return false here
94 * @param array $data
95 * @return bool|array True for success, false for didn't-try, array of errors on failure
96 */
97 abstract public function onSubmit( $data );
98
99 /**
100 * Do something exciting on successful processing of the form. This might be to show
101 * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
102 * protect, etc).
103 */
104 abstract public function onSuccess();
105
106 /**
107 * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
108 * some stuff underneath (history etc); to do some processing on submission of that
109 * form (delete, protect, etc) and to do something exciting on 'success', be that
110 * display something new or redirect to somewhere. Some actions have more exotic
111 * behavior, but that's what subclassing is for :D
112 */
113 public function show() {
114 $this->setHeaders();
115
116 // This will throw exceptions if there's a problem
117 $this->checkCanExecute( $this->getUser() );
118
119 $form = $this->getForm();
120 if ( $form->show() ) {
121 $this->onSuccess();
122 }
123 }
124
125 /**
126 * @see Action::execute()
127 *
128 * @param array|null $data
129 * @param bool $captureErrors
130 * @throws ErrorPageError|Exception
131 * @return bool
132 */
133 public function execute( array $data = null, $captureErrors = true ) {
134 try {
135 // Set a new context so output doesn't leak.
136 $this->context = clone $this->getContext();
137
138 // This will throw exceptions if there's a problem
139 $this->checkCanExecute( $this->getUser() );
140
141 $fields = array();
142 foreach ( $this->fields as $key => $params ) {
143 if ( isset( $data[$key] ) ) {
144 $fields[$key] = $data[$key];
145 } elseif ( isset( $params['default'] ) ) {
146 $fields[$key] = $params['default'];
147 } else {
148 $fields[$key] = null;
149 }
150 }
151 $status = $this->onSubmit( $fields );
152 if ( $status === true ) {
153 // This might do permanent stuff
154 $this->onSuccess();
155 return true;
156 } else {
157 return false;
158 }
159 }
160 catch ( ErrorPageError $e ) {
161 if ( $captureErrors ) {
162 return false;
163 } else {
164 throw $e;
165 }
166 }
167 }
168 }