MediaWiki  REL1_24
Status.php
Go to the documentation of this file.
00001 <?php
00040 class Status {
00042     public $ok = true;
00043 
00045     public $value;
00046 
00049     public $successCount = 0;
00050 
00052     public $failCount = 0;
00053 
00056     public $success = array();
00057 
00059     public $errors = array();
00060 
00062     public $cleanCallback = false;
00063 
00070     static function newFatal( $message /*, parameters...*/ ) {
00071         $params = func_get_args();
00072         $result = new self;
00073         call_user_func_array( array( &$result, 'error' ), $params );
00074         $result->ok = false;
00075         return $result;
00076     }
00077 
00084     static function newGood( $value = null ) {
00085         $result = new self;
00086         $result->value = $value;
00087         return $result;
00088     }
00089 
00096     public function setResult( $ok, $value = null ) {
00097         $this->ok = $ok;
00098         $this->value = $value;
00099     }
00100 
00107     public function isGood() {
00108         return $this->ok && !$this->errors;
00109     }
00110 
00116     public function isOK() {
00117         return $this->ok;
00118     }
00119 
00125     public function warning( $message /*, parameters... */ ) {
00126         $params = array_slice( func_get_args(), 1 );
00127         $this->errors[] = array(
00128             'type' => 'warning',
00129             'message' => $message,
00130             'params' => $params );
00131     }
00132 
00139     public function error( $message /*, parameters... */ ) {
00140         $params = array_slice( func_get_args(), 1 );
00141         $this->errors[] = array(
00142             'type' => 'error',
00143             'message' => $message,
00144             'params' => $params );
00145     }
00146 
00153     public function fatal( $message /*, parameters... */ ) {
00154         $params = array_slice( func_get_args(), 1 );
00155         $this->errors[] = array(
00156             'type' => 'error',
00157             'message' => $message,
00158             'params' => $params );
00159         $this->ok = false;
00160     }
00161 
00165     public function __wakeup() {
00166         $this->cleanCallback = false;
00167     }
00168 
00173     protected function cleanParams( $params ) {
00174         if ( !$this->cleanCallback ) {
00175             return $params;
00176         }
00177         $cleanParams = array();
00178         foreach ( $params as $i => $param ) {
00179             $cleanParams[$i] = call_user_func( $this->cleanCallback, $param );
00180         }
00181         return $cleanParams;
00182     }
00183 
00192     public function getWikiText( $shortContext = false, $longContext = false ) {
00193         if ( count( $this->errors ) == 0 ) {
00194             if ( $this->ok ) {
00195                 $this->fatal( 'internalerror_info',
00196                     __METHOD__ . " called for a good result, this is incorrect\n" );
00197             } else {
00198                 $this->fatal( 'internalerror_info',
00199                     __METHOD__ . ": Invalid result object: no error text but not OK\n" );
00200             }
00201         }
00202         if ( count( $this->errors ) == 1 ) {
00203             $s = $this->getErrorMessage( $this->errors[0] )->plain();
00204             if ( $shortContext ) {
00205                 $s = wfMessage( $shortContext, $s )->plain();
00206             } elseif ( $longContext ) {
00207                 $s = wfMessage( $longContext, "* $s\n" )->plain();
00208             }
00209         } else {
00210             $errors = $this->getErrorMessageArray( $this->errors );
00211             foreach ( $errors as &$error ) {
00212                 $error = $error->plain();
00213             }
00214             $s = '* ' . implode( "\n* ", $errors ) . "\n";
00215             if ( $longContext ) {
00216                 $s = wfMessage( $longContext, $s )->plain();
00217             } elseif ( $shortContext ) {
00218                 $s = wfMessage( $shortContext, "\n$s\n" )->plain();
00219             }
00220         }
00221         return $s;
00222     }
00223 
00234     public function getMessage( $shortContext = false, $longContext = false ) {
00235         if ( count( $this->errors ) == 0 ) {
00236             if ( $this->ok ) {
00237                 $this->fatal( 'internalerror_info',
00238                     __METHOD__ . " called for a good result, this is incorrect\n" );
00239             } else {
00240                 $this->fatal( 'internalerror_info',
00241                     __METHOD__ . ": Invalid result object: no error text but not OK\n" );
00242             }
00243         }
00244         if ( count( $this->errors ) == 1 ) {
00245             $s = $this->getErrorMessage( $this->errors[0] );
00246             if ( $shortContext ) {
00247                 $s = wfMessage( $shortContext, $s );
00248             } elseif ( $longContext ) {
00249                 $wrapper = new RawMessage( "* \$1\n" );
00250                 $wrapper->params( $s )->parse();
00251                 $s = wfMessage( $longContext, $wrapper );
00252             }
00253         } else {
00254             $msgs = $this->getErrorMessageArray( $this->errors );
00255             $msgCount = count( $msgs );
00256 
00257             if ( $shortContext ) {
00258                 $msgCount++;
00259             }
00260 
00261             $s = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
00262             $s->params( $msgs )->parse();
00263 
00264             if ( $longContext ) {
00265                 $s = wfMessage( $longContext, $s );
00266             } elseif ( $shortContext ) {
00267                 $wrapper = new RawMessage( "\n\$1\n", $s );
00268                 $wrapper->parse();
00269                 $s = wfMessage( $shortContext, $wrapper );
00270             }
00271         }
00272 
00273         return $s;
00274     }
00275 
00285     protected function getErrorMessage( $error ) {
00286         if ( is_array( $error ) ) {
00287             if ( isset( $error['message'] ) && $error['message'] instanceof Message ) {
00288                 $msg = $error['message'];
00289             } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
00290                 $msg = wfMessage( $error['message'],
00291                     array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) );
00292             } else {
00293                 $msgName = array_shift( $error );
00294                 $msg = wfMessage( $msgName,
00295                     array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
00296             }
00297         } else {
00298             $msg = wfMessage( $error );
00299         }
00300         return $msg;
00301     }
00302 
00311     public function getHTML( $shortContext = false, $longContext = false ) {
00312         $text = $this->getWikiText( $shortContext, $longContext );
00313         $out = MessageCache::singleton()->parse( $text, null, true, true );
00314         return $out instanceof ParserOutput ? $out->getText() : $out;
00315     }
00316 
00322     protected function getErrorMessageArray( $errors ) {
00323         return array_map( array( $this, 'getErrorMessage' ), $errors );
00324     }
00325 
00332     public function merge( $other, $overwriteValue = false ) {
00333         $this->errors = array_merge( $this->errors, $other->errors );
00334         $this->ok = $this->ok && $other->ok;
00335         if ( $overwriteValue ) {
00336             $this->value = $other->value;
00337         }
00338         $this->successCount += $other->successCount;
00339         $this->failCount += $other->failCount;
00340     }
00341 
00348     public function getErrorsArray() {
00349         return $this->getStatusArray( "error" );
00350     }
00351 
00358     public function getWarningsArray() {
00359         return $this->getStatusArray( "warning" );
00360     }
00361 
00367     protected function getStatusArray( $type ) {
00368         $result = array();
00369         foreach ( $this->errors as $error ) {
00370             if ( $error['type'] === $type ) {
00371                 if ( $error['message'] instanceof Message ) {
00372                     $result[] = array_merge(
00373                         array( $error['message']->getKey() ),
00374                         $error['message']->getParams()
00375                     );
00376                 } elseif ( $error['params'] ) {
00377                     $result[] = array_merge( array( $error['message'] ), $error['params'] );
00378                 } else {
00379                     $result[] = array( $error['message'] );
00380                 }
00381             }
00382         }
00383 
00384         return $result;
00385     }
00386 
00395     public function getErrorsByType( $type ) {
00396         $result = array();
00397         foreach ( $this->errors as $error ) {
00398             if ( $error['type'] === $type ) {
00399                 $result[] = $error;
00400             }
00401         }
00402         return $result;
00403     }
00404 
00412     public function hasMessage( $message ) {
00413         if ( $message instanceof Message ) {
00414             $message = $message->getKey();
00415         }
00416         foreach ( $this->errors as $error ) {
00417             if ( $error['message'] instanceof Message
00418                 && $error['message']->getKey() === $message
00419             ) {
00420                 return true;
00421             } elseif ( $error['message'] === $message ) {
00422                 return true;
00423             }
00424         }
00425         return false;
00426     }
00427 
00439     public function replaceMessage( $source, $dest ) {
00440         $replaced = false;
00441         foreach ( $this->errors as $index => $error ) {
00442             if ( $error['message'] === $source ) {
00443                 $this->errors[$index]['message'] = $dest;
00444                 $replaced = true;
00445             }
00446         }
00447         return $replaced;
00448     }
00449 
00453     public function getValue() {
00454         return $this->value;
00455     }
00456 }