MediaWiki  REL1_19
Message.php
Go to the documentation of this file.
00001 <?php
00139 class Message {
00144         protected $interface = true;
00145 
00152         protected $language = null;
00153 
00157         protected $key;
00158 
00162         protected $parameters = array();
00163 
00173         protected $format = 'parse';
00174 
00178         protected $useDatabase = true;
00179 
00183         protected $title = null;
00184 
00188         protected $message;
00189 
00196         public function __construct( $key, $params = array() ) {
00197                 global $wgLang;
00198                 $this->key = $key;
00199                 $this->parameters = array_values( $params );
00200                 $this->language = $wgLang;
00201         }
00202 
00211         public static function newFromKey( $key /*...*/ ) {
00212                 $params = func_get_args();
00213                 array_shift( $params );
00214                 return new self( $key, $params );
00215         }
00216 
00224         public static function newFallbackSequence( /*...*/ ) {
00225                 $keys = func_get_args();
00226                 if ( func_num_args() == 1 ) {
00227                         if ( is_array($keys[0]) ) {
00228                                 // Allow an array to be passed as the first argument instead
00229                                 $keys = array_values($keys[0]);
00230                         } else {
00231                                 // Optimize a single string to not need special fallback handling
00232                                 $keys = $keys[0];
00233                         }
00234                 }
00235                 return new self( $keys );
00236         }
00237 
00243         public function params( /*...*/ ) {
00244                 $args = func_get_args();
00245                 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
00246                         $args = $args[0];
00247                 }
00248                 $args_values = array_values( $args );
00249                 $this->parameters = array_merge( $this->parameters, $args_values );
00250                 return $this;
00251         }
00252 
00261         public function rawParams( /*...*/ ) {
00262                 $params = func_get_args();
00263                 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
00264                         $params = $params[0];
00265                 }
00266                 foreach( $params as $param ) {
00267                         $this->parameters[] = self::rawParam( $param );
00268                 }
00269                 return $this;
00270         }
00271 
00278         public function numParams( /*...*/ ) {
00279                 $params = func_get_args();
00280                 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
00281                         $params = $params[0];
00282                 }
00283                 foreach( $params as $param ) {
00284                         $this->parameters[] = self::numParam( $param );
00285                 }
00286                 return $this;
00287         }
00288 
00295         public function setContext( IContextSource $context ) {
00296                 $this->inLanguage( $context->getLanguage() );
00297                 $this->title( $context->getTitle() );
00298 
00299                 return $this;
00300         }
00301 
00309         public function inLanguage( $lang ) {
00310                 if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
00311                         $this->language = $lang;
00312                 } elseif ( is_string( $lang ) ) {
00313                         if( $this->language->getCode() != $lang ) {
00314                                 $this->language = Language::factory( $lang );
00315                         }
00316                 } else {
00317                         $type = gettype( $lang );
00318                         throw new MWException( __METHOD__ . " must be "
00319                                 . "passed a String or Language object; $type given"
00320                         );
00321                 }
00322                 $this->interface = false;
00323                 return $this;
00324         }
00325 
00332         public function inContentLanguage() {
00333                 global $wgForceUIMsgAsContentMsg;
00334                 if ( in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) {
00335                         return $this;
00336                 }
00337 
00338                 global $wgContLang;
00339                 $this->interface = false;
00340                 $this->language = $wgContLang;
00341                 return $this;
00342         }
00343 
00349         public function useDatabase( $value ) {
00350                 $this->useDatabase = (bool) $value;
00351                 return $this;
00352         }
00353 
00360         public function title( $title ) {
00361                 $this->title = $title;
00362                 return $this;
00363         }
00364 
00369         public function toString() {
00370                 $string = $this->getMessageText();
00371 
00372                 # Replace parameters before text parsing
00373                 $string = $this->replaceParameters( $string, 'before' );
00374 
00375                 # Maybe transform using the full parser
00376                 if( $this->format === 'parse' ) {
00377                         $string = $this->parseText( $string );
00378                         $m = array();
00379                         if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
00380                                 $string = $m[1];
00381                         }
00382                 } elseif( $this->format === 'block-parse' ){
00383                         $string = $this->parseText( $string );
00384                 } elseif( $this->format === 'text' ){
00385                         $string = $this->transformText( $string );
00386                 } elseif( $this->format === 'escaped' ){
00387                         $string = $this->transformText( $string );
00388                         $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false );
00389                 }
00390 
00391                 # Raw parameter replacement
00392                 $string = $this->replaceParameters( $string, 'after' );
00393 
00394                 return $string;
00395         }
00396 
00403         public function __toString() {
00404                 return $this->toString();
00405         }
00406 
00411         public function parse() {
00412                 $this->format = 'parse';
00413                 return $this->toString();
00414         }
00415 
00420         public function text() {
00421                 $this->format = 'text';
00422                 return $this->toString();
00423         }
00424 
00429         public function plain() {
00430                 $this->format = 'plain';
00431                 return $this->toString();
00432         }
00433 
00438         public function parseAsBlock() {
00439                 $this->format = 'block-parse';
00440                 return $this->toString();
00441         }
00442 
00448         public function escaped() {
00449                 $this->format = 'escaped';
00450                 return $this->toString();
00451         }
00452 
00457         public function exists() {
00458                 return $this->fetchMessage() !== false;
00459         }
00460 
00466         public function isBlank() {
00467                 $message = $this->fetchMessage();
00468                 return $message === false || $message === '';
00469         }
00470 
00475         public function isDisabled() {
00476                 $message = $this->fetchMessage();
00477                 return $message === false || $message === '' || $message === '-';
00478         }
00479 
00484         public static function rawParam( $value ) {
00485                 return array( 'raw' => $value );
00486         }
00487 
00492         public static function numParam( $value ) {
00493                 return array( 'num' => $value );
00494         }
00495 
00502         protected function replaceParameters( $message, $type = 'before' ) {
00503                 $replacementKeys = array();
00504                 foreach( $this->parameters as $n => $param ) {
00505                         list( $paramType, $value ) = $this->extractParam( $param );
00506                         if ( $type === $paramType ) {
00507                                 $replacementKeys['$' . ($n + 1)] = $value;
00508                         }
00509                 }
00510                 $message = strtr( $message, $replacementKeys );
00511                 return $message;
00512         }
00513 
00519         protected function extractParam( $param ) {
00520                 if ( is_array( $param ) && isset( $param['raw'] ) ) {
00521                         return array( 'after', $param['raw'] );
00522                 } elseif ( is_array( $param ) && isset( $param['num'] ) ) {
00523                         // Replace number params always in before step for now.
00524                         // No support for combined raw and num params
00525                         return array( 'before', $this->language->formatNum( $param['num'] ) );
00526                 } elseif ( !is_array( $param ) ) {
00527                         return array( 'before', $param );
00528                 } else {
00529                         trigger_error(
00530                                 "Invalid message parameter: " . htmlspecialchars( serialize( $param ) ),
00531                                 E_USER_WARNING
00532                         );
00533                         return array( 'before', '[INVALID]' );
00534                 }
00535         }
00536 
00542         protected function parseText( $string ) {
00543                 return MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language )->getText();
00544         }
00545 
00551         protected function transformText( $string ) {
00552                 return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title );
00553         }
00554 
00559         protected function getMessageText() {
00560                 $message = $this->fetchMessage();
00561                 if ( $message === false ) {
00562                         return '&lt;' . htmlspecialchars( is_array($this->key) ? $this->key[0] : $this->key ) . '&gt;';
00563                 } else {
00564                         return $message;
00565                 }
00566         }
00567 
00573         protected function fetchMessage() {
00574                 if ( !isset( $this->message ) ) {
00575                         $cache = MessageCache::singleton();
00576                         if ( is_array( $this->key ) ) {
00577                                 if ( !count( $this->key ) ) {
00578                                         throw new MWException( "Given empty message key array." );
00579                                 }
00580                                 foreach ( $this->key as $key ) {
00581                                         $message = $cache->get( $key, $this->useDatabase, $this->language );
00582                                         if ( $message !== false && $message !== '' ) {
00583                                                 break;
00584                                         }
00585                                 }
00586                                 $this->message = $message;
00587                         } else {
00588                                 $this->message = $cache->get( $this->key, $this->useDatabase, $this->language );
00589                         }
00590                 }
00591                 return $this->message;
00592         }
00593 
00594 }