2010-03-22 17:05:32 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2010-03-22 17:56:04 +00:00
|
|
|
* This class provides methods for fetching interface messages and
|
|
|
|
|
* processing them into variety of formats that are needed in MediaWiki.
|
|
|
|
|
*
|
|
|
|
|
* It is intented to replace the old wfMsg* functions that over time grew
|
|
|
|
|
* unusable.
|
|
|
|
|
*
|
|
|
|
|
* Examples:
|
2010-03-23 15:41:18 +00:00
|
|
|
* Fetching a message text for interface message
|
2010-09-02 17:12:56 +00:00
|
|
|
* $button = Xml::button( wfMessage( 'submit' )->text() );
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-23 15:41:18 +00:00
|
|
|
* Messages can have parameters:
|
2010-09-02 17:12:56 +00:00
|
|
|
* wfMessage( 'welcome-to' )->params( $wgSitename )->text();
|
2010-03-25 20:34:56 +00:00
|
|
|
* {{GRAMMAR}} and friends work correctly
|
2010-09-02 17:12:56 +00:00
|
|
|
* wfMessage( 'are-friends', $user, $friend );
|
|
|
|
|
* wfMessage( 'bad-message' )->rawParams( '<script>...</script>' )->escaped();
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-23 15:41:18 +00:00
|
|
|
* Sometimes the message text ends up in the database, so content language is needed.
|
2010-09-02 17:12:56 +00:00
|
|
|
* wfMessage( 'file-log', $user, $filename )->inContentLanguage()->text()
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-23 15:41:18 +00:00
|
|
|
* Checking if message exists:
|
2010-09-02 17:12:56 +00:00
|
|
|
* wfMessage( 'mysterious-message' )->exists()
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-23 15:41:18 +00:00
|
|
|
* If you want to use a different language:
|
2010-09-02 17:12:56 +00:00
|
|
|
* wfMessage( 'email-header' )->inLanguage( $user->getOption( 'language' ) )->plain()
|
2010-03-25 20:34:56 +00:00
|
|
|
* Note that you cannot parse the text except in the content or interface
|
|
|
|
|
* languages
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-23 15:41:18 +00:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Comparison with old wfMsg* functions:
|
2010-03-22 17:56:04 +00:00
|
|
|
*
|
|
|
|
|
* Use full parsing.
|
2010-03-25 20:34:56 +00:00
|
|
|
* wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
|
2010-09-02 17:12:56 +00:00
|
|
|
* === wfMessage( 'key', 'apple' )->parse();
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-22 17:56:04 +00:00
|
|
|
* 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.
|
2010-03-25 20:34:56 +00:00
|
|
|
* wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
|
2010-09-02 17:12:56 +00:00
|
|
|
* === wfMessage( 'key', 'apple', 'pear' )->text();
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-22 17:56:04 +00:00
|
|
|
*
|
|
|
|
|
* Shortcut for escaping the message too, similar to wfMsgHTML, but
|
|
|
|
|
* parameters are not replaced after escaping by default.
|
2010-09-02 17:12:56 +00:00
|
|
|
* $escaped = wfMessage( 'key' )->rawParams( 'apple' )->escaped();
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-22 17:56:04 +00:00
|
|
|
*
|
2011-05-17 22:03:20 +00:00
|
|
|
* @todo
|
2010-03-25 21:28:27 +00:00
|
|
|
* - test, can we have tests?
|
2010-03-22 17:56:04 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.17
|
2010-09-02 17:12:56 +00:00
|
|
|
* @author Niklas Laxström
|
2010-03-22 17:56:04 +00:00
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
class Message {
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* In which language to get this message. True, which is the default,
|
|
|
|
|
* means the current interface language, false content language.
|
|
|
|
|
*/
|
|
|
|
|
protected $interface = true;
|
2010-03-25 20:34:56 +00:00
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* In which language to get this message. Overrides the $interface
|
|
|
|
|
* variable.
|
2011-04-18 12:43:53 +00:00
|
|
|
*
|
|
|
|
|
* @var Language
|
2010-03-23 15:41:18 +00:00
|
|
|
*/
|
|
|
|
|
protected $language = null;
|
2010-03-25 20:34:56 +00:00
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* The message key.
|
|
|
|
|
*/
|
|
|
|
|
protected $key;
|
2010-03-22 17:05:32 +00:00
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* List of parameters which will be substituted into the message.
|
|
|
|
|
*/
|
|
|
|
|
protected $parameters = array();
|
2010-03-28 15:22:49 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format for the message.
|
|
|
|
|
* Supported formats are:
|
|
|
|
|
* * text (transform)
|
|
|
|
|
* * escaped (transform+htmlspecialchars)
|
|
|
|
|
* * block-parse
|
|
|
|
|
* * parse (default)
|
|
|
|
|
* * plain
|
|
|
|
|
*/
|
|
|
|
|
protected $format = 'parse';
|
|
|
|
|
|
2010-03-25 20:34:56 +00:00
|
|
|
/**
|
2010-03-28 15:22:49 +00:00
|
|
|
* Whether database can be used.
|
2010-03-25 20:34:56 +00:00
|
|
|
*/
|
2010-03-28 15:22:49 +00:00
|
|
|
protected $useDatabase = true;
|
2010-03-22 17:05:32 +00:00
|
|
|
|
2011-02-09 15:19:45 +00:00
|
|
|
/**
|
|
|
|
|
* Title object to use as context
|
|
|
|
|
*/
|
|
|
|
|
protected $title = null;
|
|
|
|
|
|
2010-03-22 17:05:32 +00:00
|
|
|
/**
|
|
|
|
|
* Constructor.
|
2011-01-14 08:32:10 +00:00
|
|
|
* @param $key: message key, or array of message keys to try and use the first non-empty message for
|
2010-03-25 20:52:52 +00:00
|
|
|
* @param $params Array message parameters
|
2010-03-23 15:41:18 +00:00
|
|
|
* @return Message: $this
|
2010-03-22 17:05:32 +00:00
|
|
|
*/
|
2010-03-28 15:22:49 +00:00
|
|
|
public function __construct( $key, $params = array() ) {
|
2010-09-20 13:24:31 +00:00
|
|
|
global $wgLang;
|
2010-03-22 17:05:32 +00:00
|
|
|
$this->key = $key;
|
2010-03-28 19:23:39 +00:00
|
|
|
$this->parameters = array_values( $params );
|
2010-09-20 13:24:31 +00:00
|
|
|
$this->language = $wgLang;
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
* @param $key String: message key
|
2010-03-29 14:08:23 +00:00
|
|
|
* @param Varargs: parameters as Strings
|
2010-03-23 15:41:18 +00:00
|
|
|
* @return Message: $this
|
2010-03-22 17:05:32 +00:00
|
|
|
*/
|
2010-09-02 17:12:56 +00:00
|
|
|
public static function newFromKey( $key /*...*/ ) {
|
2010-03-31 10:10:49 +00:00
|
|
|
$params = func_get_args();
|
|
|
|
|
array_shift( $params );
|
|
|
|
|
return new self( $key, $params );
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-14 08:32:10 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
* @param Varargs: message keys
|
|
|
|
|
* @return Message: $this
|
|
|
|
|
*/
|
|
|
|
|
public static function newFallbackSequence( /*...*/ ) {
|
|
|
|
|
$keys = func_get_args();
|
|
|
|
|
if ( func_num_args() == 1 ) {
|
|
|
|
|
if ( is_array($keys[0]) ) {
|
|
|
|
|
// Allow an array to be passed as the first argument instead
|
|
|
|
|
$keys = array_values($keys[0]);
|
|
|
|
|
} else {
|
|
|
|
|
// Optimize a single string to not need special fallback handling
|
|
|
|
|
$keys = $keys[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new self( $keys );
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-22 17:05:32 +00:00
|
|
|
/**
|
2010-03-23 15:41:18 +00:00
|
|
|
* Adds parameters to the parameter list of this message.
|
2010-03-28 19:23:39 +00:00
|
|
|
* @param Varargs: parameters as Strings
|
2010-03-23 15:41:18 +00:00
|
|
|
* @return Message: $this
|
2010-03-22 17:05:32 +00:00
|
|
|
*/
|
|
|
|
|
public function params( /*...*/ ) {
|
2011-02-04 16:47:24 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
|
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
|
|
|
|
|
$args = $args[0];
|
|
|
|
|
}
|
|
|
|
|
$args_values = array_values( $args );
|
2010-09-23 19:15:58 +00:00
|
|
|
$this->parameters = array_merge( $this->parameters, $args_values );
|
2010-03-22 17:05:32 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-03-25 20:34:56 +00:00
|
|
|
* 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.
|
2010-03-25 21:28:27 +00:00
|
|
|
* @param Varargs: raw parameters as Strings
|
2010-03-23 15:41:18 +00:00
|
|
|
* @return Message: $this
|
2010-03-22 17:05:32 +00:00
|
|
|
*/
|
2010-03-25 20:34:56 +00:00
|
|
|
public function rawParams( /*...*/ ) {
|
|
|
|
|
$params = func_get_args();
|
2011-02-04 16:47:24 +00:00
|
|
|
if ( isset( $params[0] ) && is_array( $params[0] ) ) {
|
|
|
|
|
$params = $params[0];
|
|
|
|
|
}
|
2010-03-28 19:23:39 +00:00
|
|
|
foreach( $params as $param ) {
|
2010-03-29 14:08:23 +00:00
|
|
|
$this->parameters[] = self::rawParam( $param );
|
2010-03-25 20:34:56 +00:00
|
|
|
}
|
2010-03-22 17:05:32 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
2010-03-25 20:34:56 +00:00
|
|
|
|
2011-01-22 20:23:51 +00:00
|
|
|
/**
|
|
|
|
|
* Add parameters that are numeric and will be passed through
|
|
|
|
|
* Language::formatNum before substitution
|
|
|
|
|
* @param Varargs: numeric parameters
|
|
|
|
|
* @return Message: $this
|
|
|
|
|
*/
|
|
|
|
|
public function numParams( /*...*/ ) {
|
|
|
|
|
$params = func_get_args();
|
2011-02-04 16:47:24 +00:00
|
|
|
if ( isset( $params[0] ) && is_array( $params[0] ) ) {
|
|
|
|
|
$params = $params[0];
|
|
|
|
|
}
|
2011-01-22 20:23:51 +00:00
|
|
|
foreach( $params as $param ) {
|
|
|
|
|
$this->parameters[] = self::numParam( $param );
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Request the message in any language that is supported.
|
|
|
|
|
* As a side effect interface message status is unconditionally
|
|
|
|
|
* turned off.
|
2010-03-28 19:23:39 +00:00
|
|
|
* @param $lang Mixed: language code or Language object.
|
2010-03-23 15:41:18 +00:00
|
|
|
* @return Message: $this
|
|
|
|
|
*/
|
2010-03-29 14:08:23 +00:00
|
|
|
public function inLanguage( $lang ) {
|
2011-04-24 10:50:51 +00:00
|
|
|
if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
|
2010-03-25 20:34:56 +00:00
|
|
|
$this->language = $lang;
|
|
|
|
|
} elseif ( is_string( $lang ) ) {
|
2010-09-22 19:19:47 +00:00
|
|
|
if( $this->language->getCode() != $lang ) {
|
|
|
|
|
$this->language = Language::factory( $lang );
|
|
|
|
|
}
|
2010-03-23 15:41:18 +00:00
|
|
|
} else {
|
2010-03-25 20:34:56 +00:00
|
|
|
$type = gettype( $lang );
|
2010-03-25 20:52:52 +00:00
|
|
|
throw new MWException( __METHOD__ . " must be "
|
2010-03-25 20:34:56 +00:00
|
|
|
. "passed a String or Language object; $type given"
|
|
|
|
|
);
|
2010-03-23 15:41:18 +00:00
|
|
|
}
|
|
|
|
|
$this->interface = false;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Request the message in the wiki's content language.
|
|
|
|
|
* @return Message: $this
|
|
|
|
|
*/
|
|
|
|
|
public function inContentLanguage() {
|
2010-09-20 13:24:31 +00:00
|
|
|
global $wgContLang;
|
2010-03-23 15:41:18 +00:00
|
|
|
$this->interface = false;
|
2010-09-20 13:24:31 +00:00
|
|
|
$this->language = $wgContLang;
|
2010-03-23 15:41:18 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-28 15:22:49 +00:00
|
|
|
/**
|
|
|
|
|
* Enable or disable database use.
|
|
|
|
|
* @param $value Boolean
|
|
|
|
|
* @return Message: $this
|
|
|
|
|
*/
|
|
|
|
|
public function useDatabase( $value ) {
|
|
|
|
|
$this->useDatabase = (bool) $value;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-09 15:19:45 +00:00
|
|
|
/**
|
|
|
|
|
* Set the Title object to use as context when transforming the message
|
|
|
|
|
*
|
|
|
|
|
* @param $title Title object
|
|
|
|
|
* @return Message: $this
|
|
|
|
|
*/
|
|
|
|
|
public function title( $title ) {
|
|
|
|
|
$this->title = $title;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the message parsed from wikitext to HTML.
|
|
|
|
|
* @return String: HTML
|
|
|
|
|
*/
|
2010-03-25 20:34:56 +00:00
|
|
|
public function toString() {
|
|
|
|
|
$string = $this->getMessageText();
|
|
|
|
|
|
|
|
|
|
# Replace parameters before text parsing
|
|
|
|
|
$string = $this->replaceParameters( $string, 'before' );
|
|
|
|
|
|
|
|
|
|
# Maybe transform using the full parser
|
2010-03-28 15:22:49 +00:00
|
|
|
if( $this->format === 'parse' ) {
|
2010-03-25 20:34:56 +00:00
|
|
|
$string = $this->parseText( $string );
|
|
|
|
|
$m = array();
|
|
|
|
|
if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
|
|
|
|
|
$string = $m[1];
|
|
|
|
|
}
|
2010-03-28 15:22:49 +00:00
|
|
|
} elseif( $this->format === 'block-parse' ){
|
|
|
|
|
$string = $this->parseText( $string );
|
|
|
|
|
} elseif( $this->format === 'text' ){
|
|
|
|
|
$string = $this->transformText( $string );
|
|
|
|
|
} elseif( $this->format === 'escaped' ){
|
|
|
|
|
$string = $this->transformText( $string );
|
2011-04-23 16:27:29 +00:00
|
|
|
$string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false );
|
2010-03-25 20:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Raw parameter replacement
|
|
|
|
|
$string = $this->replaceParameters( $string, 'after' );
|
|
|
|
|
|
2010-03-22 17:05:32 +00:00
|
|
|
return $string;
|
|
|
|
|
}
|
2011-03-13 15:12:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg:
|
|
|
|
|
* $foo = Message::get($key);
|
|
|
|
|
* $string = "<abbr>$foo</abbr>";
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public function __toString() {
|
|
|
|
|
return $this->toString();
|
|
|
|
|
}
|
2010-03-25 20:34:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fully parse the text from wikitext to HTML
|
|
|
|
|
* @return String parsed HTML
|
|
|
|
|
*/
|
2010-03-28 19:23:39 +00:00
|
|
|
public function parse() {
|
2010-03-28 15:22:49 +00:00
|
|
|
$this->format = 'parse';
|
2010-03-28 19:23:39 +00:00
|
|
|
return $this->toString();
|
2010-03-25 20:34:56 +00:00
|
|
|
}
|
2010-03-22 17:05:32 +00:00
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the message text. {{-transformation is done.
|
|
|
|
|
* @return String: Unescaped message text.
|
|
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
public function text() {
|
2010-03-28 15:22:49 +00:00
|
|
|
$this->format = 'text';
|
2010-03-28 19:23:39 +00:00
|
|
|
return $this->toString();
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the message text as-is, only parameters are subsituted.
|
|
|
|
|
* @return String: Unescaped untransformed message text.
|
|
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
public function plain() {
|
2010-03-28 15:22:49 +00:00
|
|
|
$this->format = 'plain';
|
2010-03-28 19:23:39 +00:00
|
|
|
return $this->toString();
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the parsed message text which is always surrounded by a block element.
|
|
|
|
|
* @return String: HTML
|
|
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
public function parseAsBlock() {
|
2010-03-28 15:22:49 +00:00
|
|
|
$this->format = 'block-parse';
|
2010-03-28 19:23:39 +00:00
|
|
|
return $this->toString();
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the message text. {{-transformation is done and the result
|
2010-09-20 13:24:31 +00:00
|
|
|
* is escaped excluding any raw parameters.
|
2010-03-23 15:41:18 +00:00
|
|
|
* @return String: Escaped message text.
|
|
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
public function escaped() {
|
2010-03-28 15:22:49 +00:00
|
|
|
$this->format = 'escaped';
|
2010-03-28 19:23:39 +00:00
|
|
|
return $this->toString();
|
2010-03-23 15:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether a message key has been defined currently.
|
|
|
|
|
* @return Bool: true if it is and false if not.
|
|
|
|
|
*/
|
|
|
|
|
public function exists() {
|
2010-03-29 14:30:42 +00:00
|
|
|
return $this->fetchMessage() !== false;
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-14 10:51:05 +00:00
|
|
|
/**
|
|
|
|
|
* Check whether a message does not exist, or is an empty string
|
|
|
|
|
* @return Bool: true if is is and false if not
|
2011-05-17 22:03:20 +00:00
|
|
|
* @todo FIXME: Merge with isDisabled()?
|
2011-01-14 10:51:05 +00:00
|
|
|
*/
|
|
|
|
|
public function isBlank() {
|
|
|
|
|
$message = $this->fetchMessage();
|
2011-01-14 22:46:08 +00:00
|
|
|
return $message === false || $message === '';
|
2011-01-14 10:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether a message does not exist, is an empty string, or is "-"
|
|
|
|
|
* @return Bool: true if is is and false if not
|
|
|
|
|
*/
|
|
|
|
|
public function isDisabled() {
|
|
|
|
|
$message = $this->fetchMessage();
|
2011-01-14 22:46:08 +00:00
|
|
|
return $message === false || $message === '' || $message === '-';
|
2011-01-14 10:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
2011-04-18 12:43:53 +00:00
|
|
|
/**
|
|
|
|
|
* @param $value
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2010-03-29 14:08:23 +00:00
|
|
|
public static function rawParam( $value ) {
|
|
|
|
|
return array( 'raw' => $value );
|
|
|
|
|
}
|
2011-04-18 12:43:53 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $value
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2011-01-22 20:23:51 +00:00
|
|
|
public static function numParam( $value ) {
|
|
|
|
|
return array( 'num' => $value );
|
|
|
|
|
}
|
2010-03-29 14:08:23 +00:00
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Substitutes any paramaters into the message text.
|
2011-02-10 09:10:47 +00:00
|
|
|
* @param $message String: the message text
|
2010-03-23 15:41:18 +00:00
|
|
|
* @param $type String: either before or after
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
2010-03-25 20:34:56 +00:00
|
|
|
protected function replaceParameters( $message, $type = 'before' ) {
|
2010-03-22 17:05:32 +00:00
|
|
|
$replacementKeys = array();
|
2010-03-25 20:34:56 +00:00
|
|
|
foreach( $this->parameters as $n => $param ) {
|
2011-02-10 09:10:47 +00:00
|
|
|
list( $paramType, $value ) = $this->extractParam( $param );
|
|
|
|
|
if ( $type === $paramType ) {
|
|
|
|
|
$replacementKeys['$' . ($n + 1)] = $value;
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$message = strtr( $message, $replacementKeys );
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-10 09:10:47 +00:00
|
|
|
/**
|
|
|
|
|
* Extracts the parameter type and preprocessed the value if needed.
|
|
|
|
|
* @param $param String|Array: Parameter as defined in this class.
|
|
|
|
|
* @return Tuple(type, value)
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
protected function extractParam( $param ) {
|
|
|
|
|
if ( is_array( $param ) && isset( $param['raw'] ) ) {
|
|
|
|
|
return array( 'after', $param['raw'] );
|
|
|
|
|
} elseif ( is_array( $param ) && isset( $param['num'] ) ) {
|
|
|
|
|
// Replace number params always in before step for now.
|
|
|
|
|
// No support for combined raw and num params
|
|
|
|
|
return array( 'before', $this->language->formatNum( $param['num'] ) );
|
|
|
|
|
} elseif ( !is_array( $param ) ) {
|
|
|
|
|
return array( 'before', $param );
|
|
|
|
|
} else {
|
|
|
|
|
throw new MWException( "Invalid message parameter" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Wrapper for what ever method we use to parse wikitext.
|
|
|
|
|
* @param $string String: Wikitext message contents
|
2011-04-18 12:43:53 +00:00
|
|
|
* @return string Wikitext parsed into HTML
|
2010-03-23 15:41:18 +00:00
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
protected function parseText( $string ) {
|
2011-04-22 20:17:21 +00:00
|
|
|
return MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language )->getText();
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Wrapper for what ever method we use to {{-transform wikitext.
|
|
|
|
|
* @param $string String: Wikitext message contents
|
2011-04-18 12:43:53 +00:00
|
|
|
* @return string Wikitext with {{-constructs replaced with their values.
|
2010-03-23 15:41:18 +00:00
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
protected function transformText( $string ) {
|
2011-02-09 15:19:45 +00:00
|
|
|
return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title );
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
2010-03-28 15:22:49 +00:00
|
|
|
* Returns the textual value for the message.
|
|
|
|
|
* @return Message contents or placeholder
|
2010-03-23 15:41:18 +00:00
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
protected function getMessageText() {
|
2010-03-28 15:22:49 +00:00
|
|
|
$message = $this->fetchMessage();
|
|
|
|
|
if ( $message === false ) {
|
2011-01-14 08:32:10 +00:00
|
|
|
return '<' . htmlspecialchars( is_array($this->key) ? $this->key[0] : $this->key ) . '>';
|
2010-03-28 15:22:49 +00:00
|
|
|
} else {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper for what ever method we use to get message contents
|
2011-04-18 12:43:53 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2010-03-28 15:22:49 +00:00
|
|
|
*/
|
|
|
|
|
protected function fetchMessage() {
|
|
|
|
|
if ( !isset( $this->message ) ) {
|
2011-01-26 15:42:04 +00:00
|
|
|
$cache = MessageCache::singleton();
|
2011-01-14 08:32:10 +00:00
|
|
|
if ( is_array($this->key) ) {
|
|
|
|
|
foreach ( $this->key as $key ) {
|
2011-01-26 15:42:04 +00:00
|
|
|
$message = $cache->get( $key, $this->useDatabase, $this->language );
|
2011-01-14 08:32:10 +00:00
|
|
|
if ( $message !== false && $message !== '' ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->message = $message;
|
|
|
|
|
} else {
|
2011-01-26 15:42:04 +00:00
|
|
|
$this->message = $cache->get( $this->key, $this->useDatabase, $this->language );
|
2011-01-14 08:32:10 +00:00
|
|
|
}
|
2010-03-28 15:22:49 +00:00
|
|
|
}
|
|
|
|
|
return $this->message;
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-14 08:32:10 +00:00
|
|
|
}
|