Fix syntax terror from r79884
[lhc/web/wiklou.git] / includes / Message.php
1 <?php
2 /**
3 * This class provides methods for fetching interface messages and
4 * processing them into variety of formats that are needed in MediaWiki.
5 *
6 * It is intented to replace the old wfMsg* functions that over time grew
7 * unusable.
8 *
9 * Examples:
10 * Fetching a message text for interface message
11 * $button = Xml::button( wfMessage( 'submit' )->text() );
12 * </pre>
13 * Messages can have parameters:
14 * wfMessage( 'welcome-to' )->params( $wgSitename )->text();
15 * {{GRAMMAR}} and friends work correctly
16 * wfMessage( 'are-friends', $user, $friend );
17 * wfMessage( 'bad-message' )->rawParams( '<script>...</script>' )->escaped();
18 * </pre>
19 * Sometimes the message text ends up in the database, so content language is needed.
20 * wfMessage( 'file-log', $user, $filename )->inContentLanguage()->text()
21 * </pre>
22 * Checking if message exists:
23 * wfMessage( 'mysterious-message' )->exists()
24 * </pre>
25 * If you want to use a different language:
26 * wfMessage( 'email-header' )->inLanguage( $user->getOption( 'language' ) )->plain()
27 * Note that you cannot parse the text except in the content or interface
28 * languages
29 * </pre>
30 *
31 *
32 * Comparison with old wfMsg* functions:
33 *
34 * Use full parsing.
35 * wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
36 * === wfMessage( 'key', 'apple' )->parse();
37 * </pre>
38 * Parseinline is used because it is more useful when pre-building html.
39 * In normal use it is better to use OutputPage::(add|wrap)WikiMsg.
40 *
41 * Places where html cannot be used. {{-transformation is done.
42 * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
43 * === wfMessage( 'key', 'apple', 'pear' )->text();
44 * </pre>
45 *
46 * Shortcut for escaping the message too, similar to wfMsgHTML, but
47 * parameters are not replaced after escaping by default.
48 * $escaped = wfMessage( 'key' )->rawParams( 'apple' )->escaped();
49 * </pre>
50 *
51 * TODO:
52 * - test, can we have tests?
53 * - sort out the details marked with fixme
54 *
55 * @since 1.17
56 * @author Niklas Laxström
57 */
58 class Message {
59 /**
60 * In which language to get this message. True, which is the default,
61 * means the current interface language, false content language.
62 */
63 protected $interface = true;
64
65 /**
66 * In which language to get this message. Overrides the $interface
67 * variable.
68 */
69 protected $language = null;
70
71 /**
72 * The message key.
73 */
74 protected $key;
75
76 /**
77 * List of parameters which will be substituted into the message.
78 */
79 protected $parameters = array();
80
81 /**
82 * Format for the message.
83 * Supported formats are:
84 * * text (transform)
85 * * escaped (transform+htmlspecialchars)
86 * * block-parse
87 * * parse (default)
88 * * plain
89 */
90 protected $format = 'parse';
91
92 /**
93 * Whether database can be used.
94 */
95 protected $useDatabase = true;
96
97 /**
98 * Constructor.
99 * @param $key String: message key
100 * @param $params Array message parameters
101 * @return Message: $this
102 */
103 public function __construct( $key, $params = array() ) {
104 global $wgLang;
105 $this->key = $key;
106 $this->parameters = array_values( $params );
107 $this->language = $wgLang;
108 }
109
110 /**
111 * Factory function that is just wrapper for the real constructor. It is
112 * intented to be used instead of the real constructor, because it allows
113 * chaining method calls, while new objects don't.
114 * @param $key String: message key
115 * @param Varargs: parameters as Strings
116 * @return Message: $this
117 */
118 public static function newFromKey( $key /*...*/ ) {
119 $params = func_get_args();
120 array_shift( $params );
121 return new self( $key, $params );
122 }
123
124 /**
125 * Adds parameters to the parameter list of this message.
126 * @param Varargs: parameters as Strings
127 * @return Message: $this
128 */
129 public function params( /*...*/ ) {
130 $args_values = array_values( func_get_args() );
131 $this->parameters = array_merge( $this->parameters, $args_values );
132 return $this;
133 }
134
135 /**
136 * Add parameters that are substituted after parsing or escaping.
137 * In other words the parsing process cannot access the contents
138 * of this type of parameter, and you need to make sure it is
139 * sanitized beforehand. The parser will see "$n", instead.
140 * @param Varargs: raw parameters as Strings
141 * @return Message: $this
142 */
143 public function rawParams( /*...*/ ) {
144 $params = func_get_args();
145 foreach( $params as $param ) {
146 $this->parameters[] = self::rawParam( $param );
147 }
148 return $this;
149 }
150
151 /**
152 * Request the message in any language that is supported.
153 * As a side effect interface message status is unconditionally
154 * turned off.
155 * @param $lang Mixed: language code or Language object.
156 * @return Message: $this
157 */
158 public function inLanguage( $lang ) {
159 if( $lang instanceof Language ){
160 $this->language = $lang;
161 } elseif ( is_string( $lang ) ) {
162 if( $this->language->getCode() != $lang ) {
163 $this->language = Language::factory( $lang );
164 }
165 } else {
166 $type = gettype( $lang );
167 throw new MWException( __METHOD__ . " must be "
168 . "passed a String or Language object; $type given"
169 );
170 }
171 $this->interface = false;
172 return $this;
173 }
174
175 /**
176 * Request the message in the wiki's content language.
177 * @return Message: $this
178 */
179 public function inContentLanguage() {
180 global $wgContLang;
181 $this->interface = false;
182 $this->language = $wgContLang;
183 return $this;
184 }
185
186 /**
187 * Enable or disable database use.
188 * @param $value Boolean
189 * @return Message: $this
190 */
191 public function useDatabase( $value ) {
192 $this->useDatabase = (bool) $value;
193 return $this;
194 }
195
196 /**
197 * Returns the message parsed from wikitext to HTML.
198 * TODO: in PHP >= 5.2.0, we can make this a magic method,
199 * and then we can do, eg:
200 * $foo = Message::get($key);
201 * $string = "<abbr>$foo</abbr>";
202 * But we shouldn't implement that while MediaWiki still supports
203 * PHP < 5.2; or people will start using it...
204 * @return String: HTML
205 */
206 public function toString() {
207 $string = $this->getMessageText();
208
209 # Replace parameters before text parsing
210 $string = $this->replaceParameters( $string, 'before' );
211
212 # Maybe transform using the full parser
213 if( $this->format === 'parse' ) {
214 $string = $this->parseText( $string );
215 $m = array();
216 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
217 $string = $m[1];
218 }
219 } elseif( $this->format === 'block-parse' ){
220 $string = $this->parseText( $string );
221 } elseif( $this->format === 'text' ){
222 $string = $this->transformText( $string );
223 } elseif( $this->format === 'escaped' ){
224 # FIXME: Sanitizer method here?
225 $string = $this->transformText( $string );
226 $string = htmlspecialchars( $string );
227 }
228
229 # Raw parameter replacement
230 $string = $this->replaceParameters( $string, 'after' );
231
232 return $string;
233 }
234
235 /**
236 * Fully parse the text from wikitext to HTML
237 * @return String parsed HTML
238 */
239 public function parse() {
240 $this->format = 'parse';
241 return $this->toString();
242 }
243
244 /**
245 * Returns the message text. {{-transformation is done.
246 * @return String: Unescaped message text.
247 */
248 public function text() {
249 $this->format = 'text';
250 return $this->toString();
251 }
252
253 /**
254 * Returns the message text as-is, only parameters are subsituted.
255 * @return String: Unescaped untransformed message text.
256 */
257 public function plain() {
258 $this->format = 'plain';
259 return $this->toString();
260 }
261
262 /**
263 * Returns the parsed message text which is always surrounded by a block element.
264 * @return String: HTML
265 */
266 public function parseAsBlock() {
267 $this->format = 'block-parse';
268 return $this->toString();
269 }
270
271 /**
272 * Returns the message text. {{-transformation is done and the result
273 * is escaped excluding any raw parameters.
274 * @return String: Escaped message text.
275 */
276 public function escaped() {
277 $this->format = 'escaped';
278 return $this->toString();
279 }
280
281 /**
282 * Check whether a message key has been defined currently.
283 * @return Bool: true if it is and false if not.
284 */
285 public function exists() {
286 return $this->fetchMessage() !== false;
287 }
288
289 public static function rawParam( $value ) {
290 return array( 'raw' => $value );
291 }
292
293 /**
294 * Substitutes any paramaters into the message text.
295 * @param $message String, the message text
296 * @param $type String: either before or after
297 * @return String
298 */
299 protected function replaceParameters( $message, $type = 'before' ) {
300 $replacementKeys = array();
301 foreach( $this->parameters as $n => $param ) {
302 if ( $type === 'before' && !is_array( $param ) ) {
303 $replacementKeys['$' . ($n + 1)] = $param;
304 } elseif ( $type === 'after' && isset( $param['raw'] ) ) {
305 $replacementKeys['$' . ($n + 1)] = $param['raw'];
306 }
307 }
308 $message = strtr( $message, $replacementKeys );
309 return $message;
310 }
311
312 /**
313 * Wrapper for what ever method we use to parse wikitext.
314 * @param $string String: Wikitext message contents
315 * @return Wikitext parsed into HTML
316 */
317 protected function parseText( $string ) {
318 global $wgOut;
319 return $wgOut->parse( $string, /*linestart*/true, $this->interface, $this->language );
320 }
321
322 /**
323 * Wrapper for what ever method we use to {{-transform wikitext.
324 * @param $string String: Wikitext message contents
325 * @return Wikitext with {{-constructs replaced with their values.
326 */
327 protected function transformText( $string ) {
328 global $wgMessageCache;
329 return $wgMessageCache->transform( $string, $this->interface, $this->language );
330 }
331
332 /**
333 * Returns the textual value for the message.
334 * @return Message contents or placeholder
335 */
336 protected function getMessageText() {
337 $message = $this->fetchMessage();
338 if ( $message === false ) {
339 return '&lt;' . htmlspecialchars( $this->key ) . '&gt;';
340 } else {
341 return $message;
342 }
343 }
344
345 /**
346 * Wrapper for what ever method we use to get message contents
347 */
348 protected function fetchMessage() {
349 if ( !isset( $this->message ) ) {
350 global $wgMessageCache;
351 $this->message = $wgMessageCache->get( $this->key, $this->useDatabase, $this->language );
352 }
353 return $this->message;
354 }
355
356 }