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 var $successCount = 0, $failCount = 0;
22 /*semi-private*/ var $errors = array();
23 /*semi-private*/ var $cleanCallback = false;
26 * Factory function for fatal errors
28 * @param $message String: message name
30 static function newFatal( $message /*, parameters...*/ ) {
31 $params = func_get_args();
33 call_user_func_array( array( &$result, 'error' ), $params );
39 * Factory function for good results
43 static function newGood( $value = null ) {
45 $result->value
= $value;
50 * Change operation result
52 * @param $ok Boolean: whether to operation completed
55 function setResult( $ok, $value = null ) {
57 $this->value
= $value;
61 * Returns whether the operation completed and didn't have any error or
67 return $this->ok
&& !$this->errors
;
71 * Returns whether the operation completed
82 * @param $message String: message name
84 function warning( $message /*, parameters... */ ) {
85 $params = array_slice( func_get_args(), 1 );
86 $this->errors
[] = array(
88 'message' => $message,
89 'params' => $params );
93 * Add an error, do not set fatal flag
94 * This can be used for non-fatal errors
96 * @param $message String: message name
98 function error( $message /*, parameters... */ ) {
99 $params = array_slice( func_get_args(), 1 );
100 $this->errors
[] = array(
102 'message' => $message,
103 'params' => $params );
107 * Add an error and set OK to false, indicating that the operation
108 * as a whole was fatal
110 * @param $message String: message name
112 function fatal( $message /*, parameters... */ ) {
113 $params = array_slice( func_get_args(), 1 );
114 $this->errors
[] = array(
116 'message' => $message,
117 'params' => $params );
122 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
124 function __wakeup() {
125 $this->cleanCallback
= false;
128 protected function cleanParams( $params ) {
129 if ( !$this->cleanCallback
) {
132 $cleanParams = array();
133 foreach ( $params as $i => $param ) {
134 $cleanParams[$i] = call_user_func( $this->cleanCallback
, $param );
139 protected function getItemXML( $item ) {
140 $params = $this->cleanParams( $item['params'] );
141 $xml = "<{$item['type']}>\n" .
142 Xml
::element( 'message', null, $item['message'] ) . "\n" .
143 Xml
::element( 'text', null, wfMsgReal( $item['message'], $params ) ) ."\n";
144 foreach ( $params as $param ) {
145 $xml .= Xml
::element( 'param', null, $param );
147 $xml .= "</{$this->type}>\n";
152 * Get the error list as XML
156 foreach ( $this->errors
as $error ) {
157 $xml .= $this->getItemXML( $error );
159 $xml .= "</errors>\n";
164 * Get the error list as a wikitext formatted list
166 * @param $shortContext String: a short enclosing context message name, to
167 * be used when there is a single error
168 * @param $longContext String: a long enclosing context message name, for a list
171 function getWikiText( $shortContext = false, $longContext = false ) {
172 if ( count( $this->errors
) == 0 ) {
174 $this->fatal( 'internalerror_info',
175 __METHOD__
." called for a good result, this is incorrect\n" );
177 $this->fatal( 'internalerror_info',
178 __METHOD__
.": Invalid result object: no error text but not OK\n" );
181 if ( count( $this->errors
) == 1 ) {
182 $s = $this->getWikiTextForError( $this->errors
[0], $this->errors
[0] );
183 if ( $shortContext ) {
184 $s = wfMsgNoTrans( $shortContext, $s );
185 } elseif ( $longContext ) {
186 $s = wfMsgNoTrans( $longContext, "* $s\n" );
189 $s = '* '. implode("\n* ",
190 $this->getWikiTextArray( $this->errors
) ) . "\n";
191 if ( $longContext ) {
192 $s = wfMsgNoTrans( $longContext, $s );
193 } elseif ( $shortContext ) {
194 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
201 * Return the wiki text for a single error.
202 * @param $error Mixed With an array & two values keyed by
203 * 'message' and 'params', use those keys-value pairs.
204 * Otherwise, if its an array, just use the first value as the
205 * message and the remaining items as the params.
209 protected function getWikiTextForError( $error ) {
210 if ( is_array( $error ) ) {
211 if ( isset( $error['message'] ) && isset( $error['params'] ) ) {
212 return wfMsgReal( $error['message'],
213 array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ),
214 true, false, false );
216 $message = array_shift($error);
217 return wfMsgReal( $message,
218 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ),
219 true, false, false );
222 return wfMsgReal( $error, array(), true, false, false);
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 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[] = $error['message'];
289 * Returns true if the specified message is present as a warning or error
291 * @param $msg String: message name
294 function hasMessage( $msg ) {
295 foreach ( $this->errors
as $error ) {
296 if ( $error['message'] === $msg ) {
304 * Backward compatibility function for WikiError -> Status migration
308 public function getMessage() {
309 return $this->getWikiText();