MediaWiki  REL1_20
Message.php
Go to the documentation of this file.
00001 <?php
00159 class Message {
00164         protected $interface = true;
00165 
00172         protected $language = null;
00173 
00177         protected $key;
00178 
00182         protected $parameters = array();
00183 
00193         protected $format = 'parse';
00194 
00198         protected $useDatabase = true;
00199 
00203         protected $title = null;
00204 
00208         protected $message;
00209 
00217         public function __construct( $key, $params = array() ) {
00218                 global $wgLang;
00219                 $this->key = $key;
00220                 $this->parameters = array_values( $params );
00221                 $this->language = $wgLang;
00222         }
00223 
00233         public static function newFromKey( $key /*...*/ ) {
00234                 $params = func_get_args();
00235                 array_shift( $params );
00236                 return new self( $key, $params );
00237         }
00238 
00247         public static function newFallbackSequence( /*...*/ ) {
00248                 $keys = func_get_args();
00249                 if ( func_num_args() == 1 ) {
00250                         if ( is_array($keys[0]) ) {
00251                                 // Allow an array to be passed as the first argument instead
00252                                 $keys = array_values($keys[0]);
00253                         } else {
00254                                 // Optimize a single string to not need special fallback handling
00255                                 $keys = $keys[0];
00256                         }
00257                 }
00258                 return new self( $keys );
00259         }
00260 
00267         public function params( /*...*/ ) {
00268                 $args = func_get_args();
00269                 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
00270                         $args = $args[0];
00271                 }
00272                 $args_values = array_values( $args );
00273                 $this->parameters = array_merge( $this->parameters, $args_values );
00274                 return $this;
00275         }
00276 
00286         public function rawParams( /*...*/ ) {
00287                 $params = func_get_args();
00288                 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
00289                         $params = $params[0];
00290                 }
00291                 foreach( $params as $param ) {
00292                         $this->parameters[] = self::rawParam( $param );
00293                 }
00294                 return $this;
00295         }
00296 
00304         public function numParams( /*...*/ ) {
00305                 $params = func_get_args();
00306                 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
00307                         $params = $params[0];
00308                 }
00309                 foreach( $params as $param ) {
00310                         $this->parameters[] = self::numParam( $param );
00311                 }
00312                 return $this;
00313         }
00314 
00321         public function setContext( IContextSource $context ) {
00322                 $this->inLanguage( $context->getLanguage() );
00323                 $this->title( $context->getTitle() );
00324                 $this->interface = true;
00325 
00326                 return $this;
00327         }
00328 
00337         public function inLanguage( $lang ) {
00338                 if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
00339                         $this->language = $lang;
00340                 } elseif ( is_string( $lang ) ) {
00341                         if( $this->language->getCode() != $lang ) {
00342                                 $this->language = Language::factory( $lang );
00343                         }
00344                 } else {
00345                         $type = gettype( $lang );
00346                         throw new MWException( __METHOD__ . " must be "
00347                                 . "passed a String or Language object; $type given"
00348                         );
00349                 }
00350                 $this->interface = false;
00351                 return $this;
00352         }
00353 
00361         public function inContentLanguage() {
00362                 global $wgForceUIMsgAsContentMsg;
00363                 if ( in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) {
00364                         return $this;
00365                 }
00366 
00367                 global $wgContLang;
00368                 $this->interface = false;
00369                 $this->language = $wgContLang;
00370                 return $this;
00371         }
00372 
00380         public function setInterfaceMessageFlag( $value ) {
00381                 $this->interface = (bool) $value;
00382                 return $this;
00383         }
00384 
00391         public function useDatabase( $value ) {
00392                 $this->useDatabase = (bool) $value;
00393                 return $this;
00394         }
00395 
00402         public function title( $title ) {
00403                 $this->title = $title;
00404                 return $this;
00405         }
00406 
00412         public function toString() {
00413                 $string = $this->fetchMessage();
00414 
00415                 if ( $string === false ) {
00416                         $key =  htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key );
00417                         if ( $this->format === 'plain' ) {
00418                                 return '<' . $key . '>';
00419                         }
00420                         return '&lt;' . $key . '&gt;';
00421                 }
00422 
00423                 # Replace parameters before text parsing
00424                 $string = $this->replaceParameters( $string, 'before' );
00425 
00426                 # Maybe transform using the full parser
00427                 if( $this->format === 'parse' ) {
00428                         $string = $this->parseText( $string );
00429                         $m = array();
00430                         if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
00431                                 $string = $m[1];
00432                         }
00433                 } elseif( $this->format === 'block-parse' ){
00434                         $string = $this->parseText( $string );
00435                 } elseif( $this->format === 'text' ){
00436                         $string = $this->transformText( $string );
00437                 } elseif( $this->format === 'escaped' ){
00438                         $string = $this->transformText( $string );
00439                         $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false );
00440                 }
00441 
00442                 # Raw parameter replacement
00443                 $string = $this->replaceParameters( $string, 'after' );
00444 
00445                 return $string;
00446         }
00447 
00455         public function __toString() {
00456                 return $this->toString();
00457         }
00458 
00464         public function parse() {
00465                 $this->format = 'parse';
00466                 return $this->toString();
00467         }
00468 
00474         public function text() {
00475                 $this->format = 'text';
00476                 return $this->toString();
00477         }
00478 
00484         public function plain() {
00485                 $this->format = 'plain';
00486                 return $this->toString();
00487         }
00488 
00494         public function parseAsBlock() {
00495                 $this->format = 'block-parse';
00496                 return $this->toString();
00497         }
00498 
00505         public function escaped() {
00506                 $this->format = 'escaped';
00507                 return $this->toString();
00508         }
00509 
00515         public function exists() {
00516                 return $this->fetchMessage() !== false;
00517         }
00518 
00525         public function isBlank() {
00526                 $message = $this->fetchMessage();
00527                 return $message === false || $message === '';
00528         }
00529 
00535         public function isDisabled() {
00536                 $message = $this->fetchMessage();
00537                 return $message === false || $message === '' || $message === '-';
00538         }
00539 
00545         public static function rawParam( $value ) {
00546                 return array( 'raw' => $value );
00547         }
00548 
00554         public static function numParam( $value ) {
00555                 return array( 'num' => $value );
00556         }
00557 
00565         protected function replaceParameters( $message, $type = 'before' ) {
00566                 $replacementKeys = array();
00567                 foreach( $this->parameters as $n => $param ) {
00568                         list( $paramType, $value ) = $this->extractParam( $param );
00569                         if ( $type === $paramType ) {
00570                                 $replacementKeys['$' . ($n + 1)] = $value;
00571                         }
00572                 }
00573                 $message = strtr( $message, $replacementKeys );
00574                 return $message;
00575         }
00576 
00583         protected function extractParam( $param ) {
00584                 if ( is_array( $param ) && isset( $param['raw'] ) ) {
00585                         return array( 'after', $param['raw'] );
00586                 } elseif ( is_array( $param ) && isset( $param['num'] ) ) {
00587                         // Replace number params always in before step for now.
00588                         // No support for combined raw and num params
00589                         return array( 'before', $this->language->formatNum( $param['num'] ) );
00590                 } elseif ( !is_array( $param ) ) {
00591                         return array( 'before', $param );
00592                 } else {
00593                         trigger_error(
00594                                 "Invalid message parameter: " . htmlspecialchars( serialize( $param ) ),
00595                                 E_USER_WARNING
00596                         );
00597                         return array( 'before', '[INVALID]' );
00598                 }
00599         }
00600 
00607         protected function parseText( $string ) {
00608                 return MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language )->getText();
00609         }
00610 
00617         protected function transformText( $string ) {
00618                 return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title );
00619         }
00620 
00626         protected function fetchMessage() {
00627                 if ( !isset( $this->message ) ) {
00628                         $cache = MessageCache::singleton();
00629                         if ( is_array( $this->key ) ) {
00630                                 if ( !count( $this->key ) ) {
00631                                         throw new MWException( "Given empty message key array." );
00632                                 }
00633                                 foreach ( $this->key as $key ) {
00634                                         $message = $cache->get( $key, $this->useDatabase, $this->language );
00635                                         if ( $message !== false && $message !== '' ) {
00636                                                 break;
00637                                         }
00638                                 }
00639                                 $this->message = $message;
00640                         } else {
00641                                 $this->message = $cache->get( $this->key, $this->useDatabase, $this->language );
00642                         }
00643                 }
00644                 return $this->message;
00645         }
00646 
00647 }