| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\Language; |
| 22 | |
| 23 | use MediaWiki\Message\Message; |
| 24 | |
| 25 | /** |
| 26 | * Variant of the Message class. |
| 27 | * |
| 28 | * Rather than treating the message key as a lookup |
| 29 | * value (which is passed to the MessageCache and |
| 30 | * translated as necessary), a RawMessage key is |
| 31 | * treated as the actual message. |
| 32 | * |
| 33 | * All other functionality (parsing, escaping, etc.) |
| 34 | * is preserved. |
| 35 | * |
| 36 | * @newable |
| 37 | * @since 1.21 |
| 38 | */ |
| 39 | class RawMessage extends Message { |
| 40 | |
| 41 | /** |
| 42 | * Call the parent constructor, then store the key as |
| 43 | * the message. |
| 44 | * |
| 45 | * @stable to call |
| 46 | * @see Message::__construct |
| 47 | * |
| 48 | * @param string $text Message to use. |
| 49 | * @param array $params Parameters for the message. |
| 50 | */ |
| 51 | public function __construct( string $text, $params = [] ) { |
| 52 | parent::__construct( $text, $params ); |
| 53 | |
| 54 | // The key is the message. |
| 55 | $this->message = $text; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Fetch the message (in this case, the key). |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public function fetchMessage() { |
| 64 | // Just in case the message is unset somewhere. |
| 65 | $this->message ??= $this->key; |
| 66 | |
| 67 | return $this->message; |
| 68 | } |
| 69 | |
| 70 | public function getTextOfRawMessage() { |
| 71 | return $this->key; |
| 72 | } |
| 73 | |
| 74 | public function getParamsOfRawMessage() { |
| 75 | return $this->parameters; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * To conform to the MessageSpecifier interface, always return 'rawmessage', |
| 80 | * which is a real message key that can be used with MessageValue and other classes. |
| 81 | * @return string |
| 82 | */ |
| 83 | public function getKey() { |
| 84 | return 'rawmessage'; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * To conform to the MessageSpecifier interface, return parameters that are valid with the |
| 89 | * 'rawmessage' message, and can be used with MessageValue and other classes. |
| 90 | * @return string[] |
| 91 | */ |
| 92 | public function getParams() { |
| 93 | // If the provided text is equivalent to 'rawmessage', return the provided params. |
| 94 | if ( $this->key === '$1' ) { |
| 95 | return $this->parameters; |
| 96 | } |
| 97 | // If there are no provided params, return the provided text as the single param. |
| 98 | if ( !$this->parameters ) { |
| 99 | return [ $this->key ]; |
| 100 | } |
| 101 | // As a last resort, substitute the provided params into the single param accepted by |
| 102 | // 'rawmessage'. This may access global state. |
| 103 | return [ $this->plain() ]; |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |
| 108 | /** @deprecated class alias since 1.40 */ |
| 109 | class_alias( RawMessage::class, 'RawMessage' ); |