MediaWiki
1.28.0
|
The Message class provides methods which fulfil two basic services: More...
Public Member Functions | |
__construct ($key, $params=[], Language $language=null) | |
__toString () | |
Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg: $foo = new Message( $key ); $string = "<abbr>$foo</abbr>";. More... | |
bitrateParams () | |
Add parameters that are bitrates and will be passed through Language::formatBitrate before substitution. More... | |
content () | |
Returns the message as a Content object. More... | |
durationParams () | |
Add parameters that are durations of time and will be passed through Language::formatDuration before substitution. More... | |
escaped () | |
Returns the message text. More... | |
exists () | |
Check whether a message key has been defined currently. More... | |
expiryParams () | |
Add parameters that are expiration times and will be passed through Language::formatExpiry before substitution. More... | |
getFormat () | |
Returns the message format. More... | |
getKey () | |
Returns the message key. More... | |
getKeysToTry () | |
getLanguage () | |
Returns the Language of the Message. More... | |
getParams () | |
Returns the message parameters. More... | |
getTitle () | |
Get a title object for a mediawiki message, where it can be found in the mediawiki namespace. More... | |
inContentLanguage () | |
Request the message in the wiki's content language, unless it is disabled for this message. More... | |
inLanguage ($lang) | |
Request the message in any language that is supported. More... | |
isBlank () | |
Check whether a message does not exist, or is an empty string. More... | |
isDisabled () | |
Check whether a message does not exist, is an empty string, or is "-". More... | |
isMultiKey () | |
numParams () | |
Add parameters that are numeric and will be passed through Language::formatNum before substitution. More... | |
params () | |
Adds parameters to the parameter list of this message. More... | |
parse () | |
Fully parse the text from wikitext to HTML. More... | |
parseAsBlock () | |
Returns the parsed message text which is always surrounded by a block element. More... | |
plain () | |
Returns the message text as-is, only parameters are substituted. More... | |
plaintextParams () | |
Add parameters that are plaintext and will be passed through without the content being evaluated. More... | |
rawParams () | |
Add parameters that are substituted after parsing or escaping. More... | |
serialize () | |
setContext (IContextSource $context) | |
Set the language and the title from a context object. More... | |
setInterfaceMessageFlag ($interface) | |
Allows manipulating the interface message flag directly. More... | |
sizeParams () | |
Add parameters that are file sizes and will be passed through Language::formatSize before substitution. More... | |
text () | |
Returns the message text. More... | |
timeperiodParams () | |
Add parameters that are time periods and will be passed through Language::formatTimePeriod before substitution. More... | |
title ($title) | |
Set the Title object to use as context when transforming the message. More... | |
toString () | |
Returns the message parsed from wikitext to HTML. More... | |
unserialize ($serialized) | |
useDatabase ($useDatabase) | |
Enable or disable database use. More... | |
Static Public Member Functions | |
static | bitrateParam ($bitrate) |
static | durationParam ($duration) |
static | expiryParam ($expiry) |
static | newFallbackSequence () |
Factory function accepting multiple message keys and returning a message instance for the first message which is non-empty. More... | |
static | newFromKey ($key) |
Factory function that is just wrapper for the real constructor. More... | |
static | newFromSpecifier ($value) |
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key string, or a key + params array) into a proper Message. More... | |
static | numParam ($num) |
static | plaintextParam ($plaintext) |
static | rawParam ($raw) |
static | sizeParam ($size) |
static | timeperiodParam ($period) |
Protected Member Functions | |
extractParam ($param) | |
Extracts the parameter type and preprocessed the value if needed. More... | |
fetchMessage () | |
Wrapper for what ever method we use to get message contents. More... | |
formatPlaintext ($plaintext) | |
Formats a message parameter wrapped with 'plaintext'. More... | |
parseText ($string) | |
Wrapper for what ever method we use to parse wikitext. More... | |
replaceParameters ($message, $type= 'before') | |
Substitutes any parameters into the message text. More... | |
transformText ($string) | |
Wrapper for what ever method we use to {{-transform wikitext. More... | |
Protected Attributes | |
Content | $content = null |
Content object representing the message. More... | |
string | $format = 'parse' |
Format for the message. More... | |
bool | $interface = true |
In which language to get this message. More... | |
string | $key |
The message key. More... | |
string[] | $keysToTry |
List of keys to try when fetching the message. More... | |
Language bool | $language = false |
In which language to get this message. More... | |
string | $message |
array | $parameters = [] |
List of parameters which will be substituted into the message. More... | |
Title | $title = null |
Title object to use as context. More... | |
bool | $useDatabase = true |
Whether database can be used. More... | |
The Message class provides methods which fulfil two basic services:
First implemented with MediaWiki 1.17, the Message class is intended to replace the old wfMsg* functions that over time grew unusable.
You should use the wfMessage() global function which acts as a wrapper for the Message class. The wrapper let you pass parameters as arguments.
The most basic usage cases would be:
Since wfMessage() returns a Message instance, you can chain its call with a method. Some of them return a Message instance too so you can chain them. You will find below several examples of wfMessage() usage.
Fetching a message text for interface message:
A Message instance can be passed parameters after it has been constructed, use the params() method to do so:
{{GRAMMAR}} and friends work correctly:
Messages can be requested in a different language or in whatever current content language is being used. The methods are:
Sometimes the message text ends up in the database, so content language is needed:
Checking whether a message exists:
If you want to use a different language:
Use full parsing:
Parseinline is used because it is more useful when pre-building HTML. In normal use it is better to use OutputPage::(add|wrap)WikiMsg.
Places where HTML cannot be used. {{-transformation is done.
Shortcut for escaping the message too, similar to wfMsgHTML(), but parameters are not replaced after escaping by default.
Definition at line 159 of file Message.php.
Message::__construct | ( | $key, | |
$params = [] , |
|||
Language | $language = null |
||
) |
string|string[]|MessageSpecifier | $key Message key, or array of message keys to try and use the first non-empty message for, or a MessageSpecifier to copy from. | |
array | $params | Message parameters. |
Language | $language | [optional] Language to use (defaults to current user language). |
InvalidArgumentException |
Definition at line 234 of file Message.php.
Message::__toString | ( | ) |
Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg: $foo = new Message( $key ); $string = "<abbr>$foo</abbr>";.
Definition at line 854 of file Message.php.
References $format, $key, format, MediaWiki\Logger\LoggerFactory\getInstance(), key, and toString().
|
static |
int | $bitrate |
Definition at line 1051 of file Message.php.
Message::bitrateParams | ( | ) |
Add parameters that are bitrates and will be passed through Language::formatBitrate before substitution.
int|int[] | $param,... Bit rate parameters, or a single argument that is an array of bit rate parameters. |
Definition at line 635 of file Message.php.
Message::content | ( | ) |
Returns the message as a Content object.
Definition at line 786 of file Message.php.
References $content.
|
static |
int | $duration |
Definition at line 1007 of file Message.php.
Referenced by MediaWiki\Auth\LegacyHookPreAuthenticationProvider\makeFailResponse(), and MediaWiki\Auth\LegacyHookPreAuthenticationProviderTest\provideTestForAuthentication().
Message::durationParams | ( | ) |
Add parameters that are durations of time and will be passed through Language::formatDuration before substitution.
int|int[] | $param,... Duration parameters, or a single argument that is an array of duration parameters. |
Definition at line 547 of file Message.php.
Message::escaped | ( | ) |
Returns the message text.
{{-transformation is done and the result is escaped excluding any raw parameters.
Definition at line 937 of file Message.php.
References format, and toString().
Message::exists | ( | ) |
Check whether a message key has been defined currently.
Definition at line 949 of file Message.php.
References fetchMessage().
|
static |
string | $expiry |
Definition at line 1018 of file Message.php.
Message::expiryParams | ( | ) |
Add parameters that are expiration times and will be passed through Language::formatExpiry before substitution.
string|string[] | $param,... Expiry parameters, or a single argument that is an array of expiry parameters. |
Definition at line 569 of file Message.php.
|
protected |
Extracts the parameter type and preprocessed the value if needed.
mixed | $param | Parameter as defined in this class. |
Definition at line 1097 of file Message.php.
References $e, formatPlaintext(), getKey(), getLanguage(), serialize(), and wfDebugLog().
Referenced by replaceParameters().
|
protected |
Wrapper for what ever method we use to get message contents.
MWException | If message key array is empty. |
Definition at line 1183 of file Message.php.
References $cache, $key, $message, as, getLanguage(), key, message, MessageCache\singleton(), and useDatabase().
Referenced by exists(), isBlank(), isDisabled(), and toString().
|
protected |
Formats a message parameter wrapped with 'plaintext'.
Ensures that the entire string is displayed unchanged when displayed in the output format.
string | $plaintext | String to ensure plaintext output of |
Definition at line 1213 of file Message.php.
References format.
Referenced by extractParam().
Message::getFormat | ( | ) |
Returns the message format.
Definition at line 351 of file Message.php.
References $format.
Message::getKey | ( | ) |
Returns the message key.
If a list of multiple possible keys was supplied to the constructor, this method may return any of these keys. After the message has been fetched, this method will return the key that was actually used to fetch the message.
Implements MessageSpecifier.
Definition at line 329 of file Message.php.
References $key.
Referenced by extractParam(), and ApiAuthManagerHelper\formatMessage().
Message::getKeysToTry | ( | ) |
Definition at line 314 of file Message.php.
References $keysToTry.
Message::getLanguage | ( | ) |
Returns the Language of the Message.
Definition at line 362 of file Message.php.
References RequestContext\getMain().
Referenced by extractParam(), fetchMessage(), parseText(), and transformText().
Message::getParams | ( | ) |
Returns the message parameters.
Implements MessageSpecifier.
Definition at line 340 of file Message.php.
References $parameters.
Referenced by ApiAuthManagerHelper\formatMessage(), and StatusTest\sanitizedMessageParams().
Message::getTitle | ( | ) |
Get a title object for a mediawiki message, where it can be found in the mediawiki namespace.
The title will be for the current language, if the message key is in $wgForceUIMsgAsContentMsg it will be append with the language code (except content language), because Message::inContentLanguage will also return in user language.
Definition at line 455 of file Message.php.
References $code, $key, $wgContLang, $wgForceUIMsgAsContentMsg, array(), global, key, Title\makeTitle(), and NS_MEDIAWIKI.
Message::inContentLanguage | ( | ) |
Request the message in the wiki's content language, unless it is disabled for this message.
Definition at line 727 of file Message.php.
References $wgContLang, $wgForceUIMsgAsContentMsg, array(), global, inLanguage(), and key.
Message::inLanguage | ( | $lang | ) |
Request the message in any language that is supported.
As a side effect interface message status is unconditionally turned off.
MWException |
Definition at line 698 of file Message.php.
References $lang, $type, Language\factory(), false, and message.
Referenced by inContentLanguage(), and setContext().
Message::isBlank | ( | ) |
Check whether a message does not exist, or is an empty string.
Definition at line 961 of file Message.php.
References fetchMessage().
Message::isDisabled | ( | ) |
Check whether a message does not exist, is an empty string, or is "-".
Definition at line 973 of file Message.php.
References fetchMessage().
Message::isMultiKey | ( | ) |
Definition at line 304 of file Message.php.
|
static |
Factory function accepting multiple message keys and returning a message instance for the first message which is non-empty.
If all messages are empty then an instance of the first message key is returned.
string|string[] | $keys,... Message keys, or first argument as an array of all the message keys. |
Definition at line 431 of file Message.php.
References $keys.
|
static |
Factory function that is just wrapper for the real constructor.
It is intended to be used instead of the real constructor, because it allows chaining method calls, while new objects don't.
string|string[]|MessageSpecifier | $key | |
mixed | $param,... | Parameters as strings. |
Definition at line 379 of file Message.php.
Referenced by MediaWiki\Auth\LegacyHookPreAuthenticationProviderTest\provideTestForAccountCreation(), MessageTest\testNewFromKey(), and MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider\testUserForCreation().
|
static |
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key string, or a key + params array) into a proper Message.
Also accepts a MessageSpecifier inside an array: that's not considered a valid format but is an easy error to make due to how StatusValue stores messages internally. Further array elements are ignored in that case.
string | array | MessageSpecifier | $value |
InvalidArgumentException |
Definition at line 398 of file Message.php.
References $message, $params, and $value.
Referenced by PasswordReset\execute(), HTMLFormField\getMessage(), HTMLForm\getMessage(), ApiBase\parseMsg(), MWExceptionRenderer\reportHTML(), and MessageTest\testNewFromSpecifier().
|
static |
mixed | $num |
Definition at line 996 of file Message.php.
Referenced by LogFormatter\formatParameterValue(), MergeHistory\isValidMerge(), SpecialBlock\processForm(), SpecialSearch\showCreateLink(), MediaWikiTitleCodec\splitTitleString(), and MergeHistoryTest\testIsValidMergeRevisionLimit().
Message::numParams | ( | ) |
Add parameters that are numeric and will be passed through Language::formatNum before substitution.
mixed | $param,... | Numeric parameters, or a single argument that is an array of numeric parameters. |
Definition at line 525 of file Message.php.
Message::params | ( | ) |
Adds parameters to the parameter list of this message.
mixed | ... Parameters as strings, or a single argument that is an array of strings. |
Definition at line 480 of file Message.php.
References $args.
Message::parse | ( | ) |
Fully parse the text from wikitext to HTML.
Definition at line 888 of file Message.php.
References format, and toString().
Referenced by ApiHelp\wrap().
Message::parseAsBlock | ( | ) |
Returns the parsed message text which is always surrounded by a block element.
Definition at line 924 of file Message.php.
References format, and toString().
|
protected |
Wrapper for what ever method we use to parse wikitext.
string | $string | Wikitext message contents. |
Definition at line 1145 of file Message.php.
References $out, getLanguage(), MessageCache\singleton(), and title().
Referenced by toString().
Message::plain | ( | ) |
Returns the message text as-is, only parameters are substituted.
Definition at line 912 of file Message.php.
References format, and toString().
|
static |
string | $plaintext |
Definition at line 1062 of file Message.php.
Message::plaintextParams | ( | ) |
Add parameters that are plaintext and will be passed through without the content being evaluated.
Plaintext parameters are not valid as arguments to parser functions. This differs from self::rawParams in that the Message class handles escaping to match the output format.
string|string[] | $param,... plaintext parameters, or a single argument that is an array of plaintext parameters. |
Definition at line 659 of file Message.php.
|
static |
mixed | $raw |
Definition at line 985 of file Message.php.
Referenced by LogFormatter\formatParameterValue(), BlockLogFormatter\getMessageParameters(), NewUsersLogFormatter\getMessageParameters(), MergeLogFormatter\getMessageParameters(), PatrolLogFormatter\getMessageParameters(), MoveLogFormatter\getMessageParameters(), ProtectLogFormatter\getMessageParameters(), and LogFormatter\getMessageParameters().
Message::rawParams | ( | ) |
Add parameters that are substituted after parsing or escaping.
In other words the parsing process cannot access the contents of this type of parameter, and you need to make sure it is sanitized beforehand. The parser will see "$n", instead.
mixed | $params,... | Raw parameters as strings, or a single argument that is an array of raw parameters. |
Definition at line 503 of file Message.php.
|
protected |
Substitutes any parameters into the message text.
string | $message | The message text. |
string | $type | Either "before" or "after". |
Definition at line 1076 of file Message.php.
References $message, $type, $value, as, extractParam(), and list.
Referenced by toString().
Message::serialize | ( | ) |
Definition at line 268 of file Message.php.
References $format, $key, $keysToTry, $parameters, $title, and $useDatabase.
Referenced by extractParam().
Message::setContext | ( | IContextSource | $context | ) |
Set the language and the title from a context object.
IContextSource | $context |
Definition at line 679 of file Message.php.
References IContextSource\getLanguage(), IContextSource\getTitle(), inLanguage(), title(), and true.
Referenced by ApiAuthManagerHelper\formatMessage(), and ApiBase\getFinalParamDescription().
Message::setInterfaceMessageFlag | ( | $interface | ) |
Allows manipulating the interface message flag directly.
Can be used to restore the flag after setting a language.
bool | $interface |
Definition at line 748 of file Message.php.
|
static |
int | $size |
Definition at line 1040 of file Message.php.
Message::sizeParams | ( | ) |
Add parameters that are file sizes and will be passed through Language::formatSize before substitution.
int|int[] | $param,... Size parameters, or a single argument that is an array of size parameters. |
Definition at line 613 of file Message.php.
Message::text | ( | ) |
Returns the message text.
{{-transformation is done.
Definition at line 900 of file Message.php.
References format, and toString().
|
static |
number | $period |
Definition at line 1029 of file Message.php.
Message::timeperiodParams | ( | ) |
Add parameters that are time periods and will be passed through Language::formatTimePeriod before substitution.
int|int[] | $param,... Time period parameters, or a single argument that is an array of time period parameters. |
Definition at line 591 of file Message.php.
Message::title | ( | $title | ) |
Set the Title object to use as context when transforming the message.
Title | $title |
Definition at line 776 of file Message.php.
References $title.
Referenced by parseText(), setContext(), transformText(), and unserialize().
Message::toString | ( | ) |
Returns the message parsed from wikitext to HTML.
Definition at line 801 of file Message.php.
References fetchMessage(), format, key, parseText(), replaceParameters(), Parser\stripOuterParagraph(), and transformText().
Referenced by __toString(), escaped(), parse(), parseAsBlock(), plain(), and text().
|
protected |
Wrapper for what ever method we use to {{-transform wikitext.
string | $string | Wikitext message contents. |
Definition at line 1166 of file Message.php.
References getLanguage(), MessageCache\singleton(), and title().
Referenced by toString().
Message::unserialize | ( | $serialized | ) |
string | $serialized |
Definition at line 286 of file Message.php.
References $serialized, Language\factory(), format, key, title(), and useDatabase().
Message::useDatabase | ( | $useDatabase | ) |
Enable or disable database use.
bool | $useDatabase |
Definition at line 762 of file Message.php.
Referenced by fetchMessage(), and unserialize().
|
protected |
Content object representing the message.
Definition at line 218 of file Message.php.
Referenced by content().
Format for the message.
Supported formats are:
Definition at line 203 of file Message.php.
Referenced by __toString(), getFormat(), and serialize().
|
protected |
In which language to get this message.
True, which is the default, means the current user language, false content language.
Definition at line 167 of file Message.php.
|
protected |
The message key.
If $keysToTry has more than one element, this may change to one of the keys to try when fetching the message text.
Definition at line 180 of file Message.php.
Referenced by ApiMessage\__construct(), ApiRawMessage\__construct(), __toString(), fetchMessage(), RawMessage\fetchMessage(), getKey(), getTitle(), newFromKey(), and serialize().
|
protected |
List of keys to try when fetching the message.
Definition at line 185 of file Message.php.
Referenced by getKeysToTry(), and serialize().
In which language to get this message.
Overrides the $interface setting.
Explicit language object, or false for user language
Definition at line 174 of file Message.php.
|
protected |
Definition at line 223 of file Message.php.
Referenced by ApiHelpParamValueMessage\fetchMessage(), fetchMessage(), RawMessage\fetchMessage(), newFromSpecifier(), and replaceParameters().
|
protected |
List of parameters which will be substituted into the message.
Definition at line 190 of file Message.php.
Referenced by getParams(), and serialize().
|
protected |
Title object to use as context.
Definition at line 213 of file Message.php.
Referenced by serialize(), and title().
|
protected |
Whether database can be used.
Definition at line 208 of file Message.php.
Referenced by serialize().