2010-03-22 17:05:32 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2010-03-22 17:56:04 +00:00
|
|
|
* OBS!!! *EXPERIMENTAL* This class is still under discussion.
|
|
|
|
|
*
|
|
|
|
|
* 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-03-25 20:34:56 +00:00
|
|
|
* $button = Xml::button( Message::key( 'submit' )->text() );
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-23 15:41:18 +00:00
|
|
|
* Messages can have parameters:
|
2010-03-25 20:34:56 +00:00
|
|
|
* Message::key( 'welcome-to' )->params( $wgSitename )->text();
|
|
|
|
|
* {{GRAMMAR}} and friends work correctly
|
|
|
|
|
* Message::key( 'are-friends' )->params( $user, $friend );
|
|
|
|
|
* Message::key( '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-03-25 20:34:56 +00:00
|
|
|
* Message::key( 'file-log' )->params( $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-03-25 20:34:56 +00:00
|
|
|
* Message::key( '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-03-25 20:34:56 +00:00
|
|
|
* Message::key( 'email-header' )->language( $user->getOption( 'language' ) )->plain()
|
|
|
|
|
* 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' );
|
|
|
|
|
* === Message::key( 'key' )->params( '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' );
|
|
|
|
|
* === Message::key( 'key' )->params( '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-03-25 20:34:56 +00:00
|
|
|
* $escaped = Message::key( 'key' )->rawParams( 'apple' )->escaped();
|
2010-03-25 21:28:27 +00:00
|
|
|
* </pre>
|
2010-03-22 17:56:04 +00:00
|
|
|
*
|
|
|
|
|
* TODO:
|
2010-03-25 21:28:27 +00:00
|
|
|
* - test, can we have tests?
|
|
|
|
|
* - sort out the details marked with fixme
|
|
|
|
|
* - should we have _m() or similar global wrapper?
|
2010-03-22 17:56:04 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.17
|
|
|
|
|
*/
|
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.
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
* @param $key String: message key
|
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-03-22 17:05:32 +00:00
|
|
|
$this->key = $key;
|
2010-03-28 19:23:39 +00:00
|
|
|
$this->parameters = array_values( $params );
|
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.
|
|
|
|
|
* //FIXME: key or get or something else?
|
|
|
|
|
* @param $key String: message key
|
2010-03-23 15:41:18 +00:00
|
|
|
* @return Message: $this
|
2010-03-22 17:05:32 +00:00
|
|
|
*/
|
2010-03-28 14:53:01 +00:00
|
|
|
public static function key( $key ) {
|
2010-03-28 19:23:39 +00:00
|
|
|
return new self( $key );
|
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( /*...*/ ) {
|
2010-03-28 19:23:39 +00:00
|
|
|
$this->parameters = array_merge( $this->parameters, array_values( func_get_args() ) );
|
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();
|
2010-03-28 19:23:39 +00:00
|
|
|
foreach( $params as $param ) {
|
|
|
|
|
$this->parameters[] = array( 'raw' => $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
|
|
|
|
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-25 20:34:56 +00:00
|
|
|
public function language( $lang ) {
|
|
|
|
|
if( $lang instanceof Language ){
|
|
|
|
|
$this->language = $lang;
|
|
|
|
|
} elseif ( is_string( $lang ) ) {
|
2010-03-23 15:41:18 +00:00
|
|
|
$this->language = Language::factory( $lang );
|
|
|
|
|
} 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() {
|
|
|
|
|
$this->interface = false;
|
|
|
|
|
$this->language = null;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the message parsed from wikitext to HTML.
|
2010-03-25 20:34:56 +00:00
|
|
|
* TODO: in PHP >= 5.2.0, we can make this a magic method,
|
|
|
|
|
* and then we can do, eg:
|
|
|
|
|
* $foo = Message::get($key);
|
|
|
|
|
* $string = "<abbr>$foo</abbr>";
|
|
|
|
|
* But we shouldn't implement that while MediaWiki still supports
|
|
|
|
|
* PHP < 5.2; or people will start using it...
|
2010-03-23 15:41:18 +00:00
|
|
|
* @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' ){
|
|
|
|
|
# FIXME: Sanitizer method here?
|
|
|
|
|
$string = $this->transformText( $string );
|
|
|
|
|
$string = htmlspecialchars( $string );
|
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;
|
|
|
|
|
}
|
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
|
|
|
|
|
* is excaped excluding any raw parameters.
|
|
|
|
|
* @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-28 15:22:49 +00:00
|
|
|
return $this->fetchMessage() === false;
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Substitutes any paramaters into the message text.
|
2010-03-25 20:34:56 +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 ) {
|
|
|
|
|
if ( $type === 'before' && !is_array( $param ) ) {
|
2010-03-22 17:05:32 +00:00
|
|
|
$replacementKeys['$' . ($n + 1)] = $param;
|
|
|
|
|
} elseif ( $type === 'after' && isset( $param['raw'] ) ) {
|
|
|
|
|
$replacementKeys['$' . ($n + 1)] = $param['raw'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$message = strtr( $message, $replacementKeys );
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:41:18 +00:00
|
|
|
/**
|
|
|
|
|
* Wrapper for what ever method we use to parse wikitext.
|
|
|
|
|
* @param $string String: Wikitext message contents
|
|
|
|
|
* @return Wikitext parsed into HTML
|
|
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
protected function parseText( $string ) {
|
|
|
|
|
global $wgOut;
|
2010-03-23 15:41:18 +00:00
|
|
|
if ( $this->language !== null ) {
|
2010-03-25 20:34:56 +00:00
|
|
|
# FIXME: remove this limitation
|
2010-03-23 15:41:18 +00:00
|
|
|
throw new MWException( 'Can only parse in interface or content language' );
|
|
|
|
|
}
|
2010-03-25 20:34:56 +00:00
|
|
|
return $wgOut->parse( $string, /*linestart*/true, $this->interface );
|
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
|
|
|
|
|
* @return Wikitext with {{-constructs replaced with their values.
|
|
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
protected function transformText( $string ) {
|
|
|
|
|
global $wgMessageCache;
|
2010-03-23 15:41:18 +00:00
|
|
|
return $wgMessageCache->transform( $string, $this->interface, $this->language );
|
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 ) {
|
|
|
|
|
return '<' . htmlspecialchars( $this->key ) . '>';
|
|
|
|
|
} else {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper for what ever method we use to get message contents
|
|
|
|
|
*/
|
|
|
|
|
protected function fetchMessage() {
|
|
|
|
|
if ( !isset( $this->message ) ) {
|
|
|
|
|
global $wgMessageCache;
|
|
|
|
|
$this->message = $wgMessageCache->get( $this->key, $this->useDatabase, $this->language );
|
|
|
|
|
}
|
|
|
|
|
return $this->message;
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|