4 * Generic operation result class
5 * Has warning/error list, boolean status and arbitrary value
7 * "Good" means the operation was completed with no warnings or errors.
9 * "OK" means the operation was partially or wholly completed.
11 * An operation which is not OK should have errors so that the user can be
12 * informed as to what went wrong. Calling the fatal() function sets an error
13 * message and simultaneously switches off the OK flag.
19 /** Counters for batch operations */
20 public $successCount = 0, $failCount = 0;
21 /** Array to indicate which items of the batch operations were successful */
22 public $success = array();
24 /*semi-private*/ var $errors = array();
25 /*semi-private*/ var $cleanCallback = false;
28 * Factory function for fatal errors
30 * @param $message String: message name
33 static function newFatal( $message /*, parameters...*/ ) {
34 $params = func_get_args();
36 call_user_func_array( array( &$result, 'error' ), $params );
42 * Factory function for good results
47 static function newGood( $value = null ) {
49 $result->value
= $value;
54 * Change operation result
56 * @param $ok Boolean: whether the operation completed
59 function setResult( $ok, $value = null ) {
61 $this->value
= $value;
65 * Returns whether the operation completed and didn't have any error or
71 return $this->ok
&& !$this->errors
;
75 * Returns whether the operation completed
86 * @param $message String: message name
88 function warning( $message /*, parameters... */ ) {
89 $params = array_slice( func_get_args(), 1 );
90 $this->errors
[] = array(
92 'message' => $message,
93 'params' => $params );
97 * Add an error, do not set fatal flag
98 * This can be used for non-fatal errors
100 * @param $message String: message name
102 function error( $message /*, parameters... */ ) {
103 $params = array_slice( func_get_args(), 1 );
104 $this->errors
[] = array(
106 'message' => $message,
107 'params' => $params );
111 * Add an error and set OK to false, indicating that the operation
112 * as a whole was fatal
114 * @param $message String: message name
116 function fatal( $message /*, parameters... */ ) {
117 $params = array_slice( func_get_args(), 1 );
118 $this->errors
[] = array(
120 'message' => $message,
121 'params' => $params );
126 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
128 function __wakeup() {
129 $this->cleanCallback
= false;
133 * @param $params array
136 protected function cleanParams( $params ) {
137 if ( !$this->cleanCallback
) {
140 $cleanParams = array();
141 foreach ( $params as $i => $param ) {
142 $cleanParams[$i] = call_user_func( $this->cleanCallback
, $param );
151 protected function getItemXML( $item ) {
152 $params = $this->cleanParams( $item['params'] );
153 $xml = "<{$item['type']}>\n" .
154 Xml
::element( 'message', null, $item['message'] ) . "\n" .
155 Xml
::element( 'text', null, wfMsg( $item['message'], $params ) ) ."\n";
156 foreach ( $params as $param ) {
157 $xml .= Xml
::element( 'param', null, $param );
159 $xml .= "</{$item['type']}>\n";
164 * Get the error list as XML
169 foreach ( $this->errors
as $error ) {
170 $xml .= $this->getItemXML( $error );
172 $xml .= "</errors>\n";
177 * Get the error list as a wikitext formatted list
179 * @param $shortContext String: a short enclosing context message name, to
180 * be used when there is a single error
181 * @param $longContext String: a long enclosing context message name, for a list
184 function getWikiText( $shortContext = false, $longContext = false ) {
185 if ( count( $this->errors
) == 0 ) {
187 $this->fatal( 'internalerror_info',
188 __METHOD__
." called for a good result, this is incorrect\n" );
190 $this->fatal( 'internalerror_info',
191 __METHOD__
.": Invalid result object: no error text but not OK\n" );
194 if ( count( $this->errors
) == 1 ) {
195 $s = $this->getWikiTextForError( $this->errors
[0], $this->errors
[0] );
196 if ( $shortContext ) {
197 $s = wfMsgNoTrans( $shortContext, $s );
198 } elseif ( $longContext ) {
199 $s = wfMsgNoTrans( $longContext, "* $s\n" );
202 $s = '* '. implode("\n* ",
203 $this->getWikiTextArray( $this->errors
) ) . "\n";
204 if ( $longContext ) {
205 $s = wfMsgNoTrans( $longContext, $s );
206 } elseif ( $shortContext ) {
207 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
214 * Return the wiki text for a single error.
215 * @param $error Mixed With an array & two values keyed by
216 * 'message' and 'params', use those keys-value pairs.
217 * Otherwise, if its an array, just use the first value as the
218 * message and the remaining items as the params.
222 protected function getWikiTextForError( $error ) {
223 if ( is_array( $error ) ) {
224 if ( isset( $error['message'] ) && isset( $error['params'] ) ) {
225 return wfMsgNoTrans( $error['message'],
226 array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
228 $message = array_shift($error);
229 return wfMsgNoTrans( $message,
230 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
233 return wfMsgNoTrans( $error );
238 * Return an array with the wikitext for each item in the array.
239 * @param $errors Array
242 function getWikiTextArray( $errors ) {
243 return array_map( array( $this, 'getWikiTextForError' ), $errors );
247 * Merge another status object into this one
249 * @param $other Status Other Status object
250 * @param $overwriteValue Boolean: whether to override the "value" member
252 function merge( $other, $overwriteValue = false ) {
253 $this->errors
= array_merge( $this->errors
, $other->errors
);
254 $this->ok
= $this->ok
&& $other->ok
;
255 if ( $overwriteValue ) {
256 $this->value
= $other->value
;
258 $this->successCount +
= $other->successCount
;
259 $this->failCount +
= $other->failCount
;
263 * Get the list of errors (but not warnings)
267 function getErrorsArray() {
268 return $this->getStatusArray( "error" );
272 * Get the list of warnings (but not errors)
276 function getWarningsArray() {
277 return $this->getStatusArray( "warning" );
281 * Returns a list of status messages of the given type
282 * @param $type String
286 protected function getStatusArray( $type ) {
288 foreach ( $this->errors
as $error ) {
289 if ( $error['type'] === $type ) {
290 if( $error['params'] ) {
291 $result[] = array_merge( array( $error['message'] ), $error['params'] );
293 $result[] = array( $error['message'] );
301 * Returns a list of status messages of the given type, with message and
302 * params left untouched, like a sane version of getStatusArray
304 * @param $type String
308 public function getErrorsByType( $type ) {
310 foreach ( $this->errors
as $error ) {
311 if ( $error['type'] === $type ) {
319 * Returns true if the specified message is present as a warning or error
321 * @param $msg String: message name
324 function hasMessage( $msg ) {
325 foreach ( $this->errors
as $error ) {
326 if ( $error['message'] === $msg ) {
334 * If the specified source message exists, replace it with the specified
335 * destination message, but keep the same parameters as in the original error.
337 * Return true if the replacement was done, false otherwise.
341 function replaceMessage( $source, $dest ) {
343 foreach ( $this->errors
as $index => $error ) {
344 if ( $error['message'] === $source ) {
345 $this->errors
[$index]['message'] = $dest;
353 * Backward compatibility function for WikiError -> Status migration
357 public function getMessage() {
358 return $this->getWikiText();
364 public function getValue() {