* Follow-up r84610: don't assume a Parser object is attached
[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 failed */
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 to 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 protected function cleanParams( $params ) {
131 if ( !$this->cleanCallback ) {
132 return $params;
133 }
134 $cleanParams = array();
135 foreach ( $params as $i => $param ) {
136 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
137 }
138 return $cleanParams;
139 }
140
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, wfMsgReal( $item['message'], $params ) ) ."\n";
146 foreach ( $params as $param ) {
147 $xml .= Xml::element( 'param', null, $param );
148 }
149 $xml .= "</{$this->type}>\n";
150 return $xml;
151 }
152
153 /**
154 * Get the error list as XML
155 */
156 function getXML() {
157 $xml = "<errors>\n";
158 foreach ( $this->errors as $error ) {
159 $xml .= $this->getItemXML( $error );
160 }
161 $xml .= "</errors>\n";
162 return $xml;
163 }
164
165 /**
166 * Get the error list as a wikitext formatted list
167 *
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
171 * @return String
172 */
173 function getWikiText( $shortContext = false, $longContext = false ) {
174 if ( count( $this->errors ) == 0 ) {
175 if ( $this->ok ) {
176 $this->fatal( 'internalerror_info',
177 __METHOD__." called for a good result, this is incorrect\n" );
178 } else {
179 $this->fatal( 'internalerror_info',
180 __METHOD__.": Invalid result object: no error text but not OK\n" );
181 }
182 }
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" );
189 }
190 } else {
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" );
197 }
198 }
199 return $s;
200 }
201
202 /**
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.
208 *
209 * @return String
210 */
211 protected function getWikiTextForError( $error ) {
212 if ( is_array( $error ) ) {
213 if ( isset( $error['message'] ) && isset( $error['params'] ) ) {
214 return wfMsgReal( $error['message'],
215 array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ),
216 true, false, false );
217 } else {
218 $message = array_shift($error);
219 return wfMsgReal( $message,
220 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ),
221 true, false, false );
222 }
223 } else {
224 return wfMsgReal( $error, array(), true, false, false);
225 }
226 }
227
228 /**
229 * Return an array with the wikitext for each item in the array.
230 * @param $errors Array
231 * @return Array
232 */
233 function getWikiTextArray( $errors ) {
234 return array_map( array( $this, 'getWikiTextForError' ), $errors );
235 }
236
237 /**
238 * Merge another status object into this one
239 *
240 * @param $other Status Other Status object
241 * @param $overwriteValue Boolean: whether to override the "value" member
242 */
243 function merge( $other, $overwriteValue = false ) {
244 $this->errors = array_merge( $this->errors, $other->errors );
245 $this->ok = $this->ok && $other->ok;
246 if ( $overwriteValue ) {
247 $this->value = $other->value;
248 }
249 $this->successCount += $other->successCount;
250 $this->failCount += $other->failCount;
251 }
252
253 /**
254 * Get the list of errors (but not warnings)
255 *
256 * @return Array
257 */
258 function getErrorsArray() {
259 return $this->getStatusArray( "error" );
260 }
261
262 /**
263 * Get the list of warnings (but not errors)
264 *
265 * @return Array
266 */
267 function getWarningsArray() {
268 return $this->getStatusArray( "warning" );
269 }
270
271 /**
272 * Returns a list of status messages of the given type
273 * @param $type String
274 *
275 * @return Array
276 */
277 protected function getStatusArray( $type ) {
278 $result = array();
279 foreach ( $this->errors as $error ) {
280 if ( $error['type'] === $type ) {
281 if( $error['params'] ) {
282 $result[] = array_merge( array( $error['message'] ), $error['params'] );
283 } else {
284 $result[] = array( $error['message'] );
285 }
286 }
287 }
288 return $result;
289 }
290
291 /**
292 * Returns a list of status messages of the given type, with message and
293 * params left untouched, like a sane version of getStatusArray
294 *
295 * @param $type String
296 *
297 * @return Array
298 */
299 public function getErrorsByType( $type ) {
300 $result = array();
301 foreach ( $this->errors as $error ) {
302 if ( $error['type'] === $type ) {
303 $result[] = $error;
304 }
305 }
306 return $result;
307 }
308 /**
309 * Returns true if the specified message is present as a warning or error
310 *
311 * @param $msg String: message name
312 * @return Boolean
313 */
314 function hasMessage( $msg ) {
315 foreach ( $this->errors as $error ) {
316 if ( $error['message'] === $msg ) {
317 return true;
318 }
319 }
320 return false;
321 }
322
323 /**
324 * If the specified source message exists, replace it with the specified
325 * destination message, but keep the same parameters as in the original error.
326 *
327 * Return true if the replacement was done, false otherwise.
328 */
329 function replaceMessage( $source, $dest ) {
330 $replaced = false;
331 foreach ( $this->errors as $index => $error ) {
332 if ( $error['message'] === $source ) {
333 $this->errors[$index]['message'] = $dest;
334 $replaced = true;
335 }
336 }
337 return $replaced;
338 }
339
340 /**
341 * Backward compatibility function for WikiError -> Status migration
342 *
343 * @return String
344 */
345 public function getMessage() {
346 return $this->getWikiText();
347 }
348 }