Merge "Vector: Set media screen on styles.less"
[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 * The recommended pattern for Status objects is to return a Status object
36 * unconditionally, i.e. both on success and on failure -- so that the
37 * developer of the calling code is reminded that the function can fail, and
38 * so that a lack of error-handling will be explicit.
39 */
40 class Status {
41 public $ok = true;
42 public $value;
43
44 /** Counters for batch operations */
45 public $successCount = 0;
46 public $failCount = 0;
47
48 /** Array to indicate which items of the batch operations were successful */
49 public $success = array();
50
51 public $errors = array();
52 public $cleanCallback = false;
53
54 /**
55 * Factory function for fatal errors
56 *
57 * @param string|Message $message message name or object
58 * @return Status
59 */
60 static function newFatal( $message /*, parameters...*/ ) {
61 $params = func_get_args();
62 $result = new self;
63 call_user_func_array( array( &$result, 'error' ), $params );
64 $result->ok = false;
65 return $result;
66 }
67
68 /**
69 * Factory function for good results
70 *
71 * @param $value Mixed
72 * @return Status
73 */
74 static function newGood( $value = null ) {
75 $result = new self;
76 $result->value = $value;
77 return $result;
78 }
79
80 /**
81 * Change operation result
82 *
83 * @param $ok Boolean: whether the operation completed
84 * @param $value Mixed
85 */
86 function setResult( $ok, $value = null ) {
87 $this->ok = $ok;
88 $this->value = $value;
89 }
90
91 /**
92 * Returns whether the operation completed and didn't have any error or
93 * warnings
94 *
95 * @return Boolean
96 */
97 function isGood() {
98 return $this->ok && !$this->errors;
99 }
100
101 /**
102 * Returns whether the operation completed
103 *
104 * @return Boolean
105 */
106 function isOK() {
107 return $this->ok;
108 }
109
110 /**
111 * Add a new warning
112 *
113 * @param string|Message $message message name or object
114 */
115 function warning( $message /*, parameters... */ ) {
116 $params = array_slice( func_get_args(), 1 );
117 $this->errors[] = array(
118 'type' => 'warning',
119 'message' => $message,
120 'params' => $params );
121 }
122
123 /**
124 * Add an error, do not set fatal flag
125 * This can be used for non-fatal errors
126 *
127 * @param string|Message $message message name or object
128 */
129 function error( $message /*, parameters... */ ) {
130 $params = array_slice( func_get_args(), 1 );
131 $this->errors[] = array(
132 'type' => 'error',
133 'message' => $message,
134 'params' => $params );
135 }
136
137 /**
138 * Add an error and set OK to false, indicating that the operation
139 * as a whole was fatal
140 *
141 * @param string|Message $message message name or object
142 */
143 function fatal( $message /*, parameters... */ ) {
144 $params = array_slice( func_get_args(), 1 );
145 $this->errors[] = array(
146 'type' => 'error',
147 'message' => $message,
148 'params' => $params );
149 $this->ok = false;
150 }
151
152 /**
153 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
154 */
155 function __wakeup() {
156 $this->cleanCallback = false;
157 }
158
159 /**
160 * @param $params array
161 * @return array
162 */
163 protected function cleanParams( $params ) {
164 if ( !$this->cleanCallback ) {
165 return $params;
166 }
167 $cleanParams = array();
168 foreach ( $params as $i => $param ) {
169 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
170 }
171 return $cleanParams;
172 }
173
174 /**
175 * Get the error list as a wikitext formatted list
176 *
177 * @param string $shortContext a short enclosing context message name, to
178 * be used when there is a single error
179 * @param string $longContext a long enclosing context message name, for a list
180 * @return String
181 */
182 function getWikiText( $shortContext = false, $longContext = false ) {
183 if ( count( $this->errors ) == 0 ) {
184 if ( $this->ok ) {
185 $this->fatal( 'internalerror_info',
186 __METHOD__ . " called for a good result, this is incorrect\n" );
187 } else {
188 $this->fatal( 'internalerror_info',
189 __METHOD__ . ": Invalid result object: no error text but not OK\n" );
190 }
191 }
192 if ( count( $this->errors ) == 1 ) {
193 $s = $this->getErrorMessage( $this->errors[0] )->plain();
194 if ( $shortContext ) {
195 $s = wfMessage( $shortContext, $s )->plain();
196 } elseif ( $longContext ) {
197 $s = wfMessage( $longContext, "* $s\n" )->plain();
198 }
199 } else {
200 $errors = $this->getErrorMessageArray( $this->errors );
201 foreach ( $errors as &$error ) {
202 $error = $error->plain();
203 }
204 $s = '* ' . implode( "\n* ", $errors ) . "\n";
205 if ( $longContext ) {
206 $s = wfMessage( $longContext, $s )->plain();
207 } elseif ( $shortContext ) {
208 $s = wfMessage( $shortContext, "\n$s\n" )->plain();
209 }
210 }
211 return $s;
212 }
213
214 /**
215 * Get the error list as a Message object
216 *
217 * @param string $shortContext a short enclosing context message name, to
218 * be used when there is a single error
219 * @param string $longContext a long enclosing context message name, for a list
220 * @return Message
221 */
222 function getMessage( $shortContext = false, $longContext = false ) {
223 if ( count( $this->errors ) == 0 ) {
224 if ( $this->ok ) {
225 $this->fatal( 'internalerror_info',
226 __METHOD__ . " called for a good result, this is incorrect\n" );
227 } else {
228 $this->fatal( 'internalerror_info',
229 __METHOD__ . ": Invalid result object: no error text but not OK\n" );
230 }
231 }
232 if ( count( $this->errors ) == 1 ) {
233 $s = $this->getErrorMessage( $this->errors[0] );
234 if ( $shortContext ) {
235 $s = wfMessage( $shortContext, $s );
236 } elseif ( $longContext ) {
237 $wrapper = new RawMessage( "* \$1\n" );
238 $wrapper->params( $s )->parse();
239 $s = wfMessage( $longContext, $wrapper );
240 }
241 } else {
242 $msgs = $this->getErrorMessageArray( $this->errors );
243 $msgCount = count( $msgs );
244
245 if ( $shortContext ) {
246 $msgCount++;
247 }
248
249 $wrapper = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
250 $s = $wrapper->params( $msgs )->parse();
251
252 if ( $longContext ) {
253 $s = wfMessage( $longContext, $wrapper );
254 } elseif ( $shortContext ) {
255 $wrapper = new RawMessage( "\n\$1\n", $wrapper );
256 $wrapper->parse();
257 $s = wfMessage( $shortContext, $wrapper );
258 }
259 }
260
261 return $s;
262 }
263
264 /**
265 * Return the message for a single error.
266 * @param $error Mixed With an array & two values keyed by
267 * 'message' and 'params', use those keys-value pairs.
268 * Otherwise, if its an array, just use the first value as the
269 * message and the remaining items as the params.
270 *
271 * @return String
272 */
273 protected function getErrorMessage( $error ) {
274 if ( is_array( $error ) ) {
275 if ( isset( $error['message'] ) && $error['message'] instanceof Message ) {
276 $msg = $error['message'];
277 } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
278 $msg = wfMessage( $error['message'],
279 array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
280 } else {
281 $msgName = array_shift( $error );
282 $msg = wfMessage( $msgName,
283 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
284 }
285 } else {
286 $msg = wfMessage( $error );
287 }
288 return $msg;
289 }
290
291 /**
292 * Get the error message as HTML. This is done by parsing the wikitext error
293 * message.
294 *
295 * @note: this does not perform a full wikitext to HTML conversion, it merely applies
296 * a message transformation.
297 * @todo figure out whether that is actually The Right Thing.
298 */
299 public function getHTML( $shortContext = false, $longContext = false ) {
300 $text = $this->getWikiText( $shortContext, $longContext );
301 return MessageCache::singleton()->transform( $text, true );
302 }
303
304 /**
305 * Return an array with the wikitext for each item in the array.
306 * @param $errors Array
307 * @return Array
308 */
309 protected function getErrorMessageArray( $errors ) {
310 return array_map( array( $this, 'getErrorMessage' ), $errors );
311 }
312
313 /**
314 * Merge another status object into this one
315 *
316 * @param $other Status Other Status object
317 * @param $overwriteValue Boolean: whether to override the "value" member
318 */
319 function merge( $other, $overwriteValue = false ) {
320 $this->errors = array_merge( $this->errors, $other->errors );
321 $this->ok = $this->ok && $other->ok;
322 if ( $overwriteValue ) {
323 $this->value = $other->value;
324 }
325 $this->successCount += $other->successCount;
326 $this->failCount += $other->failCount;
327 }
328
329 /**
330 * Get the list of errors (but not warnings)
331 *
332 * @return array A list in which each entry is an array with a message key as its first element.
333 * The remaining array elements are the message parameters.
334 */
335 function getErrorsArray() {
336 return $this->getStatusArray( "error" );
337 }
338
339 /**
340 * Get the list of warnings (but not errors)
341 *
342 * @return array A list in which each entry is an array with a message key as its first element.
343 * The remaining array elements are the message parameters.
344 */
345 function getWarningsArray() {
346 return $this->getStatusArray( "warning" );
347 }
348
349 /**
350 * Returns a list of status messages of the given type
351 * @param $type String
352 * @return Array
353 */
354 protected function getStatusArray( $type ) {
355 $result = array();
356 foreach ( $this->errors as $error ) {
357 if ( $error['type'] === $type ) {
358 if ( $error['message'] instanceof Message ) {
359 $result[] = array_merge(
360 array( $error['message']->getKey() ),
361 $error['message']->getParams()
362 );
363 } elseif ( $error['params'] ) {
364 $result[] = array_merge( array( $error['message'] ), $error['params'] );
365 } else {
366 $result[] = array( $error['message'] );
367 }
368 }
369 }
370
371 return $result;
372 }
373
374 /**
375 * Returns a list of status messages of the given type, with message and
376 * params left untouched, like a sane version of getStatusArray
377 *
378 * @param $type String
379 *
380 * @return Array
381 */
382 public function getErrorsByType( $type ) {
383 $result = array();
384 foreach ( $this->errors as $error ) {
385 if ( $error['type'] === $type ) {
386 $result[] = $error;
387 }
388 }
389 return $result;
390 }
391
392 /**
393 * Returns true if the specified message is present as a warning or error
394 *
395 * Note, due to the lack of tools for comparing Message objects, this
396 * function will not work when using a Message object as a parameter.
397 *
398 * @param string $msg message name
399 * @return Boolean
400 */
401 function hasMessage( $msg ) {
402 foreach ( $this->errors as $error ) {
403 if ( $error['message'] === $msg ) {
404 return true;
405 }
406 }
407 return false;
408 }
409
410 /**
411 * If the specified source message exists, replace it with the specified
412 * destination message, but keep the same parameters as in the original error.
413 *
414 * Note, due to the lack of tools for comparing Message objects, this
415 * function will not work when using a Message object as the search parameter.
416 *
417 * @param $source Message|String: Message key or object to search for
418 * @param $dest Message|String: Replacement message key or object
419 * @return bool Return true if the replacement was done, false otherwise.
420 */
421 function replaceMessage( $source, $dest ) {
422 $replaced = false;
423 foreach ( $this->errors as $index => $error ) {
424 if ( $error['message'] === $source ) {
425 $this->errors[$index]['message'] = $dest;
426 $replaced = true;
427 }
428 }
429 return $replaced;
430 }
431
432 /**
433 * @return mixed
434 */
435 public function getValue() {
436 return $this->value;
437 }
438 }