DifferenceEngine cleanup
[mediawiki.git] / includes / Status.php
blob836c24a459cd22148643fab938a491179a698f0c
1 <?php
2 /**
3 * Generic operation result.
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.
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.
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
20 * @file
23 /**
24 * Generic operation result class
25 * Has warning/error list, boolean status and arbitrary value
27 * "Good" means the operation was completed with no warnings or errors.
29 * "OK" means the operation was partially or wholly completed.
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.
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.
40 class Status {
41 public $ok = true;
42 public $value;
44 /** Counters for batch operations */
45 public $successCount = 0;
46 public $failCount = 0;
48 /** Array to indicate which items of the batch operations were successful */
49 public $success = array();
51 public $errors = array();
52 public $cleanCallback = false;
54 /**
55 * Factory function for fatal errors
57 * @param string|Message $message message name or object
58 * @return Status
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;
68 /**
69 * Factory function for good results
71 * @param $value Mixed
72 * @return Status
74 static function newGood( $value = null ) {
75 $result = new self;
76 $result->value = $value;
77 return $result;
80 /**
81 * Change operation result
83 * @param $ok Boolean: whether the operation completed
84 * @param $value Mixed
86 function setResult( $ok, $value = null ) {
87 $this->ok = $ok;
88 $this->value = $value;
91 /**
92 * Returns whether the operation completed and didn't have any error or
93 * warnings
95 * @return Boolean
97 function isGood() {
98 return $this->ok && !$this->errors;
102 * Returns whether the operation completed
104 * @return Boolean
106 function isOK() {
107 return $this->ok;
111 * Add a new warning
113 * @param string|Message $message message name or object
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 );
124 * Add an error, do not set fatal flag
125 * This can be used for non-fatal errors
127 * @param string|Message $message message name or object
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 );
138 * Add an error and set OK to false, indicating that the operation
139 * as a whole was fatal
141 * @param string|Message $message message name or object
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;
153 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
155 function __wakeup() {
156 $this->cleanCallback = false;
160 * @param $params array
161 * @return array
163 protected function cleanParams( $params ) {
164 if ( !$this->cleanCallback ) {
165 return $params;
167 $cleanParams = array();
168 foreach ( $params as $i => $param ) {
169 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
171 return $cleanParams;
175 * Get the error list as a wikitext formatted list
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
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" );
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();
199 } else {
200 $errors = $this->getErrorMessageArray( $this->errors );
201 foreach ( $errors as &$error ) {
202 $error = $error->plain();
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();
211 return $s;
215 * Get the error list as a Message object
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
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" );
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 );
241 } else {
242 $msgs = $this->getErrorMessageArray( $this->errors );
243 $msgCount = count( $msgs );
245 if ( $shortContext ) {
246 $msgCount++;
249 $wrapper = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
250 $s = $wrapper->params( $msgs )->parse();
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 );
261 return $s;
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.
271 * @return String
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 ) ) );
285 } else {
286 $msg = wfMessage( $error );
288 return $msg;
292 * Get the error message as HTML. This is done by parsing the wikitext error
293 * message.
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.
299 public function getHTML( $shortContext = false, $longContext = false ) {
300 $text = $this->getWikiText( $shortContext, $longContext );
301 return MessageCache::singleton()->transform( $text, true );
305 * Return an array with the wikitext for each item in the array.
306 * @param $errors Array
307 * @return Array
309 protected function getErrorMessageArray( $errors ) {
310 return array_map( array( $this, 'getErrorMessage' ), $errors );
314 * Merge another status object into this one
316 * @param $other Status Other Status object
317 * @param $overwriteValue Boolean: whether to override the "value" member
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;
325 $this->successCount += $other->successCount;
326 $this->failCount += $other->failCount;
330 * Get the list of errors (but not warnings)
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.
335 function getErrorsArray() {
336 return $this->getStatusArray( "error" );
340 * Get the list of warnings (but not errors)
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.
345 function getWarningsArray() {
346 return $this->getStatusArray( "warning" );
350 * Returns a list of status messages of the given type
351 * @param $type String
352 * @return Array
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()
363 } elseif ( $error['params'] ) {
364 $result[] = array_merge( array( $error['message'] ), $error['params'] );
365 } else {
366 $result[] = array( $error['message'] );
371 return $result;
375 * Returns a list of status messages of the given type, with message and
376 * params left untouched, like a sane version of getStatusArray
378 * @param $type String
380 * @return Array
382 public function getErrorsByType( $type ) {
383 $result = array();
384 foreach ( $this->errors as $error ) {
385 if ( $error['type'] === $type ) {
386 $result[] = $error;
389 return $result;
393 * Returns true if the specified message is present as a warning or error
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.
398 * @param string $msg message name
399 * @return Boolean
401 function hasMessage( $msg ) {
402 foreach ( $this->errors as $error ) {
403 if ( $error['message'] === $msg ) {
404 return true;
407 return false;
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.
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.
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.
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;
429 return $replaced;
433 * @return mixed
435 public function getValue() {
436 return $this->value;