MediaWiki
master
|
00001 <?php 00035 class Status { 00036 var $ok = true; 00037 var $value; 00038 00040 public $successCount = 0, $failCount = 0; 00042 public $success = array(); 00043 00044 /*semi-private*/ var $errors = array(); 00045 /*semi-private*/ var $cleanCallback = false; 00046 00053 static function newFatal( $message /*, parameters...*/ ) { 00054 $params = func_get_args(); 00055 $result = new self; 00056 call_user_func_array( array( &$result, 'error' ), $params ); 00057 $result->ok = false; 00058 return $result; 00059 } 00060 00067 static function newGood( $value = null ) { 00068 $result = new self; 00069 $result->value = $value; 00070 return $result; 00071 } 00072 00079 function setResult( $ok, $value = null ) { 00080 $this->ok = $ok; 00081 $this->value = $value; 00082 } 00083 00090 function isGood() { 00091 return $this->ok && !$this->errors; 00092 } 00093 00099 function isOK() { 00100 return $this->ok; 00101 } 00102 00108 function warning( $message /*, parameters... */ ) { 00109 $params = array_slice( func_get_args(), 1 ); 00110 $this->errors[] = array( 00111 'type' => 'warning', 00112 'message' => $message, 00113 'params' => $params ); 00114 } 00115 00122 function error( $message /*, parameters... */ ) { 00123 $params = array_slice( func_get_args(), 1 ); 00124 $this->errors[] = array( 00125 'type' => 'error', 00126 'message' => $message, 00127 'params' => $params ); 00128 } 00129 00136 function fatal( $message /*, parameters... */ ) { 00137 $params = array_slice( func_get_args(), 1 ); 00138 $this->errors[] = array( 00139 'type' => 'error', 00140 'message' => $message, 00141 'params' => $params ); 00142 $this->ok = false; 00143 } 00144 00148 function __wakeup() { 00149 $this->cleanCallback = false; 00150 } 00151 00156 protected function cleanParams( $params ) { 00157 if ( !$this->cleanCallback ) { 00158 return $params; 00159 } 00160 $cleanParams = array(); 00161 foreach ( $params as $i => $param ) { 00162 $cleanParams[$i] = call_user_func( $this->cleanCallback, $param ); 00163 } 00164 return $cleanParams; 00165 } 00166 00175 function getWikiText( $shortContext = false, $longContext = false ) { 00176 if ( count( $this->errors ) == 0 ) { 00177 if ( $this->ok ) { 00178 $this->fatal( 'internalerror_info', 00179 __METHOD__." called for a good result, this is incorrect\n" ); 00180 } else { 00181 $this->fatal( 'internalerror_info', 00182 __METHOD__.": Invalid result object: no error text but not OK\n" ); 00183 } 00184 } 00185 if ( count( $this->errors ) == 1 ) { 00186 $s = $this->getWikiTextForError( $this->errors[0], $this->errors[0] ); 00187 if ( $shortContext ) { 00188 $s = wfMessage( $shortContext, $s )->plain(); 00189 } elseif ( $longContext ) { 00190 $s = wfMessage( $longContext, "* $s\n" )->plain(); 00191 } 00192 } else { 00193 $s = '* '. implode("\n* ", 00194 $this->getWikiTextArray( $this->errors ) ) . "\n"; 00195 if ( $longContext ) { 00196 $s = wfMessage( $longContext, $s )->plain(); 00197 } elseif ( $shortContext ) { 00198 $s = wfMessage( $shortContext, "\n$s\n" )->plain(); 00199 } 00200 } 00201 return $s; 00202 } 00203 00213 protected function getWikiTextForError( $error ) { 00214 if ( is_array( $error ) ) { 00215 if ( isset( $error['message'] ) && isset( $error['params'] ) ) { 00216 return wfMessage( $error['message'], 00217 array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) )->plain(); 00218 } else { 00219 $message = array_shift($error); 00220 return wfMessage( $message, 00221 array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) )->plain(); 00222 } 00223 } else { 00224 return wfMessage( $error )->plain(); 00225 } 00226 } 00227 00232 public function getHTML( $shortContext = false, $longContext = false ) { 00233 $text = $this->getWikiText( $shortContext, $longContext ); 00234 return MessageCache::singleton()->transform( $text, true ); 00235 } 00236 00242 function getWikiTextArray( $errors ) { 00243 return array_map( array( $this, 'getWikiTextForError' ), $errors ); 00244 } 00245 00252 function merge( $other, $overwriteValue = false ) { 00253 $this->errors = array_merge( $this->errors, $other->errors ); 00254 $this->ok = $this->ok && $other->ok; 00255 if ( $overwriteValue ) { 00256 $this->value = $other->value; 00257 } 00258 $this->successCount += $other->successCount; 00259 $this->failCount += $other->failCount; 00260 } 00261 00267 function getErrorsArray() { 00268 return $this->getStatusArray( "error" ); 00269 } 00270 00276 function getWarningsArray() { 00277 return $this->getStatusArray( "warning" ); 00278 } 00279 00286 protected function getStatusArray( $type ) { 00287 $result = array(); 00288 foreach ( $this->errors as $error ) { 00289 if ( $error['type'] === $type ) { 00290 if( $error['params'] ) { 00291 $result[] = array_merge( array( $error['message'] ), $error['params'] ); 00292 } else { 00293 $result[] = array( $error['message'] ); 00294 } 00295 } 00296 } 00297 return $result; 00298 } 00299 00308 public function getErrorsByType( $type ) { 00309 $result = array(); 00310 foreach ( $this->errors as $error ) { 00311 if ( $error['type'] === $type ) { 00312 $result[] = $error; 00313 } 00314 } 00315 return $result; 00316 } 00317 00324 function hasMessage( $msg ) { 00325 foreach ( $this->errors as $error ) { 00326 if ( $error['message'] === $msg ) { 00327 return true; 00328 } 00329 } 00330 return false; 00331 } 00332 00341 function replaceMessage( $source, $dest ) { 00342 $replaced = false; 00343 foreach ( $this->errors as $index => $error ) { 00344 if ( $error['message'] === $source ) { 00345 $this->errors[$index]['message'] = $dest; 00346 $replaced = true; 00347 } 00348 } 00349 return $replaced; 00350 } 00351 00357 public function getMessage() { 00358 return $this->getWikiText(); 00359 } 00360 00364 public function getValue() { 00365 return $this->value; 00366 } 00367 }