3 * This class provides methods for fetching interface messages and
4 * processing them into variety of formats that are needed in MediaWiki.
6 * It is intented to replace the old wfMsg* functions that over time grew
10 * Fetching a message text for interface message
11 * $button = Xml::button( wfMessage( 'submit' )->text() );
13 * Messages can have parameters:
14 * wfMessage( 'welcome-to' )->params( $wgSitename )->text();
15 * {{GRAMMAR}} and friends work correctly
16 * wfMessage( 'are-friends', $user, $friend );
17 * wfMessage( 'bad-message' )->rawParams( '<script>...</script>' )->escaped();
19 * Sometimes the message text ends up in the database, so content language is needed.
20 * wfMessage( 'file-log', $user, $filename )->inContentLanguage()->text()
22 * Checking if message exists:
23 * wfMessage( 'mysterious-message' )->exists()
25 * If you want to use a different language:
26 * wfMessage( 'email-header' )->inLanguage( $user->getOption( 'language' ) )->plain()
27 * Note that you cannot parse the text except in the content or interface
32 * Comparison with old wfMsg* functions:
35 * wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
36 * === wfMessage( 'key', 'apple' )->parse();
38 * Parseinline is used because it is more useful when pre-building html.
39 * In normal use it is better to use OutputPage::(add|wrap)WikiMsg.
41 * Places where html cannot be used. {{-transformation is done.
42 * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
43 * === wfMessage( 'key', 'apple', 'pear' )->text();
46 * Shortcut for escaping the message too, similar to wfMsgHTML, but
47 * parameters are not replaced after escaping by default.
48 * $escaped = wfMessage( 'key' )->rawParams( 'apple' )->escaped();
52 * - test, can we have tests?
53 * - sort out the details marked with fixme
56 * @author Niklas Laxström
60 * In which language to get this message. True, which is the default,
61 * means the current interface language, false content language.
63 protected $interface = true;
66 * In which language to get this message. Overrides the $interface
69 protected $language = null;
77 * List of parameters which will be substituted into the message.
79 protected $parameters = array();
82 * Format for the message.
83 * Supported formats are:
85 * * escaped (transform+htmlspecialchars)
90 protected $format = 'parse';
93 * Whether database can be used.
95 protected $useDatabase = true;
99 * @param $key String: message key
100 * @param $params Array message parameters
101 * @return Message: $this
103 public function __construct( $key, $params = array() ) {
106 $this->parameters
= array_values( $params );
107 $this->language
= $wgLang;
111 * Factory function that is just wrapper for the real constructor. It is
112 * intented to be used instead of the real constructor, because it allows
113 * chaining method calls, while new objects don't.
114 * @param $key String: message key
115 * @param Varargs: parameters as Strings
116 * @return Message: $this
118 public static function newFromKey( $key /*...*/ ) {
119 $params = func_get_args();
120 array_shift( $params );
121 return new self( $key, $params );
125 * Adds parameters to the parameter list of this message.
126 * @param Varargs: parameters as Strings
127 * @return Message: $this
129 public function params( /*...*/ ) {
130 $this->parameters
= array_merge( $this->parameters
, array_values( func_get_args() ) );
135 * Add parameters that are substituted after parsing or escaping.
136 * In other words the parsing process cannot access the contents
137 * of this type of parameter, and you need to make sure it is
138 * sanitized beforehand. The parser will see "$n", instead.
139 * @param Varargs: raw parameters as Strings
140 * @return Message: $this
142 public function rawParams( /*...*/ ) {
143 $params = func_get_args();
144 foreach( $params as $param ) {
145 $this->parameters
[] = self
::rawParam( $param );
151 * Request the message in any language that is supported.
152 * As a side effect interface message status is unconditionally
154 * @param $lang Mixed: language code or Language object.
155 * @return Message: $this
157 public function inLanguage( $lang ) {
158 if( $lang instanceof Language
){
159 $this->language
= $lang;
160 } elseif ( is_string( $lang ) ) {
161 if( $this->language
->getCode() != $lang ) {
162 $this->language
= Language
::factory( $lang );
165 $type = gettype( $lang );
166 throw new MWException( __METHOD__
. " must be "
167 . "passed a String or Language object; $type given"
170 $this->interface = false;
175 * Request the message in the wiki's content language.
176 * @return Message: $this
178 public function inContentLanguage() {
180 $this->interface = false;
181 $this->language
= $wgContLang;
186 * Enable or disable database use.
187 * @param $value Boolean
188 * @return Message: $this
190 public function useDatabase( $value ) {
191 $this->useDatabase
= (bool) $value;
196 * Returns the message parsed from wikitext to HTML.
197 * TODO: in PHP >= 5.2.0, we can make this a magic method,
198 * and then we can do, eg:
199 * $foo = Message::get($key);
200 * $string = "<abbr>$foo</abbr>";
201 * But we shouldn't implement that while MediaWiki still supports
202 * PHP < 5.2; or people will start using it...
203 * @return String: HTML
205 public function toString() {
206 $string = $this->getMessageText();
208 # Replace parameters before text parsing
209 $string = $this->replaceParameters( $string, 'before' );
211 # Maybe transform using the full parser
212 if( $this->format
=== 'parse' ) {
213 $string = $this->parseText( $string );
215 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
218 } elseif( $this->format
=== 'block-parse' ){
219 $string = $this->parseText( $string );
220 } elseif( $this->format
=== 'text' ){
221 $string = $this->transformText( $string );
222 } elseif( $this->format
=== 'escaped' ){
223 # FIXME: Sanitizer method here?
224 $string = $this->transformText( $string );
225 $string = htmlspecialchars( $string );
228 # Raw parameter replacement
229 $string = $this->replaceParameters( $string, 'after' );
235 * Fully parse the text from wikitext to HTML
236 * @return String parsed HTML
238 public function parse() {
239 $this->format
= 'parse';
240 return $this->toString();
244 * Returns the message text. {{-transformation is done.
245 * @return String: Unescaped message text.
247 public function text() {
248 $this->format
= 'text';
249 return $this->toString();
253 * Returns the message text as-is, only parameters are subsituted.
254 * @return String: Unescaped untransformed message text.
256 public function plain() {
257 $this->format
= 'plain';
258 return $this->toString();
262 * Returns the parsed message text which is always surrounded by a block element.
263 * @return String: HTML
265 public function parseAsBlock() {
266 $this->format
= 'block-parse';
267 return $this->toString();
271 * Returns the message text. {{-transformation is done and the result
272 * is escaped excluding any raw parameters.
273 * @return String: Escaped message text.
275 public function escaped() {
276 $this->format
= 'escaped';
277 return $this->toString();
281 * Check whether a message key has been defined currently.
282 * @return Bool: true if it is and false if not.
284 public function exists() {
285 return $this->fetchMessage() !== false;
288 public static function rawParam( $value ) {
289 return array( 'raw' => $value );
293 * Substitutes any paramaters into the message text.
294 * @param $message String, the message text
295 * @param $type String: either before or after
298 protected function replaceParameters( $message, $type = 'before' ) {
299 $replacementKeys = array();
300 foreach( $this->parameters
as $n => $param ) {
301 if ( $type === 'before' && !is_array( $param ) ) {
302 $replacementKeys['$' . ($n +
1)] = $param;
303 } elseif ( $type === 'after' && isset( $param['raw'] ) ) {
304 $replacementKeys['$' . ($n +
1)] = $param['raw'];
307 $message = strtr( $message, $replacementKeys );
312 * Wrapper for what ever method we use to parse wikitext.
313 * @param $string String: Wikitext message contents
314 * @return Wikitext parsed into HTML
316 protected function parseText( $string ) {
317 global $wgOut, $wgLang, $wgContLang;
318 if ( $this->language
!== $wgLang && $this->language
!== $wgContLang ) {
319 # FIXME: remove this limitation
320 throw new MWException( 'Can only parse in interface or content language' );
322 return $wgOut->parse( $string, /*linestart*/true, $this->interface );
326 * Wrapper for what ever method we use to {{-transform wikitext.
327 * @param $string String: Wikitext message contents
328 * @return Wikitext with {{-constructs replaced with their values.
330 protected function transformText( $string ) {
331 global $wgMessageCache;
332 return $wgMessageCache->transform( $string, $this->interface, $this->language
);
336 * Returns the textual value for the message.
337 * @return Message contents or placeholder
339 protected function getMessageText() {
340 $message = $this->fetchMessage();
341 if ( $message === false ) {
342 return '<' . htmlspecialchars( $this->key
) . '>';
349 * Wrapper for what ever method we use to get message contents
351 protected function fetchMessage() {
352 if ( !isset( $this->message
) ) {
353 global $wgMessageCache;
354 $this->message
= $wgMessageCache->get( $this->key
, $this->useDatabase
, $this->language
);
356 return $this->message
;