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: message key, or array of message keys to try and use the first non-empty message for
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 * Factory function accepting multiple message keys and returning a message instance
126 * for the first message which is non-empty. If all messages are empty then an
127 * instance of the first message key is returned.
128 * @param Varargs: message keys
129 * @return Message: $this
131 public static function newFallbackSequence( /*...*/ ) {
132 global $wgMessageCache;
133 $keys = func_get_args();
134 if ( func_num_args() == 1 ) {
135 if ( is_array($keys[0]) ) {
136 // Allow an array to be passed as the first argument instead
137 $keys = array_values($keys[0]);
139 // Optimize a single string to not need special fallback handling
143 return new self( $keys );
147 * Adds parameters to the parameter list of this message.
148 * @param Varargs: parameters as Strings
149 * @return Message: $this
151 public function params( /*...*/ ) {
152 $args_values = array_values( func_get_args() );
153 $this->parameters
= array_merge( $this->parameters
, $args_values );
158 * Add parameters that are substituted after parsing or escaping.
159 * In other words the parsing process cannot access the contents
160 * of this type of parameter, and you need to make sure it is
161 * sanitized beforehand. The parser will see "$n", instead.
162 * @param Varargs: raw parameters as Strings
163 * @return Message: $this
165 public function rawParams( /*...*/ ) {
166 $params = func_get_args();
167 foreach( $params as $param ) {
168 $this->parameters
[] = self
::rawParam( $param );
174 * Request the message in any language that is supported.
175 * As a side effect interface message status is unconditionally
177 * @param $lang Mixed: language code or Language object.
178 * @return Message: $this
180 public function inLanguage( $lang ) {
181 if( $lang instanceof Language
){
182 $this->language
= $lang;
183 } elseif ( is_string( $lang ) ) {
184 if( $this->language
->getCode() != $lang ) {
185 $this->language
= Language
::factory( $lang );
188 $type = gettype( $lang );
189 throw new MWException( __METHOD__
. " must be "
190 . "passed a String or Language object; $type given"
193 $this->interface = false;
198 * Request the message in the wiki's content language.
199 * @return Message: $this
201 public function inContentLanguage() {
203 $this->interface = false;
204 $this->language
= $wgContLang;
209 * Enable or disable database use.
210 * @param $value Boolean
211 * @return Message: $this
213 public function useDatabase( $value ) {
214 $this->useDatabase
= (bool) $value;
219 * Returns the message parsed from wikitext to HTML.
220 * TODO: in PHP >= 5.2.0, we can make this a magic method,
221 * and then we can do, eg:
222 * $foo = Message::get($key);
223 * $string = "<abbr>$foo</abbr>";
224 * But we shouldn't implement that while MediaWiki still supports
225 * PHP < 5.2; or people will start using it...
226 * @return String: HTML
228 public function toString() {
229 $string = $this->getMessageText();
231 # Replace parameters before text parsing
232 $string = $this->replaceParameters( $string, 'before' );
234 # Maybe transform using the full parser
235 if( $this->format
=== 'parse' ) {
236 $string = $this->parseText( $string );
238 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
241 } elseif( $this->format
=== 'block-parse' ){
242 $string = $this->parseText( $string );
243 } elseif( $this->format
=== 'text' ){
244 $string = $this->transformText( $string );
245 } elseif( $this->format
=== 'escaped' ){
246 # FIXME: Sanitizer method here?
247 $string = $this->transformText( $string );
248 $string = htmlspecialchars( $string );
251 # Raw parameter replacement
252 $string = $this->replaceParameters( $string, 'after' );
258 * Fully parse the text from wikitext to HTML
259 * @return String parsed HTML
261 public function parse() {
262 $this->format
= 'parse';
263 return $this->toString();
267 * Returns the message text. {{-transformation is done.
268 * @return String: Unescaped message text.
270 public function text() {
271 $this->format
= 'text';
272 return $this->toString();
276 * Returns the message text as-is, only parameters are subsituted.
277 * @return String: Unescaped untransformed message text.
279 public function plain() {
280 $this->format
= 'plain';
281 return $this->toString();
285 * Returns the parsed message text which is always surrounded by a block element.
286 * @return String: HTML
288 public function parseAsBlock() {
289 $this->format
= 'block-parse';
290 return $this->toString();
294 * Returns the message text. {{-transformation is done and the result
295 * is escaped excluding any raw parameters.
296 * @return String: Escaped message text.
298 public function escaped() {
299 $this->format
= 'escaped';
300 return $this->toString();
304 * Check whether a message key has been defined currently.
305 * @return Bool: true if it is and false if not.
307 public function exists() {
308 return $this->fetchMessage() !== false;
312 * Check whether a message does not exist, or is an empty string
313 * @return Bool: true if is is and false if not
315 public function isBlank() {
316 $message = $this->fetchMessage();
317 return $message === false ||
$message === '';
321 * Check whether a message does not exist, is an empty string, or is "-"
322 * @return Bool: true if is is and false if not
324 public function isDisabled() {
325 $message = $this->fetchMessage();
326 return $message === false ||
$message === '' ||
$message === '-';
329 public static function rawParam( $value ) {
330 return array( 'raw' => $value );
334 * Substitutes any paramaters into the message text.
335 * @param $message String, the message text
336 * @param $type String: either before or after
339 protected function replaceParameters( $message, $type = 'before' ) {
340 $replacementKeys = array();
341 foreach( $this->parameters
as $n => $param ) {
342 if ( $type === 'before' && !is_array( $param ) ) {
343 $replacementKeys['$' . ($n +
1)] = $param;
344 } elseif ( $type === 'after' && isset( $param['raw'] ) ) {
345 $replacementKeys['$' . ($n +
1)] = $param['raw'];
348 $message = strtr( $message, $replacementKeys );
353 * Wrapper for what ever method we use to parse wikitext.
354 * @param $string String: Wikitext message contents
355 * @return Wikitext parsed into HTML
357 protected function parseText( $string ) {
359 return $wgOut->parse( $string, /*linestart*/true, $this->interface, $this->language
);
363 * Wrapper for what ever method we use to {{-transform wikitext.
364 * @param $string String: Wikitext message contents
365 * @return Wikitext with {{-constructs replaced with their values.
367 protected function transformText( $string ) {
368 global $wgMessageCache;
369 return $wgMessageCache->transform( $string, $this->interface, $this->language
);
373 * Returns the textual value for the message.
374 * @return Message contents or placeholder
376 protected function getMessageText() {
377 $message = $this->fetchMessage();
378 if ( $message === false ) {
379 return '<' . htmlspecialchars( is_array($this->key
) ?
$this->key
[0] : $this->key
) . '>';
386 * Wrapper for what ever method we use to get message contents
388 protected function fetchMessage() {
389 if ( !isset( $this->message
) ) {
390 global $wgMessageCache;
391 if ( is_array($this->key
) ) {
392 foreach ( $this->key
as $key ) {
393 $message = $wgMessageCache->get( $key, $this->useDatabase
, $this->language
);
394 if ( $message !== false && $message !== '' ) {
398 $this->message
= $message;
400 $this->message
= $wgMessageCache->get( $this->key
, $this->useDatabase
, $this->language
);
403 return $this->message
;