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