MediaWiki REL1_40
|
The Message class deals with fetching and processing of interface message into a variety of formats. More...
Inherits MessageSpecifier, and Serializable.
Inherited by ApiHelpParamValueMessage, ApiMessage, and MediaWiki\Language\RawMessage.
Public Member Functions | |
__construct ( $key, $params=[], Language $language=null) | |
__serialize () | |
__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>";. | |
__unserialize ( $data) | |
bitrateParams (... $params) | |
Add parameters that are bitrates and will be passed through Language::formatBitrate before substitution. | |
content () | |
Returns the message as a Content object. | |
dateParams (... $params) | |
Add parameters that are dates and will be passed through Language::date before substitution. | |
dateTimeParams (... $params) | |
Add parameters that are date-times and will be passed through Language::timeanddate before substitution. | |
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. | |
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. | |
objectParams (... $params) | |
Add parameters that represent stringable objects. | |
page (?PageReference $page) | |
Set the page object to use as context when transforming the message. | |
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. | |
timeParams (... $params) | |
Add parameters that are times and will be passed through Language::time before substitution. | |
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 (string $format) | |
Returns the message formatted a certain way. | |
unserialize ( $serialized) | |
useDatabase ( $useDatabase) | |
Enable or disable database use. | |
userGroupParams (... $params) | |
Add parameters that represent user groups. | |
Static Public Member Functions | |
static | bitrateParam ( $bitrate) |
static | dateParam (string $date) |
static | dateTimeParam (string $dateTime) |
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 | objectParam (Stringable $object) |
static | plaintextParam ( $plaintext) |
static | rawParam ( $raw) |
static | sizeParam ( $size) |
static | timeParam (string $time) |
static | timeperiodParam ( $period) |
static | userGroupParam (string $userGroup) |
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 null | $content = null |
Content object representing the message. | |
PageReference | $contextPage = null |
page object to use as context. | |
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 null | $language = null |
In which language to get this message. | |
string null false | $message |
array | $parameters = [] |
List of parameters which will be substituted into the message. | |
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 deals with fetching and processing of interface message into a variety of formats.
First implemented with MediaWiki 1.17, the Message class is intended to replace the old wfMsg* functions that over time grew unusable.
The preferred way to create Message objects is via the msg() method of of an available RequestContext and ResourceLoader Context object; this will ensure that the message uses the correct language. When that is not possible, the wfMessage() global function can be used, which will cause Message to get the language from the global RequestContext object. In rare circumstances when sessions are not available or not initialized, that can lead to errors.
The most basic usage cases would be:
Since msg() 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 msg() 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:
Definition at line 144 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 228 of file Message.php.
Message::__serialize | ( | ) |
Definition at line 272 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 1031 of file Message.php.
Message::__unserialize | ( | $data | ) |
array | $data |
Definition at line 304 of file Message.php.
|
static |
int | $bitrate |
Definition at line 1256 of file Message.php.
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 775 of file Message.php.
Message::content | ( | ) |
Returns the message as a Content object.
Definition at line 943 of file Message.php.
References $content, and wfDeprecated().
|
static |
string | $date |
Definition at line 1190 of file Message.php.
Message::dateParams | ( | $params | ) |
Add parameters that are dates and will be passed through Language::date before substitution.
string|string[] | ...$params Date parameters, or a single argument that is an array of date parameters. |
Definition at line 651 of file Message.php.
|
static |
string | $dateTime |
Definition at line 1179 of file Message.php.
Message::dateTimeParams | ( | $params | ) |
Add parameters that are date-times and will be passed through Language::timeanddate before substitution.
string|string[] | ...$params Date-time parameters, or a single argument that is an array of date-time parameters. |
Definition at line 630 of file Message.php.
|
static |
int | $duration |
Definition at line 1157 of file Message.php.
Referenced by SpecialBlock\getFormFields().
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 588 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 1087 of file Message.php.
Referenced by MediaWiki\Search\SearchWidgets\BasicSearchResultSetWidget\header().
Message::exists | ( | ) |
Check whether a message key has been defined currently.
Definition at line 1098 of file Message.php.
|
static |
string | $expiry |
Definition at line 1168 of file Message.php.
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 609 of file Message.php.
|
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 1335 of file Message.php.
|
protected |
Wrapper for what ever method we use to get message contents.
MWException | If message key array is empty. |
Reimplemented in ApiHelpParamValueMessage, and MediaWiki\Language\RawMessage.
Definition at line 1471 of file Message.php.
|
protected |
Formats a list of parameters as a concatenated string.
array | $params | |
string | $listType | |
string | $format | One of the FORMAT_* constants. |
Definition at line 1527 of file Message.php.
References $type, and wfDebugLog().
|
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 1505 of file Message.php.
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 364 of file Message.php.
Referenced by MediaWiki\Message\Converter\convertMessage().
Message::getKeysToTry | ( | ) |
Definition at line 349 of file Message.php.
Message::getLanguage | ( | ) |
Returns the Language of the Message.
Definition at line 386 of file Message.php.
Message::getParams | ( | ) |
Returns the message parameters.
Implements MessageSpecifier.
Definition at line 375 of file Message.php.
Referenced by MediaWiki\Message\Converter\convertMessage().
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 $lang, $title, and NS_MEDIAWIKI.
Message::inContentLanguage | ( | ) |
Request the message in the wiki's content language, unless it is disabled for this message.
Definition at line 869 of file Message.php.
Message::inLanguage | ( | $lang | ) |
Request the message in any language that is supported.
As a side effect interface message status is unconditionally turned off.
Definition at line 835 of file Message.php.
References $lang, and Language\getCode().
Referenced by MediaWiki\CommentStore\CommentStoreComment\newUnsavedComment().
Message::isBlank | ( | ) |
Check whether a message does not exist, or is an empty string.
Definition at line 1110 of file Message.php.
Message::isDisabled | ( | ) |
Check whether a message does not exist, is an empty string, or is "-".
Definition at line 1122 of file Message.php.
Message::isMultiKey | ( | ) |
Definition at line 339 of file Message.php.
|
static |
array | $list | |
string | $type | 'comma', 'semicolon', 'pipe', 'text' |
Definition at line 1278 of file Message.php.
References $type.
Referenced by PermissionsError\__construct(), ApiUpload\checkVerification(), ApiQueryLanguageinfo\execute(), ApiPageSet\getAllowedParams(), SpecialVersion\getCopyrightAndAuthorList(), ApiMain\modifyHelp(), User\newFatalPermissionDeniedStatus(), 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 last message key is returned.
string|string[] | ...$keys Message keys, or first argument as an array of all the message keys. |
-taint | ...$keys tainted |
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 403 of file Message.php.
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.
When the MessageSpecifier object is an instance of Message, a clone of the object is returned. This is unlike the new Message( … )
constructor, which returns a new object constructed from scratch with the same key. This difference is mostly relevant when the passed object is an instance of a subclass like RawMessage or ApiMessage.
string | array | MessageSpecifier | $value | |
-taint | $value | tainted |
InvalidArgumentException |
Definition at line 426 of file Message.php.
Referenced by PermissionsError\__construct(), ApiMain\errorMessagesFromException(), PasswordReset\execute(), HTMLForm\getMessage(), HTMLFormField\getMessage(), ApiErrorFormatter\getMessageFromException(), LocalizedException\getMessageObject(), UploadStashException\getMessageObject(), and wfMessage().
|
static |
mixed | $num |
Definition at line 1146 of file Message.php.
Referenced by ApiResult\addValue(), MediaWiki\Linker\Linker\buildRollbackLink(), ApiOptions\execute(), LogFormatter\formatParameterValue(), MediaWiki\Page\MergeHistory\isValidMerge(), Parser\limitationWarn(), PPFrame_Hash\newChild(), MediaWiki\Preferences\DefaultPreferencesFactory\rcPreferences(), SpecialSearch\showCreateLink(), MediaWikiTitleCodec\splitTitleString(), and MediaWiki\Preferences\DefaultPreferencesFactory\watchlistPreferences().
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 567 of file Message.php.
|
static |
Stringable | $object |
Definition at line 1223 of file Message.php.
Message::objectParams | ( | $params | ) |
Add parameters that represent stringable objects.
Stringable|Stringable[] | ...$params stringable parameters, or a single argument that is an array of stringable parameters. |
Definition at line 691 of file Message.php.
Message::page | ( | ?PageReference | $page | ) |
Set the page object to use as context when transforming the message.
?PageReference | $page |
Definition at line 933 of file Message.php.
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 511 of file Message.php.
Message::parse | ( | ) |
Fully parse the text from wikitext to HTML.
Definition at line 1042 of file Message.php.
Message::parseAsBlock | ( | ) |
Returns the parsed message text which is always surrounded by a block element.
Definition at line 1075 of file Message.php.
|
protected |
Wrapper for what ever method we use to parse wikitext.
string | $string | Wikitext message contents. |
Definition at line 1422 of file Message.php.
References ParserOutput\getText().
Message::plain | ( | ) |
Returns the message text as-is, only parameters are substituted.
Definition at line 1064 of file Message.php.
|
static |
string | $plaintext |
Definition at line 1267 of file Message.php.
Referenced by MediaWiki\CommentStore\CommentStoreComment\__construct(), Parser\braceSubstitution(), CoreParserFunctions\displaytitle(), ApiEditPage\execute(), PPFrame_Hash\newChild(), 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 798 of file Message.php.
|
static |
mixed | $raw | |
-taint | $raw | html,exec_html |
Definition at line 1135 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(), RenameuserLogFormatter\getMessageParameters(), RightsLogFormatter\getMessageParameters(), TagLogFormatter\getMessageParameters(), RollbackAction\handleRollbackRequest(), and MarkpatrolledAction\preText().
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 546 of file Message.php.
|
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 1298 of file Message.php.
References $type.
Message::serialize | ( | ) |
Definition at line 263 of file Message.php.
Message::setContext | ( | IContextSource | $context | ) |
Set the language and the title from a context object.
IContextSource | $context |
Definition at line 817 of file Message.php.
References IContextSource\getLanguage(), IContextSource\getTitle(), and true.
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 890 of file Message.php.
|
static |
int | $size |
Definition at line 1245 of file Message.php.
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 754 of file Message.php.
Message::text | ( | ) |
Returns the message text.
{{-transformation is done.
Definition at line 1053 of file Message.php.
|
static |
string | $time |
Definition at line 1201 of file Message.php.
Message::timeParams | ( | $params | ) |
Add parameters that are times and will be passed through Language::time before substitution.
string|string[] | ...$params Time parameters, or a single argument that is an array of time parameters. |
Definition at line 712 of file Message.php.
|
static |
int | $period |
Definition at line 1234 of file Message.php.
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 733 of file Message.php.
Message::title | ( | $title | ) |
Set the Title object to use as context when transforming the message.
Title | $title |
Definition at line 920 of file Message.php.
References $title.
Message::toString | ( | string | $format | ) |
Returns the message formatted a certain way.
string | $format | One of the FORMAT_* constants. |
Definition at line 959 of file Message.php.
|
protected |
Wrapper for what ever method we use to {{-transform wikitext.
string | $string | Wikitext message contents. |
Definition at line 1454 of file Message.php.
Message::unserialize | ( | $serialized | ) |
string | $serialized |
Definition at line 295 of file Message.php.
Message::useDatabase | ( | $useDatabase | ) |
Enable or disable database use.
bool | $useDatabase |
Definition at line 904 of file Message.php.
|
static |
string | $userGroup |
Definition at line 1212 of file Message.php.
Message::userGroupParams | ( | $params | ) |
Add parameters that represent user groups.
string|string[] | ...$params User Group parameters, or a single argument that is an array of user group parameters. |
Definition at line 671 of file Message.php.
|
protected |
Content object representing the message.
Definition at line 211 of file Message.php.
|
protected |
page object to use as context.
Definition at line 206 of file Message.php.
|
protected |
In which language to get this message.
True, which is the default, means the current user language, false content language.
Definition at line 173 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 186 of file Message.php.
Referenced by __construct(), ApiMessage\__construct(), ApiRawMessage\__construct(), and MediaWiki\Language\RawMessage\fetchMessage().
|
protected |
List of keys to try when fetching the message.
Definition at line 191 of file Message.php.
|
protected |
In which language to get this message.
Overrides the $interface setting.
Explicit language object, or null for user language
Definition at line 180 of file Message.php.
Referenced by __construct().
|
staticprotected |
Mapping from Message::listParam() types to Language methods.
Definition at line 160 of file Message.php.
|
protected |
Definition at line 216 of file Message.php.
Referenced by ApiHelpParamValueMessage\fetchMessage(), and MediaWiki\Language\RawMessage\fetchMessage().
|
protected |
List of parameters which will be substituted into the message.
Definition at line 196 of file Message.php.
|
protected |
Whether database can be used.
Definition at line 201 of file Message.php.
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 148 of file Message.php.
const Message::FORMAT_ESCAPED = 'escaped' |
Transform {{..}} constructs, HTML-escape the result.
Definition at line 154 of file Message.php.
const Message::FORMAT_PARSE = 'parse' |
Use normal wikitext -> HTML parsing but strip the block-level wrapper.
Definition at line 150 of file Message.php.
const Message::FORMAT_PLAIN = 'plain' |
Use message text as-is.
Definition at line 146 of file Message.php.
const Message::FORMAT_TEXT = 'text' |
Transform {{..}} constructs but don't transform to HTML.
Definition at line 152 of file Message.php.