931a3f9dcc5bdaeb5102e4c6d0be17b3c0af616f
[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 $string = Parser::stripOuterParagraph( $string );
663 } elseif ( $this->format === 'block-parse' ) {
664 $string = $this->parseText( $string );
665 } elseif ( $this->format === 'text' ) {
666 $string = $this->transformText( $string );
667 } elseif ( $this->format === 'escaped' ) {
668 $string = $this->transformText( $string );
669 $string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8', false );
670 }
671
672 # Raw parameter replacement
673 $string = $this->replaceParameters( $string, 'after' );
674
675 return $string;
676 }
677
678 /**
679 * Magic method implementation of the above (for PHP >= 5.2.0), so we can do, eg:
680 * $foo = Message::get( $key );
681 * $string = "<abbr>$foo</abbr>";
682 *
683 * @since 1.18
684 *
685 * @return string
686 */
687 public function __toString() {
688 // PHP doesn't allow __toString to throw exceptions and will
689 // trigger a fatal error if it does. So, catch any exceptions.
690
691 try {
692 return $this->toString();
693 } catch ( Exception $ex ) {
694 try {
695 trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): "
696 . $ex, E_USER_WARNING );
697 } catch ( Exception $ex ) {
698 // Doh! Cause a fatal error after all?
699 }
700
701 if ( $this->format === 'plain' ) {
702 return '<' . $this->key . '>';
703 }
704 return '&lt;' . $this->key . '&gt;';
705 }
706 }
707
708 /**
709 * Fully parse the text from wikitext to HTML.
710 *
711 * @since 1.17
712 *
713 * @return string Parsed HTML.
714 */
715 public function parse() {
716 $this->format = 'parse';
717 return $this->toString();
718 }
719
720 /**
721 * Returns the message text. {{-transformation is done.
722 *
723 * @since 1.17
724 *
725 * @return string Unescaped message text.
726 */
727 public function text() {
728 $this->format = 'text';
729 return $this->toString();
730 }
731
732 /**
733 * Returns the message text as-is, only parameters are substituted.
734 *
735 * @since 1.17
736 *
737 * @return string Unescaped untransformed message text.
738 */
739 public function plain() {
740 $this->format = 'plain';
741 return $this->toString();
742 }
743
744 /**
745 * Returns the parsed message text which is always surrounded by a block element.
746 *
747 * @since 1.17
748 *
749 * @return string HTML
750 */
751 public function parseAsBlock() {
752 $this->format = 'block-parse';
753 return $this->toString();
754 }
755
756 /**
757 * Returns the message text. {{-transformation is done and the result
758 * is escaped excluding any raw parameters.
759 *
760 * @since 1.17
761 *
762 * @return string Escaped message text.
763 */
764 public function escaped() {
765 $this->format = 'escaped';
766 return $this->toString();
767 }
768
769 /**
770 * Check whether a message key has been defined currently.
771 *
772 * @since 1.17
773 *
774 * @return bool
775 */
776 public function exists() {
777 return $this->fetchMessage() !== false;
778 }
779
780 /**
781 * Check whether a message does not exist, or is an empty string
782 *
783 * @since 1.18
784 * @todo FIXME: Merge with isDisabled()?
785 *
786 * @return bool
787 */
788 public function isBlank() {
789 $message = $this->fetchMessage();
790 return $message === false || $message === '';
791 }
792
793 /**
794 * Check whether a message does not exist, is an empty string, or is "-".
795 *
796 * @since 1.18
797 *
798 * @return bool
799 */
800 public function isDisabled() {
801 $message = $this->fetchMessage();
802 return $message === false || $message === '' || $message === '-';
803 }
804
805 /**
806 * @since 1.17
807 *
808 * @param mixed $raw
809 *
810 * @return array Array with a single "raw" key.
811 */
812 public static function rawParam( $raw ) {
813 return array( 'raw' => $raw );
814 }
815
816 /**
817 * @since 1.18
818 *
819 * @param mixed $num
820 *
821 * @return array Array with a single "num" key.
822 */
823 public static function numParam( $num ) {
824 return array( 'num' => $num );
825 }
826
827 /**
828 * @since 1.22
829 *
830 * @param int $duration
831 *
832 * @return int[] Array with a single "duration" key.
833 */
834 public static function durationParam( $duration ) {
835 return array( 'duration' => $duration );
836 }
837
838 /**
839 * @since 1.22
840 *
841 * @param string $expiry
842 *
843 * @return string[] Array with a single "expiry" key.
844 */
845 public static function expiryParam( $expiry ) {
846 return array( 'expiry' => $expiry );
847 }
848
849 /**
850 * @since 1.22
851 *
852 * @param number $period
853 *
854 * @return number[] Array with a single "period" key.
855 */
856 public static function timeperiodParam( $period ) {
857 return array( 'period' => $period );
858 }
859
860 /**
861 * @since 1.22
862 *
863 * @param int $size
864 *
865 * @return int[] Array with a single "size" key.
866 */
867 public static function sizeParam( $size ) {
868 return array( 'size' => $size );
869 }
870
871 /**
872 * @since 1.22
873 *
874 * @param int $bitrate
875 *
876 * @return int[] Array with a single "bitrate" key.
877 */
878 public static function bitrateParam( $bitrate ) {
879 return array( 'bitrate' => $bitrate );
880 }
881
882 /**
883 * Substitutes any parameters into the message text.
884 *
885 * @since 1.17
886 *
887 * @param string $message The message text.
888 * @param string $type Either "before" or "after".
889 *
890 * @return string
891 */
892 protected function replaceParameters( $message, $type = 'before' ) {
893 $replacementKeys = array();
894 foreach ( $this->parameters as $n => $param ) {
895 list( $paramType, $value ) = $this->extractParam( $param );
896 if ( $type === $paramType ) {
897 $replacementKeys['$' . ( $n + 1 )] = $value;
898 }
899 }
900 $message = strtr( $message, $replacementKeys );
901 return $message;
902 }
903
904 /**
905 * Extracts the parameter type and preprocessed the value if needed.
906 *
907 * @since 1.18
908 *
909 * @param mixed $param Parameter as defined in this class.
910 *
911 * @return array Array with the parameter type (either "before" or "after") and the value.
912 */
913 protected function extractParam( $param ) {
914 if ( is_array( $param ) ) {
915 if ( isset( $param['raw'] ) ) {
916 return array( 'after', $param['raw'] );
917 } elseif ( isset( $param['num'] ) ) {
918 // Replace number params always in before step for now.
919 // No support for combined raw and num params
920 return array( 'before', $this->language->formatNum( $param['num'] ) );
921 } elseif ( isset( $param['duration'] ) ) {
922 return array( 'before', $this->language->formatDuration( $param['duration'] ) );
923 } elseif ( isset( $param['expiry'] ) ) {
924 return array( 'before', $this->language->formatExpiry( $param['expiry'] ) );
925 } elseif ( isset( $param['period'] ) ) {
926 return array( 'before', $this->language->formatTimePeriod( $param['period'] ) );
927 } elseif ( isset( $param['size'] ) ) {
928 return array( 'before', $this->language->formatSize( $param['size'] ) );
929 } elseif ( isset( $param['bitrate'] ) ) {
930 return array( 'before', $this->language->formatBitrate( $param['bitrate'] ) );
931 } else {
932 $warning = 'Invalid parameter for message "' . $this->getKey() . '": ' .
933 htmlspecialchars( serialize( $param ) );
934 trigger_error( $warning, E_USER_WARNING );
935 $e = new Exception;
936 wfDebugLog( 'Bug58676', $warning . "\n" . $e->getTraceAsString() );
937
938 return array( 'before', '[INVALID]' );
939 }
940 } elseif ( $param instanceof Message ) {
941 // Message objects should not be before parameters because
942 // then they'll get double escaped. If the message needs to be
943 // escaped, it'll happen right here when we call toString().
944 return array( 'after', $param->toString() );
945 } else {
946 return array( 'before', $param );
947 }
948 }
949
950 /**
951 * Wrapper for what ever method we use to parse wikitext.
952 *
953 * @since 1.17
954 *
955 * @param string $string Wikitext message contents.
956 *
957 * @return string Wikitext parsed into HTML.
958 */
959 protected function parseText( $string ) {
960 $out = MessageCache::singleton()->parse(
961 $string,
962 $this->title,
963 /*linestart*/true,
964 $this->interface,
965 $this->language
966 );
967
968 return $out instanceof ParserOutput ? $out->getText() : $out;
969 }
970
971 /**
972 * Wrapper for what ever method we use to {{-transform wikitext.
973 *
974 * @since 1.17
975 *
976 * @param string $string Wikitext message contents.
977 *
978 * @return string Wikitext with {{-constructs replaced with their values.
979 */
980 protected function transformText( $string ) {
981 return MessageCache::singleton()->transform(
982 $string,
983 $this->interface,
984 $this->language,
985 $this->title
986 );
987 }
988
989 /**
990 * Wrapper for what ever method we use to get message contents.
991 *
992 * @since 1.17
993 *
994 * @return string
995 * @throws MWException If message key array is empty.
996 */
997 protected function fetchMessage() {
998 if ( $this->message === null ) {
999 $cache = MessageCache::singleton();
1000 if ( is_array( $this->key ) ) {
1001 if ( !count( $this->key ) ) {
1002 throw new MWException( "Given empty message key array." );
1003 }
1004 foreach ( $this->key as $key ) {
1005 $message = $cache->get( $key, $this->useDatabase, $this->language );
1006 if ( $message !== false && $message !== '' ) {
1007 break;
1008 }
1009 }
1010 $this->message = $message;
1011 } else {
1012 $this->message = $cache->get( $this->key, $this->useDatabase, $this->language );
1013 }
1014 }
1015 return $this->message;
1016 }
1017
1018 }
1019
1020 /**
1021 * Variant of the Message class.
1022 *
1023 * Rather than treating the message key as a lookup
1024 * value (which is passed to the MessageCache and
1025 * translated as necessary), a RawMessage key is
1026 * treated as the actual message.
1027 *
1028 * All other functionality (parsing, escaping, etc.)
1029 * is preserved.
1030 *
1031 * @since 1.21
1032 */
1033 class RawMessage extends Message {
1034
1035 /**
1036 * Call the parent constructor, then store the key as
1037 * the message.
1038 *
1039 * @see Message::__construct
1040 *
1041 * @param string|string[] $key Message to use.
1042 * @param array $params Parameters for the message.
1043 */
1044 public function __construct( $key, $params = array() ) {
1045 parent::__construct( $key, $params );
1046 // The key is the message.
1047 $this->message = $key;
1048 }
1049
1050 /**
1051 * Fetch the message (in this case, the key).
1052 *
1053 * @return string
1054 */
1055 public function fetchMessage() {
1056 // Just in case the message is unset somewhere.
1057 if ( $this->message === null ) {
1058 $this->message = $this->key;
1059 }
1060 return $this->message;
1061 }
1062
1063 }