MediaWiki  REL1_21
Status.php
Go to the documentation of this file.
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->getErrorMessage( $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->getErrorMessageArray( $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 getErrorMessage( $error ) {
00214                 if ( is_array( $error ) ) {
00215                         if( isset( $error['message'] ) && $error['message'] instanceof Message ) {
00216                                 $msg = $error['message'];
00217                         } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
00218                                 $msg = wfMessage( $error['message'],
00219                                         array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
00220                         } else {
00221                                 $msgName = array_shift( $error );
00222                                 $msg = wfMessage( $msgName,
00223                                         array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
00224                         }
00225                 } else {
00226                         $msg = wfMessage( $error );
00227                 }
00228                 return $msg->plain();
00229         }
00230 
00239         public function getHTML( $shortContext = false, $longContext = false ) {
00240                 $text = $this->getWikiText( $shortContext, $longContext );
00241                 return MessageCache::singleton()->transform( $text, true );
00242         }
00243 
00249         protected function getErrorMessageArray( $errors ) {
00250                 return array_map( array( $this, 'getErrorMessage' ), $errors );
00251         }
00252 
00259         function merge( $other, $overwriteValue = false ) {
00260                 $this->errors = array_merge( $this->errors, $other->errors );
00261                 $this->ok = $this->ok && $other->ok;
00262                 if ( $overwriteValue ) {
00263                         $this->value = $other->value;
00264                 }
00265                 $this->successCount += $other->successCount;
00266                 $this->failCount += $other->failCount;
00267         }
00268 
00274         function getErrorsArray() {
00275                 return $this->getStatusArray( "error" );
00276         }
00277 
00283         function getWarningsArray() {
00284                 return $this->getStatusArray( "warning" );
00285         }
00286 
00293         protected function getStatusArray( $type ) {
00294                 $result = array();
00295                 foreach ( $this->errors as $error ) {
00296                         if ( $error['type'] === $type ) {
00297                                 if( $error['message'] instanceof Message ) {
00298                                         $result[] = $error['message'];
00299                                 } elseif( $error['params'] ) {
00300                                         $result[] = array_merge( array( $error['message'] ), $error['params'] );
00301                                 } else {
00302                                         $result[] = array( $error['message'] );
00303                                 }
00304                         }
00305                 }
00306                 return $result;
00307         }
00308 
00317         public function getErrorsByType( $type ) {
00318                 $result = array();
00319                 foreach ( $this->errors as $error ) {
00320                         if ( $error['type'] === $type ) {
00321                                 $result[] = $error;
00322                         }
00323                 }
00324                 return $result;
00325         }
00326 
00336         function hasMessage( $msg ) {
00337                 foreach ( $this->errors as $error ) {
00338                         if ( $error['message'] === $msg ) {
00339                                 return true;
00340                         }
00341                 }
00342                 return false;
00343         }
00344 
00356         function replaceMessage( $source, $dest ) {
00357                 $replaced = false;
00358                 foreach ( $this->errors as $index => $error ) {
00359                         if ( $error['message'] === $source ) {
00360                                 $this->errors[$index]['message'] = $dest;
00361                                 $replaced = true;
00362                         }
00363                 }
00364                 return $replaced;
00365         }
00366 
00372         public function getMessage() {
00373                 return $this->getWikiText();
00374         }
00375 
00379         public function getValue() {
00380                 return $this->value;
00381         }
00382 }