Update the Chinese conversion tables.
[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', $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' )->inLanguage( $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', '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', '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 * Format for the message.
85 * Supported formats are:
86 * * text (transform)
87 * * escaped (transform+htmlspecialchars)
88 * * block-parse
89 * * parse (default)
90 * * plain
91 */
92 protected $format = 'parse';
93
94 /**
95 * Whether database can be used.
96 */
97 protected $useDatabase = true;
98
99 /**
100 * Constructor.
101 * @param $key String: message key
102 * @param $params Array message parameters
103 * @return Message: $this
104 */
105 public function __construct( $key, $params = array() ) {
106 $this->key = $key;
107 $this->parameters = array_values( $params );
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 key( $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 $this->parameters = array_merge( $this->parameters, array_values( func_get_args() ) );
131 return $this;
132 }
133
134 /**
135 * Add parameters that are substituted after parsing or escaping.
136 * In other words the parsing process cannot access the contents
137 * of this type of parameter, and you need to make sure it is
138 * sanitized beforehand. The parser will see "$n", instead.
139 * @param Varargs: raw parameters as Strings
140 * @return Message: $this
141 */
142 public function rawParams( /*...*/ ) {
143 $params = func_get_args();
144 foreach( $params as $param ) {
145 $this->parameters[] = self::rawParam( $param );
146 }
147 return $this;
148 }
149
150 /**
151 * Request the message in any language that is supported.
152 * As a side effect interface message status is unconditionally
153 * turned off.
154 * @param $lang Mixed: language code or Language object.
155 * @return Message: $this
156 */
157 public function inLanguage( $lang ) {
158 if( $lang instanceof Language ){
159 $this->language = $lang;
160 } elseif ( is_string( $lang ) ) {
161 $this->language = Language::factory( $lang );
162 } else {
163 $type = gettype( $lang );
164 throw new MWException( __METHOD__ . " must be "
165 . "passed a String or Language object; $type given"
166 );
167 }
168 $this->interface = false;
169 return $this;
170 }
171
172 /**
173 * Request the message in the wiki's content language.
174 * @return Message: $this
175 */
176 public function inContentLanguage() {
177 $this->interface = false;
178 $this->language = null;
179 return $this;
180 }
181
182 /**
183 * Enable or disable database use.
184 * @param $value Boolean
185 * @return Message: $this
186 */
187 public function useDatabase( $value ) {
188 $this->useDatabase = (bool) $value;
189 return $this;
190 }
191
192 /**
193 * Returns the message parsed from wikitext to HTML.
194 * TODO: in PHP >= 5.2.0, we can make this a magic method,
195 * and then we can do, eg:
196 * $foo = Message::get($key);
197 * $string = "<abbr>$foo</abbr>";
198 * But we shouldn't implement that while MediaWiki still supports
199 * PHP < 5.2; or people will start using it...
200 * @return String: HTML
201 */
202 public function toString() {
203 $string = $this->getMessageText();
204
205 # Replace parameters before text parsing
206 $string = $this->replaceParameters( $string, 'before' );
207
208 # Maybe transform using the full parser
209 if( $this->format === 'parse' ) {
210 $string = $this->parseText( $string );
211 $m = array();
212 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
213 $string = $m[1];
214 }
215 } elseif( $this->format === 'block-parse' ){
216 $string = $this->parseText( $string );
217 } elseif( $this->format === 'text' ){
218 $string = $this->transformText( $string );
219 } elseif( $this->format === 'escaped' ){
220 # FIXME: Sanitizer method here?
221 $string = $this->transformText( $string );
222 $string = htmlspecialchars( $string );
223 }
224
225 # Raw parameter replacement
226 $string = $this->replaceParameters( $string, 'after' );
227
228 return $string;
229 }
230
231 /**
232 * Fully parse the text from wikitext to HTML
233 * @return String parsed HTML
234 */
235 public function parse() {
236 $this->format = 'parse';
237 return $this->toString();
238 }
239
240 /**
241 * Returns the message text. {{-transformation is done.
242 * @return String: Unescaped message text.
243 */
244 public function text() {
245 $this->format = 'text';
246 return $this->toString();
247 }
248
249 /**
250 * Returns the message text as-is, only parameters are subsituted.
251 * @return String: Unescaped untransformed message text.
252 */
253 public function plain() {
254 $this->format = 'plain';
255 return $this->toString();
256 }
257
258 /**
259 * Returns the parsed message text which is always surrounded by a block element.
260 * @return String: HTML
261 */
262 public function parseAsBlock() {
263 $this->format = 'block-parse';
264 return $this->toString();
265 }
266
267 /**
268 * Returns the message text. {{-transformation is done and the result
269 * is excaped excluding any raw parameters.
270 * @return String: Escaped message text.
271 */
272 public function escaped() {
273 $this->format = 'escaped';
274 return $this->toString();
275 }
276
277 /**
278 * Check whether a message key has been defined currently.
279 * @return Bool: true if it is and false if not.
280 */
281 public function exists() {
282 return $this->fetchMessage() !== false;
283 }
284
285 public static function rawParam( $value ) {
286 return array( 'raw' => $value );
287 }
288
289 /**
290 * Substitutes any paramaters into the message text.
291 * @param $message String, the message text
292 * @param $type String: either before or after
293 * @return String
294 */
295 protected function replaceParameters( $message, $type = 'before' ) {
296 $replacementKeys = array();
297 foreach( $this->parameters as $n => $param ) {
298 if ( $type === 'before' && !is_array( $param ) ) {
299 $replacementKeys['$' . ($n + 1)] = $param;
300 } elseif ( $type === 'after' && isset( $param['raw'] ) ) {
301 $replacementKeys['$' . ($n + 1)] = $param['raw'];
302 }
303 }
304 $message = strtr( $message, $replacementKeys );
305 return $message;
306 }
307
308 /**
309 * Wrapper for what ever method we use to parse wikitext.
310 * @param $string String: Wikitext message contents
311 * @return Wikitext parsed into HTML
312 */
313 protected function parseText( $string ) {
314 global $wgOut;
315 if ( $this->language !== null ) {
316 # FIXME: remove this limitation
317 throw new MWException( 'Can only parse in interface or content language' );
318 }
319 return $wgOut->parse( $string, /*linestart*/true, $this->interface );
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 }