* Added FileRepo::SKIP_LOCKING constant and made storeBatch() check it.
[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 */
32 static function newFatal( $message /*, parameters...*/ ) {
33 $params = func_get_args();
34 $result = new self;
35 call_user_func_array( array( &$result, 'error' ), $params );
36 $result->ok = false;
37 return $result;
38 }
39
40 /**
41 * Factory function for good results
42 *
43 * @param $value Mixed
44 */
45 static function newGood( $value = null ) {
46 $result = new self;
47 $result->value = $value;
48 return $result;
49 }
50
51 /**
52 * Change operation result
53 *
54 * @param $ok Boolean: whether the operation completed
55 * @param $value Mixed
56 */
57 function setResult( $ok, $value = null ) {
58 $this->ok = $ok;
59 $this->value = $value;
60 }
61
62 /**
63 * Returns whether the operation completed and didn't have any error or
64 * warnings
65 *
66 * @return Boolean
67 */
68 function isGood() {
69 return $this->ok && !$this->errors;
70 }
71
72 /**
73 * Returns whether the operation completed
74 *
75 * @return Boolean
76 */
77 function isOK() {
78 return $this->ok;
79 }
80
81 /**
82 * Add a new warning
83 *
84 * @param $message String: message name
85 */
86 function warning( $message /*, parameters... */ ) {
87 $params = array_slice( func_get_args(), 1 );
88 $this->errors[] = array(
89 'type' => 'warning',
90 'message' => $message,
91 'params' => $params );
92 }
93
94 /**
95 * Add an error, do not set fatal flag
96 * This can be used for non-fatal errors
97 *
98 * @param $message String: message name
99 */
100 function error( $message /*, parameters... */ ) {
101 $params = array_slice( func_get_args(), 1 );
102 $this->errors[] = array(
103 'type' => 'error',
104 'message' => $message,
105 'params' => $params );
106 }
107
108 /**
109 * Add an error and set OK to false, indicating that the operation
110 * as a whole was fatal
111 *
112 * @param $message String: message name
113 */
114 function fatal( $message /*, parameters... */ ) {
115 $params = array_slice( func_get_args(), 1 );
116 $this->errors[] = array(
117 'type' => 'error',
118 'message' => $message,
119 'params' => $params );
120 $this->ok = false;
121 }
122
123 /**
124 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
125 */
126 function __wakeup() {
127 $this->cleanCallback = false;
128 }
129
130 /**
131 * @param $params array
132 * @return array
133 */
134 protected function cleanParams( $params ) {
135 if ( !$this->cleanCallback ) {
136 return $params;
137 }
138 $cleanParams = array();
139 foreach ( $params as $i => $param ) {
140 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
141 }
142 return $cleanParams;
143 }
144
145 /**
146 * @param $item
147 * @return string
148 */
149 protected function getItemXML( $item ) {
150 $params = $this->cleanParams( $item['params'] );
151 $xml = "<{$item['type']}>\n" .
152 Xml::element( 'message', null, $item['message'] ) . "\n" .
153 Xml::element( 'text', null, wfMsg( $item['message'], $params ) ) ."\n";
154 foreach ( $params as $param ) {
155 $xml .= Xml::element( 'param', null, $param );
156 }
157 $xml .= "</{$item['type']}>\n";
158 return $xml;
159 }
160
161 /**
162 * Get the error list as XML
163 * @return string
164 */
165 function getXML() {
166 $xml = "<errors>\n";
167 foreach ( $this->errors as $error ) {
168 $xml .= $this->getItemXML( $error );
169 }
170 $xml .= "</errors>\n";
171 return $xml;
172 }
173
174 /**
175 * Get the error list as a wikitext formatted list
176 *
177 * @param $shortContext String: a short enclosing context message name, to
178 * be used when there is a single error
179 * @param $longContext String: a long enclosing context message name, for a list
180 * @return String
181 */
182 function getWikiText( $shortContext = false, $longContext = false ) {
183 if ( count( $this->errors ) == 0 ) {
184 if ( $this->ok ) {
185 $this->fatal( 'internalerror_info',
186 __METHOD__." called for a good result, this is incorrect\n" );
187 } else {
188 $this->fatal( 'internalerror_info',
189 __METHOD__.": Invalid result object: no error text but not OK\n" );
190 }
191 }
192 if ( count( $this->errors ) == 1 ) {
193 $s = $this->getWikiTextForError( $this->errors[0], $this->errors[0] );
194 if ( $shortContext ) {
195 $s = wfMsgNoTrans( $shortContext, $s );
196 } elseif ( $longContext ) {
197 $s = wfMsgNoTrans( $longContext, "* $s\n" );
198 }
199 } else {
200 $s = '* '. implode("\n* ",
201 $this->getWikiTextArray( $this->errors ) ) . "\n";
202 if ( $longContext ) {
203 $s = wfMsgNoTrans( $longContext, $s );
204 } elseif ( $shortContext ) {
205 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
206 }
207 }
208 return $s;
209 }
210
211 /**
212 * Return the wiki text for a single error.
213 * @param $error Mixed With an array & two values keyed by
214 * 'message' and 'params', use those keys-value pairs.
215 * Otherwise, if its an array, just use the first value as the
216 * message and the remaining items as the params.
217 *
218 * @return String
219 */
220 protected function getWikiTextForError( $error ) {
221 if ( is_array( $error ) ) {
222 if ( isset( $error['message'] ) && isset( $error['params'] ) ) {
223 return wfMsgNoTrans( $error['message'],
224 array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
225 } else {
226 $message = array_shift($error);
227 return wfMsgNoTrans( $message,
228 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
229 }
230 } else {
231 return wfMsgNoTrans( $error );
232 }
233 }
234
235 /**
236 * Return an array with the wikitext for each item in the array.
237 * @param $errors Array
238 * @return Array
239 */
240 function getWikiTextArray( $errors ) {
241 return array_map( array( $this, 'getWikiTextForError' ), $errors );
242 }
243
244 /**
245 * Merge another status object into this one
246 *
247 * @param $other Status Other Status object
248 * @param $overwriteValue Boolean: whether to override the "value" member
249 */
250 function merge( $other, $overwriteValue = false ) {
251 $this->errors = array_merge( $this->errors, $other->errors );
252 $this->ok = $this->ok && $other->ok;
253 if ( $overwriteValue ) {
254 $this->value = $other->value;
255 }
256 $this->successCount += $other->successCount;
257 $this->failCount += $other->failCount;
258 }
259
260 /**
261 * Get the list of errors (but not warnings)
262 *
263 * @return Array
264 */
265 function getErrorsArray() {
266 return $this->getStatusArray( "error" );
267 }
268
269 /**
270 * Get the list of warnings (but not errors)
271 *
272 * @return Array
273 */
274 function getWarningsArray() {
275 return $this->getStatusArray( "warning" );
276 }
277
278 /**
279 * Returns a list of status messages of the given type
280 * @param $type String
281 *
282 * @return Array
283 */
284 protected function getStatusArray( $type ) {
285 $result = array();
286 foreach ( $this->errors as $error ) {
287 if ( $error['type'] === $type ) {
288 if( $error['params'] ) {
289 $result[] = array_merge( array( $error['message'] ), $error['params'] );
290 } else {
291 $result[] = array( $error['message'] );
292 }
293 }
294 }
295 return $result;
296 }
297
298 /**
299 * Returns a list of status messages of the given type, with message and
300 * params left untouched, like a sane version of getStatusArray
301 *
302 * @param $type String
303 *
304 * @return Array
305 */
306 public function getErrorsByType( $type ) {
307 $result = array();
308 foreach ( $this->errors as $error ) {
309 if ( $error['type'] === $type ) {
310 $result[] = $error;
311 }
312 }
313 return $result;
314 }
315
316 /**
317 * Returns true if the specified message is present as a warning or error
318 *
319 * @param $msg String: message name
320 * @return Boolean
321 */
322 function hasMessage( $msg ) {
323 foreach ( $this->errors as $error ) {
324 if ( $error['message'] === $msg ) {
325 return true;
326 }
327 }
328 return false;
329 }
330
331 /**
332 * If the specified source message exists, replace it with the specified
333 * destination message, but keep the same parameters as in the original error.
334 *
335 * Return true if the replacement was done, false otherwise.
336 *
337 * @return bool
338 */
339 function replaceMessage( $source, $dest ) {
340 $replaced = false;
341 foreach ( $this->errors as $index => $error ) {
342 if ( $error['message'] === $source ) {
343 $this->errors[$index]['message'] = $dest;
344 $replaced = true;
345 }
346 }
347 return $replaced;
348 }
349
350 /**
351 * Backward compatibility function for WikiError -> Status migration
352 *
353 * @return String
354 */
355 public function getMessage() {
356 return $this->getWikiText();
357 }
358 }