Merge "Add .mw-editsection-like class, behavior same as .mw-editsection"
[lhc/web/wiklou.git] / includes / Message.php
1 <?php
2 /**
3 * Fetching and processing of interface messages.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Niklas Laxström
22 */
23
24 /**
25 * The Message class provides methods which fulfil two basic services:
26 * - fetching interface messages
27 * - processing messages into a variety of formats
28 *
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.
33 *
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.
36 *
37 * The most basic usage cases would be:
38 *
39 * @code
40 * // Initialize a Message object using the 'some_key' message key
41 * $message = wfMessage( 'some_key' );
42 *
43 * // Using two parameters those values are strings 'value1' and 'value2':
44 * $message = wfMessage( 'some_key',
45 * 'value1', 'value2'
46 * );
47 * @endcode
48 *
49 * @section message_global_fn Global function wrapper:
50 *
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.
54 *
55 * Fetching a message text for interface message:
56 *
57 * @code
58 * $button = Xml::button(
59 * wfMessage( 'submit' )->text()
60 * );
61 * @endcode
62 *
63 * A Message instance can be passed parameters after it has been constructed,
64 * use the params() method to do so:
65 *
66 * @code
67 * wfMessage( 'welcome-to' )
68 * ->params( $wgSitename )
69 * ->text();
70 * @endcode
71 *
72 * {{GRAMMAR}} and friends work correctly:
73 *
74 * @code
75 * wfMessage( 'are-friends',
76 * $user, $friend
77 * );
78 * wfMessage( 'bad-message' )
79 * ->rawParams( '<script>...</script>' )
80 * ->escaped();
81 * @endcode
82 *
83 * @section message_language Changing language:
84 *
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()
89 *
90 * Sometimes the message text ends up in the database, so content language is
91 * needed:
92 *
93 * @code
94 * wfMessage( 'file-log',
95 * $user, $filename
96 * )->inContentLanguage()->text();
97 * @endcode
98 *
99 * Checking whether a message exists:
100 *
101 * @code
102 * wfMessage( 'mysterious-message' )->exists()
103 * // returns a boolean whether the 'mysterious-message' key exist.
104 * @endcode
105 *
106 * If you want to use a different language:
107 *
108 * @code
109 * $userLanguage = $user->getOption( 'language' );
110 * wfMessage( 'email-header' )
111 * ->inLanguage( $userLanguage )
112 * ->plain();
113 * @endcode
114 *
115 * @note You can parse the text only in the content or interface languages
116 *
117 * @section message_compare_old Comparison with old wfMsg* functions:
118 *
119 * Use full parsing:
120 *
121 * @code
122 * // old style:
123 * wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
124 * // new style:
125 * wfMessage( 'key', 'apple' )->parse();
126 * @endcode
127 *
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.
130 *
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
138 *
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
146 *
147 * @section message_appendix Appendix:
148 *
149 * @todo
150 * - test, can we have tests?
151 * - this documentation needs to be extended
152 *
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
156 *
157 * @since 1.17
158 */
159 class Message {
160 /**
161 * In which language to get this message. True, which is the default,
162 * means the current interface language, false content language.
163 */
164 protected $interface = true;
165
166 /**
167 * In which language to get this message. Overrides the $interface
168 * variable.
169 *
170 * @var Language
171 */
172 protected $language = null;
173
174 /**
175 * The message key.
176 */
177 protected $key;
178
179 /**
180 * List of parameters which will be substituted into the message.
181 */
182 protected $parameters = array();
183
184 /**
185 * Format for the message.
186 * Supported formats are:
187 * * text (transform)
188 * * escaped (transform+htmlspecialchars)
189 * * block-parse
190 * * parse (default)
191 * * plain
192 */
193 protected $format = 'parse';
194
195 /**
196 * Whether database can be used.
197 */
198 protected $useDatabase = true;
199
200 /**
201 * Title object to use as context
202 */
203 protected $title = null;
204
205 /**
206 * Content object representing the message
207 */
208 protected $content = null;
209
210 /**
211 * @var string
212 */
213 protected $message;
214
215 /**
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
221 */
222 public function __construct( $key, $params = array() ) {
223 global $wgLang;
224 $this->key = $key;
225 $this->parameters = array_values( $params );
226 $this->language = $wgLang;
227 }
228
229 /**
230 * Returns the message key
231 *
232 * @since 1.21
233 *
234 * @return string
235 */
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;
243 }
244 }
245
246 /**
247 * Returns the message parameters
248 *
249 * @since 1.21
250 *
251 * @return string[]
252 */
253 public function getParams() {
254 return $this->parameters;
255 }
256
257 /**
258 * Returns the message format
259 *
260 * @since 1.21
261 *
262 * @return string
263 */
264 public function getFormat() {
265 return $this->format;
266 }
267
268 /**
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
276 */
277 public static function newFromKey( $key /*...*/ ) {
278 $params = func_get_args();
279 array_shift( $params );
280 return new self( $key, $params );
281 }
282
283 /**
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
290 */
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];
300 }
301 }
302 return new self( $keys );
303 }
304
305 /**
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
310 */
311 public function params( /*...*/ ) {
312 $args = func_get_args();
313 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
314 $args = $args[0];
315 }
316 $args_values = array_values( $args );
317 $this->parameters = array_merge( $this->parameters, $args_values );
318 return $this;
319 }
320
321 /**
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
329 */
330 public function rawParams( /*...*/ ) {
331 $params = func_get_args();
332 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
333 $params = $params[0];
334 }
335 foreach ( $params as $param ) {
336 $this->parameters[] = self::rawParam( $param );
337 }
338 return $this;
339 }
340
341 /**
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
347 */
348 public function numParams( /*...*/ ) {
349 $params = func_get_args();
350 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
351 $params = $params[0];
352 }
353 foreach ( $params as $param ) {
354 $this->parameters[] = self::numParam( $param );
355 }
356 return $this;
357 }
358
359 /**
360 * Add parameters that are durations of time and will be passed through
361 * Language::formatDuration before substitution
362 * @since 1.22
363 * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
364 * @return Message: $this
365 */
366 public function durationParams( /*...*/ ) {
367 $params = func_get_args();
368 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
369 $params = $params[0];
370 }
371 foreach ( $params as $param ) {
372 $this->parameters[] = self::durationParam( $param );
373 }
374 return $this;
375 }
376
377 /**
378 * Add parameters that are expiration times and will be passed through
379 * Language::formatExpiry before substitution
380 * @since 1.22
381 * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
382 * @return Message: $this
383 */
384 public function expiryParams( /*...*/ ) {
385 $params = func_get_args();
386 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
387 $params = $params[0];
388 }
389 foreach ( $params as $param ) {
390 $this->parameters[] = self::expiryParam( $param );
391 }
392 return $this;
393 }
394
395 /**
396 * Add parameters that are time periods and will be passed through
397 * Language::formatTimePeriod before substitution
398 * @since 1.22
399 * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
400 * @return Message: $this
401 */
402 public function timeperiodParams( /*...*/ ) {
403 $params = func_get_args();
404 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
405 $params = $params[0];
406 }
407 foreach ( $params as $param ) {
408 $this->parameters[] = self::timeperiodParam( $param );
409 }
410 return $this;
411 }
412
413 /**
414 * Add parameters that are file sizes and will be passed through
415 * Language::formatSize before substitution
416 * @since 1.22
417 * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
418 * @return Message: $this
419 */
420 public function sizeParams( /*...*/ ) {
421 $params = func_get_args();
422 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
423 $params = $params[0];
424 }
425 foreach ( $params as $param ) {
426 $this->parameters[] = self::sizeParam( $param );
427 }
428 return $this;
429 }
430
431 /**
432 * Add parameters that are bitrates and will be passed through
433 * Language::formatBitrate before substitution
434 * @since 1.22
435 * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
436 * @return Message: $this
437 */
438 public function bitrateParams( /*...*/ ) {
439 $params = func_get_args();
440 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
441 $params = $params[0];
442 }
443 foreach ( $params as $param ) {
444 $this->parameters[] = self::bitrateParam( $param );
445 }
446 return $this;
447 }
448
449 /**
450 * Set the language and the title from a context object
451 * @since 1.19
452 * @param $context IContextSource
453 * @return Message: $this
454 */
455 public function setContext( IContextSource $context ) {
456 $this->inLanguage( $context->getLanguage() );
457 $this->title( $context->getTitle() );
458 $this->interface = true;
459
460 return $this;
461 }
462
463 /**
464 * Request the message in any language that is supported.
465 * As a side effect interface message status is unconditionally
466 * turned off.
467 * @since 1.17
468 * @param $lang Mixed: language code or Language object.
469 * @throws MWException
470 * @return Message: $this
471 */
472 public function inLanguage( $lang ) {
473 if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
474 $this->language = $lang;
475 } elseif ( is_string( $lang ) ) {
476 if ( $this->language->getCode() != $lang ) {
477 $this->language = Language::factory( $lang );
478 }
479 } else {
480 $type = gettype( $lang );
481 throw new MWException( __METHOD__ . " must be "
482 . "passed a String or Language object; $type given"
483 );
484 }
485 $this->interface = false;
486 return $this;
487 }
488
489 /**
490 * Request the message in the wiki's content language,
491 * unless it is disabled for this message.
492 * @since 1.17
493 * @see $wgForceUIMsgAsContentMsg
494 * @return Message: $this
495 */
496 public function inContentLanguage() {
497 global $wgForceUIMsgAsContentMsg;
498 if ( in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) {
499 return $this;
500 }
501
502 global $wgContLang;
503 $this->interface = false;
504 $this->language = $wgContLang;
505 return $this;
506 }
507
508 /**
509 * Allows manipulating the interface message flag directly.
510 * Can be used to restore the flag after setting a language.
511 * @param $value bool
512 * @return Message: $this
513 * @since 1.20
514 */
515 public function setInterfaceMessageFlag( $value ) {
516 $this->interface = (bool)$value;
517 return $this;
518 }
519
520 /**
521 * Enable or disable database use.
522 * @since 1.17
523 * @param $value Boolean
524 * @return Message: $this
525 */
526 public function useDatabase( $value ) {
527 $this->useDatabase = (bool)$value;
528 return $this;
529 }
530
531 /**
532 * Set the Title object to use as context when transforming the message
533 * @since 1.18
534 * @param $title Title object
535 * @return Message: $this
536 */
537 public function title( $title ) {
538 $this->title = $title;
539 return $this;
540 }
541
542 /**
543 * Returns the message as a Content object.
544 * @return Content
545 */
546 public function content() {
547 if ( !$this->content ) {
548 $this->content = new MessageContent( $this );
549 }
550
551 return $this->content;
552 }
553
554 /**
555 * Returns the message parsed from wikitext to HTML.
556 * @since 1.17
557 * @return String: HTML
558 */
559 public function toString() {
560 $string = $this->fetchMessage();
561
562 if ( $string === false ) {
563 $key = htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key );
564 if ( $this->format === 'plain' ) {
565 return '<' . $key . '>';
566 }
567 return '&lt;' . $key . '&gt;';
568 }
569
570 # Replace $* with a list of parameters for &uselang=qqx.
571 if ( strpos( $string, '$*' ) !== false ) {
572 $paramlist = '';
573 if ( $this->parameters !== array() ) {
574 $paramlist = ': $' . implode( ', $', range( 1, count( $this->parameters ) ) );
575 }
576 $string = str_replace( '$*', $paramlist, $string );
577 }
578
579 # Replace parameters before text parsing
580 $string = $this->replaceParameters( $string, 'before' );
581
582 # Maybe transform using the full parser
583 if ( $this->format === 'parse' ) {
584 $string = $this->parseText( $string );
585 $m = array();
586 if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
587 $string = $m[1];
588 }
589 } elseif ( $this->format === 'block-parse' ) {
590 $string = $this->parseText( $string );
591 } elseif ( $this->format === 'text' ) {
592 $string = $this->transformText( $string );
593 } elseif ( $this->format === 'escaped' ) {
594 $string = $this->transformText( $string );
595 $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false );
596 }
597
598 # Raw parameter replacement
599 $string = $this->replaceParameters( $string, 'after' );
600
601 return $string;
602 }
603
604 /**
605 * Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg:
606 * $foo = Message::get( $key );
607 * $string = "<abbr>$foo</abbr>";
608 * @since 1.18
609 * @return String
610 */
611 public function __toString() {
612 // PHP doesn't allow __toString to throw exceptions and will
613 // trigger a fatal error if it does. So, catch any exceptions.
614
615 try {
616 return $this->toString();
617 } catch ( Exception $ex ) {
618 try {
619 trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): "
620 . $ex, E_USER_WARNING );
621 } catch ( Exception $ex ) {
622 // Doh! Cause a fatal error after all?
623 }
624
625 if ( $this->format === 'plain' ) {
626 return '<' . $this->key . '>';
627 }
628 return '&lt;' . $this->key . '&gt;';
629 }
630 }
631
632 /**
633 * Fully parse the text from wikitext to HTML
634 * @since 1.17
635 * @return String parsed HTML
636 */
637 public function parse() {
638 $this->format = 'parse';
639 return $this->toString();
640 }
641
642 /**
643 * Returns the message text. {{-transformation is done.
644 * @since 1.17
645 * @return String: Unescaped message text.
646 */
647 public function text() {
648 $this->format = 'text';
649 return $this->toString();
650 }
651
652 /**
653 * Returns the message text as-is, only parameters are substituted.
654 * @since 1.17
655 * @return String: Unescaped untransformed message text.
656 */
657 public function plain() {
658 $this->format = 'plain';
659 return $this->toString();
660 }
661
662 /**
663 * Returns the parsed message text which is always surrounded by a block element.
664 * @since 1.17
665 * @return String: HTML
666 */
667 public function parseAsBlock() {
668 $this->format = 'block-parse';
669 return $this->toString();
670 }
671
672 /**
673 * Returns the message text. {{-transformation is done and the result
674 * is escaped excluding any raw parameters.
675 * @since 1.17
676 * @return String: Escaped message text.
677 */
678 public function escaped() {
679 $this->format = 'escaped';
680 return $this->toString();
681 }
682
683 /**
684 * Check whether a message key has been defined currently.
685 * @since 1.17
686 * @return Bool: true if it is and false if not.
687 */
688 public function exists() {
689 return $this->fetchMessage() !== false;
690 }
691
692 /**
693 * Check whether a message does not exist, or is an empty string
694 * @since 1.18
695 * @return Bool: true if is is and false if not
696 * @todo FIXME: Merge with isDisabled()?
697 */
698 public function isBlank() {
699 $message = $this->fetchMessage();
700 return $message === false || $message === '';
701 }
702
703 /**
704 * Check whether a message does not exist, is an empty string, or is "-"
705 * @since 1.18
706 * @return Bool: true if it is and false if not
707 */
708 public function isDisabled() {
709 $message = $this->fetchMessage();
710 return $message === false || $message === '' || $message === '-';
711 }
712
713 /**
714 * @since 1.17
715 * @param $value
716 * @return array
717 */
718 public static function rawParam( $value ) {
719 return array( 'raw' => $value );
720 }
721
722 /**
723 * @since 1.18
724 * @param $value
725 * @return array
726 */
727 public static function numParam( $value ) {
728 return array( 'num' => $value );
729 }
730
731 /**
732 * @since 1.22
733 * @param $value
734 * @return array
735 */
736 public static function durationParam( $value ) {
737 return array( 'duration' => $value );
738 }
739
740 /**
741 * @since 1.22
742 * @param $value
743 * @return array
744 */
745 public static function expiryParam( $value ) {
746 return array( 'expiry' => $value );
747 }
748
749 /**
750 * @since 1.22
751 * @param $value
752 * @return array
753 */
754 public static function timeperiodParam( $value ) {
755 return array( 'period' => $value );
756 }
757
758 /**
759 * @since 1.22
760 * @param $value
761 * @return array
762 */
763 public static function sizeParam( $value ) {
764 return array( 'size' => $value );
765 }
766
767 /**
768 * @since 1.22
769 * @param $value
770 * @return array
771 */
772 public static function bitrateParam( $value ) {
773 return array( 'bitrate' => $value );
774 }
775
776 /**
777 * Substitutes any parameters into the message text.
778 * @since 1.17
779 * @param string $message the message text
780 * @param string $type either before or after
781 * @return String
782 */
783 protected function replaceParameters( $message, $type = 'before' ) {
784 $replacementKeys = array();
785 foreach ( $this->parameters as $n => $param ) {
786 list( $paramType, $value ) = $this->extractParam( $param );
787 if ( $type === $paramType ) {
788 $replacementKeys['$' . ( $n + 1 )] = $value;
789 }
790 }
791 $message = strtr( $message, $replacementKeys );
792 return $message;
793 }
794
795 /**
796 * Extracts the parameter type and preprocessed the value if needed.
797 * @since 1.18
798 * @param string|array $param Parameter as defined in this class.
799 * @return Tuple(type, value)
800 */
801 protected function extractParam( $param ) {
802 if ( is_array( $param ) ) {
803 if ( isset( $param['raw'] ) ) {
804 return array( 'after', $param['raw'] );
805 } elseif ( isset( $param['num'] ) ) {
806 // Replace number params always in before step for now.
807 // No support for combined raw and num params
808 return array( 'before', $this->language->formatNum( $param['num'] ) );
809 } elseif ( isset( $param['duration'] ) ) {
810 return array( 'before', $this->language->formatDuration( $param['duration'] ) );
811 } elseif ( isset( $param['expiry'] ) ) {
812 return array( 'before', $this->language->formatExpiry( $param['expiry'] ) );
813 } elseif ( isset( $param['period'] ) ) {
814 return array( 'before', $this->language->formatTimePeriod( $param['period'] ) );
815 } elseif ( isset( $param['size'] ) ) {
816 return array( 'before', $this->language->formatSize( $param['size'] ) );
817 } elseif ( isset( $param['bitrate'] ) ) {
818 return array( 'before', $this->language->formatBitrate( $param['bitrate'] ) );
819 } else {
820 trigger_error(
821 "Invalid message parameter: " . htmlspecialchars( serialize( $param ) ),
822 E_USER_WARNING
823 );
824 return array( 'before', '[INVALID]' );
825 }
826 } elseif ( $param instanceof Message ) {
827 // Message objects should not be before parameters because
828 // then they'll get double escaped. If the message needs to be
829 // escaped, it'll happen right here when we call toString().
830 return array( 'after', $param->toString() );
831 } else {
832 return array( 'before', $param );
833 }
834 }
835
836 /**
837 * Wrapper for what ever method we use to parse wikitext.
838 * @since 1.17
839 * @param string $string Wikitext message contents
840 * @return string Wikitext parsed into HTML
841 */
842 protected function parseText( $string ) {
843 $out = MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language );
844 return is_object( $out ) ? $out->getText() : $out;
845 }
846
847 /**
848 * Wrapper for what ever method we use to {{-transform wikitext.
849 * @since 1.17
850 * @param string $string Wikitext message contents
851 * @return string Wikitext with {{-constructs replaced with their values.
852 */
853 protected function transformText( $string ) {
854 return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title );
855 }
856
857 /**
858 * Wrapper for what ever method we use to get message contents
859 * @since 1.17
860 * @throws MWException
861 * @return string
862 */
863 protected function fetchMessage() {
864 if ( !isset( $this->message ) ) {
865 $cache = MessageCache::singleton();
866 if ( is_array( $this->key ) ) {
867 if ( !count( $this->key ) ) {
868 throw new MWException( "Given empty message key array." );
869 }
870 foreach ( $this->key as $key ) {
871 $message = $cache->get( $key, $this->useDatabase, $this->language );
872 if ( $message !== false && $message !== '' ) {
873 break;
874 }
875 }
876 $this->message = $message;
877 } else {
878 $this->message = $cache->get( $this->key, $this->useDatabase, $this->language );
879 }
880 }
881 return $this->message;
882 }
883
884 }
885
886 /**
887 * Variant of the Message class.
888 *
889 * Rather than treating the message key as a lookup
890 * value (which is passed to the MessageCache and
891 * translated as necessary), a RawMessage key is
892 * treated as the actual message.
893 *
894 * All other functionality (parsing, escaping, etc.)
895 * is preserved.
896 *
897 * @since 1.21
898 */
899 class RawMessage extends Message {
900 /**
901 * Call the parent constructor, then store the key as
902 * the message.
903 *
904 * @param string $key Message to use
905 * @param array $params Parameters for the message
906 * @see Message::__construct
907 */
908 public function __construct( $key, $params = array() ) {
909 parent::__construct( $key, $params );
910 // The key is the message.
911 $this->message = $key;
912 }
913
914 /**
915 * Fetch the message (in this case, the key).
916 *
917 * @return string
918 */
919 public function fetchMessage() {
920 // Just in case the message is unset somewhere.
921 if ( !isset( $this->message ) ) {
922 $this->message = $this->key;
923 }
924 return $this->message;
925 }
926 }