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-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-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-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-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-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-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-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-22 17:56:04 +00:00
|
|
|
*
|
|
|
|
|
* TODO:
|
|
|
|
|
* * test, can we have tests?
|
|
|
|
|
* * sort out the details marked with fixme
|
|
|
|
|
* * should we have _m() or similar global wrapper?
|
|
|
|
|
*
|
|
|
|
|
* @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-25 20:34:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Some situations need exotic combinations of options to the
|
|
|
|
|
* underlying Language modules; which can be specified here.
|
|
|
|
|
* Dependencies:
|
|
|
|
|
* 'parse' implies 'transform', 'escape'
|
|
|
|
|
*/
|
|
|
|
|
protected $options = array(
|
|
|
|
|
# Don't wrap the output in a block-level element
|
|
|
|
|
'inline' => true,
|
|
|
|
|
# Expand {{ constructs
|
|
|
|
|
'transform' => true,
|
|
|
|
|
# Output will be safe HTML
|
|
|
|
|
'escape' => true,
|
|
|
|
|
# Parse the text with the full parser
|
|
|
|
|
'parse' => true,
|
|
|
|
|
# Access the database when getting the message text
|
|
|
|
|
'usedb' => true,
|
|
|
|
|
);
|
2010-03-22 17:05:32 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
* @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-25 20:34:56 +00:00
|
|
|
public function __construct( $key, $params=array(), $options=array() ) {
|
2010-03-22 17:05:32 +00:00
|
|
|
$this->key = $key;
|
2010-03-25 20:34:56 +00:00
|
|
|
if( $params ){
|
|
|
|
|
$this->params( $params );
|
|
|
|
|
}
|
|
|
|
|
if( $options ){
|
|
|
|
|
$this->options( $options );
|
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
|
public function key( $key ) {
|
|
|
|
|
return new Message( $key );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-03-23 15:41:18 +00:00
|
|
|
* Adds parameters to the parameter list of this message.
|
2010-03-25 20:34:56 +00:00
|
|
|
* @params Vargars: 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-25 20:34:56 +00:00
|
|
|
$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.
|
|
|
|
|
* @param $value 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();
|
|
|
|
|
foreach( $params as $param ){
|
|
|
|
|
$this->parameters[] = array( 'raw' => $param );
|
|
|
|
|
}
|
2010-03-22 17:05:32 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
2010-03-25 20:34:56 +00:00
|
|
|
|
2010-03-22 17:05:32 +00:00
|
|
|
/**
|
2010-03-25 20:34:56 +00:00
|
|
|
* Set some of the individual options, if you need to use some
|
|
|
|
|
* funky combination of them.
|
|
|
|
|
* @param $options Array $option => $value
|
|
|
|
|
* @return Message $this
|
2010-03-22 17:05:32 +00:00
|
|
|
*/
|
2010-03-25 20:34:56 +00:00
|
|
|
public function options( array $options ){
|
|
|
|
|
foreach( $options as $key => $value ){
|
|
|
|
|
if( in_array( $key, $this->options ) ){
|
|
|
|
|
$this->options[$key] = (bool)$value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-22 17:05:32 +00:00
|
|
|
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.
|
|
|
|
|
* @param $lang Mixed: langauge code or language object.
|
|
|
|
|
* @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 );
|
|
|
|
|
throw new MWException( "Message::langauge() must be "
|
|
|
|
|
. "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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
if( $this->options['parse'] ){
|
|
|
|
|
$string = $this->parseText( $string );
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
# Transform {{ constructs
|
|
|
|
|
if( $this->options['transform'] ){
|
|
|
|
|
$string = $this->transformText( $string );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Sanitise
|
|
|
|
|
if( $this->options['escape'] ){
|
|
|
|
|
# FIXME: Sanitizer method here?
|
|
|
|
|
$string = htmlspecialchars( $string );
|
|
|
|
|
}
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
2010-03-25 20:34:56 +00:00
|
|
|
|
|
|
|
|
# Strip the block element
|
|
|
|
|
if( !$this->options['inline'] ){
|
|
|
|
|
$m = array();
|
|
|
|
|
if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
|
|
|
|
|
$string = $m[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
public function __tostring(){ return $this->toString(); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fully parse the text from wikitext to HTML
|
|
|
|
|
* @return String parsed HTML
|
|
|
|
|
*/
|
|
|
|
|
public function parse(){
|
|
|
|
|
$this->options( array(
|
|
|
|
|
'parse' => true,
|
|
|
|
|
));
|
|
|
|
|
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.
|
|
|
|
|
* @return String: Unescaped message text.
|
|
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
public function text() {
|
2010-03-25 20:34:56 +00:00
|
|
|
$this->options( array(
|
|
|
|
|
'parse' => false,
|
|
|
|
|
'transform' => true,
|
|
|
|
|
'escape' => false,
|
|
|
|
|
));
|
|
|
|
|
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-25 20:34:56 +00:00
|
|
|
$this->options( array(
|
|
|
|
|
'parse' => false,
|
|
|
|
|
'transform' => false,
|
|
|
|
|
'escape' => false,
|
|
|
|
|
'inline' => false,
|
|
|
|
|
));
|
|
|
|
|
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-25 20:34:56 +00:00
|
|
|
$this->options( array(
|
|
|
|
|
'parse' => true,
|
|
|
|
|
'inline' => true,
|
|
|
|
|
));
|
|
|
|
|
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-25 20:34:56 +00:00
|
|
|
$this->options( array(
|
|
|
|
|
'parse' => false,
|
|
|
|
|
'transform' => true,
|
|
|
|
|
'escape' => true,
|
|
|
|
|
'inline' => false,
|
|
|
|
|
));
|
|
|
|
|
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-25 20:34:56 +00:00
|
|
|
global $wgMessageCache;
|
|
|
|
|
return $wgMessageCache->get(
|
|
|
|
|
$this->key,
|
|
|
|
|
$this->options['usedb'],
|
|
|
|
|
$this->language
|
|
|
|
|
) !== 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
|
|
|
/**
|
|
|
|
|
* Wrapper for what ever method we use to get message contents
|
|
|
|
|
* @return Unmodified message contents
|
|
|
|
|
*/
|
2010-03-22 17:05:32 +00:00
|
|
|
protected function getMessageText() {
|
2010-03-23 15:41:18 +00:00
|
|
|
global $wgMessageCache;
|
2010-03-25 20:34:56 +00:00
|
|
|
$message = $wgMessageCache->get( $this->key, $this->options['usedb'], $this->language );
|
|
|
|
|
return $message === false
|
|
|
|
|
? '<' . htmlspecialchars( $this->key ) . '>'
|
|
|
|
|
: $message;
|
2010-03-22 17:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|