167 protected $interface =
true;
175 protected $language =
null;
185 protected $parameters =
array();
198 protected $format =
'parse';
203 protected $useDatabase =
true;
213 protected $content =
null;
232 $this->parameters = array_values(
$params );
233 $this->language = $language ? $language :
$wgLang;
243 public function getKey() {
244 if ( is_array( $this->
key ) ) {
247 return $this->
key[0];
260 public function getParams() {
261 return $this->parameters;
271 public function getFormat() {
272 return $this->format;
282 public function getLanguage() {
283 return $this->language;
298 public static function newFromKey( $key ) {
301 return new self( $key,
$params );
316 public static function newFallbackSequence( ) {
317 $keys = func_get_args();
318 if ( func_num_args() == 1 ) {
319 if ( is_array(
$keys[0] ) ) {
327 return new self(
$keys );
340 public function params( ) {
341 $args = func_get_args();
342 if ( isset(
$args[0] ) && is_array(
$args[0] ) ) {
345 $args_values = array_values(
$args );
346 $this->parameters = array_merge( $this->parameters, $args_values );
363 public function rawParams( ) {
369 $this->parameters[] = self::rawParam( $param );
385 public function numParams( ) {
391 $this->parameters[] = self::numParam( $param );
407 public function durationParams( ) {
413 $this->parameters[] = self::durationParam( $param );
429 public function expiryParams( ) {
435 $this->parameters[] = self::expiryParam( $param );
451 public function timeperiodParams( ) {
457 $this->parameters[] = self::timeperiodParam( $param );
473 public function sizeParams( ) {
479 $this->parameters[] = self::sizeParam( $param );
495 public function bitrateParams( ) {
501 $this->parameters[] = self::bitrateParam( $param );
518 $this->
interface =
true;
535 public function inLanguage( $lang ) {
537 $this->language = $lang;
538 } elseif ( is_string( $lang ) ) {
539 if ( $this->language->getCode() != $lang ) {
543 $type = gettype( $lang );
545 .
"passed a String or Language object; $type given"
548 $this->
interface =
false;
561 public function inContentLanguage() {
562 global $wgForceUIMsgAsContentMsg;
563 if ( in_array( $this->
key, (
array)$wgForceUIMsgAsContentMsg ) ) {
568 $this->
interface =
false;
583 public function setInterfaceMessageFlag( $interface ) {
584 $this->
interface = (bool)$interface;
597 public function useDatabase( $useDatabase ) {
598 $this->useDatabase = (bool)$useDatabase;
626 return $this->content;
636 public function toString() {
637 $string = $this->fetchMessage();
639 if ( $string ===
false ) {
640 $key = htmlspecialchars( is_array( $this->
key ) ? $this->
key[0] : $this->
key );
641 if ( $this->
format ===
'plain' ) {
642 return '<' . $key .
'>';
644 return '<' . $key .
'>';
647 # Replace $* with a list of parameters for &uselang=qqx.
648 if ( strpos( $string,
'$*' ) !==
false ) {
650 if ( $this->parameters !==
array() ) {
651 $paramlist =
': $' . implode(
', $', range( 1, count( $this->parameters ) ) );
653 $string = str_replace(
'$*', $paramlist, $string );
656 # Replace parameters before text parsing
657 $string = $this->replaceParameters( $string,
'before' );
659 # Maybe transform using the full parser
660 if ( $this->
format ===
'parse' ) {
661 $string = $this->parseText( $string );
663 if ( preg_match(
'/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
666 } elseif ( $this->
format ===
'block-parse' ) {
667 $string = $this->parseText( $string );
668 } elseif ( $this->
format ===
'text' ) {
669 $string = $this->transformText( $string );
670 } elseif ( $this->
format ===
'escaped' ) {
671 $string = $this->transformText( $string );
672 $string = htmlspecialchars( $string, ENT_QUOTES,
'UTF-8',
false );
675 # Raw parameter replacement
676 $string = $this->replaceParameters( $string,
'after' );
690 public function __toString() {
695 return $this->toString();
696 }
catch ( Exception $ex ) {
698 trigger_error(
"Exception caught in " . __METHOD__ .
" (message " . $this->
key .
"): "
699 . $ex, E_USER_WARNING );
700 }
catch ( Exception $ex ) {
704 if ( $this->
format ===
'plain' ) {
705 return '<' . $this->
key .
'>';
707 return '<' . $this->
key .
'>';
718 public function parse() {
720 return $this->toString();
730 public function text() {
732 return $this->toString();
742 public function plain() {
744 return $this->toString();
754 public function parseAsBlock() {
755 $this->
format =
'block-parse';
756 return $this->toString();
767 public function escaped() {
768 $this->
format =
'escaped';
769 return $this->toString();
779 public function exists() {
780 return $this->fetchMessage() !==
false;
791 public function isBlank() {
792 $message = $this->fetchMessage();
793 return $message ===
false || $message ===
'';
803 public function isDisabled() {
804 $message = $this->fetchMessage();
805 return $message ===
false || $message ===
'' || $message ===
'-';
815 public static function rawParam( $raw ) {
816 return array(
'raw' => $raw );
826 public static function numParam( $num ) {
827 return array(
'num' => $num );
837 public static function durationParam( $duration ) {
838 return array(
'duration' => $duration );
848 public static function expiryParam( $expiry ) {
849 return array(
'expiry' => $expiry );
859 public static function timeperiodParam( $period ) {
860 return array(
'period' => $period );
870 public static function sizeParam(
$size ) {
881 public static function bitrateParam( $bitrate ) {
882 return array(
'bitrate' => $bitrate );
895 protected function replaceParameters( $message,
$type =
'before' ) {
896 $replacementKeys =
array();
897 foreach ( $this->parameters
as $n => $param ) {
898 list( $paramType,
$value ) = $this->extractParam( $param );
899 if (
$type === $paramType ) {
900 $replacementKeys[
'$' . (
$n + 1 )] =
$value;
903 $message = strtr( $message, $replacementKeys );
916 protected function extractParam( $param ) {
917 if ( is_array( $param ) ) {
918 if ( isset( $param[
'raw'] ) ) {
919 return array(
'after', $param[
'raw'] );
920 } elseif ( isset( $param[
'num'] ) ) {
923 return array(
'before', $this->language->formatNum( $param[
'num'] ) );
924 } elseif ( isset( $param[
'duration'] ) ) {
925 return array(
'before', $this->language->formatDuration( $param[
'duration'] ) );
926 } elseif ( isset( $param[
'expiry'] ) ) {
927 return array(
'before', $this->language->formatExpiry( $param[
'expiry'] ) );
928 } elseif ( isset( $param[
'period'] ) ) {
929 return array(
'before', $this->language->formatTimePeriod( $param[
'period'] ) );
930 } elseif ( isset( $param[
'size'] ) ) {
931 return array(
'before', $this->language->formatSize( $param[
'size'] ) );
932 } elseif ( isset( $param[
'bitrate'] ) ) {
933 return array(
'before', $this->language->formatBitrate( $param[
'bitrate'] ) );
935 $warning =
'Invalid parameter for message "' . $this->getKey() .
'": ' .
936 htmlspecialchars( serialize( $param ) );
937 trigger_error( $warning, E_USER_WARNING );
939 wfDebugLog(
'Bug58676', $warning .
"\n" .
$e->getTraceAsString() );
941 return array(
'before',
'[INVALID]' );
943 } elseif ( $param instanceof Message ) {
947 return array(
'after', $param->toString() );
949 return array(
'before', $param );
962 protected function parseText( $string ) {
976 protected function transformText( $string ) {
988 protected function fetchMessage() {
989 if ( !isset( $this->
message ) ) {
991 if ( is_array( $this->
key ) ) {
992 if ( !count( $this->
key ) ) {
993 throw new MWException(
"Given empty message key array." );
995 foreach ( $this->
key as $key ) {
996 $message =
$cache->get( $key, $this->useDatabase, $this->language );
997 if ( $message !==
false && $message !==
'' ) {
1003 $this->
message =
$cache->get( $this->
key, $this->useDatabase, $this->language );
1006 return $this->message;
1024 class RawMessage
extends Message {
1035 public function __construct( $key,
$params =
array() ) {
1036 parent::__construct( $key,
$params );
1046 public function fetchMessage() {
1048 if ( !isset( $this->
message ) ) {
1051 return $this->message;