MediaWiki  REL1_22
Status.php
Go to the documentation of this file.
00001 <?php
00040 class Status {
00041     var $ok = true;
00042     var $value;
00043 
00045     public $successCount = 0, $failCount = 0;
00047     public $success = array();
00048 
00049     /*semi-private*/ var $errors = array();
00050     /*semi-private*/ var $cleanCallback = false;
00051 
00058     static function newFatal( $message /*, parameters...*/ ) {
00059         $params = func_get_args();
00060         $result = new self;
00061         call_user_func_array( array( &$result, 'error' ), $params );
00062         $result->ok = false;
00063         return $result;
00064     }
00065 
00072     static function newGood( $value = null ) {
00073         $result = new self;
00074         $result->value = $value;
00075         return $result;
00076     }
00077 
00084     function setResult( $ok, $value = null ) {
00085         $this->ok = $ok;
00086         $this->value = $value;
00087     }
00088 
00095     function isGood() {
00096         return $this->ok && !$this->errors;
00097     }
00098 
00104     function isOK() {
00105         return $this->ok;
00106     }
00107 
00113     function warning( $message /*, parameters... */ ) {
00114         $params = array_slice( func_get_args(), 1 );
00115         $this->errors[] = array(
00116             'type' => 'warning',
00117             'message' => $message,
00118             'params' => $params );
00119     }
00120 
00127     function error( $message /*, parameters... */ ) {
00128         $params = array_slice( func_get_args(), 1 );
00129         $this->errors[] = array(
00130             'type' => 'error',
00131             'message' => $message,
00132             'params' => $params );
00133     }
00134 
00141     function fatal( $message /*, parameters... */ ) {
00142         $params = array_slice( func_get_args(), 1 );
00143         $this->errors[] = array(
00144             'type' => 'error',
00145             'message' => $message,
00146             'params' => $params );
00147         $this->ok = false;
00148     }
00149 
00153     function __wakeup() {
00154         $this->cleanCallback = false;
00155     }
00156 
00161     protected function cleanParams( $params ) {
00162         if ( !$this->cleanCallback ) {
00163             return $params;
00164         }
00165         $cleanParams = array();
00166         foreach ( $params as $i => $param ) {
00167             $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
00168         }
00169         return $cleanParams;
00170     }
00171 
00180     function getWikiText( $shortContext = false, $longContext = false ) {
00181         if ( count( $this->errors ) == 0 ) {
00182             if ( $this->ok ) {
00183                 $this->fatal( 'internalerror_info',
00184                     __METHOD__ . " called for a good result, this is incorrect\n" );
00185             } else {
00186                 $this->fatal( 'internalerror_info',
00187                     __METHOD__ . ": Invalid result object: no error text but not OK\n" );
00188             }
00189         }
00190         if ( count( $this->errors ) == 1 ) {
00191             $s = $this->getErrorMessage( $this->errors[0] )->plain();
00192             if ( $shortContext ) {
00193                 $s = wfMessage( $shortContext, $s )->plain();
00194             } elseif ( $longContext ) {
00195                 $s = wfMessage( $longContext, "* $s\n" )->plain();
00196             }
00197         } else {
00198             $errors = $this->getErrorMessageArray( $this->errors );
00199             foreach ( $errors as &$error ) {
00200                 $error = $error->plain();
00201             }
00202             $s = '* ' . implode( "\n* ", $errors ) . "\n";
00203             if ( $longContext ) {
00204                 $s = wfMessage( $longContext, $s )->plain();
00205             } elseif ( $shortContext ) {
00206                 $s = wfMessage( $shortContext, "\n$s\n" )->plain();
00207             }
00208         }
00209         return $s;
00210     }
00211 
00220     function getMessage( $shortContext = false, $longContext = false ) {
00221         if ( count( $this->errors ) == 0 ) {
00222             if ( $this->ok ) {
00223                 $this->fatal( 'internalerror_info',
00224                     __METHOD__ . " called for a good result, this is incorrect\n" );
00225             } else {
00226                 $this->fatal( 'internalerror_info',
00227                     __METHOD__ . ": Invalid result object: no error text but not OK\n" );
00228             }
00229         }
00230         if ( count( $this->errors ) == 1 ) {
00231             $s = $this->getErrorMessage( $this->errors[0] );
00232             if ( $shortContext ) {
00233                 $s = wfMessage( $shortContext, $s );
00234             } elseif ( $longContext ) {
00235                 $wrapper = new RawMessage( "* \$1\n" );
00236                 $wrapper->params( $s )->parse();
00237                 $s = wfMessage( $longContext, $wrapper );
00238             }
00239         } else {
00240             $msgs =  $this->getErrorMessageArray( $this->errors );
00241             $msgCount = count( $msgs );
00242 
00243             if ( $shortContext ) {
00244                 $msgCount++;
00245             }
00246 
00247             $wrapper = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
00248             $s = $wrapper->params( $msgs )->parse();
00249 
00250             if ( $longContext ) {
00251                 $s = wfMessage( $longContext, $wrapper );
00252             } elseif ( $shortContext ) {
00253                 $wrapper = new RawMessage( "\n\$1\n", $wrapper );
00254                 $wrapper->parse();
00255                 $s = wfMessage( $shortContext, $wrapper );
00256             }
00257         }
00258 
00259         return $s;
00260     }
00261 
00271     protected function getErrorMessage( $error ) {
00272         if ( is_array( $error ) ) {
00273             if ( isset( $error['message'] ) && $error['message'] instanceof Message ) {
00274                 $msg = $error['message'];
00275             } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
00276                 $msg = wfMessage( $error['message'],
00277                     array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
00278             } else {
00279                 $msgName = array_shift( $error );
00280                 $msg = wfMessage( $msgName,
00281                     array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
00282             }
00283         } else {
00284             $msg = wfMessage( $error );
00285         }
00286         return $msg;
00287     }
00288 
00297     public function getHTML( $shortContext = false, $longContext = false ) {
00298         $text = $this->getWikiText( $shortContext, $longContext );
00299         return MessageCache::singleton()->transform( $text, true );
00300     }
00301 
00307     protected function getErrorMessageArray( $errors ) {
00308         return array_map( array( $this, 'getErrorMessage' ), $errors );
00309     }
00310 
00317     function merge( $other, $overwriteValue = false ) {
00318         $this->errors = array_merge( $this->errors, $other->errors );
00319         $this->ok = $this->ok && $other->ok;
00320         if ( $overwriteValue ) {
00321             $this->value = $other->value;
00322         }
00323         $this->successCount += $other->successCount;
00324         $this->failCount += $other->failCount;
00325     }
00326 
00333     function getErrorsArray() {
00334         return $this->getStatusArray( "error" );
00335     }
00336 
00343     function getWarningsArray() {
00344         return $this->getStatusArray( "warning" );
00345     }
00346 
00353     protected function getStatusArray( $type ) {
00354         $result = array();
00355         foreach ( $this->errors as $error ) {
00356             if ( $error['type'] === $type ) {
00357                 if ( $error['message'] instanceof Message ) {
00358                     $result[] = array_merge( array( $error['message']->getKey() ), $error['message']->getParams() );
00359                 } elseif ( $error['params'] ) {
00360                     $result[] = array_merge( array( $error['message'] ), $error['params'] );
00361                 } else {
00362                     $result[] = array( $error['message'] );
00363                 }
00364             }
00365         }
00366         return $result;
00367     }
00368 
00377     public function getErrorsByType( $type ) {
00378         $result = array();
00379         foreach ( $this->errors as $error ) {
00380             if ( $error['type'] === $type ) {
00381                 $result[] = $error;
00382             }
00383         }
00384         return $result;
00385     }
00386 
00396     function hasMessage( $msg ) {
00397         foreach ( $this->errors as $error ) {
00398             if ( $error['message'] === $msg ) {
00399                 return true;
00400             }
00401         }
00402         return false;
00403     }
00404 
00416     function replaceMessage( $source, $dest ) {
00417         $replaced = false;
00418         foreach ( $this->errors as $index => $error ) {
00419             if ( $error['message'] === $source ) {
00420                 $this->errors[$index]['message'] = $dest;
00421                 $replaced = true;
00422             }
00423         }
00424         return $replaced;
00425     }
00426 
00430     public function getValue() {
00431         return $this->value;
00432     }
00433 }