MediaWiki  REL1_19
Message Class Reference

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

List of all members.

Public Member Functions

 __construct ($key, $params=array())
 Constructor.
 __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>";.
 escaped ()
 Returns the message text.
 exists ()
 Check whether a message key has been defined currently.
 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 subsituted.
 rawParams ()
 Add parameters that are substituted after parsing or escaping.
 setContext (IContextSource $context)
 Set the language and the title from a context object.
 text ()
 Returns the message text.
 title ($title)
 Set the Title object to use as context when transforming the message.
 toString ()
 Returns the message parsed from wikitext to HTML.
 useDatabase ($value)
 Enable or disable database use.

Static Public Member Functions

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 ($value)
static rawParam ($value)

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.
 getMessageText ()
 Returns the textual value for the message.
 parseText ($string)
 Wrapper for what ever method we use to parse wikitext.
 replaceParameters ($message, $type= 'before')
 Substitutes any paramaters into the message text.
 transformText ($string)
 Wrapper for what ever method we use to {{-transform wikitext.

Protected Attributes

 $format = 'parse'
 Format for the message.
 $interface = true
 In which language to get this message.
 $key
 The message key.
 $language = null
 $message
 $parameters = array()
 List of parameters which will be substituted into the message.
 $title = null
 Title object to use as context.
 $useDatabase = true
 Whether database can be used.

Detailed Description

The Message class provides methods which fullfil two basic services:

  • fetching interface messages
  • processing messages into a variety of formats

First implemented with MediaWiki 1.17, the Message class is intented to replace the old wfMsg* functions that over time grew unusable.

See also:
https://www.mediawiki.org/wiki/New_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 cannot parse the text except 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
Author:
Niklas Laxström

Definition at line 139 of file Message.php.


Constructor & Destructor Documentation

Message::__construct ( key,
params = array() 
)

Constructor.

Parameters:
$key,:message key, or array of message keys to try and use the first non-empty message for
$paramsArray message parameters
Returns:
Message: $this

Definition at line 196 of file Message.php.

References $key, and $wgLang.


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>";.

Returns:
String

Definition at line 403 of file Message.php.

References toString().

Here is the call graph for this function:

Returns the message text.

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

Returns:
String: Escaped message text.

Definition at line 448 of file Message.php.

References toString().

Here is the call graph for this function:

Check whether a message key has been defined currently.

Returns:
Bool: true if it is and false if not.

Definition at line 457 of file Message.php.

References fetchMessage().

Here is the call graph for this function:

Message::extractParam ( param) [protected]

Extracts the parameter type and preprocessed the value if needed.

Parameters:
$paramString|Array: Parameter as defined in this class.
Returns:
Tuple(type, value)

Definition at line 519 of file Message.php.

Referenced by replaceParameters().

Here is the caller graph for this function:

Message::fetchMessage ( ) [protected]

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

Returns:
string

Definition at line 573 of file Message.php.

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

Referenced by exists(), getMessageText(), isBlank(), and isDisabled().

Here is the call graph for this function:

Here is the caller graph for this function:

Message::getMessageText ( ) [protected]

Returns the textual value for the message.

Returns:
Message contents or placeholder

Definition at line 559 of file Message.php.

References $message, and fetchMessage().

Referenced by toString().

Here is the call graph for this function:

Here is the caller graph for this function:

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

See also:
$wgForceUIMsgAsContentMsg
Returns:
Message: $this

Definition at line 332 of file Message.php.

References $wgContLang, $wgForceUIMsgAsContentMsg, and false.

Message::inLanguage ( lang)

Request the message in any language that is supported.

As a side effect interface message status is unconditionally turned off.

Parameters:
$langMixed: language code or Language object.
Returns:
Message: $this

Definition at line 309 of file Message.php.

References Language\factory(), and false.

Referenced by setContext().

Here is the call graph for this function:

Here is the caller graph for this function:

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

Returns:
Bool: true if is is and false if not
Todo:
FIXME: Merge with isDisabled()?

Definition at line 466 of file Message.php.

References $message, and fetchMessage().

Here is the call graph for this function:

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

Returns:
Bool: true if is is and false if not

Definition at line 475 of file Message.php.

References $message, and fetchMessage().

Here is the call graph for this function:

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.

Parameters:
Varargs,:message keys (or first arg as an array of all the message keys)
Returns:
Message: $this

Definition at line 224 of file Message.php.

References $keys.

static Message::newFromKey ( key) [static]

Factory function that is just wrapper for the real constructor.

It is intented to be used instead of the real constructor, because it allows chaining method calls, while new objects don't.

Parameters:
$keyString: message key
Varargs,:parameters as Strings
Returns:
Message: $this

Definition at line 211 of file Message.php.

References $key.

static Message::numParam ( value) [static]
Parameters:
$value
Returns:
array

Definition at line 492 of file Message.php.

Referenced by numParams().

Here is the caller graph for this function:

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

Parameters:
Varargs,:numeric parameters (or single argument that is array of numeric parameters)
Returns:
Message: $this

Definition at line 278 of file Message.php.

References numParam().

Here is the call graph for this function:

Adds parameters to the parameter list of this message.

Parameters:
Varargs,:parameters as Strings, or a single argument that is an array of Strings
Returns:
Message: $this

Definition at line 243 of file Message.php.

Fully parse the text from wikitext to HTML.

Returns:
String parsed HTML

Definition at line 411 of file Message.php.

References toString().

Here is the call graph for this function:

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

Returns:
String: HTML

Definition at line 438 of file Message.php.

References toString().

Here is the call graph for this function:

Message::parseText ( string) [protected]

Wrapper for what ever method we use to parse wikitext.

Parameters:
$stringString: Wikitext message contents
Returns:
string Wikitext parsed into HTML

Definition at line 542 of file Message.php.

References MessageCache\singleton(), and title().

Referenced by toString().

Here is the call graph for this function:

Here is the caller graph for this function:

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

Returns:
String: Unescaped untransformed message text.

Definition at line 429 of file Message.php.

References toString().

Here is the call graph for this function:

static Message::rawParam ( value) [static]
Parameters:
$value
Returns:
array

Definition at line 484 of file Message.php.

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

Here is the caller graph for this function:

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.

Parameters:
Varargs,:raw parameters as Strings (or single argument that is an array of raw parameters)
Returns:
Message: $this

Definition at line 261 of file Message.php.

References rawParam().

Here is the call graph for this function:

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

Substitutes any paramaters into the message text.

Parameters:
$messageString: the message text
$typeString: either before or after
Returns:
String

Definition at line 502 of file Message.php.

References $message, $n, and extractParam().

Referenced by toString().

Here is the call graph for this function:

Here is the caller graph for this function:

Set the language and the title from a context object.

Parameters:
$contextIContextSource
Returns:
Message: $this

Definition at line 295 of file Message.php.

References IContextSource\getLanguage(), IContextSource\getTitle(), inLanguage(), and title().

Here is the call graph for this function:

Returns the message text.

{{-transformation is done.

Returns:
String: Unescaped message text.

Definition at line 420 of file Message.php.

References toString().

Here is the call graph for this function:

Message::title ( title)

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

Parameters:
$titleTitle object
Returns:
Message: $this

Definition at line 360 of file Message.php.

References $title.

Referenced by parseText(), setContext(), and transformText().

Here is the caller graph for this function:

Returns the message parsed from wikitext to HTML.

Returns:
String: HTML

Definition at line 369 of file Message.php.

References getMessageText(), parseText(), replaceParameters(), and transformText().

Referenced by __toString(), escaped(), parse(), parseAsBlock(), plain(), and text().

Here is the call graph for this function:

Here is the caller graph for this function:

Message::transformText ( string) [protected]

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

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

Definition at line 551 of file Message.php.

References MessageCache\singleton(), and title().

Referenced by toString().

Here is the call graph for this function:

Here is the caller graph for this function:

Message::useDatabase ( value)

Enable or disable database use.

Parameters:
$valueBoolean
Returns:
Message: $this

Definition at line 349 of file Message.php.

Referenced by fetchMessage().

Here is the caller graph for this function:


Member Data Documentation

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

Format for the message.

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

Definition at line 173 of file Message.php.

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 144 of file Message.php.

Message::$key [protected]

The message key.

Definition at line 157 of file Message.php.

Referenced by __construct(), fetchMessage(), and newFromKey().

Message::$language = null [protected]

Definition at line 152 of file Message.php.

Message::$message [protected]

Definition at line 188 of file Message.php.

Referenced by fetchMessage(), getMessageText(), isBlank(), isDisabled(), and replaceParameters().

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

List of parameters which will be substituted into the message.

Definition at line 162 of file Message.php.

Message::$title = null [protected]

Title object to use as context.

Definition at line 183 of file Message.php.

Referenced by title().

Message::$useDatabase = true [protected]

Whether database can be used.

Definition at line 178 of file Message.php.


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