MediaWiki  REL1_19
Status.php
Go to the documentation of this file.
00001 <?php
00002 
00015 class Status {
00016         var $ok = true;
00017         var $value;
00018 
00020         public $successCount = 0, $failCount = 0;
00022         public $success = array();
00023 
00024         /*semi-private*/ var $errors = array();
00025         /*semi-private*/ var $cleanCallback = false;
00026 
00032         static function newFatal( $message /*, parameters...*/ ) {
00033                 $params = func_get_args();
00034                 $result = new self;
00035                 call_user_func_array( array( &$result, 'error' ), $params );
00036                 $result->ok = false;
00037                 return $result;
00038         }
00039 
00045         static function newGood( $value = null ) {
00046                 $result = new self;
00047                 $result->value = $value;
00048                 return $result;
00049         }
00050 
00057         function setResult( $ok, $value = null ) {
00058                 $this->ok = $ok;
00059                 $this->value = $value;
00060         }
00061 
00068         function isGood() {
00069                 return $this->ok && !$this->errors;
00070         }
00071 
00077         function isOK() {
00078                 return $this->ok;
00079         }
00080 
00086         function warning( $message /*, parameters... */ ) {
00087                 $params = array_slice( func_get_args(), 1 );
00088                 $this->errors[] = array(
00089                         'type' => 'warning',
00090                         'message' => $message,
00091                         'params' => $params );
00092         }
00093 
00100         function error( $message /*, parameters... */ ) {
00101                 $params = array_slice( func_get_args(), 1 );
00102                 $this->errors[] = array(
00103                         'type' => 'error',
00104                         'message' => $message,
00105                         'params' => $params );
00106         }
00107 
00114         function fatal( $message /*, parameters... */ ) {
00115                 $params = array_slice( func_get_args(), 1 );
00116                 $this->errors[] = array(
00117                         'type' => 'error',
00118                         'message' => $message,
00119                         'params' => $params );
00120                 $this->ok = false;
00121         }
00122 
00126         function __wakeup() {
00127                 $this->cleanCallback = false;
00128         }
00129 
00134         protected function cleanParams( $params ) {
00135                 if ( !$this->cleanCallback ) {
00136                         return $params;
00137                 }
00138                 $cleanParams = array();
00139                 foreach ( $params as $i => $param ) {
00140                         $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
00141                 }
00142                 return $cleanParams;
00143         }
00144 
00149         protected function getItemXML( $item ) {
00150                 $params = $this->cleanParams( $item['params'] );
00151                 $xml = "<{$item['type']}>\n" .
00152                         Xml::element( 'message', null, $item['message'] ) . "\n" .
00153                         Xml::element( 'text', null, wfMsg( $item['message'], $params ) ) ."\n";
00154                 foreach ( $params as $param ) {
00155                         $xml .= Xml::element( 'param', null, $param );
00156                 }
00157                 $xml .= "</{$item['type']}>\n";
00158                 return $xml;
00159         }
00160 
00165         function getXML() {
00166                 $xml = "<errors>\n";
00167                 foreach ( $this->errors as $error ) {
00168                         $xml .= $this->getItemXML( $error );
00169                 }
00170                 $xml .= "</errors>\n";
00171                 return $xml;
00172         }
00173 
00182         function getWikiText( $shortContext = false, $longContext = false ) {
00183                 if ( count( $this->errors ) == 0 ) {
00184                         if ( $this->ok ) {
00185                                 $this->fatal( 'internalerror_info',
00186                                         __METHOD__." called for a good result, this is incorrect\n" );
00187                         } else {
00188                                 $this->fatal( 'internalerror_info',
00189                                         __METHOD__.": Invalid result object: no error text but not OK\n" );
00190                         }
00191                 }
00192                 if ( count( $this->errors ) == 1 ) {
00193                         $s = $this->getWikiTextForError( $this->errors[0], $this->errors[0]  );
00194                         if ( $shortContext ) {
00195                                 $s = wfMsgNoTrans( $shortContext, $s );
00196                         } elseif ( $longContext ) {
00197                                 $s = wfMsgNoTrans( $longContext, "* $s\n" );
00198                         }
00199                 } else {
00200                         $s = '* '. implode("\n* ",
00201                                 $this->getWikiTextArray( $this->errors ) ) . "\n";
00202                         if ( $longContext ) {
00203                                 $s = wfMsgNoTrans( $longContext, $s );
00204                         } elseif ( $shortContext ) {
00205                                 $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
00206                         }
00207                 }
00208                 return $s;
00209         }
00210 
00220         protected function getWikiTextForError( $error ) {
00221                 if ( is_array( $error ) ) {
00222                         if ( isset( $error['message'] ) && isset( $error['params'] ) ) {
00223                                 return wfMsgNoTrans( $error['message'],
00224                                         array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) )  );
00225                         } else {
00226                                 $message = array_shift($error);
00227                                 return wfMsgNoTrans( $message,
00228                                         array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
00229                         }
00230                 } else {
00231                         return wfMsgNoTrans( $error );
00232                 }
00233         }
00234 
00240         function getWikiTextArray( $errors ) {
00241                 return array_map( array( $this, 'getWikiTextForError' ), $errors );
00242         }
00243 
00250         function merge( $other, $overwriteValue = false ) {
00251                 $this->errors = array_merge( $this->errors, $other->errors );
00252                 $this->ok = $this->ok && $other->ok;
00253                 if ( $overwriteValue ) {
00254                         $this->value = $other->value;
00255                 }
00256                 $this->successCount += $other->successCount;
00257                 $this->failCount += $other->failCount;
00258         }
00259 
00265         function getErrorsArray() {
00266                 return $this->getStatusArray( "error" );
00267         }
00268 
00274         function getWarningsArray() {
00275                 return $this->getStatusArray( "warning" );
00276         }
00277 
00284         protected function getStatusArray( $type ) {
00285                 $result = array();
00286                 foreach ( $this->errors as $error ) {
00287                         if ( $error['type'] === $type ) {
00288                                 if( $error['params'] ) {
00289                                         $result[] = array_merge( array( $error['message'] ), $error['params'] );
00290                                 } else {
00291                                         $result[] = array( $error['message'] );
00292                                 }
00293                         }
00294                 }
00295                 return $result;
00296         }
00297 
00306         public function getErrorsByType( $type ) {
00307                 $result = array();
00308                 foreach ( $this->errors as $error ) {
00309                         if ( $error['type'] === $type ) {
00310                                 $result[] = $error;
00311                         }
00312                 }
00313                 return $result;
00314         }
00315 
00322         function hasMessage( $msg ) {
00323                 foreach ( $this->errors as $error ) {
00324                         if ( $error['message'] === $msg ) {
00325                                 return true;
00326                         }
00327                 }
00328                 return false;
00329         }
00330 
00339         function replaceMessage( $source, $dest ) {
00340                 $replaced = false;
00341                 foreach ( $this->errors as $index => $error ) {
00342                         if ( $error['message'] === $source ) {
00343                                 $this->errors[$index]['message'] = $dest;
00344                                 $replaced = true;
00345                         }
00346                 }
00347                 return $replaced;
00348         }
00349 
00355         public function getMessage() {
00356                 return $this->getWikiText();
00357         }
00358 }