179 'comma' =>
'commaList',
180 'semicolon' =>
'semicolonList',
181 'pipe' =>
'pipeList',
182 'text' =>
'listToText',
254 throw new InvalidArgumentException(
255 '$params must be empty if $key is a MessageSpecifier'
258 $params =
$key->getParams();
262 if ( !is_string(
$key ) && !is_array(
$key ) ) {
263 throw new InvalidArgumentException(
'$key must be a string or an array' );
266 $this->keysToTry = (array)
$key;
268 if ( empty( $this->keysToTry ) ) {
269 throw new InvalidArgumentException(
'$key must not be an empty list' );
272 $this->key = reset( $this->keysToTry );
274 $this->parameters = array_values( $params );
287 'interface' => $this->interface,
288 'language' => $this->language ? $this->language->getCode() :
false,
294 'titlestr' => $this->
title ? $this->
title->getFullText() :
null,
305 if ( !is_array( $data ) ) {
306 throw new InvalidArgumentException( __METHOD__ .
': Invalid serialized data' );
309 $this->
interface = $data['interface'];
310 $this->key = $data[
'key'];
311 $this->keysToTry = $data[
'keysToTry'];
312 $this->parameters = $data[
'parameters'];
313 $this->format = $data[
'format'];
315 $this->language = $data[
'language'] ? Language::factory( $data[
'language'] ) :
false;
317 if ( isset( $data[
'titlestr'] ) ) {
318 $this->
title = Title::newFromText( $data[
'titlestr'] );
319 } elseif ( isset( $data[
'title'] ) && $data[
'title'] instanceof
Title ) {
321 $this->
title = $data[
'title'];
334 return count( $this->keysToTry ) > 1;
395 return $this->language ?: RequestContext::getMain()->getLanguage();
411 return new self(
$key, $params );
429 if ( is_array( $value ) ) {
431 $value = array_shift( $params );
434 if ( $value instanceof
Message ) {
438 } elseif ( is_string( $value ) ) {
441 throw new InvalidArgumentException( __METHOD__ .
': invalid argument type '
442 . gettype( $value ) );
461 if ( func_num_args() == 1 ) {
462 if ( is_array(
$keys[0] ) ) {
470 return new self(
$keys );
486 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
490 !
$lang->equals( $contLang )
496 return Title::makeTitle(
515 if ( count(
$args ) === 1 && isset(
$args[0] ) && is_array(
$args[0] ) ) {
516 if (
$args[0] === [] ) {
520 if ( is_int(
$key ) ) {
528 $this->parameters = array_merge( $this->parameters, array_values(
$args ) );
546 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
547 $params = $params[0];
549 foreach ( $params as $param ) {
567 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
568 $params = $params[0];
570 foreach ( $params as $param ) {
588 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
589 $params = $params[0];
591 foreach ( $params as $param ) {
609 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
610 $params = $params[0];
612 foreach ( $params as $param ) {
630 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
631 $params = $params[0];
633 foreach ( $params as $param ) {
651 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
652 $params = $params[0];
654 foreach ( $params as $param ) {
672 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
673 $params = $params[0];
675 foreach ( $params as $param ) {
695 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
696 $params = $params[0];
698 foreach ( $params as $param ) {
716 $this->
interface =
true;
736 $this->language =
$lang;
737 } elseif ( is_string(
$lang ) ) {
739 $this->language = Language::factory(
$lang );
742 $this->language =
false;
746 .
"passed a String or Language object; $type given"
750 if ( $this->language !== $previousLanguage ) {
752 $this->message =
null;
754 $this->
interface = false;
773 $this->
inLanguage( MediaWikiServices::getInstance()->getContentLanguage() );
803 $this->message =
null;
847 $ex =
new LogicException( __METHOD__ .
' using implicit format: ' . $this->format );
848 LoggerFactory::getInstance(
'message-format' )->warning(
849 $ex->getMessage(), [
'exception' => $ex,
'format' => $this->format,
'key' => $this->key ] );
854 if ( $string ===
false ) {
862 return '⧼' . htmlspecialchars( $this->key ) .
'⧽';
865 # Replace $* with a list of parameters for &uselang=qqx.
866 if ( strpos( $string,
'$*' ) !==
false ) {
868 if ( $this->parameters !== [] ) {
869 $paramlist =
': $' . implode(
', $', range( 1, count( $this->parameters ) ) );
871 $string = str_replace(
'$*', $paramlist, $string );
874 # Replace parameters before text parsing
877 # Maybe transform using the full parser
878 if (
$format === self::FORMAT_PARSE ) {
880 $string = Parser::stripOuterParagraph( $string );
881 } elseif (
$format === self::FORMAT_BLOCK_PARSE ) {
883 } elseif (
$format === self::FORMAT_TEXT ) {
885 } elseif (
$format === self::FORMAT_ESCAPED ) {
887 $string = htmlspecialchars( $string, ENT_QUOTES,
'UTF-8',
false );
890 # Raw parameter replacement
910 return $this->
toString( self::FORMAT_PARSE );
911 }
catch ( Exception $ex ) {
913 trigger_error(
"Exception caught in " . __METHOD__ .
" (message " . $this->key .
"): "
914 . $ex, E_USER_WARNING );
915 }
catch ( Exception $ex ) {
919 return '⧼' . htmlspecialchars( $this->key ) .
'⧽';
932 return $this->
toString( self::FORMAT_PARSE );
944 return $this->
toString( self::FORMAT_TEXT );
956 return $this->
toString( self::FORMAT_PLAIN );
968 return $this->
toString( self::FORMAT_BLOCK_PARSE );
981 return $this->
toString( self::FORMAT_ESCAPED );
1028 return [
'raw' => $raw ];
1039 return [
'num' => $num ];
1050 return [
'duration' => $duration ];
1061 return [
'expiry' => $expiry ];
1072 return [
'period' => $period ];
1083 return [
'size' => $size ];
1094 return [
'bitrate' => $bitrate ];
1105 return [
'plaintext' => $plaintext ];
1116 if ( !isset( self::$listTypeMap[
$type] ) ) {
1117 throw new InvalidArgumentException(
1118 "Invalid type '$type'. Known types are: " . implode(
', ', array_keys( self::$listTypeMap ) )
1121 return [
'list' => $list,
'type' =>
$type ];
1140 $marker =
$format === self::FORMAT_ESCAPED ?
'$' :
'$\'"';
1141 $replacementKeys = [];
1142 foreach ( $this->parameters as $n => $param ) {
1144 if (
$type ===
'before' ) {
1145 if ( $paramType ===
'before' ) {
1146 $replacementKeys[
'$' . ( $n + 1 )] = $value;
1153 $replacementKeys[
'$' . ( $n + 1 )] = $marker . ( $n + 1 );
1155 } elseif ( $paramType ===
'after' ) {
1156 $replacementKeys[$marker . ( $n + 1 )] = $value;
1159 return strtr(
$message, $replacementKeys );
1173 if ( is_array( $param ) ) {
1174 if ( isset( $param[
'raw'] ) ) {
1175 return [
'after', $param[
'raw'] ];
1176 } elseif ( isset( $param[
'num'] ) ) {
1179 return [
'before', $this->
getLanguage()->formatNum( $param[
'num'] ) ];
1180 } elseif ( isset( $param[
'duration'] ) ) {
1181 return [
'before', $this->
getLanguage()->formatDuration( $param[
'duration'] ) ];
1182 } elseif ( isset( $param[
'expiry'] ) ) {
1183 return [
'before', $this->
getLanguage()->formatExpiry( $param[
'expiry'] ) ];
1184 } elseif ( isset( $param[
'period'] ) ) {
1185 return [
'before', $this->
getLanguage()->formatTimePeriod( $param[
'period'] ) ];
1186 } elseif ( isset( $param[
'size'] ) ) {
1187 return [
'before', $this->
getLanguage()->formatSize( $param[
'size'] ) ];
1188 } elseif ( isset( $param[
'bitrate'] ) ) {
1189 return [
'before', $this->
getLanguage()->formatBitrate( $param[
'bitrate'] ) ];
1190 } elseif ( isset( $param[
'plaintext'] ) ) {
1192 } elseif ( isset( $param[
'list'] ) ) {
1195 if ( !is_scalar( $param ) ) {
1198 LoggerFactory::getInstance(
'Bug58676' )->warning(
1199 'Invalid parameter for message "{msgkey}": {param}',
1201 'exception' =>
new Exception,
1202 'msgkey' => $this->
getKey(),
1203 'param' => htmlspecialchars( $param ),
1207 return [
'before',
'[INVALID]' ];
1209 } elseif ( $param instanceof
Message ) {
1211 $msg = clone $param;
1212 if ( $msg->language !== $this->language || $msg->useDatabase !== $this->useDatabase ) {
1214 $msg->message =
null;
1222 if (
$format ===
'block-parse' ) {
1230 return [
'after', $msg->toString(
$format ) ];
1232 return [
'before', $param ];
1246 $out = MessageCache::singleton()->parse(
1256 'enableSectionEditLinks' =>
false,
1276 return MessageCache::singleton()->transform(
1293 if ( $this->message ===
null ) {
1294 $cache = MessageCache::singleton();
1296 foreach ( $this->keysToTry as
$key ) {
1333 return htmlspecialchars( $plaintext, ENT_QUOTES );
1346 if ( !isset( self::$listTypeMap[$listType] ) ) {
1347 $warning =
'Invalid list type for message "' . $this->
getKey() .
'": '
1348 . htmlspecialchars( $listType )
1349 .
' (params are ' . htmlspecialchars(
serialize( $params ) ) .
')';
1350 trigger_error( $warning, E_USER_WARNING );
1352 wfDebugLog(
'Bug58676', $warning .
"\n" . $e->getTraceAsString() );
1353 return [
'before',
'[INVALID]' ];
1355 $func = self::$listTypeMap[$listType];
1359 return [
'before', $this->
getLanguage()->$func( [] ) ];
1366 foreach ( $params as $n => $p ) {
1368 $types[
$type] =
true;
1370 $vars[] =
'$' . ( $n + 1 );
1375 if ( count( $types ) === 1 ) {
1376 return [ key( $types ), $this->
getLanguage()->$func( $list ) ];
$wgForceUIMsgAsContentMsg
When translating messages with wfMessage(), it is not always clear what should be considered UI messa...
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Internationalisation code.
getCode()
Get the internal language code for this language object.
Wrapper allowing us to handle a system message as a Content object.
The Message class provides methods which fulfil two basic services:
static newFallbackSequence(... $keys)
Factory function accepting multiple message keys and returning a message instance for the first messa...
sizeParams(... $params)
Add parameters that are file sizes and will be passed through Language::formatSize before substitutio...
extractParam( $param, $format)
Extracts the parameter type and preprocessed the value if needed.
static listParam(array $list, $type='text')
params(... $args)
Adds parameters to the parameter list of this message.
const FORMAT_TEXT
Transform {{..}} constructs but don't transform to HTML.
transformText( $string)
Wrapper for what ever method we use to {{-transform wikitext.
getParams()
Returns the message parameters.
static bitrateParam( $bitrate)
static array $listTypeMap
Mapping from Message::listParam() types to Language methods.
durationParams(... $params)
Add parameters that are durations of time and will be passed through Language::formatDuration before ...
isBlank()
Check whether a message does not exist, or is an empty string.
expiryParams(... $params)
Add parameters that are expiration times and will be passed through Language::formatExpiry before sub...
setInterfaceMessageFlag( $interface)
Allows manipulating the interface message flag directly.
plaintextParams(... $params)
Add parameters that are plaintext and will be passed through without the content being evaluated.
bitrateParams(... $params)
Add parameters that are bitrates and will be passed through Language::formatBitrate before substituti...
parseAsBlock()
Returns the parsed message text which is always surrounded by a block element.
__construct( $key, $params=[], Language $language=null)
bool $useDatabase
Whether database can be used.
parse()
Fully parse the text from wikitext to HTML.
const FORMAT_PARSE
Use normal wikitext -> HTML parsing but strip the block-level wrapper.
__toString()
Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg: $foo = new Message( $k...
unserialize( $serialized)
const FORMAT_BLOCK_PARSE
Use normal wikitext -> HTML parsing (the result will be wrapped in a block-level HTML tag)
formatListParam(array $params, $listType, $format)
Formats a list of parameters as a concatenated string.
static timeperiodParam( $period)
array $parameters
List of parameters which will be substituted into the message.
static plaintextParam( $plaintext)
const FORMAT_PLAIN
Use message text as-is.
static durationParam( $duration)
rawParams(... $params)
Add parameters that are substituted after parsing or escaping.
const FORMAT_ESCAPED
Transform {{..}} constructs, HTML-escape the result.
inLanguage( $lang)
Request the message in any language that is supported.
replaceParameters( $message, $type, $format)
Substitutes any parameters into the message text.
string $key
The message key.
numParams(... $params)
Add parameters that are numeric and will be passed through Language::formatNum before substitution.
inContentLanguage()
Request the message in the wiki's content language, unless it is disabled for this message.
plain()
Returns the message text as-is, only parameters are substituted.
exists()
Check whether a message key has been defined currently.
Content $content
Content object representing the message.
static newFromKey( $key,... $params)
Factory function that is just wrapper for the real constructor.
title( $title)
Set the Title object to use as context when transforming the message.
Language bool $language
In which language to get this message.
getFormat()
Returns the message format.
useDatabase( $useDatabase)
Enable or disable database use.
fetchMessage()
Wrapper for what ever method we use to get message contents.
content()
Returns the message as a Content object.
formatPlaintext( $plaintext, $format)
Formats a message parameter wrapped with 'plaintext'.
timeperiodParams(... $params)
Add parameters that are time periods and will be passed through Language::formatTimePeriod before sub...
getTitle()
Get a title object for a mediawiki message, where it can be found in the mediawiki namespace.
parseText( $string)
Wrapper for what ever method we use to parse wikitext.
getKey()
Returns the message key.
isDisabled()
Check whether a message does not exist, is an empty string, or is "-".
escaped()
Returns the message text.
getLanguage()
Returns the Language of the Message.
string[] $keysToTry
List of keys to try when fetching the message.
setContext(IContextSource $context)
Set the language and the title from a context object.
toString( $format=null)
Returns the message parsed from wikitext to HTML.
Title $title
Title object to use as context.
text()
Returns the message text.
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
bool $interface
In which language to get this message.
static expiryParam( $expiry)
getText( $options=[])
Get the output HTML.
Variant of the Message class.
Stub object for the user language.
Represents a title within MediaWiki.
Base interface for content objects.
Interface for objects which can provide a MediaWiki context on request.
foreach( $res as $row) $serialized
if(!isset( $args[0])) $lang