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