"<p id="userloginlink"><p>Don't have an account" is illegal xhtml
[mediawiki.git] / includes / Status.php
blob7f47c1f65f86e7f941d9ce5f9bf02d525183b92f
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 var $successCount = 0, $failCount = 0;
22 /*semi-private*/ var $errors = array();
23 /*semi-private*/ var $cleanCallback = false;
25 /**
26 * Factory function for fatal errors
28 * @param $message String: message name
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;
38 /**
39 * Factory function for good results
41 * @param $value Mixed
43 static function newGood( $value = null ) {
44 $result = new self;
45 $result->value = $value;
46 return $result;
49 /**
50 * Change operation result
52 * @param $ok Boolean: whether to operation completed
53 * @param $value Mixed
55 function setResult( $ok, $value = null ) {
56 $this->ok = $ok;
57 $this->value = $value;
60 /**
61 * Returns whether the operation completed and didn't have any error or
62 * warnings
64 * @return Boolean
66 function isGood() {
67 return $this->ok && !$this->errors;
70 /**
71 * Returns whether the operation completed
73 * @return Boolean
75 function isOK() {
76 return $this->ok;
79 /**
80 * Add a new warning
82 * @param $message String: message name
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 );
92 /**
93 * Add an error, do not set fatal flag
94 * This can be used for non-fatal errors
96 * @param $message String: message name
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 );
107 * Add an error and set OK to false, indicating that the operation
108 * as a whole was fatal
110 * @param $message String: message name
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;
122 * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
124 function __wakeup() {
125 $this->cleanCallback = false;
128 protected function cleanParams( $params ) {
129 if ( !$this->cleanCallback ) {
130 return $params;
132 $cleanParams = array();
133 foreach ( $params as $i => $param ) {
134 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
136 return $cleanParams;
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 );
147 $xml .= "</{$this->type}>\n";
148 return $xml;
152 * Get the error list as XML
154 function getXML() {
155 $xml = "<errors>\n";
156 foreach ( $this->errors as $error ) {
157 $xml .= $this->getItemXML( $error );
159 $xml .= "</errors>\n";
160 return $xml;
164 * Get the error list as a wikitext formatted list
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
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" );
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" );
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";
195 if ( $longContext ) {
196 $s = wfMsgNoTrans( $longContext, $s );
197 } elseif ( $shortContext ) {
198 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
201 return $s;
205 * Merge another status object into this one
207 * @param $other Other Status object
208 * @param $overwriteValue Boolean: whether to override the "value" member
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;
216 $this->successCount += $other->successCount;
217 $this->failCount += $other->failCount;
221 * Get the list of errors (but not warnings)
223 * @return Array
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'];
234 return $result;
238 * Returns true if the specified message is present as a warning or error
240 * @param $msg String: message name
241 * @return Boolean
243 function hasMessage( $msg ) {
244 foreach ( $this->errors as $error ) {
245 if ( $error['message'] === $msg ) {
246 return true;
249 return false;