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 failed */
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
32 static function newFatal( $message /*, parameters...*/ ) {
33 $params = func_get_args();
35 call_user_func_array( array( &$result, 'error' ), $params );
41 * Factory function for good results
45 static function newGood( $value = null ) {
47 $result->value
= $value;
52 * Change operation result
54 * @param $ok Boolean: whether to operation completed
57 function setResult( $ok, $value = null ) {
59 $this->value
= $value;
63 * Returns whether the operation completed and didn't have any error or
69 return $this->ok
&& !$this->errors
;
73 * Returns whether the operation completed
84 * @param $message String: message name
86 function warning( $message /*, parameters... */ ) {
87 $params = array_slice( func_get_args(), 1 );
88 $this->errors
[] = array(
90 'message' => $message,
91 'params' => $params );
95 * Add an error, do not set fatal flag
96 * This can be used for non-fatal errors
98 * @param $message String: message name
100 function error( $message /*, parameters... */ ) {
101 $params = array_slice( func_get_args(), 1 );
102 $this->errors
[] = array(
104 'message' => $message,
105 'params' => $params );
109 * Add an error and set OK to false, indicating that the operation
110 * as a whole was fatal
112 * @param $message String: message name
114 function fatal( $message /*, parameters... */ ) {
115 $params = array_slice( func_get_args(), 1 );
116 $this->errors
[] = array(
118 'message' => $message,
119 'params' => $params );
124 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
126 function __wakeup() {
127 $this->cleanCallback
= false;
130 protected function cleanParams( $params ) {
131 if ( !$this->cleanCallback
) {
134 $cleanParams = array();
135 foreach ( $params as $i => $param ) {
136 $cleanParams[$i] = call_user_func( $this->cleanCallback
, $param );
141 protected function getItemXML( $item ) {
142 $params = $this->cleanParams( $item['params'] );
143 $xml = "<{$item['type']}>\n" .
144 Xml
::element( 'message', null, $item['message'] ) . "\n" .
145 Xml
::element( 'text', null, wfMsg( $item['message'], $params ) ) ."\n";
146 foreach ( $params as $param ) {
147 $xml .= Xml
::element( 'param', null, $param );
149 $xml .= "</{$item['type']}>\n";
154 * Get the error list as XML
158 foreach ( $this->errors
as $error ) {
159 $xml .= $this->getItemXML( $error );
161 $xml .= "</errors>\n";
166 * Get the error list as a wikitext formatted list
168 * @param $shortContext String: a short enclosing context message name, to
169 * be used when there is a single error
170 * @param $longContext String: a long enclosing context message name, for a list
173 function getWikiText( $shortContext = false, $longContext = false ) {
174 if ( count( $this->errors
) == 0 ) {
176 $this->fatal( 'internalerror_info',
177 __METHOD__
." called for a good result, this is incorrect\n" );
179 $this->fatal( 'internalerror_info',
180 __METHOD__
.": Invalid result object: no error text but not OK\n" );
183 if ( count( $this->errors
) == 1 ) {
184 $s = $this->getWikiTextForError( $this->errors
[0], $this->errors
[0] );
185 if ( $shortContext ) {
186 $s = wfMsgNoTrans( $shortContext, $s );
187 } elseif ( $longContext ) {
188 $s = wfMsgNoTrans( $longContext, "* $s\n" );
191 $s = '* '. implode("\n* ",
192 $this->getWikiTextArray( $this->errors
) ) . "\n";
193 if ( $longContext ) {
194 $s = wfMsgNoTrans( $longContext, $s );
195 } elseif ( $shortContext ) {
196 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
203 * Return the wiki text for a single error.
204 * @param $error Mixed With an array & two values keyed by
205 * 'message' and 'params', use those keys-value pairs.
206 * Otherwise, if its an array, just use the first value as the
207 * message and the remaining items as the params.
211 protected function getWikiTextForError( $error ) {
212 if ( is_array( $error ) ) {
213 if ( isset( $error['message'] ) && isset( $error['params'] ) ) {
214 return wfMsgNoTrans( $error['message'],
215 array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
217 $message = array_shift($error);
218 return wfMsgNoTrans( $message,
219 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
222 return wfMsgNoTrans( $error );
227 * Return an array with the wikitext for each item in the array.
228 * @param $errors Array
231 function getWikiTextArray( $errors ) {
232 return array_map( array( $this, 'getWikiTextForError' ), $errors );
236 * Merge another status object into this one
238 * @param $other Status Other Status object
239 * @param $overwriteValue Boolean: whether to override the "value" member
241 function merge( $other, $overwriteValue = false ) {
242 $this->errors
= array_merge( $this->errors
, $other->errors
);
243 $this->ok
= $this->ok
&& $other->ok
;
244 if ( $overwriteValue ) {
245 $this->value
= $other->value
;
247 $this->successCount +
= $other->successCount
;
248 $this->failCount +
= $other->failCount
;
252 * Get the list of errors (but not warnings)
256 function getErrorsArray() {
257 return $this->getStatusArray( "error" );
261 * Get the list of warnings (but not errors)
265 function getWarningsArray() {
266 return $this->getStatusArray( "warning" );
270 * Returns a list of status messages of the given type
271 * @param $type String
275 protected function getStatusArray( $type ) {
277 foreach ( $this->errors
as $error ) {
278 if ( $error['type'] === $type ) {
279 if( $error['params'] ) {
280 $result[] = array_merge( array( $error['message'] ), $error['params'] );
282 $result[] = array( $error['message'] );
290 * Returns a list of status messages of the given type, with message and
291 * params left untouched, like a sane version of getStatusArray
293 * @param $type String
297 public function getErrorsByType( $type ) {
299 foreach ( $this->errors
as $error ) {
300 if ( $error['type'] === $type ) {
308 * Returns true if the specified message is present as a warning or error
310 * @param $msg String: message name
313 function hasMessage( $msg ) {
314 foreach ( $this->errors
as $error ) {
315 if ( $error['message'] === $msg ) {
323 * If the specified source message exists, replace it with the specified
324 * destination message, but keep the same parameters as in the original error.
326 * Return true if the replacement was done, false otherwise.
328 function replaceMessage( $source, $dest ) {
330 foreach ( $this->errors
as $index => $error ) {
331 if ( $error['message'] === $source ) {
332 $this->errors
[$index]['message'] = $dest;
340 * Backward compatibility function for WikiError -> Status migration
344 public function getMessage() {
345 return $this->getWikiText();