Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups().
[lhc/web/wiklou.git] / includes / Status.php
1 <?php
2 /**
3 * Generic operation result.
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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Generic operation result class
25 * Has warning/error list, boolean status and arbitrary value
26 *
27 * "Good" means the operation was completed with no warnings or errors.
28 *
29 * "OK" means the operation was partially or wholly completed.
30 *
31 * An operation which is not OK should have errors so that the user can be
32 * informed as to what went wrong. Calling the fatal() function sets an error
33 * message and simultaneously switches off the OK flag.
34 */
35 class Status {
36 var $ok = true;
37 var $value;
38
39 /** Counters for batch operations */
40 public $successCount = 0, $failCount = 0;
41 /** Array to indicate which items of the batch operations were successful */
42 public $success = array();
43
44 /*semi-private*/ var $errors = array();
45 /*semi-private*/ var $cleanCallback = false;
46
47 /**
48 * Factory function for fatal errors
49 *
50 * @param $message String: message name
51 * @return Status
52 */
53 static function newFatal( $message /*, parameters...*/ ) {
54 $params = func_get_args();
55 $result = new self;
56 call_user_func_array( array( &$result, 'error' ), $params );
57 $result->ok = false;
58 return $result;
59 }
60
61 /**
62 * Factory function for good results
63 *
64 * @param $value Mixed
65 * @return Status
66 */
67 static function newGood( $value = null ) {
68 $result = new self;
69 $result->value = $value;
70 return $result;
71 }
72
73 /**
74 * Change operation result
75 *
76 * @param $ok Boolean: whether the operation completed
77 * @param $value Mixed
78 */
79 function setResult( $ok, $value = null ) {
80 $this->ok = $ok;
81 $this->value = $value;
82 }
83
84 /**
85 * Returns whether the operation completed and didn't have any error or
86 * warnings
87 *
88 * @return Boolean
89 */
90 function isGood() {
91 return $this->ok && !$this->errors;
92 }
93
94 /**
95 * Returns whether the operation completed
96 *
97 * @return Boolean
98 */
99 function isOK() {
100 return $this->ok;
101 }
102
103 /**
104 * Add a new warning
105 *
106 * @param $message String: message name
107 */
108 function warning( $message /*, parameters... */ ) {
109 $params = array_slice( func_get_args(), 1 );
110 $this->errors[] = array(
111 'type' => 'warning',
112 'message' => $message,
113 'params' => $params );
114 }
115
116 /**
117 * Add an error, do not set fatal flag
118 * This can be used for non-fatal errors
119 *
120 * @param $message String: message name
121 */
122 function error( $message /*, parameters... */ ) {
123 $params = array_slice( func_get_args(), 1 );
124 $this->errors[] = array(
125 'type' => 'error',
126 'message' => $message,
127 'params' => $params );
128 }
129
130 /**
131 * Add an error and set OK to false, indicating that the operation
132 * as a whole was fatal
133 *
134 * @param $message String: message name
135 */
136 function fatal( $message /*, parameters... */ ) {
137 $params = array_slice( func_get_args(), 1 );
138 $this->errors[] = array(
139 'type' => 'error',
140 'message' => $message,
141 'params' => $params );
142 $this->ok = false;
143 }
144
145 /**
146 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
147 */
148 function __wakeup() {
149 $this->cleanCallback = false;
150 }
151
152 /**
153 * @param $params array
154 * @return array
155 */
156 protected function cleanParams( $params ) {
157 if ( !$this->cleanCallback ) {
158 return $params;
159 }
160 $cleanParams = array();
161 foreach ( $params as $i => $param ) {
162 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
163 }
164 return $cleanParams;
165 }
166
167 /**
168 * @param $item
169 * @return string
170 */
171 protected function getItemXML( $item ) {
172 $params = $this->cleanParams( $item['params'] );
173 $xml = "<{$item['type']}>\n" .
174 Xml::element( 'message', null, $item['message'] ) . "\n" .
175 Xml::element( 'text', null, wfMsg( $item['message'], $params ) ) ."\n";
176 foreach ( $params as $param ) {
177 $xml .= Xml::element( 'param', null, $param );
178 }
179 $xml .= "</{$item['type']}>\n";
180 return $xml;
181 }
182
183 /**
184 * Get the error list as XML
185 * @return string
186 */
187 function getXML() {
188 $xml = "<errors>\n";
189 foreach ( $this->errors as $error ) {
190 $xml .= $this->getItemXML( $error );
191 }
192 $xml .= "</errors>\n";
193 return $xml;
194 }
195
196 /**
197 * Get the error list as a wikitext formatted list
198 *
199 * @param $shortContext String: a short enclosing context message name, to
200 * be used when there is a single error
201 * @param $longContext String: a long enclosing context message name, for a list
202 * @return String
203 */
204 function getWikiText( $shortContext = false, $longContext = false ) {
205 if ( count( $this->errors ) == 0 ) {
206 if ( $this->ok ) {
207 $this->fatal( 'internalerror_info',
208 __METHOD__." called for a good result, this is incorrect\n" );
209 } else {
210 $this->fatal( 'internalerror_info',
211 __METHOD__.": Invalid result object: no error text but not OK\n" );
212 }
213 }
214 if ( count( $this->errors ) == 1 ) {
215 $s = $this->getWikiTextForError( $this->errors[0], $this->errors[0] );
216 if ( $shortContext ) {
217 $s = wfMsgNoTrans( $shortContext, $s );
218 } elseif ( $longContext ) {
219 $s = wfMsgNoTrans( $longContext, "* $s\n" );
220 }
221 } else {
222 $s = '* '. implode("\n* ",
223 $this->getWikiTextArray( $this->errors ) ) . "\n";
224 if ( $longContext ) {
225 $s = wfMsgNoTrans( $longContext, $s );
226 } elseif ( $shortContext ) {
227 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
228 }
229 }
230 return $s;
231 }
232
233 /**
234 * Return the wiki text for a single error.
235 * @param $error Mixed With an array & two values keyed by
236 * 'message' and 'params', use those keys-value pairs.
237 * Otherwise, if its an array, just use the first value as the
238 * message and the remaining items as the params.
239 *
240 * @return String
241 */
242 protected function getWikiTextForError( $error ) {
243 if ( is_array( $error ) ) {
244 if ( isset( $error['message'] ) && isset( $error['params'] ) ) {
245 return wfMsgNoTrans( $error['message'],
246 array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
247 } else {
248 $message = array_shift($error);
249 return wfMsgNoTrans( $message,
250 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
251 }
252 } else {
253 return wfMsgNoTrans( $error );
254 }
255 }
256
257 /**
258 * Return an array with the wikitext for each item in the array.
259 * @param $errors Array
260 * @return Array
261 */
262 function getWikiTextArray( $errors ) {
263 return array_map( array( $this, 'getWikiTextForError' ), $errors );
264 }
265
266 /**
267 * Merge another status object into this one
268 *
269 * @param $other Status Other Status object
270 * @param $overwriteValue Boolean: whether to override the "value" member
271 */
272 function merge( $other, $overwriteValue = false ) {
273 $this->errors = array_merge( $this->errors, $other->errors );
274 $this->ok = $this->ok && $other->ok;
275 if ( $overwriteValue ) {
276 $this->value = $other->value;
277 }
278 $this->successCount += $other->successCount;
279 $this->failCount += $other->failCount;
280 }
281
282 /**
283 * Get the list of errors (but not warnings)
284 *
285 * @return Array
286 */
287 function getErrorsArray() {
288 return $this->getStatusArray( "error" );
289 }
290
291 /**
292 * Get the list of warnings (but not errors)
293 *
294 * @return Array
295 */
296 function getWarningsArray() {
297 return $this->getStatusArray( "warning" );
298 }
299
300 /**
301 * Returns a list of status messages of the given type
302 * @param $type String
303 *
304 * @return Array
305 */
306 protected function getStatusArray( $type ) {
307 $result = array();
308 foreach ( $this->errors as $error ) {
309 if ( $error['type'] === $type ) {
310 if( $error['params'] ) {
311 $result[] = array_merge( array( $error['message'] ), $error['params'] );
312 } else {
313 $result[] = array( $error['message'] );
314 }
315 }
316 }
317 return $result;
318 }
319
320 /**
321 * Returns a list of status messages of the given type, with message and
322 * params left untouched, like a sane version of getStatusArray
323 *
324 * @param $type String
325 *
326 * @return Array
327 */
328 public function getErrorsByType( $type ) {
329 $result = array();
330 foreach ( $this->errors as $error ) {
331 if ( $error['type'] === $type ) {
332 $result[] = $error;
333 }
334 }
335 return $result;
336 }
337
338 /**
339 * Returns true if the specified message is present as a warning or error
340 *
341 * @param $msg String: message name
342 * @return Boolean
343 */
344 function hasMessage( $msg ) {
345 foreach ( $this->errors as $error ) {
346 if ( $error['message'] === $msg ) {
347 return true;
348 }
349 }
350 return false;
351 }
352
353 /**
354 * If the specified source message exists, replace it with the specified
355 * destination message, but keep the same parameters as in the original error.
356 *
357 * Return true if the replacement was done, false otherwise.
358 *
359 * @return bool
360 */
361 function replaceMessage( $source, $dest ) {
362 $replaced = false;
363 foreach ( $this->errors as $index => $error ) {
364 if ( $error['message'] === $source ) {
365 $this->errors[$index]['message'] = $dest;
366 $replaced = true;
367 }
368 }
369 return $replaced;
370 }
371
372 /**
373 * Backward compatibility function for WikiError -> Status migration
374 *
375 * @return String
376 */
377 public function getMessage() {
378 return $this->getWikiText();
379 }
380
381 /**
382 * @return mixed
383 */
384 public function getValue() {
385 return $this->value;
386 }
387 }