Customise linktrail for Gujarati (gu)
[mediawiki.git] / includes / Message.php
blob74b4021ba896e6d25a886a64235e28fe23abbac3
1 <?php
2 /**
3 * Fetching and processing of interface messages.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @author Niklas Laxström
24 /**
25 * The Message class provides methods which fulfil two basic services:
26 * - fetching interface messages
27 * - processing messages into a variety of formats
29 * First implemented with MediaWiki 1.17, the Message class is intended to
30 * replace the old wfMsg* functions that over time grew unusable.
31 * @see https://www.mediawiki.org/wiki/Manual:Messages_API for equivalences
32 * between old and new functions.
34 * You should use the wfMessage() global function which acts as a wrapper for
35 * the Message class. The wrapper let you pass parameters as arguments.
37 * The most basic usage cases would be:
39 * @code
40 * // Initialize a Message object using the 'some_key' message key
41 * $message = wfMessage( 'some_key' );
43 * // Using two parameters those values are strings 'value1' and 'value2':
44 * $message = wfMessage( 'some_key',
45 * 'value1', 'value2'
46 * );
47 * @endcode
49 * @section message_global_fn Global function wrapper:
51 * Since wfMessage() returns a Message instance, you can chain its call with
52 * a method. Some of them return a Message instance too so you can chain them.
53 * You will find below several examples of wfMessage() usage.
55 * Fetching a message text for interface message:
57 * @code
58 * $button = Xml::button(
59 * wfMessage( 'submit' )->text()
60 * );
61 * @endcode
63 * A Message instance can be passed parameters after it has been constructed,
64 * use the params() method to do so:
66 * @code
67 * wfMessage( 'welcome-to' )
68 * ->params( $wgSitename )
69 * ->text();
70 * @endcode
72 * {{GRAMMAR}} and friends work correctly:
74 * @code
75 * wfMessage( 'are-friends',
76 * $user, $friend
77 * );
78 * wfMessage( 'bad-message' )
79 * ->rawParams( '<script>...</script>' )
80 * ->escaped();
81 * @endcode
83 * @section message_language Changing language:
85 * Messages can be requested in a different language or in whatever current
86 * content language is being used. The methods are:
87 * - Message->inContentLanguage()
88 * - Message->inLanguage()
90 * Sometimes the message text ends up in the database, so content language is
91 * needed:
93 * @code
94 * wfMessage( 'file-log',
95 * $user, $filename
96 * )->inContentLanguage()->text();
97 * @endcode
99 * Checking whether a message exists:
101 * @code
102 * wfMessage( 'mysterious-message' )->exists()
103 * // returns a boolean whether the 'mysterious-message' key exist.
104 * @endcode
106 * If you want to use a different language:
108 * @code
109 * $userLanguage = $user->getOption( 'language' );
110 * wfMessage( 'email-header' )
111 * ->inLanguage( $userLanguage )
112 * ->plain();
113 * @endcode
115 * @note You can parse the text only in the content or interface languages
117 * @section message_compare_old Comparison with old wfMsg* functions:
119 * Use full parsing:
121 * @code
122 * // old style:
123 * wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
124 * // new style:
125 * wfMessage( 'key', 'apple' )->parse();
126 * @endcode
128 * Parseinline is used because it is more useful when pre-building HTML.
129 * In normal use it is better to use OutputPage::(add|wrap)WikiMsg.
131 * Places where HTML cannot be used. {{-transformation is done.
132 * @code
133 * // old style:
134 * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
135 * // new style:
136 * wfMessage( 'key', 'apple', 'pear' )->text();
137 * @endcode
139 * Shortcut for escaping the message too, similar to wfMsgHTML(), but
140 * parameters are not replaced after escaping by default.
141 * @code
142 * $escaped = wfMessage( 'key' )
143 * ->rawParams( 'apple' )
144 * ->escaped();
145 * @endcode
147 * @section message_appendix Appendix:
149 * @todo
150 * - test, can we have tests?
151 * - this documentation needs to be extended
153 * @see https://www.mediawiki.org/wiki/WfMessage()
154 * @see https://www.mediawiki.org/wiki/New_messages_API
155 * @see https://www.mediawiki.org/wiki/Localisation
157 * @since 1.17
159 class Message {
161 * In which language to get this message. True, which is the default,
162 * means the current interface language, false content language.
164 protected $interface = true;
167 * In which language to get this message. Overrides the $interface
168 * variable.
170 * @var Language
172 protected $language = null;
175 * The message key.
177 protected $key;
180 * List of parameters which will be substituted into the message.
182 protected $parameters = array();
185 * Format for the message.
186 * Supported formats are:
187 * * text (transform)
188 * * escaped (transform+htmlspecialchars)
189 * * block-parse
190 * * parse (default)
191 * * plain
193 protected $format = 'parse';
196 * Whether database can be used.
198 protected $useDatabase = true;
201 * Title object to use as context
203 protected $title = null;
206 * Content object representing the message
208 protected $content = null;
211 * @var string
213 protected $message;
216 * Constructor.
217 * @since 1.17
218 * @param $key: message key, or array of message keys to try and use the first non-empty message for
219 * @param array $params message parameters
220 * @return Message: $this
222 public function __construct( $key, $params = array() ) {
223 global $wgLang;
224 $this->key = $key;
225 $this->parameters = array_values( $params );
226 $this->language = $wgLang;
230 * Returns the message key
232 * @since 1.21
234 * @return string
236 public function getKey() {
237 if ( is_array( $this->key ) ) {
238 // May happen if some kind of fallback is applied.
239 // For now, just use the first key. We really need a better solution.
240 return $this->key[0];
241 } else {
242 return $this->key;
247 * Returns the message parameters
249 * @since 1.21
251 * @return string[]
253 public function getParams() {
254 return $this->parameters;
258 * Returns the message format
260 * @since 1.21
262 * @return string
264 public function getFormat() {
265 return $this->format;
269 * Factory function that is just wrapper for the real constructor. It is
270 * intended to be used instead of the real constructor, because it allows
271 * chaining method calls, while new objects don't.
272 * @since 1.17
273 * @param string $key message key
274 * @param Varargs: parameters as Strings
275 * @return Message: $this
277 public static function newFromKey( $key /*...*/ ) {
278 $params = func_get_args();
279 array_shift( $params );
280 return new self( $key, $params );
284 * Factory function accepting multiple message keys and returning a message instance
285 * for the first message which is non-empty. If all messages are empty then an
286 * instance of the first message key is returned.
287 * @since 1.18
288 * @param Varargs: message keys (or first arg as an array of all the message keys)
289 * @return Message: $this
291 public static function newFallbackSequence( /*...*/ ) {
292 $keys = func_get_args();
293 if ( func_num_args() == 1 ) {
294 if ( is_array( $keys[0] ) ) {
295 // Allow an array to be passed as the first argument instead
296 $keys = array_values( $keys[0] );
297 } else {
298 // Optimize a single string to not need special fallback handling
299 $keys = $keys[0];
302 return new self( $keys );
306 * Adds parameters to the parameter list of this message.
307 * @since 1.17
308 * @param Varargs: parameters as Strings, or a single argument that is an array of Strings
309 * @return Message: $this
311 public function params( /*...*/ ) {
312 $args = func_get_args();
313 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
314 $args = $args[0];
316 $args_values = array_values( $args );
317 $this->parameters = array_merge( $this->parameters, $args_values );
318 return $this;
322 * Add parameters that are substituted after parsing or escaping.
323 * In other words the parsing process cannot access the contents
324 * of this type of parameter, and you need to make sure it is
325 * sanitized beforehand. The parser will see "$n", instead.
326 * @since 1.17
327 * @param Varargs: raw parameters as Strings (or single argument that is an array of raw parameters)
328 * @return Message: $this
330 public function rawParams( /*...*/ ) {
331 $params = func_get_args();
332 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
333 $params = $params[0];
335 foreach ( $params as $param ) {
336 $this->parameters[] = self::rawParam( $param );
338 return $this;
342 * Add parameters that are numeric and will be passed through
343 * Language::formatNum before substitution
344 * @since 1.18
345 * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
346 * @return Message: $this
348 public function numParams( /*...*/ ) {
349 $params = func_get_args();
350 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
351 $params = $params[0];
353 foreach ( $params as $param ) {
354 $this->parameters[] = self::numParam( $param );
356 return $this;
360 * Set the language and the title from a context object
361 * @since 1.19
362 * @param $context IContextSource
363 * @return Message: $this
365 public function setContext( IContextSource $context ) {
366 $this->inLanguage( $context->getLanguage() );
367 $this->title( $context->getTitle() );
368 $this->interface = true;
370 return $this;
374 * Request the message in any language that is supported.
375 * As a side effect interface message status is unconditionally
376 * turned off.
377 * @since 1.17
378 * @param $lang Mixed: language code or Language object.
379 * @throws MWException
380 * @return Message: $this
382 public function inLanguage( $lang ) {
383 if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
384 $this->language = $lang;
385 } elseif ( is_string( $lang ) ) {
386 if ( $this->language->getCode() != $lang ) {
387 $this->language = Language::factory( $lang );
389 } else {
390 $type = gettype( $lang );
391 throw new MWException( __METHOD__ . " must be "
392 . "passed a String or Language object; $type given"
395 $this->interface = false;
396 return $this;
400 * Request the message in the wiki's content language,
401 * unless it is disabled for this message.
402 * @since 1.17
403 * @see $wgForceUIMsgAsContentMsg
404 * @return Message: $this
406 public function inContentLanguage() {
407 global $wgForceUIMsgAsContentMsg;
408 if ( in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) {
409 return $this;
412 global $wgContLang;
413 $this->interface = false;
414 $this->language = $wgContLang;
415 return $this;
419 * Allows manipulating the interface message flag directly.
420 * Can be used to restore the flag after setting a language.
421 * @param $value bool
422 * @return Message: $this
423 * @since 1.20
425 public function setInterfaceMessageFlag( $value ) {
426 $this->interface = (bool) $value;
427 return $this;
431 * Enable or disable database use.
432 * @since 1.17
433 * @param $value Boolean
434 * @return Message: $this
436 public function useDatabase( $value ) {
437 $this->useDatabase = (bool) $value;
438 return $this;
442 * Set the Title object to use as context when transforming the message
443 * @since 1.18
444 * @param $title Title object
445 * @return Message: $this
447 public function title( $title ) {
448 $this->title = $title;
449 return $this;
453 * Returns the message as a Content object.
454 * @return Content
456 public function content() {
457 if ( !$this->content ) {
458 $this->content = new MessageContent( $this );
461 return $this->content;
465 * Returns the message parsed from wikitext to HTML.
466 * @since 1.17
467 * @return String: HTML
469 public function toString() {
470 $string = $this->fetchMessage();
472 if ( $string === false ) {
473 $key = htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key );
474 if ( $this->format === 'plain' ) {
475 return '<' . $key . '>';
477 return '&lt;' . $key . '&gt;';
480 # Replace $* with a list of parameters for &uselang=qqx.
481 if ( strpos( $string, '$*' ) !== false ) {
482 $paramlist = '';
483 if ( $this->parameters !== array() ) {
484 $paramlist = ': $' . implode( ', $', range( 1, count( $this->parameters ) ) );
486 $string = str_replace( '$*', $paramlist, $string );
489 # Replace parameters before text parsing
490 $string = $this->replaceParameters( $string, 'before' );
492 # Maybe transform using the full parser
493 if ( $this->format === 'parse' ) {
494 $string = $this->parseText( $string );
495 $m = array();
496 if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
497 $string = $m[1];
499 } elseif ( $this->format === 'block-parse' ) {
500 $string = $this->parseText( $string );
501 } elseif ( $this->format === 'text' ) {
502 $string = $this->transformText( $string );
503 } elseif ( $this->format === 'escaped' ) {
504 $string = $this->transformText( $string );
505 $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false );
508 # Raw parameter replacement
509 $string = $this->replaceParameters( $string, 'after' );
511 return $string;
515 * Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg:
516 * $foo = Message::get( $key );
517 * $string = "<abbr>$foo</abbr>";
518 * @since 1.18
519 * @return String
521 public function __toString() {
522 // PHP doesn't allow __toString to throw exceptions and will
523 // trigger a fatal error if it does. So, catch any exceptions.
525 try {
526 return $this->toString();
527 } catch ( Exception $ex ) {
528 try {
529 trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): "
530 . $ex, E_USER_WARNING );
531 } catch ( Exception $ex ) {
532 // Doh! Cause a fatal error after all?
535 if ( $this->format === 'plain' ) {
536 return '<' . $this->key . '>';
538 return '&lt;' . $this->key . '&gt;';
543 * Fully parse the text from wikitext to HTML
544 * @since 1.17
545 * @return String parsed HTML
547 public function parse() {
548 $this->format = 'parse';
549 return $this->toString();
553 * Returns the message text. {{-transformation is done.
554 * @since 1.17
555 * @return String: Unescaped message text.
557 public function text() {
558 $this->format = 'text';
559 return $this->toString();
563 * Returns the message text as-is, only parameters are substituted.
564 * @since 1.17
565 * @return String: Unescaped untransformed message text.
567 public function plain() {
568 $this->format = 'plain';
569 return $this->toString();
573 * Returns the parsed message text which is always surrounded by a block element.
574 * @since 1.17
575 * @return String: HTML
577 public function parseAsBlock() {
578 $this->format = 'block-parse';
579 return $this->toString();
583 * Returns the message text. {{-transformation is done and the result
584 * is escaped excluding any raw parameters.
585 * @since 1.17
586 * @return String: Escaped message text.
588 public function escaped() {
589 $this->format = 'escaped';
590 return $this->toString();
594 * Check whether a message key has been defined currently.
595 * @since 1.17
596 * @return Bool: true if it is and false if not.
598 public function exists() {
599 return $this->fetchMessage() !== false;
603 * Check whether a message does not exist, or is an empty string
604 * @since 1.18
605 * @return Bool: true if is is and false if not
606 * @todo FIXME: Merge with isDisabled()?
608 public function isBlank() {
609 $message = $this->fetchMessage();
610 return $message === false || $message === '';
614 * Check whether a message does not exist, is an empty string, or is "-"
615 * @since 1.18
616 * @return Bool: true if it is and false if not
618 public function isDisabled() {
619 $message = $this->fetchMessage();
620 return $message === false || $message === '' || $message === '-';
624 * @since 1.17
625 * @param $value
626 * @return array
628 public static function rawParam( $value ) {
629 return array( 'raw' => $value );
633 * @since 1.18
634 * @param $value
635 * @return array
637 public static function numParam( $value ) {
638 return array( 'num' => $value );
642 * Substitutes any parameters into the message text.
643 * @since 1.17
644 * @param string $message the message text
645 * @param string $type either before or after
646 * @return String
648 protected function replaceParameters( $message, $type = 'before' ) {
649 $replacementKeys = array();
650 foreach ( $this->parameters as $n => $param ) {
651 list( $paramType, $value ) = $this->extractParam( $param );
652 if ( $type === $paramType ) {
653 $replacementKeys['$' . ( $n + 1 )] = $value;
656 $message = strtr( $message, $replacementKeys );
657 return $message;
661 * Extracts the parameter type and preprocessed the value if needed.
662 * @since 1.18
663 * @param string|array $param Parameter as defined in this class.
664 * @return Tuple(type, value)
666 protected function extractParam( $param ) {
667 if ( is_array( $param ) && isset( $param['raw'] ) ) {
668 return array( 'after', $param['raw'] );
669 } elseif ( is_array( $param ) && isset( $param['num'] ) ) {
670 // Replace number params always in before step for now.
671 // No support for combined raw and num params
672 return array( 'before', $this->language->formatNum( $param['num'] ) );
673 } elseif ( !is_array( $param ) ) {
674 return array( 'before', $param );
675 } else {
676 trigger_error(
677 "Invalid message parameter: " . htmlspecialchars( serialize( $param ) ),
678 E_USER_WARNING
680 return array( 'before', '[INVALID]' );
685 * Wrapper for what ever method we use to parse wikitext.
686 * @since 1.17
687 * @param string $string Wikitext message contents
688 * @return string Wikitext parsed into HTML
690 protected function parseText( $string ) {
691 $out = MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language );
692 return is_object( $out ) ? $out->getText() : $out;
696 * Wrapper for what ever method we use to {{-transform wikitext.
697 * @since 1.17
698 * @param string $string Wikitext message contents
699 * @return string Wikitext with {{-constructs replaced with their values.
701 protected function transformText( $string ) {
702 return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title );
706 * Wrapper for what ever method we use to get message contents
707 * @since 1.17
708 * @throws MWException
709 * @return string
711 protected function fetchMessage() {
712 if ( !isset( $this->message ) ) {
713 $cache = MessageCache::singleton();
714 if ( is_array( $this->key ) ) {
715 if ( !count( $this->key ) ) {
716 throw new MWException( "Given empty message key array." );
718 foreach ( $this->key as $key ) {
719 $message = $cache->get( $key, $this->useDatabase, $this->language );
720 if ( $message !== false && $message !== '' ) {
721 break;
724 $this->message = $message;
725 } else {
726 $this->message = $cache->get( $this->key, $this->useDatabase, $this->language );
729 return $this->message;
735 * Variant of the Message class.
737 * Rather than treating the message key as a lookup
738 * value (which is passed to the MessageCache and
739 * translated as necessary), a RawMessage key is
740 * treated as the actual message.
742 * All other functionality (parsing, escaping, etc.)
743 * is preserved.
745 * @since 1.21
747 class RawMessage extends Message {
749 * Call the parent constructor, then store the key as
750 * the message.
752 * @param string $key Message to use
753 * @param array $params Parameters for the message
754 * @see Message::__construct
756 public function __construct( $key, $params = array() ) {
757 parent::__construct( $key, $params );
758 // The key is the message.
759 $this->message = $key;
763 * Fetch the message (in this case, the key).
765 * @return string
767 public function fetchMessage() {
768 // Just in case the message is unset somewhere.
769 if ( !isset( $this->message ) ) {
770 $this->message = $this->key;
772 return $this->message;