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
21 * @author Niklas Laxström
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:
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',
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:
58 * $button = Xml::button(
59 * wfMessage( 'submit' )->text()
63 * A Message instance can be passed parameters after it has been constructed,
64 * use the params() method to do so:
67 * wfMessage( 'welcome-to' )
68 * ->params( $wgSitename )
72 * {{GRAMMAR}} and friends work correctly:
75 * wfMessage( 'are-friends',
78 * wfMessage( 'bad-message' )
79 * ->rawParams( '<script>...</script>' )
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
94 * wfMessage( 'file-log',
96 * )->inContentLanguage()->text();
99 * Checking whether a message exists:
102 * wfMessage( 'mysterious-message' )->exists()
103 * // returns a boolean whether the 'mysterious-message' key exist.
106 * If you want to use a different language:
109 * $userLanguage = $user->getOption( 'language' );
110 * wfMessage( 'email-header' )
111 * ->inLanguage( $userLanguage )
115 * @note You can parse the text only in the content or interface languages
117 * @section message_compare_old Comparison with old wfMsg* functions:
123 * wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
125 * wfMessage( 'key', 'apple' )->parse();
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.
134 * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
136 * wfMessage( 'key', 'apple', 'pear' )->text();
139 * Shortcut for escaping the message too, similar to wfMsgHTML(), but
140 * parameters are not replaced after escaping by default.
142 * $escaped = wfMessage( 'key' )
143 * ->rawParams( 'apple' )
147 * @section message_appendix Appendix:
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
162 * In which language to get this message. True, which is the default,
163 * means the current interface language, false content language.
167 protected $interface = true;
170 * In which language to get this message. Overrides the $interface
175 protected $language = null;
178 * @var string The message key. If $keysToTry has more than one element,
179 * this may change to one of the keys to try when fetching the message text.
184 * @var string[] List of keys to try when fetching the message.
186 protected $keysToTry;
189 * @var array List of parameters which will be substituted into the message.
191 protected $parameters = array();
194 * Format for the message.
195 * Supported formats are:
197 * * escaped (transform+htmlspecialchars)
204 protected $format = 'parse';
207 * @var bool Whether database can be used.
209 protected $useDatabase = true;
212 * @var Title Title object to use as context.
214 protected $title = null;
217 * @var Content Content object representing the message.
219 protected $content = null;
229 * @param string|string[] $key Message key or array of message keys to try and use the first
230 * non-empty message for.
231 * @param array $params Message parameters.
232 * @param Language $language Optional language of the message, defaults to $wgLang.
234 * @throws InvalidArgumentException
236 public function __construct( $key, $params = array(), Language
$language = null ) {
239 if ( !is_string( $key ) && !is_array( $key ) ) {
240 throw new InvalidArgumentException( '$key must be a string or an array' );
243 $this->keysToTry
= (array)$key;
245 if ( empty( $this->keysToTry
) ) {
246 throw new InvalidArgumentException( '$key must not be an empty list' );
249 $this->key
= reset( $this->keysToTry
);
251 $this->parameters
= array_values( $params );
252 $this->language
= $language ?
$language : $wgLang;
258 * @return bool True if this is a multi-key message, that is, if the key provided to the
259 * constructor was a fallback list of keys to try.
261 public function isMultiKey() {
262 return count( $this->keysToTry
) > 1;
268 * @return string[] The list of keys to try when fetching the message text,
269 * in order of preference.
271 public function getKeysToTry() {
272 return $this->keysToTry
;
276 * Returns the message key.
278 * If a list of multiple possible keys was supplied to the constructor, this method may
279 * return any of these keys. After the message ahs been fetched, this method will return
280 * the key that was actually used to fetch the message.
286 public function getKey() {
291 * Returns the message parameters.
297 public function getParams() {
298 return $this->parameters
;
302 * Returns the message format.
308 public function getFormat() {
309 return $this->format
;
313 * Returns the Language of the Message.
319 public function getLanguage() {
320 return $this->language
;
324 * Factory function that is just wrapper for the real constructor. It is
325 * intended to be used instead of the real constructor, because it allows
326 * chaining method calls, while new objects don't.
330 * @param string|string[] $key Message key or array of keys.
331 * @param mixed $param,... Parameters as strings.
335 public static function newFromKey( $key /*...*/ ) {
336 $params = func_get_args();
337 array_shift( $params );
338 return new self( $key, $params );
342 * Factory function accepting multiple message keys and returning a message instance
343 * for the first message which is non-empty. If all messages are empty then an
344 * instance of the first message key is returned.
348 * @param string|string[] $keys,... Message keys, or first argument as an array of all the
353 public static function newFallbackSequence( /*...*/ ) {
354 $keys = func_get_args();
355 if ( func_num_args() == 1 ) {
356 if ( is_array( $keys[0] ) ) {
357 // Allow an array to be passed as the first argument instead
358 $keys = array_values( $keys[0] );
360 // Optimize a single string to not need special fallback handling
364 return new self( $keys );
368 * Adds parameters to the parameter list of this message.
372 * @param mixed $params,... Parameters as strings, or a single argument that is
373 * an array of strings.
375 * @return Message $this
377 public function params( /*...*/ ) {
378 $args = func_get_args();
379 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
382 $args_values = array_values( $args );
383 $this->parameters
= array_merge( $this->parameters
, $args_values );
388 * Add parameters that are substituted after parsing or escaping.
389 * In other words the parsing process cannot access the contents
390 * of this type of parameter, and you need to make sure it is
391 * sanitized beforehand. The parser will see "$n", instead.
395 * @param mixed $params,... Raw parameters as strings, or a single argument that is
396 * an array of raw parameters.
398 * @return Message $this
400 public function rawParams( /*...*/ ) {
401 $params = func_get_args();
402 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
403 $params = $params[0];
405 foreach ( $params as $param ) {
406 $this->parameters
[] = self
::rawParam( $param );
412 * Add parameters that are numeric and will be passed through
413 * Language::formatNum before substitution
417 * @param mixed $param,... Numeric parameters, or a single argument that is
418 * an array of numeric parameters.
420 * @return Message $this
422 public function numParams( /*...*/ ) {
423 $params = func_get_args();
424 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
425 $params = $params[0];
427 foreach ( $params as $param ) {
428 $this->parameters
[] = self
::numParam( $param );
434 * Add parameters that are durations of time and will be passed through
435 * Language::formatDuration before substitution
439 * @param int|int[] $param,... Duration parameters, or a single argument that is
440 * an array of duration parameters.
442 * @return Message $this
444 public function durationParams( /*...*/ ) {
445 $params = func_get_args();
446 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
447 $params = $params[0];
449 foreach ( $params as $param ) {
450 $this->parameters
[] = self
::durationParam( $param );
456 * Add parameters that are expiration times and will be passed through
457 * Language::formatExpiry before substitution
461 * @param string|string[] $param,... Expiry parameters, or a single argument that is
462 * an array of expiry parameters.
464 * @return Message $this
466 public function expiryParams( /*...*/ ) {
467 $params = func_get_args();
468 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
469 $params = $params[0];
471 foreach ( $params as $param ) {
472 $this->parameters
[] = self
::expiryParam( $param );
478 * Add parameters that are time periods and will be passed through
479 * Language::formatTimePeriod before substitution
483 * @param int|int[] $param,... Time period parameters, or a single argument that is
484 * an array of time period parameters.
486 * @return Message $this
488 public function timeperiodParams( /*...*/ ) {
489 $params = func_get_args();
490 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
491 $params = $params[0];
493 foreach ( $params as $param ) {
494 $this->parameters
[] = self
::timeperiodParam( $param );
500 * Add parameters that are file sizes and will be passed through
501 * Language::formatSize before substitution
505 * @param int|int[] $param,... Size parameters, or a single argument that is
506 * an array of size parameters.
508 * @return Message $this
510 public function sizeParams( /*...*/ ) {
511 $params = func_get_args();
512 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
513 $params = $params[0];
515 foreach ( $params as $param ) {
516 $this->parameters
[] = self
::sizeParam( $param );
522 * Add parameters that are bitrates and will be passed through
523 * Language::formatBitrate before substitution
527 * @param int|int[] $param,... Bit rate parameters, or a single argument that is
528 * an array of bit rate parameters.
530 * @return Message $this
532 public function bitrateParams( /*...*/ ) {
533 $params = func_get_args();
534 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
535 $params = $params[0];
537 foreach ( $params as $param ) {
538 $this->parameters
[] = self
::bitrateParam( $param );
544 * Set the language and the title from a context object
548 * @param IContextSource $context
550 * @return Message $this
552 public function setContext( IContextSource
$context ) {
553 $this->inLanguage( $context->getLanguage() );
554 $this->title( $context->getTitle() );
555 $this->interface = true;
561 * Request the message in any language that is supported.
562 * As a side effect interface message status is unconditionally
567 * @param Language|string $lang Language code or Language object.
569 * @return Message $this
570 * @throws MWException
572 public function inLanguage( $lang ) {
573 if ( $lang instanceof Language ||
$lang instanceof StubUserLang
) {
574 $this->language
= $lang;
575 } elseif ( is_string( $lang ) ) {
576 if ( $this->language
->getCode() != $lang ) {
577 $this->language
= Language
::factory( $lang );
580 $type = gettype( $lang );
581 throw new MWException( __METHOD__
. " must be "
582 . "passed a String or Language object; $type given"
585 $this->message
= null;
586 $this->interface = false;
591 * Request the message in the wiki's content language,
592 * unless it is disabled for this message.
595 * @see $wgForceUIMsgAsContentMsg
597 * @return Message $this
599 public function inContentLanguage() {
600 global $wgForceUIMsgAsContentMsg;
601 if ( in_array( $this->key
, (array)$wgForceUIMsgAsContentMsg ) ) {
606 $this->inLanguage( $wgContLang );
611 * Allows manipulating the interface message flag directly.
612 * Can be used to restore the flag after setting a language.
616 * @param bool $interface
618 * @return Message $this
620 public function setInterfaceMessageFlag( $interface ) {
621 $this->interface = (bool)$interface;
626 * Enable or disable database use.
630 * @param bool $useDatabase
632 * @return Message $this
634 public function useDatabase( $useDatabase ) {
635 $this->useDatabase
= (bool)$useDatabase;
640 * Set the Title object to use as context when transforming the message
644 * @param Title $title
646 * @return Message $this
648 public function title( $title ) {
649 $this->title
= $title;
654 * Returns the message as a Content object.
658 public function content() {
659 if ( !$this->content
) {
660 $this->content
= new MessageContent( $this );
663 return $this->content
;
667 * Returns the message parsed from wikitext to HTML.
671 * @return string HTML
673 public function toString() {
674 $string = $this->fetchMessage();
676 if ( $string === false ) {
677 $key = htmlspecialchars( $this->key
);
678 if ( $this->format
=== 'plain' ) {
679 return '<' . $key . '>';
681 return '<' . $key . '>';
684 # Replace $* with a list of parameters for &uselang=qqx.
685 if ( strpos( $string, '$*' ) !== false ) {
687 if ( $this->parameters
!== array() ) {
688 $paramlist = ': $' . implode( ', $', range( 1, count( $this->parameters
) ) );
690 $string = str_replace( '$*', $paramlist, $string );
693 # Replace parameters before text parsing
694 $string = $this->replaceParameters( $string, 'before' );
696 # Maybe transform using the full parser
697 if ( $this->format
=== 'parse' ) {
698 $string = $this->parseText( $string );
699 $string = Parser
::stripOuterParagraph( $string );
700 } elseif ( $this->format
=== 'block-parse' ) {
701 $string = $this->parseText( $string );
702 } elseif ( $this->format
=== 'text' ) {
703 $string = $this->transformText( $string );
704 } elseif ( $this->format
=== 'escaped' ) {
705 $string = $this->transformText( $string );
706 $string = htmlspecialchars( $string, ENT_QUOTES
, 'UTF-8', false );
709 # Raw parameter replacement
710 $string = $this->replaceParameters( $string, 'after' );
716 * Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg:
717 * $foo = Message::get( $key );
718 * $string = "<abbr>$foo</abbr>";
724 public function __toString() {
725 // PHP doesn't allow __toString to throw exceptions and will
726 // trigger a fatal error if it does. So, catch any exceptions.
729 return $this->toString();
730 } catch ( Exception
$ex ) {
732 trigger_error( "Exception caught in " . __METHOD__
. " (message " . $this->key
. "): "
733 . $ex, E_USER_WARNING
);
734 } catch ( Exception
$ex ) {
735 // Doh! Cause a fatal error after all?
738 if ( $this->format
=== 'plain' ) {
739 return '<' . $this->key
. '>';
741 return '<' . $this->key
. '>';
746 * Fully parse the text from wikitext to HTML.
750 * @return string Parsed HTML.
752 public function parse() {
753 $this->format
= 'parse';
754 return $this->toString();
758 * Returns the message text. {{-transformation is done.
762 * @return string Unescaped message text.
764 public function text() {
765 $this->format
= 'text';
766 return $this->toString();
770 * Returns the message text as-is, only parameters are substituted.
774 * @return string Unescaped untransformed message text.
776 public function plain() {
777 $this->format
= 'plain';
778 return $this->toString();
782 * Returns the parsed message text which is always surrounded by a block element.
786 * @return string HTML
788 public function parseAsBlock() {
789 $this->format
= 'block-parse';
790 return $this->toString();
794 * Returns the message text. {{-transformation is done and the result
795 * is escaped excluding any raw parameters.
799 * @return string Escaped message text.
801 public function escaped() {
802 $this->format
= 'escaped';
803 return $this->toString();
807 * Check whether a message key has been defined currently.
813 public function exists() {
814 return $this->fetchMessage() !== false;
818 * Check whether a message does not exist, or is an empty string
821 * @todo FIXME: Merge with isDisabled()?
825 public function isBlank() {
826 $message = $this->fetchMessage();
827 return $message === false ||
$message === '';
831 * Check whether a message does not exist, is an empty string, or is "-".
837 public function isDisabled() {
838 $message = $this->fetchMessage();
839 return $message === false ||
$message === '' ||
$message === '-';
847 * @return array Array with a single "raw" key.
849 public static function rawParam( $raw ) {
850 return array( 'raw' => $raw );
858 * @return array Array with a single "num" key.
860 public static function numParam( $num ) {
861 return array( 'num' => $num );
867 * @param int $duration
869 * @return int[] Array with a single "duration" key.
871 public static function durationParam( $duration ) {
872 return array( 'duration' => $duration );
878 * @param string $expiry
880 * @return string[] Array with a single "expiry" key.
882 public static function expiryParam( $expiry ) {
883 return array( 'expiry' => $expiry );
889 * @param number $period
891 * @return number[] Array with a single "period" key.
893 public static function timeperiodParam( $period ) {
894 return array( 'period' => $period );
902 * @return int[] Array with a single "size" key.
904 public static function sizeParam( $size ) {
905 return array( 'size' => $size );
911 * @param int $bitrate
913 * @return int[] Array with a single "bitrate" key.
915 public static function bitrateParam( $bitrate ) {
916 return array( 'bitrate' => $bitrate );
920 * Substitutes any parameters into the message text.
924 * @param string $message The message text.
925 * @param string $type Either "before" or "after".
929 protected function replaceParameters( $message, $type = 'before' ) {
930 $replacementKeys = array();
931 foreach ( $this->parameters
as $n => $param ) {
932 list( $paramType, $value ) = $this->extractParam( $param );
933 if ( $type === $paramType ) {
934 $replacementKeys['$' . ( $n +
1 )] = $value;
937 $message = strtr( $message, $replacementKeys );
942 * Extracts the parameter type and preprocessed the value if needed.
946 * @param mixed $param Parameter as defined in this class.
948 * @return array Array with the parameter type (either "before" or "after") and the value.
950 protected function extractParam( $param ) {
951 if ( is_array( $param ) ) {
952 if ( isset( $param['raw'] ) ) {
953 return array( 'after', $param['raw'] );
954 } elseif ( isset( $param['num'] ) ) {
955 // Replace number params always in before step for now.
956 // No support for combined raw and num params
957 return array( 'before', $this->language
->formatNum( $param['num'] ) );
958 } elseif ( isset( $param['duration'] ) ) {
959 return array( 'before', $this->language
->formatDuration( $param['duration'] ) );
960 } elseif ( isset( $param['expiry'] ) ) {
961 return array( 'before', $this->language
->formatExpiry( $param['expiry'] ) );
962 } elseif ( isset( $param['period'] ) ) {
963 return array( 'before', $this->language
->formatTimePeriod( $param['period'] ) );
964 } elseif ( isset( $param['size'] ) ) {
965 return array( 'before', $this->language
->formatSize( $param['size'] ) );
966 } elseif ( isset( $param['bitrate'] ) ) {
967 return array( 'before', $this->language
->formatBitrate( $param['bitrate'] ) );
969 $warning = 'Invalid parameter for message "' . $this->getKey() . '": ' .
970 htmlspecialchars( serialize( $param ) );
971 trigger_error( $warning, E_USER_WARNING
);
973 wfDebugLog( 'Bug58676', $warning . "\n" . $e->getTraceAsString() );
975 return array( 'before', '[INVALID]' );
977 } elseif ( $param instanceof Message
) {
978 // Message objects should not be before parameters because
979 // then they'll get double escaped. If the message needs to be
980 // escaped, it'll happen right here when we call toString().
981 return array( 'after', $param->toString() );
983 return array( 'before', $param );
988 * Wrapper for what ever method we use to parse wikitext.
992 * @param string $string Wikitext message contents.
994 * @return string Wikitext parsed into HTML.
996 protected function parseText( $string ) {
997 $out = MessageCache
::singleton()->parse(
1005 return $out instanceof ParserOutput ?
$out->getText() : $out;
1009 * Wrapper for what ever method we use to {{-transform wikitext.
1013 * @param string $string Wikitext message contents.
1015 * @return string Wikitext with {{-constructs replaced with their values.
1017 protected function transformText( $string ) {
1018 return MessageCache
::singleton()->transform(
1027 * Wrapper for what ever method we use to get message contents.
1032 * @throws MWException If message key array is empty.
1034 protected function fetchMessage() {
1035 if ( $this->message
=== null ) {
1036 $cache = MessageCache
::singleton();
1038 foreach ( $this->keysToTry
as $key ) {
1039 $message = $cache->get( $key, $this->useDatabase
, $this->language
);
1040 if ( $message !== false && $message !== '' ) {
1045 // NOTE: The constructor makes sure keysToTry isn't empty,
1046 // so we know that $key and $message are initialized.
1048 $this->message
= $message;
1050 return $this->message
;
1056 * Variant of the Message class.
1058 * Rather than treating the message key as a lookup
1059 * value (which is passed to the MessageCache and
1060 * translated as necessary), a RawMessage key is
1061 * treated as the actual message.
1063 * All other functionality (parsing, escaping, etc.)
1068 class RawMessage
extends Message
{
1071 * Call the parent constructor, then store the key as
1074 * @see Message::__construct
1076 * @param string $text Message to use.
1077 * @param array $params Parameters for the message.
1079 * @throws InvalidArgumentException
1081 public function __construct( $text, $params = array() ) {
1082 if ( !is_string( $text ) ) {
1083 throw new InvalidArgumentException( '$text must be a string' );
1086 parent
::__construct( $text, $params );
1088 // The key is the message.
1089 $this->message
= $text;
1093 * Fetch the message (in this case, the key).
1097 public function fetchMessage() {
1098 // Just in case the message is unset somewhere.
1099 if ( $this->message
=== null ) {
1100 $this->message
= $this->key
;
1103 return $this->message
;