Merge "Allow to optionally set language in Message constructor"
[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 /**
162 * In which language to get this message. True, which is the default,
163 * means the current interface language, false content language.
164 *
165 * @var bool
166 */
167 protected $interface = true;
168
169 /**
170 * In which language to get this message. Overrides the $interface
171 * variable.
172 *
173 * @var Language
174 */
175 protected $language = null;
176
177 /**
178 * @var string|string[] The message key or array of keys.
179 */
180 protected $key;
181
182 /**
183 * @var array List of parameters which will be substituted into the message.
184 */
185 protected $parameters = array();
186
187 /**
188 * Format for the message.
189 * Supported formats are:
190 * * text (transform)
191 * * escaped (transform+htmlspecialchars)
192 * * block-parse
193 * * parse (default)
194 * * plain
195 *
196 * @var string
197 */
198 protected $format = 'parse';
199
200 /**
201 * @var bool Whether database can be used.
202 */
203 protected $useDatabase = true;
204
205 /**
206 * @var Title Title object to use as context.
207 */
208 protected $title = null;
209
210 /**
211 * @var Content Content object representing the message.
212 */
213 protected $content = null;
214
215 /**
216 * @var string
217 */
218 protected $message;
219
220 /**
221 * @since 1.17
222 *
223 * @param string|string[] $key Message key or array of message keys to try and use the first
224 * non-empty message for.
225 * @param array $params Message parameters.
226 * @param Language $language Optional language of the message, defaults to $wgLang.
227 */
228 public function __construct( $key, $params = array(), Language $language = null ) {
229 global $wgLang;
230
231 $this->key = $key;
232 $this->parameters = array_values( $params );
233 $this->language = $language ? $language : $wgLang;
234 }
235
236 /**
237 * Returns the message key or the first from an array of message keys.
238 *
239 * @since 1.21
240 *
241 * @return string
242 */
243 public function getKey() {
244 if ( is_array( $this->key ) ) {
245 // May happen if some kind of fallback is applied.
246 // For now, just use the first key. We really need a better solution.
247 return $this->key[0];
248 } else {
249 return $this->key;
250 }
251 }
252
253 /**
254 * Returns the message parameters.
255 *
256 * @since 1.21
257 *
258 * @return array
259 */
260 public function getParams() {
261 return $this->parameters;
262 }
263
264 /**
265 * Returns the message format.
266 *
267 * @since 1.21
268 *
269 * @return string
270 */
271 public function getFormat() {
272 return $this->format;
273 }
274
275 /**
276 * Factory function that is just wrapper for the real constructor. It is
277 * intended to be used instead of the real constructor, because it allows
278 * chaining method calls, while new objects don't.
279 *
280 * @since 1.17
281 *
282 * @param string|string[] $key Message key or array of keys.
283 * @param mixed [$param,...] Parameters as strings.
284 *
285 * @return Message
286 */
287 public static function newFromKey( $key /*...*/ ) {
288 $params = func_get_args();
289 array_shift( $params );
290 return new self( $key, $params );
291 }
292
293 /**
294 * Factory function accepting multiple message keys and returning a message instance
295 * for the first message which is non-empty. If all messages are empty then an
296 * instance of the first message key is returned.
297 *
298 * @since 1.18
299 *
300 * @param string|string[] [$keys,...] Message keys, or first argument as an array of all the
301 * message keys.
302 *
303 * @return Message
304 */
305 public static function newFallbackSequence( /*...*/ ) {
306 $keys = func_get_args();
307 if ( func_num_args() == 1 ) {
308 if ( is_array( $keys[0] ) ) {
309 // Allow an array to be passed as the first argument instead
310 $keys = array_values( $keys[0] );
311 } else {
312 // Optimize a single string to not need special fallback handling
313 $keys = $keys[0];
314 }
315 }
316 return new self( $keys );
317 }
318
319 /**
320 * Adds parameters to the parameter list of this message.
321 *
322 * @since 1.17
323 *
324 * @param mixed [$params,...] Parameters as strings, or a single argument that is
325 * an array of strings.
326 *
327 * @return Message $this
328 */
329 public function params( /*...*/ ) {
330 $args = func_get_args();
331 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
332 $args = $args[0];
333 }
334 $args_values = array_values( $args );
335 $this->parameters = array_merge( $this->parameters, $args_values );
336 return $this;
337 }
338
339 /**
340 * Add parameters that are substituted after parsing or escaping.
341 * In other words the parsing process cannot access the contents
342 * of this type of parameter, and you need to make sure it is
343 * sanitized beforehand. The parser will see "$n", instead.
344 *
345 * @since 1.17
346 *
347 * @param mixed [$params,...] Raw parameters as strings, or a single argument that is
348 * an array of raw parameters.
349 *
350 * @return Message $this
351 */
352 public function rawParams( /*...*/ ) {
353 $params = func_get_args();
354 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
355 $params = $params[0];
356 }
357 foreach ( $params as $param ) {
358 $this->parameters[] = self::rawParam( $param );
359 }
360 return $this;
361 }
362
363 /**
364 * Add parameters that are numeric and will be passed through
365 * Language::formatNum before substitution
366 *
367 * @since 1.18
368 *
369 * @param mixed [$param,...] Numeric parameters, or a single argument that is
370 * an array of numeric parameters.
371 *
372 * @return Message $this
373 */
374 public function numParams( /*...*/ ) {
375 $params = func_get_args();
376 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
377 $params = $params[0];
378 }
379 foreach ( $params as $param ) {
380 $this->parameters[] = self::numParam( $param );
381 }
382 return $this;
383 }
384
385 /**
386 * Add parameters that are durations of time and will be passed through
387 * Language::formatDuration before substitution
388 *
389 * @since 1.22
390 *
391 * @param int|int[] [$param,...] Duration parameters, or a single argument that is
392 * an array of duration parameters.
393 *
394 * @return Message $this
395 */
396 public function durationParams( /*...*/ ) {
397 $params = func_get_args();
398 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
399 $params = $params[0];
400 }
401 foreach ( $params as $param ) {
402 $this->parameters[] = self::durationParam( $param );
403 }
404 return $this;
405 }
406
407 /**
408 * Add parameters that are expiration times and will be passed through
409 * Language::formatExpiry before substitution
410 *
411 * @since 1.22
412 *
413 * @param string|string[] [$param,...] Expiry parameters, or a single argument that is
414 * an array of expiry parameters.
415 *
416 * @return Message $this
417 */
418 public function expiryParams( /*...*/ ) {
419 $params = func_get_args();
420 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
421 $params = $params[0];
422 }
423 foreach ( $params as $param ) {
424 $this->parameters[] = self::expiryParam( $param );
425 }
426 return $this;
427 }
428
429 /**
430 * Add parameters that are time periods and will be passed through
431 * Language::formatTimePeriod before substitution
432 *
433 * @since 1.22
434 *
435 * @param number|number[] [$param,...] Time period parameters, or a single argument that is
436 * an array of time period parameters.
437 *
438 * @return Message $this
439 */
440 public function timeperiodParams( /*...*/ ) {
441 $params = func_get_args();
442 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
443 $params = $params[0];
444 }
445 foreach ( $params as $param ) {
446 $this->parameters[] = self::timeperiodParam( $param );
447 }
448 return $this;
449 }
450
451 /**
452 * Add parameters that are file sizes and will be passed through
453 * Language::formatSize before substitution
454 *
455 * @since 1.22
456 *
457 * @param int|int[] [$param,...] Size parameters, or a single argument that is
458 * an array of size parameters.
459 *
460 * @return Message $this
461 */
462 public function sizeParams( /*...*/ ) {
463 $params = func_get_args();
464 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
465 $params = $params[0];
466 }
467 foreach ( $params as $param ) {
468 $this->parameters[] = self::sizeParam( $param );
469 }
470 return $this;
471 }
472
473 /**
474 * Add parameters that are bitrates and will be passed through
475 * Language::formatBitrate before substitution
476 *
477 * @since 1.22
478 *
479 * @param int|int[] [$param,...] Bit rate parameters, or a single argument that is
480 * an array of bit rate parameters.
481 *
482 * @return Message $this
483 */
484 public function bitrateParams( /*...*/ ) {
485 $params = func_get_args();
486 if ( isset( $params[0] ) && is_array( $params[0] ) ) {
487 $params = $params[0];
488 }
489 foreach ( $params as $param ) {
490 $this->parameters[] = self::bitrateParam( $param );
491 }
492 return $this;
493 }
494
495 /**
496 * Set the language and the title from a context object
497 *
498 * @since 1.19
499 *
500 * @param $context IContextSource
501 *
502 * @return Message $this
503 */
504 public function setContext( IContextSource $context ) {
505 $this->inLanguage( $context->getLanguage() );
506 $this->title( $context->getTitle() );
507 $this->interface = true;
508
509 return $this;
510 }
511
512 /**
513 * Request the message in any language that is supported.
514 * As a side effect interface message status is unconditionally
515 * turned off.
516 *
517 * @since 1.17
518 *
519 * @param Language|string $lang Language code or Language object.
520 *
521 * @return Message $this
522 * @throws MWException
523 */
524 public function inLanguage( $lang ) {
525 if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
526 $this->language = $lang;
527 } elseif ( is_string( $lang ) ) {
528 if ( $this->language->getCode() != $lang ) {
529 $this->language = Language::factory( $lang );
530 }
531 } else {
532 $type = gettype( $lang );
533 throw new MWException( __METHOD__ . " must be "
534 . "passed a String or Language object; $type given"
535 );
536 }
537 $this->interface = false;
538 return $this;
539 }
540
541 /**
542 * Request the message in the wiki's content language,
543 * unless it is disabled for this message.
544 *
545 * @since 1.17
546 * @see $wgForceUIMsgAsContentMsg
547 *
548 * @return Message $this
549 */
550 public function inContentLanguage() {
551 global $wgForceUIMsgAsContentMsg;
552 if ( in_array( $this->key, (array)$wgForceUIMsgAsContentMsg ) ) {
553 return $this;
554 }
555
556 global $wgContLang;
557 $this->interface = false;
558 $this->language = $wgContLang;
559 return $this;
560 }
561
562 /**
563 * Allows manipulating the interface message flag directly.
564 * Can be used to restore the flag after setting a language.
565 *
566 * @since 1.20
567 *
568 * @param bool $interface
569 *
570 * @return Message $this
571 */
572 public function setInterfaceMessageFlag( $interface ) {
573 $this->interface = (bool)$interface;
574 return $this;
575 }
576
577 /**
578 * Enable or disable database use.
579 *
580 * @since 1.17
581 *
582 * @param bool $useDatabase
583 *
584 * @return Message $this
585 */
586 public function useDatabase( $useDatabase ) {
587 $this->useDatabase = (bool)$useDatabase;
588 return $this;
589 }
590
591 /**
592 * Set the Title object to use as context when transforming the message
593 *
594 * @since 1.18
595 *
596 * @param $title Title object
597 *
598 * @return Message $this
599 */
600 public function title( $title ) {
601 $this->title = $title;
602 return $this;
603 }
604
605 /**
606 * Returns the message as a Content object.
607 *
608 * @return Content
609 */
610 public function content() {
611 if ( !$this->content ) {
612 $this->content = new MessageContent( $this );
613 }
614
615 return $this->content;
616 }
617
618 /**
619 * Returns the message parsed from wikitext to HTML.
620 *
621 * @since 1.17
622 *
623 * @return string HTML
624 */
625 public function toString() {
626 $string = $this->fetchMessage();
627
628 if ( $string === false ) {
629 $key = htmlspecialchars( is_array( $this->key ) ? $this->key[0] : $this->key );
630 if ( $this->format === 'plain' ) {
631 return '<' . $key . '>';
632 }
633 return '&lt;' . $key . '&gt;';
634 }
635
636 # Replace $* with a list of parameters for &uselang=qqx.
637 if ( strpos( $string, '$*' ) !== false ) {
638 $paramlist = '';
639 if ( $this->parameters !== array() ) {
640 $paramlist = ': $' . implode( ', $', range( 1, count( $this->parameters ) ) );
641 }
642 $string = str_replace( '$*', $paramlist, $string );
643 }
644
645 # Replace parameters before text parsing
646 $string = $this->replaceParameters( $string, 'before' );
647
648 # Maybe transform using the full parser
649 if ( $this->format === 'parse' ) {
650 $string = $this->parseText( $string );
651 $m = array();
652 if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
653 $string = $m[1];
654 }
655 } elseif ( $this->format === 'block-parse' ) {
656 $string = $this->parseText( $string );
657 } elseif ( $this->format === 'text' ) {
658 $string = $this->transformText( $string );
659 } elseif ( $this->format === 'escaped' ) {
660 $string = $this->transformText( $string );
661 $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false );
662 }
663
664 # Raw parameter replacement
665 $string = $this->replaceParameters( $string, 'after' );
666
667 return $string;
668 }
669
670 /**
671 * Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg:
672 * $foo = Message::get( $key );
673 * $string = "<abbr>$foo</abbr>";
674 *
675 * @since 1.18
676 *
677 * @return string
678 */
679 public function __toString() {
680 // PHP doesn't allow __toString to throw exceptions and will
681 // trigger a fatal error if it does. So, catch any exceptions.
682
683 try {
684 return $this->toString();
685 } catch ( Exception $ex ) {
686 try {
687 trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): "
688 . $ex, E_USER_WARNING );
689 } catch ( Exception $ex ) {
690 // Doh! Cause a fatal error after all?
691 }
692
693 if ( $this->format === 'plain' ) {
694 return '<' . $this->key . '>';
695 }
696 return '&lt;' . $this->key . '&gt;';
697 }
698 }
699
700 /**
701 * Fully parse the text from wikitext to HTML.
702 *
703 * @since 1.17
704 *
705 * @return string Parsed HTML.
706 */
707 public function parse() {
708 $this->format = 'parse';
709 return $this->toString();
710 }
711
712 /**
713 * Returns the message text. {{-transformation is done.
714 *
715 * @since 1.17
716 *
717 * @return string Unescaped message text.
718 */
719 public function text() {
720 $this->format = 'text';
721 return $this->toString();
722 }
723
724 /**
725 * Returns the message text as-is, only parameters are substituted.
726 *
727 * @since 1.17
728 *
729 * @return string Unescaped untransformed message text.
730 */
731 public function plain() {
732 $this->format = 'plain';
733 return $this->toString();
734 }
735
736 /**
737 * Returns the parsed message text which is always surrounded by a block element.
738 *
739 * @since 1.17
740 *
741 * @return string HTML
742 */
743 public function parseAsBlock() {
744 $this->format = 'block-parse';
745 return $this->toString();
746 }
747
748 /**
749 * Returns the message text. {{-transformation is done and the result
750 * is escaped excluding any raw parameters.
751 *
752 * @since 1.17
753 *
754 * @return string Escaped message text.
755 */
756 public function escaped() {
757 $this->format = 'escaped';
758 return $this->toString();
759 }
760
761 /**
762 * Check whether a message key has been defined currently.
763 *
764 * @since 1.17
765 *
766 * @return bool
767 */
768 public function exists() {
769 return $this->fetchMessage() !== false;
770 }
771
772 /**
773 * Check whether a message does not exist, or is an empty string
774 *
775 * @since 1.18
776 * @todo FIXME: Merge with isDisabled()?
777 *
778 * @return bool
779 */
780 public function isBlank() {
781 $message = $this->fetchMessage();
782 return $message === false || $message === '';
783 }
784
785 /**
786 * Check whether a message does not exist, is an empty string, or is "-".
787 *
788 * @since 1.18
789 *
790 * @return bool
791 */
792 public function isDisabled() {
793 $message = $this->fetchMessage();
794 return $message === false || $message === '' || $message === '-';
795 }
796
797 /**
798 * @since 1.17
799 *
800 * @param mixed $raw
801 *
802 * @return array Array with a single "raw" key.
803 */
804 public static function rawParam( $raw ) {
805 return array( 'raw' => $raw );
806 }
807
808 /**
809 * @since 1.18
810 *
811 * @param mixed $num
812 *
813 * @return array Array with a single "num" key.
814 */
815 public static function numParam( $num ) {
816 return array( 'num' => $num );
817 }
818
819 /**
820 * @since 1.22
821 *
822 * @param int $duration
823 *
824 * @return int[] Array with a single "duration" key.
825 */
826 public static function durationParam( $duration ) {
827 return array( 'duration' => $duration );
828 }
829
830 /**
831 * @since 1.22
832 *
833 * @param string $expiry
834 *
835 * @return string[] Array with a single "expiry" key.
836 */
837 public static function expiryParam( $expiry ) {
838 return array( 'expiry' => $expiry );
839 }
840
841 /**
842 * @since 1.22
843 *
844 * @param number $period
845 *
846 * @return number[] Array with a single "period" key.
847 */
848 public static function timeperiodParam( $period ) {
849 return array( 'period' => $period );
850 }
851
852 /**
853 * @since 1.22
854 *
855 * @param int $size
856 *
857 * @return int[] Array with a single "size" key.
858 */
859 public static function sizeParam( $size ) {
860 return array( 'size' => $size );
861 }
862
863 /**
864 * @since 1.22
865 *
866 * @param int $bitrate
867 *
868 * @return int[] Array with a single "bitrate" key.
869 */
870 public static function bitrateParam( $bitrate ) {
871 return array( 'bitrate' => $bitrate );
872 }
873
874 /**
875 * Substitutes any parameters into the message text.
876 *
877 * @since 1.17
878 *
879 * @param string $message The message text.
880 * @param string $type Either "before" or "after".
881 *
882 * @return String
883 */
884 protected function replaceParameters( $message, $type = 'before' ) {
885 $replacementKeys = array();
886 foreach ( $this->parameters as $n => $param ) {
887 list( $paramType, $value ) = $this->extractParam( $param );
888 if ( $type === $paramType ) {
889 $replacementKeys['$' . ( $n + 1 )] = $value;
890 }
891 }
892 $message = strtr( $message, $replacementKeys );
893 return $message;
894 }
895
896 /**
897 * Extracts the parameter type and preprocessed the value if needed.
898 *
899 * @since 1.18
900 *
901 * @param mixed $param Parameter as defined in this class.
902 *
903 * @return array Array with the parameter type (either "before" or "after") and the value.
904 */
905 protected function extractParam( $param ) {
906 if ( is_array( $param ) ) {
907 if ( isset( $param['raw'] ) ) {
908 return array( 'after', $param['raw'] );
909 } elseif ( isset( $param['num'] ) ) {
910 // Replace number params always in before step for now.
911 // No support for combined raw and num params
912 return array( 'before', $this->language->formatNum( $param['num'] ) );
913 } elseif ( isset( $param['duration'] ) ) {
914 return array( 'before', $this->language->formatDuration( $param['duration'] ) );
915 } elseif ( isset( $param['expiry'] ) ) {
916 return array( 'before', $this->language->formatExpiry( $param['expiry'] ) );
917 } elseif ( isset( $param['period'] ) ) {
918 return array( 'before', $this->language->formatTimePeriod( $param['period'] ) );
919 } elseif ( isset( $param['size'] ) ) {
920 return array( 'before', $this->language->formatSize( $param['size'] ) );
921 } elseif ( isset( $param['bitrate'] ) ) {
922 return array( 'before', $this->language->formatBitrate( $param['bitrate'] ) );
923 } else {
924 $warning = 'Invalid parameter for message "' . $this->getKey() . '": ' .
925 htmlspecialchars( serialize( $param ) );
926 trigger_error( $warning, E_USER_WARNING );
927 $e = new Exception;
928 wfDebugLog( 'Bug58676', $warning . "\n" . $e->getTraceAsString() );
929
930 return array( 'before', '[INVALID]' );
931 }
932 } elseif ( $param instanceof Message ) {
933 // Message objects should not be before parameters because
934 // then they'll get double escaped. If the message needs to be
935 // escaped, it'll happen right here when we call toString().
936 return array( 'after', $param->toString() );
937 } else {
938 return array( 'before', $param );
939 }
940 }
941
942 /**
943 * Wrapper for what ever method we use to parse wikitext.
944 *
945 * @since 1.17
946 *
947 * @param string $string Wikitext message contents.
948 *
949 * @return string Wikitext parsed into HTML.
950 */
951 protected function parseText( $string ) {
952 $out = MessageCache::singleton()->parse( $string, $this->title, /*linestart*/true, $this->interface, $this->language );
953 return $out instanceof ParserOutput ? $out->getText() : $out;
954 }
955
956 /**
957 * Wrapper for what ever method we use to {{-transform wikitext.
958 *
959 * @since 1.17
960 *
961 * @param string $string Wikitext message contents.
962 *
963 * @return string Wikitext with {{-constructs replaced with their values.
964 */
965 protected function transformText( $string ) {
966 return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title );
967 }
968
969 /**
970 * Wrapper for what ever method we use to get message contents.
971 *
972 * @since 1.17
973 *
974 * @return string
975 * @throws MWException If message key array is empty.
976 */
977 protected function fetchMessage() {
978 if ( !isset( $this->message ) ) {
979 $cache = MessageCache::singleton();
980 if ( is_array( $this->key ) ) {
981 if ( !count( $this->key ) ) {
982 throw new MWException( "Given empty message key array." );
983 }
984 foreach ( $this->key as $key ) {
985 $message = $cache->get( $key, $this->useDatabase, $this->language );
986 if ( $message !== false && $message !== '' ) {
987 break;
988 }
989 }
990 $this->message = $message;
991 } else {
992 $this->message = $cache->get( $this->key, $this->useDatabase, $this->language );
993 }
994 }
995 return $this->message;
996 }
997
998 }
999
1000 /**
1001 * Variant of the Message class.
1002 *
1003 * Rather than treating the message key as a lookup
1004 * value (which is passed to the MessageCache and
1005 * translated as necessary), a RawMessage key is
1006 * treated as the actual message.
1007 *
1008 * All other functionality (parsing, escaping, etc.)
1009 * is preserved.
1010 *
1011 * @since 1.21
1012 */
1013 class RawMessage extends Message {
1014
1015 /**
1016 * Call the parent constructor, then store the key as
1017 * the message.
1018 *
1019 * @see Message::__construct
1020 *
1021 * @param string|string[] $key Message to use.
1022 * @param array $params Parameters for the message.
1023 */
1024 public function __construct( $key, $params = array() ) {
1025 parent::__construct( $key, $params );
1026 // The key is the message.
1027 $this->message = $key;
1028 }
1029
1030 /**
1031 * Fetch the message (in this case, the key).
1032 *
1033 * @return string
1034 */
1035 public function fetchMessage() {
1036 // Just in case the message is unset somewhere.
1037 if ( !isset( $this->message ) ) {
1038 $this->message = $this->key;
1039 }
1040 return $this->message;
1041 }
1042
1043 }