Merge "Remove unused $wgDebugDBTransactions"
[lhc/web/wiklou.git] / includes / Status.php
1 <?php
2
3 /**
4 * Generic operation result class
5 * Has warning/error list, boolean status and arbitrary value
6 *
7 * "Good" means the operation was completed with no warnings or errors.
8 *
9 * "OK" means the operation was partially or wholly completed.
10 *
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.
14 */
15 class Status {
16 var $ok = true;
17 var $value;
18
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();
23
24 /*semi-private*/ var $errors = array();
25 /*semi-private*/ var $cleanCallback = false;
26
27 /**
28 * Factory function for fatal errors
29 *
30 * @param $message String: message name
31 * @return Status
32 */
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;
39 }
40
41 /**
42 * Factory function for good results
43 *
44 * @param $value Mixed
45 * @return Status
46 */
47 static function newGood( $value = null ) {
48 $result = new self;
49 $result->value = $value;
50 return $result;
51 }
52
53 /**
54 * Change operation result
55 *
56 * @param $ok Boolean: whether the operation completed
57 * @param $value Mixed
58 */
59 function setResult( $ok, $value = null ) {
60 $this->ok = $ok;
61 $this->value = $value;
62 }
63
64 /**
65 * Returns whether the operation completed and didn't have any error or
66 * warnings
67 *
68 * @return Boolean
69 */
70 function isGood() {
71 return $this->ok && !$this->errors;
72 }
73
74 /**
75 * Returns whether the operation completed
76 *
77 * @return Boolean
78 */
79 function isOK() {
80 return $this->ok;
81 }
82
83 /**
84 * Add a new warning
85 *
86 * @param $message String: message name
87 */
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 );
94 }
95
96 /**
97 * Add an error, do not set fatal flag
98 * This can be used for non-fatal errors
99 *
100 * @param $message String: message name
101 */
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 );
108 }
109
110 /**
111 * Add an error and set OK to false, indicating that the operation
112 * as a whole was fatal
113 *
114 * @param $message String: message name
115 */
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;
123 }
124
125 /**
126 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
127 */
128 function __wakeup() {
129 $this->cleanCallback = false;
130 }
131
132 /**
133 * @param $params array
134 * @return array
135 */
136 protected function cleanParams( $params ) {
137 if ( !$this->cleanCallback ) {
138 return $params;
139 }
140 $cleanParams = array();
141 foreach ( $params as $i => $param ) {
142 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
143 }
144 return $cleanParams;
145 }
146
147 /**
148 * @param $item
149 * @return string
150 */
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 );
158 }
159 $xml .= "</{$item['type']}>\n";
160 return $xml;
161 }
162
163 /**
164 * Get the error list as XML
165 * @return string
166 */
167 function getXML() {
168 $xml = "<errors>\n";
169 foreach ( $this->errors as $error ) {
170 $xml .= $this->getItemXML( $error );
171 }
172 $xml .= "</errors>\n";
173 return $xml;
174 }
175
176 /**
177 * Get the error list as a wikitext formatted list
178 *
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
183 */
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" );
192 }
193 }
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" );
200 }
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" );
208 }
209 }
210 return $s;
211 }
212
213 /**
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.
219 *
220 * @return String
221 */
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 ) ) );
231 }
232 } else {
233 return wfMsgNoTrans( $error );
234 }
235 }
236
237 /**
238 * Return an array with the wikitext for each item in the array.
239 * @param $errors Array
240 * @return Array
241 */
242 function getWikiTextArray( $errors ) {
243 return array_map( array( $this, 'getWikiTextForError' ), $errors );
244 }
245
246 /**
247 * Merge another status object into this one
248 *
249 * @param $other Status Other Status object
250 * @param $overwriteValue Boolean: whether to override the "value" member
251 */
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;
257 }
258 $this->successCount += $other->successCount;
259 $this->failCount += $other->failCount;
260 }
261
262 /**
263 * Get the list of errors (but not warnings)
264 *
265 * @return Array
266 */
267 function getErrorsArray() {
268 return $this->getStatusArray( "error" );
269 }
270
271 /**
272 * Get the list of warnings (but not errors)
273 *
274 * @return Array
275 */
276 function getWarningsArray() {
277 return $this->getStatusArray( "warning" );
278 }
279
280 /**
281 * Returns a list of status messages of the given type
282 * @param $type String
283 *
284 * @return Array
285 */
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'] );
294 }
295 }
296 }
297 return $result;
298 }
299
300 /**
301 * Returns a list of status messages of the given type, with message and
302 * params left untouched, like a sane version of getStatusArray
303 *
304 * @param $type String
305 *
306 * @return Array
307 */
308 public function getErrorsByType( $type ) {
309 $result = array();
310 foreach ( $this->errors as $error ) {
311 if ( $error['type'] === $type ) {
312 $result[] = $error;
313 }
314 }
315 return $result;
316 }
317
318 /**
319 * Returns true if the specified message is present as a warning or error
320 *
321 * @param $msg String: message name
322 * @return Boolean
323 */
324 function hasMessage( $msg ) {
325 foreach ( $this->errors as $error ) {
326 if ( $error['message'] === $msg ) {
327 return true;
328 }
329 }
330 return false;
331 }
332
333 /**
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.
336 *
337 * Return true if the replacement was done, false otherwise.
338 *
339 * @return bool
340 */
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;
347 }
348 }
349 return $replaced;
350 }
351
352 /**
353 * Backward compatibility function for WikiError -> Status migration
354 *
355 * @return String
356 */
357 public function getMessage() {
358 return $this->getWikiText();
359 }
360
361 /**
362 * @return mixed
363 */
364 public function getValue() {
365 return $this->value;
366 }
367 }