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