Fixed some doxygen warnings and documented a bit
[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 var $successCount = 0, $failCount = 0;
21
22 /*semi-private*/ var $errors = array();
23 /*semi-private*/ var $cleanCallback = false;
24
25 /**
26 * Factory function for fatal errors
27 *
28 * @param $message String: message name
29 */
30 static function newFatal( $message /*, parameters...*/ ) {
31 $params = func_get_args();
32 $result = new self;
33 call_user_func_array( array( &$result, 'error' ), $params );
34 $result->ok = false;
35 return $result;
36 }
37
38 /**
39 * Factory function for good results
40 *
41 * @param $value Mixed
42 */
43 static function newGood( $value = null ) {
44 $result = new self;
45 $result->value = $value;
46 return $result;
47 }
48
49 /**
50 * Change operation result
51 *
52 * @param $ok Boolean: whether to operation completed
53 * @param $value Mixed
54 */
55 function setResult( $ok, $value = null ) {
56 $this->ok = $ok;
57 $this->value = $value;
58 }
59
60 /**
61 * Returns whether the operation completed and didn't have any error or
62 * warnings
63 *
64 * @return Boolean
65 */
66 function isGood() {
67 return $this->ok && !$this->errors;
68 }
69
70 /**
71 * Returns whether the operation completed
72 *
73 * @return Boolean
74 */
75 function isOK() {
76 return $this->ok;
77 }
78
79 /**
80 * Add a new warning
81 *
82 * @param $message String: message name
83 */
84 function warning( $message /*, parameters... */ ) {
85 $params = array_slice( func_get_args(), 1 );
86 $this->errors[] = array(
87 'type' => 'warning',
88 'message' => $message,
89 'params' => $params );
90 }
91
92 /**
93 * Add an error, do not set fatal flag
94 * This can be used for non-fatal errors
95 *
96 * @param $message String: message name
97 */
98 function error( $message /*, parameters... */ ) {
99 $params = array_slice( func_get_args(), 1 );
100 $this->errors[] = array(
101 'type' => 'error',
102 'message' => $message,
103 'params' => $params );
104 }
105
106 /**
107 * Add an error and set OK to false, indicating that the operation
108 * as a whole was fatal
109 *
110 * @param $message String: message name
111 */
112 function fatal( $message /*, parameters... */ ) {
113 $params = array_slice( func_get_args(), 1 );
114 $this->errors[] = array(
115 'type' => 'error',
116 'message' => $message,
117 'params' => $params );
118 $this->ok = false;
119 }
120
121 /**
122 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
123 */
124 function __wakeup() {
125 $this->cleanCallback = false;
126 }
127
128 protected function cleanParams( $params ) {
129 if ( !$this->cleanCallback ) {
130 return $params;
131 }
132 $cleanParams = array();
133 foreach ( $params as $i => $param ) {
134 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
135 }
136 return $cleanParams;
137 }
138
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 );
146 }
147 $xml .= "</{$this->type}>\n";
148 return $xml;
149 }
150
151 /**
152 * Get the error list as XML
153 */
154 function getXML() {
155 $xml = "<errors>\n";
156 foreach ( $this->errors as $error ) {
157 $xml .= $this->getItemXML( $error );
158 }
159 $xml .= "</errors>\n";
160 return $xml;
161 }
162
163 /**
164 * Get the error list as a wikitext formatted list
165 *
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
169 * @return String
170 */
171 function getWikiText( $shortContext = false, $longContext = false ) {
172 if ( count( $this->errors ) == 0 ) {
173 if ( $this->ok ) {
174 $this->fatal( 'internalerror_info',
175 __METHOD__." called for a good result, this is incorrect\n" );
176 } else {
177 $this->fatal( 'internalerror_info',
178 __METHOD__.": Invalid result object: no error text but not OK\n" );
179 }
180 }
181 if ( count( $this->errors ) == 1 ) {
182 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $this->errors[0]['params'] ) );
183 $s = wfMsgReal( $this->errors[0]['message'], $params, true, false, false );
184 if ( $shortContext ) {
185 $s = wfMsgNoTrans( $shortContext, $s );
186 } elseif ( $longContext ) {
187 $s = wfMsgNoTrans( $longContext, "* $s\n" );
188 }
189 } else {
190 $s = '';
191 foreach ( $this->errors as $error ) {
192 $params = array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) );
193 $s .= '* ' . wfMsgReal( $error['message'], $params, true, false, false ) . "\n";
194 }
195 if ( $longContext ) {
196 $s = wfMsgNoTrans( $longContext, $s );
197 } elseif ( $shortContext ) {
198 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
199 }
200 }
201 return $s;
202 }
203
204 /**
205 * Merge another status object into this one
206 *
207 * @param $other Other Status object
208 * @param $overwriteValue Boolean: whether to override the "value" member
209 */
210 function merge( $other, $overwriteValue = false ) {
211 $this->errors = array_merge( $this->errors, $other->errors );
212 $this->ok = $this->ok && $other->ok;
213 if ( $overwriteValue ) {
214 $this->value = $other->value;
215 }
216 $this->successCount += $other->successCount;
217 $this->failCount += $other->failCount;
218 }
219
220 /**
221 * Get the list of errors (but not warnings)
222 *
223 * @return Array
224 */
225 function getErrorsArray() {
226 $result = array();
227 foreach ( $this->errors as $error ) {
228 if ( $error['type'] == 'error' )
229 if( $error['params'] )
230 $result[] = array_merge( array( $error['message'] ), $error['params'] );
231 else
232 $result[] = $error['message'];
233 }
234 return $result;
235 }
236
237 /**
238 * Returns true if the specified message is present as a warning or error
239 *
240 * @param $msg String: message name
241 * @return Boolean
242 */
243 function hasMessage( $msg ) {
244 foreach ( $this->errors as $error ) {
245 if ( $error['message'] === $msg ) {
246 return true;
247 }
248 }
249 return false;
250 }
251 }