MediaWiki
REL1_20
|
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 00233 function getWikiTextArray( $errors ) { 00234 return array_map( array( $this, 'getWikiTextForError' ), $errors ); 00235 } 00236 00243 function merge( $other, $overwriteValue = false ) { 00244 $this->errors = array_merge( $this->errors, $other->errors ); 00245 $this->ok = $this->ok && $other->ok; 00246 if ( $overwriteValue ) { 00247 $this->value = $other->value; 00248 } 00249 $this->successCount += $other->successCount; 00250 $this->failCount += $other->failCount; 00251 } 00252 00258 function getErrorsArray() { 00259 return $this->getStatusArray( "error" ); 00260 } 00261 00267 function getWarningsArray() { 00268 return $this->getStatusArray( "warning" ); 00269 } 00270 00277 protected function getStatusArray( $type ) { 00278 $result = array(); 00279 foreach ( $this->errors as $error ) { 00280 if ( $error['type'] === $type ) { 00281 if( $error['params'] ) { 00282 $result[] = array_merge( array( $error['message'] ), $error['params'] ); 00283 } else { 00284 $result[] = array( $error['message'] ); 00285 } 00286 } 00287 } 00288 return $result; 00289 } 00290 00299 public function getErrorsByType( $type ) { 00300 $result = array(); 00301 foreach ( $this->errors as $error ) { 00302 if ( $error['type'] === $type ) { 00303 $result[] = $error; 00304 } 00305 } 00306 return $result; 00307 } 00308 00315 function hasMessage( $msg ) { 00316 foreach ( $this->errors as $error ) { 00317 if ( $error['message'] === $msg ) { 00318 return true; 00319 } 00320 } 00321 return false; 00322 } 00323 00332 function replaceMessage( $source, $dest ) { 00333 $replaced = false; 00334 foreach ( $this->errors as $index => $error ) { 00335 if ( $error['message'] === $source ) { 00336 $this->errors[$index]['message'] = $dest; 00337 $replaced = true; 00338 } 00339 } 00340 return $replaced; 00341 } 00342 00348 public function getMessage() { 00349 return $this->getWikiText(); 00350 } 00351 00355 public function getValue() { 00356 return $this->value; 00357 } 00358 }