MediaWiki REL1_34
|
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>";. | |
bitrateParams (... $params) | |
Add parameters that are bitrates and will be passed through Language::formatBitrate before substitution. | |
content () | |
Returns the message as a Content object. | |
durationParams (... $params) | |
Add parameters that are durations of time and will be passed through Language::formatDuration before substitution. | |
escaped () | |
Returns the message text. | |
exists () | |
Check whether a message key has been defined currently. | |
expiryParams (... $params) | |
Add parameters that are expiration times and will be passed through Language::formatExpiry before substitution. | |
getFormat () | |
Returns the message format. | |
getKey () | |
Returns the message key. | |
getKeysToTry () | |
getLanguage () | |
Returns the Language of the Message. | |
getParams () | |
Returns the message parameters. | |
getTitle () | |
Get a title object for a mediawiki message, where it can be found in the mediawiki namespace. | |
inContentLanguage () | |
Request the message in the wiki's content language, unless it is disabled for this message. | |
inLanguage ( $lang) | |
Request the message in any language that is supported. | |
isBlank () | |
Check whether a message does not exist, or is an empty string. | |
isDisabled () | |
Check whether a message does not exist, is an empty string, or is "-". | |
isMultiKey () | |
numParams (... $params) | |
Add parameters that are numeric and will be passed through Language::formatNum before substitution. | |
params (... $args) | |
Adds parameters to the parameter list of this message. | |
parse () | |
Fully parse the text from wikitext to HTML. | |
parseAsBlock () | |
Returns the parsed message text which is always surrounded by a block element. | |
plain () | |
Returns the message text as-is, only parameters are substituted. | |
plaintextParams (... $params) | |
Add parameters that are plaintext and will be passed through without the content being evaluated. | |
rawParams (... $params) | |
Add parameters that are substituted after parsing or escaping. | |
serialize () | |
setContext (IContextSource $context) | |
Set the language and the title from a context object. | |
setInterfaceMessageFlag ( $interface) | |
Allows manipulating the interface message flag directly. | |
sizeParams (... $params) | |
Add parameters that are file sizes and will be passed through Language::formatSize before substitution. | |
text () | |
Returns the message text. | |
timeperiodParams (... $params) | |
Add parameters that are time periods and will be passed through Language::formatTimePeriod before substitution. | |
title ( $title) | |
Set the Title object to use as context when transforming the message. | |
toString ( $format=null) | |
Returns the message parsed from wikitext to HTML. | |
unserialize ( $serialized) | |
useDatabase ( $useDatabase) | |
Enable or disable database use. | |
Static Public Member Functions | |
static | bitrateParam ( $bitrate) |
static | durationParam ( $duration) |
static | expiryParam ( $expiry) |
static | listParam (array $list, $type='text') |
static | newFallbackSequence (... $keys) |
Factory function accepting multiple message keys and returning a message instance for the first message which is non-empty. | |
static | newFromKey ( $key,... $params) |
Factory function that is just wrapper for the real constructor. | |
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. | |
static | numParam ( $num) |
static | plaintextParam ( $plaintext) |
static | rawParam ( $raw) |
static | sizeParam ( $size) |
static | timeperiodParam ( $period) |
Public Attributes | |
const | FORMAT_BLOCK_PARSE = 'block-parse' |
Use normal wikitext -> HTML parsing (the result will be wrapped in a block-level HTML tag) | |
const | FORMAT_ESCAPED = 'escaped' |
Transform {{..}} constructs, HTML-escape the result. | |
const | FORMAT_PARSE = 'parse' |
Use normal wikitext -> HTML parsing but strip the block-level wrapper. | |
const | FORMAT_PLAIN = 'plain' |
Use message text as-is. | |
const | FORMAT_TEXT = 'text' |
Transform {{..}} constructs but don't transform to HTML. | |
Protected Member Functions | |
extractParam ( $param, $format) | |
Extracts the parameter type and preprocessed the value if needed. | |
fetchMessage () | |
Wrapper for what ever method we use to get message contents. | |
formatListParam (array $params, $listType, $format) | |
Formats a list of parameters as a concatenated string. | |
formatPlaintext ( $plaintext, $format) | |
Formats a message parameter wrapped with 'plaintext'. | |
parseText ( $string) | |
Wrapper for what ever method we use to parse wikitext. | |
replaceParameters ( $message, $type, $format) | |
Substitutes any parameters into the message text. | |
transformText ( $string) | |
Wrapper for what ever method we use to {{-transform wikitext. | |
Protected Attributes | |
Content | $content = null |
Content object representing the message. | |
string | $format = 'parse' |
bool | $interface = true |
In which language to get this message. | |
string | $key |
The message key. | |
string[] | $keysToTry |
List of keys to try when fetching the message. | |
Language bool | $language = false |
In which language to get this message. | |
string | $message |
array | $parameters = [] |
List of parameters which will be substituted into the message. | |
Title | $title = null |
Title object to use as context. | |
bool | $useDatabase = true |
Whether database can be used. | |
Static Protected Attributes | |
static array | $listTypeMap |
Mapping from Message::listParam() types to Language methods. | |
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 162 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 | null | $language | [optional] Language to use (defaults to current user language). |
InvalidArgumentException |
Definition at line 251 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 905 of file Message.php.
References toString().
|
static |
int | $bitrate |
Definition at line 1093 of file Message.php.
Referenced by bitrateParams().
Message::bitrateParams | ( | $params | ) |
Add parameters that are bitrates and will be passed through Language::formatBitrate before substitution.
int|int[] | ...$params Bit rate parameters, or a single argument that is an array of bit rate parameters. |
Definition at line 671 of file Message.php.
References bitrateParam().
Message::content | ( | ) |
|
static |
int | $duration |
Definition at line 1049 of file Message.php.
Referenced by durationParams().
Message::durationParams | ( | $params | ) |
Add parameters that are durations of time and will be passed through Language::formatDuration before substitution.
int|int[] | ...$params Duration parameters, or a single argument that is an array of duration parameters. |
Definition at line 587 of file Message.php.
References durationParam().
Message::escaped | ( | ) |
Returns the message text.
{{-transformation is done and the result is escaped excluding any raw parameters.
Definition at line 979 of file Message.php.
References FORMAT_ESCAPED, and toString().
Referenced by MediaWiki\Widget\Search\BasicSearchResultSetWidget\header().
Message::exists | ( | ) |
Check whether a message key has been defined currently.
Definition at line 991 of file Message.php.
References fetchMessage().
|
static |
string | $expiry |
Definition at line 1060 of file Message.php.
Referenced by expiryParams().
Message::expiryParams | ( | $params | ) |
Add parameters that are expiration times and will be passed through Language::formatExpiry before substitution.
string|string[] | ...$params Expiry parameters, or a single argument that is an array of expiry parameters. |
Definition at line 608 of file Message.php.
References expiryParam().
|
protected |
Extracts the parameter type and preprocessed the value if needed.
mixed | $param | Parameter as defined in this class. |
string | $format | One of the FORMAT_* constants. |
Definition at line 1172 of file Message.php.
References $format, $interface, $language, $title, $useDatabase, formatListParam(), formatPlaintext(), getKey(), getLanguage(), and serialize().
Referenced by formatListParam(), and replaceParameters().
|
protected |
Wrapper for what ever method we use to get message contents.
MWException | If message key array is empty. |
Reimplemented in ApiHelpParamValueMessage, and RawMessage.
Definition at line 1292 of file Message.php.
References $cache, $key, $message, getLanguage(), and useDatabase().
Referenced by exists(), isBlank(), isDisabled(), and toString().
|
protected |
Formats a list of parameters as a concatenated string.
array | $params | |
string | $listType | |
string | $format | One of the FORMAT_* constants. |
Definition at line 1345 of file Message.php.
References $format, $type, extractParam(), getKey(), getLanguage(), serialize(), and wfDebugLog().
Referenced by extractParam().
|
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 |
string | $format | One of the FORMAT_* constants. |
Definition at line 1323 of file Message.php.
References $format, FORMAT_BLOCK_PARSE, FORMAT_ESCAPED, FORMAT_PARSE, FORMAT_PLAIN, and FORMAT_TEXT.
Referenced by extractParam().
Message::getFormat | ( | ) |
Returns the message format.
Definition at line 381 of file Message.php.
References $format, and wfDeprecated().
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 358 of file Message.php.
References $key.
Referenced by CommentStore\encodeMessage(), extractParam(), formatListParam(), and ApiAuthManagerHelper\formatMessage().
Message::getKeysToTry | ( | ) |
Definition at line 343 of file Message.php.
References $keysToTry.
Referenced by CommentStore\encodeMessage().
Message::getLanguage | ( | ) |
Returns the Language of the Message.
Definition at line 393 of file Message.php.
Referenced by TransformTooBigImageAreaError\__construct(), extractParam(), fetchMessage(), formatListParam(), getTitle(), parseText(), and transformText().
Message::getParams | ( | ) |
Returns the message parameters.
Implements MessageSpecifier.
Definition at line 369 of file Message.php.
References $parameters.
Referenced by CommentStore\encodeMessage(), and ApiAuthManagerHelper\formatMessage().
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 483 of file Message.php.
References $key, $lang, $title, $wgForceUIMsgAsContentMsg, getLanguage(), and NS_MEDIAWIKI.
Message::inContentLanguage | ( | ) |
Request the message in the wiki's content language, unless it is disabled for this message.
Definition at line 767 of file Message.php.
References $wgForceUIMsgAsContentMsg, and inLanguage().
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 732 of file Message.php.
References $lang, $language, $type, and Language\getCode().
Referenced by inContentLanguage(), CommentStoreComment\newUnsavedComment(), and setContext().
Message::isBlank | ( | ) |
Check whether a message does not exist, or is an empty string.
Definition at line 1003 of file Message.php.
References $message, and fetchMessage().
Message::isDisabled | ( | ) |
Check whether a message does not exist, is an empty string, or is "-".
Definition at line 1015 of file Message.php.
References $message, and fetchMessage().
Message::isMultiKey | ( | ) |
Definition at line 333 of file Message.php.
|
static |
array | $list | |
string | $type | 'comma', 'semicolon', 'pipe', 'text' |
Definition at line 1115 of file Message.php.
References $type.
Referenced by ApiUpload\checkVerification(), MediaWiki\Message\TextFormatter\convertParam(), ApiQueryLanguageinfo\execute(), ApiPageSet\getAllowedParams(), ApiHelp\getHelpInternal(), ApiMain\modifyHelp(), ApiBase\parseMultiValue(), ApiMain\reportUnusedParams(), ApiBase\requireAtLeastOneParameter(), ApiBase\requireMaxOneParameter(), and ApiBase\requireOnlyOneParameter().
|
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 460 of file Message.php.
References $keys.
Referenced by wfMessageFallback().
|
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 | ...$params Parameters as strings. |
Definition at line 410 of file Message.php.
References $key.
Referenced by MediaWiki\Auth\ButtonAuthenticationRequest\__set_state().
|
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 427 of file Message.php.
References $message.
Referenced by PermissionsError\__construct(), ApiMain\errorMessagesFromException(), PasswordReset\execute(), ApiLogin\formatMessage(), HTMLForm\getMessage(), HTMLFormField\getMessage(), ApiErrorFormatter\getMessageFromException(), LocalizedException\getMessageObject(), UploadStashException\getMessageObject(), and MWExceptionRenderer\reportHTML().
|
static |
mixed | $num |
Definition at line 1038 of file Message.php.
Referenced by ApiResult\addValue(), Article\confirmDelete(), ApiOptions\execute(), LogFormatter\formatParameterValue(), ApiUpload\getChunkResult(), MergeHistory\isValidMerge(), numParams(), SpecialBlock\processForm(), SpecialSearch\showCreateLink(), and MediaWikiTitleCodec\splitTitleString().
Message::numParams | ( | $params | ) |
Add parameters that are numeric and will be passed through Language::formatNum before substitution.
mixed | ...$params Numeric parameters, or a single argument that is an array of numeric parameters. |
Definition at line 566 of file Message.php.
References numParam().
Message::params | ( | $args | ) |
Adds parameters to the parameter list of this message.
mixed | ...$args Parameters as strings or arrays from Message::numParam() and the like, or a single array of parameters. |
Definition at line 510 of file Message.php.
Referenced by TransformTooBigImageAreaError\__construct().
Message::parse | ( | ) |
Fully parse the text from wikitext to HTML.
Definition at line 930 of file Message.php.
References FORMAT_PARSE, and toString().
Referenced by ApiHelp\wrap().
Message::parseAsBlock | ( | ) |
Returns the parsed message text which is always surrounded by a block element.
Definition at line 966 of file Message.php.
References FORMAT_BLOCK_PARSE, and toString().
|
protected |
Wrapper for what ever method we use to parse wikitext.
string | $string | Wikitext message contents. |
Definition at line 1245 of file Message.php.
References getLanguage(), ParserOutput\getText(), and title().
Referenced by toString().
Message::plain | ( | ) |
Returns the message text as-is, only parameters are substituted.
Definition at line 954 of file Message.php.
References FORMAT_PLAIN, and toString().
|
static |
string | $plaintext |
Definition at line 1104 of file Message.php.
Referenced by CommentStoreComment\__construct(), ApiEditPage\execute(), plaintextParams(), and ChangeTags\tagShortDescriptionMessage().
Message::plaintextParams | ( | $params | ) |
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[] | ...$params plaintext parameters, or a single argument that is an array of plaintext parameters. |
Definition at line 694 of file Message.php.
References plaintextParam().
|
static |
mixed | $raw |
Definition at line 1027 of file Message.php.
Referenced by WebInstallerComplete\execute(), LogFormatter\formatParameterValue(), UserGroupMembership\getLink(), BlockLogFormatter\getMessageParameters(), LogFormatter\getMessageParameters(), MergeLogFormatter\getMessageParameters(), MoveLogFormatter\getMessageParameters(), NewUsersLogFormatter\getMessageParameters(), PatrolLogFormatter\getMessageParameters(), ProtectLogFormatter\getMessageParameters(), TagLogFormatter\getMessageParameters(), RollbackAction\handleRollbackRequest(), MarkpatrolledAction\preText(), rawParams(), SpecialUndelete\showRevision(), SpecialEmailUser\submit(), and SpecialUndelete\undelete().
Message::rawParams | ( | $params | ) |
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 545 of file Message.php.
References rawParam().
|
protected |
Substitutes any parameters into the message text.
string | $message | The message text. |
string | $type | Either "before" or "after". |
string | $format | One of the FORMAT_* constants. |
Definition at line 1135 of file Message.php.
References $format, $message, $type, and extractParam().
Referenced by toString().
Message::serialize | ( | ) |
Definition at line 285 of file Message.php.
References $format, $key, $keysToTry, $parameters, $useDatabase, serialize(), and title().
Referenced by extractParam(), formatListParam(), and serialize().
Message::setContext | ( | IContextSource | $context | ) |
Set the language and the title from a context object.
IContextSource | $context |
Definition at line 713 of file Message.php.
References IContextSource\getLanguage(), IContextSource\getTitle(), inLanguage(), title(), and true.
Referenced by ApiAuthManagerHelper\formatMessage().
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 787 of file Message.php.
References $interface.
|
static |
int | $size |
Definition at line 1082 of file Message.php.
Referenced by sizeParams().
Message::sizeParams | ( | $params | ) |
Add parameters that are file sizes and will be passed through Language::formatSize before substitution.
int|int[] | ...$params Size parameters, or a single argument that is an array of size parameters. |
Definition at line 650 of file Message.php.
References sizeParam().
Message::text | ( | ) |
Returns the message text.
{{-transformation is done.
Definition at line 942 of file Message.php.
References FORMAT_TEXT, and toString().
|
static |
int | $period |
Definition at line 1071 of file Message.php.
Referenced by timeperiodParams().
Message::timeperiodParams | ( | $params | ) |
Add parameters that are time periods and will be passed through Language::formatTimePeriod before substitution.
int|int[] | ...$params Time period parameters, or a single argument that is an array of time period parameters. |
Definition at line 629 of file Message.php.
References timeperiodParam().
Message::title | ( | $title | ) |
Set the Title object to use as context when transforming the message.
Title | $title |
Definition at line 816 of file Message.php.
References $title, and title().
Referenced by parseText(), serialize(), setContext(), title(), transformText(), and unserialize().
Message::toString | ( | $format = null | ) |
Returns the message parsed from wikitext to HTML.
string | null | $format | One of the FORMAT_* constants. Null means use whatever was used the last time (this is for B/C and should be avoided). |
Definition at line 845 of file Message.php.
References $format, fetchMessage(), parseText(), replaceParameters(), 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 1275 of file Message.php.
References getLanguage(), and title().
Referenced by toString().
Message::unserialize | ( | $serialized | ) |
string | $serialized |
Definition at line 303 of file Message.php.
References $serialized, title(), unserialize(), and useDatabase().
Referenced by unserialize().
Message::useDatabase | ( | $useDatabase | ) |
Enable or disable database use.
bool | $useDatabase |
Definition at line 801 of file Message.php.
References $useDatabase, and useDatabase().
Referenced by fetchMessage(), unserialize(), and useDatabase().
|
protected |
Content object representing the message.
Definition at line 235 of file Message.php.
Referenced by content().
|
protected |
Definition at line 220 of file Message.php.
Referenced by extractParam(), formatListParam(), formatPlaintext(), getFormat(), replaceParameters(), serialize(), and toString().
|
protected |
In which language to get this message.
True, which is the default, means the current user language, false content language.
Definition at line 191 of file Message.php.
Referenced by extractParam(), ApiHelpParamValueMessage\fetchMessage(), and setInterfaceMessageFlag().
|
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 204 of file Message.php.
Referenced by __construct(), ApiMessage\__construct(), ApiRawMessage\__construct(), fetchMessage(), RawMessage\fetchMessage(), getKey(), getTitle(), newFromKey(), params(), and serialize().
|
protected |
List of keys to try when fetching the message.
Definition at line 209 of file Message.php.
Referenced by getKeysToTry(), and serialize().
|
protected |
In which language to get this message.
Overrides the $interface setting.
Explicit language object, or false for user language
Definition at line 198 of file Message.php.
Referenced by __construct(), extractParam(), ApiHelpParamValueMessage\fetchMessage(), and inLanguage().
|
staticprotected |
Mapping from Message::listParam() types to Language methods.
Definition at line 178 of file Message.php.
|
protected |
Definition at line 240 of file Message.php.
Referenced by ApiHelpParamValueMessage\fetchMessage(), fetchMessage(), RawMessage\fetchMessage(), isBlank(), isDisabled(), newFromSpecifier(), and replaceParameters().
|
protected |
List of parameters which will be substituted into the message.
Definition at line 214 of file Message.php.
Referenced by getParams(), and serialize().
|
protected |
Title object to use as context.
Definition at line 230 of file Message.php.
Referenced by extractParam(), ApiHelpParamValueMessage\fetchMessage(), getTitle(), and title().
|
protected |
Whether database can be used.
Definition at line 225 of file Message.php.
Referenced by extractParam(), ApiHelpParamValueMessage\fetchMessage(), serialize(), and useDatabase().
const Message::FORMAT_BLOCK_PARSE = 'block-parse' |
Use normal wikitext -> HTML parsing (the result will be wrapped in a block-level HTML tag)
Definition at line 166 of file Message.php.
Referenced by formatPlaintext(), and parseAsBlock().
const Message::FORMAT_ESCAPED = 'escaped' |
Transform {{..}} constructs, HTML-escape the result.
Definition at line 172 of file Message.php.
Referenced by escaped(), and formatPlaintext().
const Message::FORMAT_PARSE = 'parse' |
Use normal wikitext -> HTML parsing but strip the block-level wrapper.
Definition at line 168 of file Message.php.
Referenced by formatPlaintext(), and parse().
const Message::FORMAT_PLAIN = 'plain' |
Use message text as-is.
Definition at line 164 of file Message.php.
Referenced by formatPlaintext(), and plain().
const Message::FORMAT_TEXT = 'text' |
Transform {{..}} constructs but don't transform to HTML.
Definition at line 170 of file Message.php.
Referenced by formatPlaintext(), and text().