MediaWiki  REL1_23
Message Class Reference

The Message class provides methods which fulfil two basic services: More...

Inheritance diagram for Message:
Collaboration diagram for Message:

List of all members.

Public Member Functions

 __construct ($key, $params=array(), Language $language=null)
 __toString ()
 Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg: $foo = Message::get( $key ); $string = "<abbr>$foo</abbr>";.
 bitrateParams ()
 Add parameters that are bitrates and will be passed through Language::formatBitrate before substitution.
 content ()
 Returns the message as a Content object.
 durationParams ()
 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 ()
 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 or the first from an array of message keys.
 getLanguage ()
 Returns the Language of the Message.
 getParams ()
 Returns the message parameters.
 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 "-".
 numParams ()
 Add parameters that are numeric and will be passed through Language::formatNum before substitution.
 params ()
 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.
 rawParams ()
 Add parameters that are substituted after parsing or escaping.
 setContext (IContextSource $context)
 Set the language and the title from a context object.
 setInterfaceMessageFlag ($interface)
 Allows manipulating the interface message flag directly.
 sizeParams ()
 Add parameters that are file sizes and will be passed through Language::formatSize before substitution.
 text ()
 Returns the message text.
 timeperiodParams ()
 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 ()
 Returns the message parsed from wikitext to HTML.
 useDatabase ($useDatabase)
 Enable or disable database use.

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.
static newFromKey ($key)
 Factory function that is just wrapper for the real constructor.
static numParam ($num)
static rawParam ($raw)
static sizeParam ($size)
static timeperiodParam ($period)

Protected Member Functions

 extractParam ($param)
 Extracts the parameter type and preprocessed the value if needed.
 fetchMessage ()
 Wrapper for what ever method we use to get message contents.
 parseText ($string)
 Wrapper for what ever method we use to parse wikitext.
 replaceParameters ($message, $type= 'before')
 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'
 Format for the message.
bool $interface = true
 In which language to get this message.
string string[] $key
 The message key or array of keys.
Language $language = null
 In which language to get this message.
string $message
array $parameters = array()
 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.

Detailed Description

The Message class provides methods which fulfil two basic services:

  • fetching interface messages
  • processing messages 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.

See also:
https://www.mediawiki.org/wiki/Manual:Messages_API for equivalences between old and new functions.

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:

     // Initialize a Message object using the 'some_key' message key
     $message = wfMessage( 'some_key' );

     // Using two parameters those values are strings 'value1' and 'value2':
     $message = wfMessage( 'some_key',
          'value1', 'value2'
     );

Global function wrapper:

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:

    $button = Xml::button(
         wfMessage( 'submit' )->text()
    );

A Message instance can be passed parameters after it has been constructed, use the params() method to do so:

     wfMessage( 'welcome-to' )
         ->params( $wgSitename )
         ->text();

{{GRAMMAR}} and friends work correctly:

    wfMessage( 'are-friends',
        $user, $friend
    );
    wfMessage( 'bad-message' )
         ->rawParams( '<script>...</script>' )
         ->escaped();

Changing language:

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:

    wfMessage( 'file-log',
        $user, $filename
    )->inContentLanguage()->text();

Checking whether a message exists:

    wfMessage( 'mysterious-message' )->exists()
    // returns a boolean whether the 'mysterious-message' key exist.

If you want to use a different language:

    $userLanguage = $user->getOption( 'language' );
    wfMessage( 'email-header' )
         ->inLanguage( $userLanguage )
         ->plain();
Note:
You can parse the text only in the content or interface languages

Comparison with old wfMsg* functions:

Use full parsing:

     // old style:
     wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
     // new style:
     wfMessage( 'key', 'apple' )->parse();

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.

     // old style:
     wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
     // new style:
     wfMessage( 'key', 'apple', 'pear' )->text();

Shortcut for escaping the message too, similar to wfMsgHTML(), but parameters are not replaced after escaping by default.

     $escaped = wfMessage( 'key' )
          ->rawParams( 'apple' )
          ->escaped();

Appendix:

Todo:
  • test, can we have tests?
  • this documentation needs to be extended
See also:
https://www.mediawiki.org/wiki/WfMessage()
https://www.mediawiki.org/wiki/New_messages_API
https://www.mediawiki.org/wiki/Localisation
Since:
1.17

Definition at line 159 of file Message.php.


Constructor & Destructor Documentation

Message::__construct ( key,
params = array(),
Language language = null 
)
Since:
1.17
Parameters:
string|string[]$key Message key or array of message keys to try and use the first non-empty message for.
array$paramsMessage parameters.
Language$languageOptional language of the message, defaults to $wgLang.

Definition at line 219 of file Message.php.

References $key, $language, $params, $wgLang, global, key, and language.


Member Function Documentation

Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg: $foo = Message::get( $key ); $string = "<abbr>$foo</abbr>";.

Since:
1.18
Returns:
string

Definition at line 681 of file Message.php.

static Message::bitrateParam ( bitrate) [static]
Since:
1.22
Parameters:
int$bitrate
Returns:
int[] Array with a single "bitrate" key.

Definition at line 872 of file Message.php.

Add parameters that are bitrates and will be passed through Language::formatBitrate before substitution.

Since:
1.22
Parameters:
int|int[][$param,...] Bit rate parameters, or a single argument that is an array of bit rate parameters.
Returns:
Message $this

Definition at line 486 of file Message.php.

Returns the message as a Content object.

Returns:
Content

Definition at line 612 of file Message.php.

static Message::durationParam ( duration) [static]
Since:
1.22
Parameters:
int$duration
Returns:
int[] Array with a single "duration" key.

Definition at line 828 of file Message.php.

Add parameters that are durations of time and will be passed through Language::formatDuration before substitution.

Since:
1.22
Parameters:
int|int[][$param,...] Duration parameters, or a single argument that is an array of duration parameters.
Returns:
Message $this

Definition at line 398 of file Message.php.

Returns the message text.

{{-transformation is done and the result is escaped excluding any raw parameters.

Since:
1.17
Returns:
string Escaped message text.

Definition at line 758 of file Message.php.

References format, and toString().

Check whether a message key has been defined currently.

Since:
1.17
Returns:
bool

Definition at line 770 of file Message.php.

static Message::expiryParam ( expiry) [static]
Since:
1.22
Parameters:
string$expiry
Returns:
string[] Array with a single "expiry" key.

Definition at line 839 of file Message.php.

Add parameters that are expiration times and will be passed through Language::formatExpiry before substitution.

Since:
1.22
Parameters:
string|string[][$param,...] Expiry parameters, or a single argument that is an array of expiry parameters.
Returns:
Message $this

Definition at line 420 of file Message.php.

Message::extractParam ( param) [protected]

Extracts the parameter type and preprocessed the value if needed.

Since:
1.18
Parameters:
mixed$paramParameter as defined in this class.
Returns:
array Array with the parameter type (either "before" or "after") and the value.

Definition at line 907 of file Message.php.

Message::fetchMessage ( ) [protected]

Wrapper for what ever method we use to get message contents.

Since:
1.17
Returns:
string
Exceptions:
MWExceptionIf message key array is empty.

Reimplemented in RawMessage.

Definition at line 979 of file Message.php.

References $cache, $key, $message, as, key, language, message, MessageCache\singleton(), and useDatabase().

Referenced by isBlank().

Returns the message format.

Since:
1.21
Returns:
string

Definition at line 262 of file Message.php.

Returns the message key or the first from an array of message keys.

Since:
1.21
Returns:
string

Definition at line 234 of file Message.php.

Returns the Language of the Message.

Since:
1.23
Returns:
Language

Definition at line 273 of file Message.php.

Returns the message parameters.

Since:
1.21
Returns:
array

Definition at line 251 of file Message.php.

Request the message in the wiki's content language, unless it is disabled for this message.

Since:
1.17
See also:
$wgForceUIMsgAsContentMsg
Returns:
Message $this

Definition at line 552 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.

Since:
1.17
Parameters:
Language | string$langLanguage code or Language object.
Returns:
Message $this
Exceptions:
MWException

Definition at line 526 of file Message.php.

Check whether a message does not exist, or is an empty string.

Since:
1.18
Todo:
FIXME: Merge with isDisabled()?
Returns:
bool

Definition at line 782 of file Message.php.

References $message, and fetchMessage().

Check whether a message does not exist, is an empty string, or is "-".

Since:
1.18
Returns:
bool

Definition at line 794 of file Message.php.

static Message::newFallbackSequence ( ) [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.

Since:
1.18
Parameters:
string|string[][$keys,...] Message keys, or first argument as an array of all the message keys.
Returns:
Message

Definition at line 307 of file Message.php.

static Message::newFromKey ( key) [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.

Since:
1.17
Parameters:
string|string[]$key Message key or array of keys.
mixed[$param,...] Parameters as strings.
Returns:
Message

Definition at line 289 of file Message.php.

static Message::numParam ( num) [static]
Since:
1.18
Parameters:
mixed$num
Returns:
array Array with a single "num" key.

Definition at line 817 of file Message.php.

Add parameters that are numeric and will be passed through Language::formatNum before substitution.

Since:
1.18
Parameters:
mixed[$param,...] Numeric parameters, or a single argument that is an array of numeric parameters.
Returns:
Message $this

Definition at line 376 of file Message.php.

Adds parameters to the parameter list of this message.

Since:
1.17
Parameters:
mixed[$params,...] Parameters as strings, or a single argument that is an array of strings.
Returns:
Message $this

Definition at line 331 of file Message.php.

Fully parse the text from wikitext to HTML.

Since:
1.17
Returns:
string Parsed HTML.

Definition at line 709 of file Message.php.

Returns the parsed message text which is always surrounded by a block element.

Since:
1.17
Returns:
string HTML

Definition at line 745 of file Message.php.

Message::parseText ( string) [protected]

Wrapper for what ever method we use to parse wikitext.

Since:
1.17
Parameters:
string$stringWikitext message contents.
Returns:
string Wikitext parsed into HTML.

Definition at line 953 of file Message.php.

Returns the message text as-is, only parameters are substituted.

Since:
1.17
Returns:
string Unescaped untransformed message text.

Definition at line 733 of file Message.php.

static Message::rawParam ( raw) [static]
Since:
1.17
Parameters:
mixed$raw
Returns:
array Array with a single "raw" key.

Definition at line 806 of file Message.php.

Referenced by NewUsersLogFormatter\getMessageParameters(), PatrolLogFormatter\getMessageParameters(), and MoveLogFormatter\getMessageParameters().

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.

Since:
1.17
Parameters:
mixed[$params,...] Raw parameters as strings, or a single argument that is an array of raw parameters.
Returns:
Message $this

Definition at line 354 of file Message.php.

Message::replaceParameters ( message,
type = 'before' 
) [protected]

Substitutes any parameters into the message text.

Since:
1.17
Parameters:
string$messageThe message text.
string$typeEither "before" or "after".
Returns:
String

Definition at line 886 of file Message.php.

Set the language and the title from a context object.

Since:
1.19
Parameters:
$contextIContextSource
Returns:
Message $this

Definition at line 506 of file Message.php.

Allows manipulating the interface message flag directly.

Can be used to restore the flag after setting a language.

Since:
1.20
Parameters:
bool$interface
Returns:
Message $this

Definition at line 574 of file Message.php.

static Message::sizeParam ( size) [static]
Since:
1.22
Parameters:
int$size
Returns:
int[] Array with a single "size" key.

Definition at line 861 of file Message.php.

Add parameters that are file sizes and will be passed through Language::formatSize before substitution.

Since:
1.22
Parameters:
int|int[][$param,...] Size parameters, or a single argument that is an array of size parameters.
Returns:
Message $this

Definition at line 464 of file Message.php.

Returns the message text.

{{-transformation is done.

Since:
1.17
Returns:
string Unescaped message text.

Definition at line 721 of file Message.php.

static Message::timeperiodParam ( period) [static]
Since:
1.22
Parameters:
number$period
Returns:
number[] Array with a single "period" key.

Definition at line 850 of file Message.php.

Add parameters that are time periods and will be passed through Language::formatTimePeriod before substitution.

Since:
1.22
Parameters:
number|number[][$param,...] Time period parameters, or a single argument that is an array of time period parameters.
Returns:
Message $this

Definition at line 442 of file Message.php.

Message::title ( title)

Set the Title object to use as context when transforming the message.

Since:
1.18
Parameters:
$titleTitle object
Returns:
Message $this

Definition at line 602 of file Message.php.

Returns the message parsed from wikitext to HTML.

Since:
1.17
Returns:
string HTML

Definition at line 627 of file Message.php.

Referenced by escaped().

Message::transformText ( string) [protected]

Wrapper for what ever method we use to {{-transform wikitext.

Since:
1.17
Parameters:
string$stringWikitext message contents.
Returns:
string Wikitext with {{-constructs replaced with their values.

Definition at line 967 of file Message.php.

Message::useDatabase ( useDatabase)

Enable or disable database use.

Since:
1.17
Parameters:
bool$useDatabase
Returns:
Message $this

Definition at line 588 of file Message.php.

Referenced by fetchMessage().


Member Data Documentation

Content Message::$content = null [protected]

Content object representing the message.

Definition at line 205 of file Message.php.

string Message::$format = 'parse' [protected]

Format for the message.

Supported formats are: * text (transform) * escaped (transform+htmlspecialchars) * block-parse * parse (default) * plain

Definition at line 193 of file Message.php.

bool Message::$interface = true [protected]

In which language to get this message.

True, which is the default, means the current interface language, false content language.

Definition at line 166 of file Message.php.

string string [] Message::$key [protected]

The message key or array of keys.

Definition at line 177 of file Message.php.

Referenced by __construct(), and fetchMessage().

Language Message::$language = null [protected]

In which language to get this message.

Overrides the $interface variable.

Definition at line 173 of file Message.php.

Referenced by __construct().

string Message::$message [protected]

Definition at line 209 of file Message.php.

Referenced by fetchMessage(), and isBlank().

array Message::$parameters = array() [protected]

List of parameters which will be substituted into the message.

Definition at line 181 of file Message.php.

Title Message::$title = null [protected]

Title object to use as context.

Definition at line 201 of file Message.php.

bool Message::$useDatabase = true [protected]

Whether database can be used.

Definition at line 197 of file Message.php.


The documentation for this class was generated from the following file: