MediaWiki
master
|
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 $content = null; 00209 00213 protected $message; 00214 00222 public function __construct( $key, $params = array() ) { 00223 global $wgLang; 00224 $this->key = $key; 00225 $this->parameters = array_values( $params ); 00226 $this->language = $wgLang; 00227 } 00228 00238 public static function newFromKey( $key /*...*/ ) { 00239 $params = func_get_args(); 00240 array_shift( $params ); 00241 return new self( $key, $params ); 00242 } 00243 00252 public static function newFallbackSequence( /*...*/ ) { 00253 $keys = func_get_args(); 00254 if ( func_num_args() == 1 ) { 00255 if ( is_array($keys[0]) ) { 00256 // Allow an array to be passed as the first argument instead 00257 $keys = array_values($keys[0]); 00258 } else { 00259 // Optimize a single string to not need special fallback handling 00260 $keys = $keys[0]; 00261 } 00262 } 00263 return new self( $keys ); 00264 } 00265 00272 public function params( /*...*/ ) { 00273 $args = func_get_args(); 00274 if ( isset( $args[0] ) && is_array( $args[0] ) ) { 00275 $args = $args[0]; 00276 } 00277 $args_values = array_values( $args ); 00278 $this->parameters = array_merge( $this->parameters, $args_values ); 00279 return $this; 00280 } 00281 00291 public function rawParams( /*...*/ ) { 00292 $params = func_get_args(); 00293 if ( isset( $params[0] ) && is_array( $params[0] ) ) { 00294 $params = $params[0]; 00295 } 00296 foreach( $params as $param ) { 00297 $this->parameters[] = self::rawParam( $param ); 00298 } 00299 return $this; 00300 } 00301 00309 public function numParams( /*...*/ ) { 00310 $params = func_get_args(); 00311 if ( isset( $params[0] ) && is_array( $params[0] ) ) { 00312 $params = $params[0]; 00313 } 00314 foreach( $params as $param ) { 00315 $this->parameters[] = self::numParam( $param ); 00316 } 00317 return $this; 00318 } 00319 00326 public function setContext( IContextSource $context ) { 00327 $this->inLanguage( $context->getLanguage() ); 00328 $this->title( $context->getTitle() ); 00329 $this->interface = true; 00330 00331 return $this; 00332 } 00333 00343 public function inLanguage( $lang ) { 00344 if ( $lang instanceof Language || $lang instanceof StubUserLang ) { 00345 $this->language = $lang; 00346 } elseif ( is_string( $lang ) ) { 00347 if( $this->language->getCode() != $lang ) { 00348 $this->language = Language::factory( $lang ); 00349 } 00350 } else { 00351 $type = gettype( $lang ); 00352 throw new MWException( __METHOD__ . " must be " 00353 . "passed a String or Language object; $type given" 00354 ); 00355 } 00356 $this->interface = false; 00357 return $this; 00358 } 00359 00367 public function inContentLanguage() { 00368 global $wgForceUIMsgAsContentMsg; 00369 if ( in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) { 00370 return $this; 00371 } 00372 00373 global $wgContLang; 00374 $this->interface = false; 00375 $this->language = $wgContLang; 00376 return $this; 00377 } 00378 00386 public function setInterfaceMessageFlag( $value ) { 00387 $this->interface = (bool) $value; 00388 return $this; 00389 } 00390 00397 public function useDatabase( $value ) { 00398 $this->useDatabase = (bool) $value; 00399 return $this; 00400 } 00401 00408 public function title( $title ) { 00409 $this->title = $title; 00410 return $this; 00411 } 00412 00417 public function content() { 00418 if ( !$this->content ) { 00419 $this->content = new MessageContent( $this->key ); 00420 } 00421 00422 return $this->content; 00423 } 00424 00430 public function toString() { 00431 $string = $this->fetchMessage(); 00432 00433 if ( $string === false ) { 00434 $key = htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key ); 00435 if ( $this->format === 'plain' ) { 00436 return '<' . $key . '>'; 00437 } 00438 return '<' . $key . '>'; 00439 } 00440 00441 # Replace $* with a list of parameters for &uselang=qqx. 00442 if ( strpos( $string, '$*' ) !== false ) { 00443 $paramlist = ''; 00444 if ( $this->parameters !== array() ) { 00445 $paramlist = ': $' . implode( ', $', range( 1, count( $this->parameters ) ) ); 00446 } 00447 $string = str_replace( '$*', $paramlist, $string ); 00448 } 00449 00450 # Replace parameters before text parsing 00451 $string = $this->replaceParameters( $string, 'before' ); 00452 00453 # Maybe transform using the full parser 00454 if( $this->format === 'parse' ) { 00455 $string = $this->parseText( $string ); 00456 $m = array(); 00457 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) { 00458 $string = $m[1]; 00459 } 00460 } elseif( $this->format === 'block-parse' ){ 00461 $string = $this->parseText( $string ); 00462 } elseif( $this->format === 'text' ){ 00463 $string = $this->transformText( $string ); 00464 } elseif( $this->format === 'escaped' ){ 00465 $string = $this->transformText( $string ); 00466 $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false ); 00467 } 00468 00469 # Raw parameter replacement 00470 $string = $this->replaceParameters( $string, 'after' ); 00471 00472 return $string; 00473 } 00474 00482 public function __toString() { 00483 return $this->toString(); 00484 } 00485 00491 public function parse() { 00492 $this->format = 'parse'; 00493 return $this->toString(); 00494 } 00495 00501 public function text() { 00502 $this->format = 'text'; 00503 return $this->toString(); 00504 } 00505 00511 public function plain() { 00512 $this->format = 'plain'; 00513 return $this->toString(); 00514 } 00515 00521 public function parseAsBlock() { 00522 $this->format = 'block-parse'; 00523 return $this->toString(); 00524 } 00525 00532 public function escaped() { 00533 $this->format = 'escaped'; 00534 return $this->toString(); 00535 } 00536 00542 public function exists() { 00543 return $this->fetchMessage() !== false; 00544 } 00545 00552 public function isBlank() { 00553 $message = $this->fetchMessage(); 00554 return $message === false || $message === ''; 00555 } 00556 00562 public function isDisabled() { 00563 $message = $this->fetchMessage(); 00564 return $message === false || $message === '' || $message === '-'; 00565 } 00566 00572 public static function rawParam( $value ) { 00573 return array( 'raw' => $value ); 00574 } 00575 00581 public static function numParam( $value ) { 00582 return array( 'num' => $value ); 00583 } 00584 00592 protected function replaceParameters( $message, $type = 'before' ) { 00593 $replacementKeys = array(); 00594 foreach( $this->parameters as $n => $param ) { 00595 list( $paramType, $value ) = $this->extractParam( $param ); 00596 if ( $type === $paramType ) { 00597 $replacementKeys['$' . ($n + 1)] = $value; 00598 } 00599 } 00600 $message = strtr( $message, $replacementKeys ); 00601 return $message; 00602 } 00603 00611 protected function extractParam( $param ) { 00612 if ( is_array( $param ) && isset( $param['raw'] ) ) { 00613 return array( 'after', $param['raw'] ); 00614 } elseif ( is_array( $param ) && isset( $param['num'] ) ) { 00615 // Replace number params always in before step for now. 00616 // No support for combined raw and num params 00617 return array( 'before', $this->language->formatNum( $param['num'] ) ); 00618 } elseif ( !is_array( $param ) ) { 00619 return array( 'before', $param ); 00620 } else { 00621 throw new MWException( "Invalid message parameter: " . serialize( $param ) ); 00622 } 00623 } 00624 00631 protected function parseText( $string ) { 00632 $out = MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language ); 00633 return is_object( $out ) ? $out->getText() : $out; 00634 } 00635 00642 protected function transformText( $string ) { 00643 return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title ); 00644 } 00645 00652 protected function fetchMessage() { 00653 if ( !isset( $this->message ) ) { 00654 $cache = MessageCache::singleton(); 00655 if ( is_array( $this->key ) ) { 00656 if ( !count( $this->key ) ) { 00657 throw new MWException( "Given empty message key array." ); 00658 } 00659 foreach ( $this->key as $key ) { 00660 $message = $cache->get( $key, $this->useDatabase, $this->language ); 00661 if ( $message !== false && $message !== '' ) { 00662 break; 00663 } 00664 } 00665 $this->message = $message; 00666 } else { 00667 $this->message = $cache->get( $this->key, $this->useDatabase, $this->language ); 00668 } 00669 } 00670 return $this->message; 00671 } 00672 00673 }