fix subpage linking
[mediawiki.git] / includes / Status.php
blob835b2edf56a47b9b8d5b8adbab52a6a60165e422
1 <?php
3 /**
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.
15 class Status {
16 var $ok = true;
17 var $value;
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;
27 /**
28 * Factory function for fatal errors
30 * @param $message String: message name
31 * @return Status
33 static function newFatal( $message /*, parameters...*/ ) {
34 $params = func_get_args();
35 $result = new self;
36 call_user_func_array( array( &$result, 'error' ), $params );
37 $result->ok = false;
38 return $result;
41 /**
42 * Factory function for good results
44 * @param $value Mixed
45 * @return Status
47 static function newGood( $value = null ) {
48 $result = new self;
49 $result->value = $value;
50 return $result;
53 /**
54 * Change operation result
56 * @param $ok Boolean: whether the operation completed
57 * @param $value Mixed
59 function setResult( $ok, $value = null ) {
60 $this->ok = $ok;
61 $this->value = $value;
64 /**
65 * Returns whether the operation completed and didn't have any error or
66 * warnings
68 * @return Boolean
70 function isGood() {
71 return $this->ok && !$this->errors;
74 /**
75 * Returns whether the operation completed
77 * @return Boolean
79 function isOK() {
80 return $this->ok;
83 /**
84 * Add a new warning
86 * @param $message String: message name
88 function warning( $message /*, parameters... */ ) {
89 $params = array_slice( func_get_args(), 1 );
90 $this->errors[] = array(
91 'type' => 'warning',
92 'message' => $message,
93 'params' => $params );
96 /**
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(
105 'type' => 'error',
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(
119 'type' => 'error',
120 'message' => $message,
121 'params' => $params );
122 $this->ok = false;
126 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
128 function __wakeup() {
129 $this->cleanCallback = false;
133 * @param $params array
134 * @return array
136 protected function cleanParams( $params ) {
137 if ( !$this->cleanCallback ) {
138 return $params;
140 $cleanParams = array();
141 foreach ( $params as $i => $param ) {
142 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
144 return $cleanParams;
148 * @param $item
149 * @return string
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";
160 return $xml;
164 * Get the error list as XML
165 * @return string
167 function getXML() {
168 $xml = "<errors>\n";
169 foreach ( $this->errors as $error ) {
170 $xml .= $this->getItemXML( $error );
172 $xml .= "</errors>\n";
173 return $xml;
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
182 * @return String
184 function getWikiText( $shortContext = false, $longContext = false ) {
185 if ( count( $this->errors ) == 0 ) {
186 if ( $this->ok ) {
187 $this->fatal( 'internalerror_info',
188 __METHOD__." called for a good result, this is incorrect\n" );
189 } else {
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" );
201 } else {
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" );
210 return $s;
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.
220 * @return String
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'] ) ) );
227 } else {
228 $message = array_shift($error);
229 return wfMsgNoTrans( $message,
230 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
232 } else {
233 return wfMsgNoTrans( $error );
238 * Return an array with the wikitext for each item in the array.
239 * @param $errors Array
240 * @return 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)
265 * @return Array
267 function getErrorsArray() {
268 return $this->getStatusArray( "error" );
272 * Get the list of warnings (but not errors)
274 * @return Array
276 function getWarningsArray() {
277 return $this->getStatusArray( "warning" );
281 * Returns a list of status messages of the given type
282 * @param $type String
284 * @return Array
286 protected function getStatusArray( $type ) {
287 $result = array();
288 foreach ( $this->errors as $error ) {
289 if ( $error['type'] === $type ) {
290 if( $error['params'] ) {
291 $result[] = array_merge( array( $error['message'] ), $error['params'] );
292 } else {
293 $result[] = array( $error['message'] );
297 return $result;
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
306 * @return Array
308 public function getErrorsByType( $type ) {
309 $result = array();
310 foreach ( $this->errors as $error ) {
311 if ( $error['type'] === $type ) {
312 $result[] = $error;
315 return $result;
319 * Returns true if the specified message is present as a warning or error
321 * @param $msg String: message name
322 * @return Boolean
324 function hasMessage( $msg ) {
325 foreach ( $this->errors as $error ) {
326 if ( $error['message'] === $msg ) {
327 return true;
330 return false;
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.
339 * @return bool
341 function replaceMessage( $source, $dest ) {
342 $replaced = false;
343 foreach ( $this->errors as $index => $error ) {
344 if ( $error['message'] === $source ) {
345 $this->errors[$index]['message'] = $dest;
346 $replaced = true;
349 return $replaced;
353 * Backward compatibility function for WikiError -> Status migration
355 * @return String
357 public function getMessage() {
358 return $this->getWikiText();
362 * @return mixed
364 public function getValue() {
365 return $this->value;