Index: trunk/phase3/docs/hooks.txt |
— | — | @@ -868,6 +868,7 @@ |
869 | 869 | &$useDB: whether or not to look up the message in the database (bool) |
870 | 870 | &$langCode: the language code to get the message for (string) - or - |
871 | 871 | whether to use the content language (true) or site language (false) (bool) |
| 872 | +&$transform: whether or not to expand variables and templates in the message (bool) |
872 | 873 | |
873 | 874 | 'OpenSearchUrls': Called when constructing the OpenSearch description XML. |
874 | 875 | Hooks can alter or append to the array of URLs for search & suggestion formats. |
Index: trunk/phase3/includes/MessageFunctions.php |
— | — | @@ -1,349 +0,0 @@ |
2 | | -<?php |
3 | | -/** |
4 | | - * Definitions of wfMsg and all it's incarnations. $key refers to the unique key |
5 | | - * of the message. Most of the messages are defined in |
6 | | - * $IP/languages/messages/MessagesEn.php with default values. |
7 | | - * |
8 | | - * Some function accept $language as parameter. It can be either be a language |
9 | | - * code, one of the array keys returned by Language::getLanguageNames(); or bool |
10 | | - * in which case true is shortcut for content language and false for interface |
11 | | - * language. |
12 | | - * |
13 | | - * Most functions take parameters for the message in variable argument list |
14 | | - * after defined parameters. |
15 | | - * |
16 | | - * Some functions do "transforming", which means {{..}} items are substituted. |
17 | | - * These include magic words for plural and grammar function. It is important |
18 | | - * to call the right function, so that these function will produce the correct |
19 | | - * results. |
20 | | - * |
21 | | - * To produce correct results two things need to be taken care of. The language |
22 | | - * information must be passed, so that correct language is called for the |
23 | | - * substition. Another thing is that variables must be substited before doing |
24 | | - * this process. |
25 | | - * @file |
26 | | - */ |
27 | | - |
28 | | - |
29 | | -/** |
30 | | - * Equivalent to: wfMsgExt( $key, 'parsemag' ); |
31 | | - * Use cases: Getting interface messages that are later passed to functions that |
32 | | - * escape their input. |
33 | | - * |
34 | | - * Use wfMsgForContent() instead if the message should NOT |
35 | | - * change depending on the user preferences. |
36 | | - */ |
37 | | -function wfMsg( $key ) { |
38 | | - $args = func_get_args(); |
39 | | - array_shift( $args ); |
40 | | - |
41 | | - $message = MessageGetter::get( $key ); |
42 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
43 | | - $message = MessageGetter::transform( $message ); |
44 | | - return $message; |
45 | | -} |
46 | | - |
47 | | -/** |
48 | | - * Equivalent to: wfMsgExt( $key ); |
49 | | - * Use cases: Getting interface messages that are later passed to function that |
50 | | - * parse their contents. Make sure that the function does know the correct |
51 | | - * language. |
52 | | - */ |
53 | | -function wfMsgNoTrans( $key ) { |
54 | | - $args = func_get_args(); |
55 | | - array_shift( $args ); |
56 | | - |
57 | | - $message = MessageGetter::get( $key ); |
58 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
59 | | - return $message; |
60 | | -} |
61 | | - |
62 | | -/** |
63 | | - * Use cases: Message that should NOT change dependent on the language set in |
64 | | - * the user's preferences. This is the case for most text written into logs, as |
65 | | - * well as link targets (such as the name of the copyright policy page) and else |
66 | | - * that goes back into the database. Link titles, on the other hand, should be |
67 | | - * shown in the UI language. |
68 | | - */ |
69 | | -function wfMsgForContent( $key ) { |
70 | | - $args = func_get_args(); |
71 | | - array_shift( $args ); |
72 | | - |
73 | | - $content = MessageGetter::forContentLanguage( $key ); |
74 | | - $message = MessageGetter::get( $key, /*Language*/ $content ); |
75 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
76 | | - $message = MessageGetter::transform( $message, /*Langugage*/ $content ); |
77 | | - return $message; |
78 | | -} |
79 | | - |
80 | | -/** |
81 | | - * Use cases: Messages for content language that are later passed to a function |
82 | | - * that parses it. Make sure the function uses the correct language for parsing. |
83 | | - * Or just for getting the raw message without conversions. |
84 | | - */ |
85 | | -function wfMsgForContentNoTrans( $key ) { |
86 | | - $args = func_get_args(); |
87 | | - array_shift( $args ); |
88 | | - |
89 | | - $content = MessageGetter::forContentLanguage( $key ); |
90 | | - $message = MessageGetter::get( $key, /*Language*/ $content ); |
91 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
92 | | - return $message; |
93 | | -} |
94 | | - |
95 | | -/** |
96 | | - * Use cases: Getting messages when the database is not available. Also used in |
97 | | - * Special:Allmessages. |
98 | | - */ |
99 | | -function wfMsgNoDB( $key ) { |
100 | | - $args = func_get_args(); |
101 | | - array_shift( $args ); |
102 | | - |
103 | | - $message = MessageGetter::get( $key, MessageGetter::LANG_UI, /*DB*/false ); |
104 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
105 | | - $message = MessageGetter::transform( $message ); |
106 | | - return $message; |
107 | | -} |
108 | | - |
109 | | -/** |
110 | | - * Use cases: Getting the unmodified message when database is not available, |
111 | | - * perhaps for later parsing. |
112 | | - */ |
113 | | -function wfMsgNoDBForContent( $key ) { |
114 | | - $args = func_get_args(); |
115 | | - array_shift( $args ); |
116 | | - |
117 | | - $message = MessageGetter::get( $key, /*Language*/ $content, /*DB*/false ); |
118 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
119 | | - return $message; |
120 | | -} |
121 | | - |
122 | | - |
123 | | -/** |
124 | | - * Use cases: Getting messages in different languages. |
125 | | - */ |
126 | | -function wfMsgReal( $key, $args, $useDB = true, $language = MessageGetter::LANG_UI, $transform = true ) { |
127 | | - $message = MessageGetter::get( $key, /*Language*/ $language, /*DB*/ $useDB ); |
128 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
129 | | - if ( $transform ) |
130 | | - $message = MessageGetter::transform( $message, /*Langugage*/ $language ); |
131 | | - return $message; |
132 | | -} |
133 | | - |
134 | | -/** |
135 | | - * Use cases: Getting the message content or empty string if it doesn't exist |
136 | | - * for showing as the default value when editing MediaWiki namespace. |
137 | | - */ |
138 | | -function wfMsgWeirdKey ( $key ) { |
139 | | - $message = MessageGetter::get( $key, MessageGetter::LANG_UI, /*DB*/ false ); |
140 | | - return wfEmptyMsg( $key, $message ) ? '' : $message; |
141 | | -} |
142 | | - |
143 | | - |
144 | | -// Private marked |
145 | | -function wfMsgGetKey( $key, $useDB, $language = MessageGetter::LANG_UI, $transform = true ) { |
146 | | - //wfDeprecated( __METHOD__ ); |
147 | | - $message = MessageGetter::get( $key, $language, $useDB ); |
148 | | - |
149 | | - // Plural and grammar will go wrong here, no arguments replaced |
150 | | - if ( $transform ) { |
151 | | - wfDebug( __METHOD__ . " called with transform = true for key $key\n" ); |
152 | | - $message = MessageGetter::transform( $message, $language ); |
153 | | - } |
154 | | - |
155 | | - return $message; |
156 | | -} |
157 | | - |
158 | | -function wfMsgReplaceArgs( $message, $args ) { |
159 | | - //wfDeprecated( __METHOD__ ); |
160 | | - return MessageGetter::replaceArgs( $message, $args ); |
161 | | -} |
162 | | - |
163 | | -/** |
164 | | - * Return an HTML-escaped version of a message. |
165 | | - * Parameter replacements, if any, are done *after* the HTML-escaping, |
166 | | - * so parameters may contain HTML (eg links or form controls). Be sure |
167 | | - * to pre-escape them if you really do want plaintext, or just wrap |
168 | | - * the whole thing in htmlspecialchars(). |
169 | | - */ |
170 | | -function wfMsgHtml( $key ) { |
171 | | - $args = func_get_args(); |
172 | | - array_shift( $args ); |
173 | | - |
174 | | - $message = MessageGetter::get( $key ); |
175 | | - $message = MessageGetter::escapeHtml( $message, /* Entities */ false ); |
176 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
177 | | - return $message; |
178 | | -} |
179 | | - |
180 | | -/** |
181 | | - * Return an HTML version of message |
182 | | - * Parameter replacements, if any, are done *after* parsing the wiki-text message, |
183 | | - * so parameters may contain HTML (eg links or form controls). Be sure |
184 | | - * to pre-escape them if you really do want plaintext, or just wrap |
185 | | - * the whole thing in htmlspecialchars(). |
186 | | - */ |
187 | | -function wfMsgWikiHtml( $key ) { |
188 | | - $args = func_get_args(); |
189 | | - array_shift( $args ); |
190 | | - |
191 | | - $message = MessageGetter::get( $key ); |
192 | | - $message = MessageGetter::parse( $message ); |
193 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
194 | | - return $message; |
195 | | -} |
196 | | - |
197 | | -/** |
198 | | - * Use cases: When the previous just aren't enough. |
199 | | - * @param $key String: Key of the message |
200 | | - * @param $options Array: Processing rules: |
201 | | - * @param $... Arguments |
202 | | - * <i>parse</i>: parses wikitext to html |
203 | | - * <i>parseinline</i>: parses wikitext to html and removes the surrounding p's added by parser or tidy |
204 | | - * <i>escape</i>: filters message through htmlspecialchars |
205 | | - * <i>escapenoentities</i>: same, but allows entity references like through |
206 | | - * <i>replaceafter</i>: parameters are substituted after parsing or escaping |
207 | | - * <i>parsemag</i>: transform the message using magic phrases |
208 | | - * <i>content</i>: fetch message for content language instead of interface |
209 | | - * <i>language</i>: language code to fetch message for (overriden by <i>content</i>), its behaviour |
210 | | - * with parse, parseinline and parsemag is undefined. |
211 | | - * Behavior for conflicting options (e.g., parse+parseinline) is undefined. |
212 | | - */ |
213 | | -function wfMsgExt( $key, $options ) { |
214 | | - $args = func_get_args(); |
215 | | - array_shift( $args ); |
216 | | - array_shift( $args ); |
217 | | - |
218 | | - if( !is_array($options) ) { |
219 | | - $options = array($options); |
220 | | - } |
221 | | - |
222 | | - $language = MessageGetter::LANG_UI; |
223 | | - |
224 | | - if( in_array('content', $options) ) { |
225 | | - $language = MessageGetter::LANG_CONTENT; |
226 | | - } elseif( array_key_exists('language', $options) ) { |
227 | | - $language = $options['language']; |
228 | | - $validCodes = array_keys( Language::getLanguageNames() ); |
229 | | - if( !in_array($language, $validCodes) ) { |
230 | | - # Fallback to en, instead of whatsever interface language we might have |
231 | | - $language = 'en'; |
232 | | - } |
233 | | - } |
234 | | - |
235 | | - $message = MessageGetter::get( $key, $language ); |
236 | | - |
237 | | - if( !in_array('replaceafter', $options) ) { |
238 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
239 | | - } |
240 | | - |
241 | | - if( in_array('parse', $options) ) { |
242 | | - $message = MessageGetter::parse( $message, $language ); |
243 | | - } elseif ( in_array('parseinline', $options) ) { |
244 | | - $message = MessageGetter::parse( $message, $language, /*inline*/true ); |
245 | | - } elseif ( in_array('parsemag', $options) ) { |
246 | | - $message = MessageGetter::transform( $message, $language ); |
247 | | - } |
248 | | - |
249 | | - if ( in_array('escape', $options) ) { |
250 | | - $message = MessageGetter::escapeHtml( $message, /*allowEntities*/false ); |
251 | | - } elseif ( in_array( 'escapenoentities', $options ) ) { |
252 | | - $message = MessageGetter::escapeHtml( $message ); |
253 | | - } |
254 | | - |
255 | | - if( in_array('replaceafter', $options) ) { |
256 | | - $message = MessageGetter::replaceArgs( $message, $args ); |
257 | | - } |
258 | | - |
259 | | - return $message; |
260 | | -} |
261 | | - |
262 | | -class MessageGetter { |
263 | | - |
264 | | - const LANG_UI = false; |
265 | | - const LANG_CONTENT = true; |
266 | | - |
267 | | - public static function get( $key, $language = self::LANG_UI, $database = true ) { |
268 | | - global $wgMessageCache; |
269 | | - if( !is_object($wgMessageCache) ) { |
270 | | - throw new MWException( "Message cache not initialised\n" ); |
271 | | - } |
272 | | - |
273 | | - wfRunHooks('NormalizeMessageKey', array(&$key, &$database, &$language)); |
274 | | - |
275 | | - $message = $wgMessageCache->get( $key, $database, $language ); |
276 | | - # Fix windows line-endings |
277 | | - # Some messages are split with explode("\n", $msg) |
278 | | - $message = str_replace( "\r", '', $message ); |
279 | | - return $message; |
280 | | - |
281 | | - } |
282 | | - |
283 | | - public static function forContentLanguage( $key ) { |
284 | | - global $wgForceUIMsgAsContentMsg; |
285 | | - if( is_array( $wgForceUIMsgAsContentMsg ) && |
286 | | - in_array( $key, $wgForceUIMsgAsContentMsg ) ) { |
287 | | - return self::LANG_UI; |
288 | | - } else { |
289 | | - return self::LANG_CONTENT; |
290 | | - } |
291 | | - } |
292 | | - |
293 | | - public static function replaceArgs( $message, $args ) { |
294 | | - // Replace arguments |
295 | | - if ( count( $args ) ) { |
296 | | - if ( is_array( $args[0] ) ) { |
297 | | - $args = array_values( $args[0] ); |
298 | | - } |
299 | | - $replacementKeys = array(); |
300 | | - foreach( $args as $n => $param ) { |
301 | | - $replacementKeys['$' . ($n + 1)] = $param; |
302 | | - } |
303 | | - $message = strtr( $message, $replacementKeys ); |
304 | | - } |
305 | | - |
306 | | - return $message; |
307 | | - } |
308 | | - |
309 | | - /** |
310 | | - * @param $language LANG_UI or LANG_CONTENT. |
311 | | - */ |
312 | | - public static function transform( $message, $language = self::LANG_UI ) { |
313 | | - global $wgMessageCache; |
314 | | - // transform accepts only boolean values |
315 | | - if ( !is_bool($language) ) |
316 | | - throw new MWException( __METHOD__ . ': only ui/content language supported' ); |
317 | | - return $wgMessageCache->transform( $message, !$language ); |
318 | | - } |
319 | | - |
320 | | - /** |
321 | | - * @param $language LANG_UI or LANG_CONTENT. |
322 | | - */ |
323 | | - public static function parse( $message, $language = self::LANG_UI, $inline = false ) { |
324 | | - global $wgOut; |
325 | | - // parse accepts only boolean values |
326 | | - if ( !is_bool($language) ) |
327 | | - throw new MWException( __METHOD__ . ': only ui/content language supported' ); |
328 | | - $message = $wgOut->parse( $message, true, !$language ); |
329 | | - |
330 | | - if ( $inline ) { |
331 | | - $m = array(); |
332 | | - if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $message, $m ) ) { |
333 | | - $message = $m[1]; |
334 | | - } |
335 | | - } |
336 | | - |
337 | | - return $message; |
338 | | - } |
339 | | - |
340 | | - public static function escapeHtml( $message, $allowEntities = true ) { |
341 | | - $message = htmlspecialchars( $message ); |
342 | | - if ( $allowEntities ) { |
343 | | - $message = str_replace( '&', '&', $message ); |
344 | | - $message = Sanitizer::normalizeCharReferences( $message ); |
345 | | - } |
346 | | - |
347 | | - return $message; |
348 | | - } |
349 | | - |
350 | | -} |
\ No newline at end of file |
Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -11,7 +11,6 @@ |
12 | 12 | require_once dirname(__FILE__) . '/LogPage.php'; |
13 | 13 | require_once dirname(__FILE__) . '/normal/UtfNormalUtil.php'; |
14 | 14 | require_once dirname(__FILE__) . '/XmlFunctions.php'; |
15 | | -require_once dirname(__FILE__) . '/MessageFunctions.php'; |
16 | 15 | |
17 | 16 | /** |
18 | 17 | * Compatibility functions |
— | — | @@ -321,6 +320,328 @@ |
322 | 321 | } |
323 | 322 | |
324 | 323 | /** |
| 324 | + * Get a message from anywhere, for the current user language. |
| 325 | + * |
| 326 | + * Use wfMsgForContent() instead if the message should NOT |
| 327 | + * change depending on the user preferences. |
| 328 | + * |
| 329 | + * @param $key String: lookup key for the message, usually |
| 330 | + * defined in languages/Language.php |
| 331 | + * |
| 332 | + * This function also takes extra optional parameters (not |
| 333 | + * shown in the function definition), which can by used to |
| 334 | + * insert variable text into the predefined message. |
| 335 | + */ |
| 336 | +function wfMsg( $key ) { |
| 337 | + $args = func_get_args(); |
| 338 | + array_shift( $args ); |
| 339 | + return wfMsgReal( $key, $args, true ); |
| 340 | +} |
| 341 | + |
| 342 | +/** |
| 343 | + * Same as above except doesn't transform the message |
| 344 | + */ |
| 345 | +function wfMsgNoTrans( $key ) { |
| 346 | + $args = func_get_args(); |
| 347 | + array_shift( $args ); |
| 348 | + return wfMsgReal( $key, $args, true, false, false ); |
| 349 | +} |
| 350 | + |
| 351 | +/** |
| 352 | + * Get a message from anywhere, for the current global language |
| 353 | + * set with $wgLanguageCode. |
| 354 | + * |
| 355 | + * Use this if the message should NOT change dependent on the |
| 356 | + * language set in the user's preferences. This is the case for |
| 357 | + * most text written into logs, as well as link targets (such as |
| 358 | + * the name of the copyright policy page). Link titles, on the |
| 359 | + * other hand, should be shown in the UI language. |
| 360 | + * |
| 361 | + * Note that MediaWiki allows users to change the user interface |
| 362 | + * language in their preferences, but a single installation |
| 363 | + * typically only contains content in one language. |
| 364 | + * |
| 365 | + * Be wary of this distinction: If you use wfMsg() where you should |
| 366 | + * use wfMsgForContent(), a user of the software may have to |
| 367 | + * customize over 70 messages in order to, e.g., fix a link in every |
| 368 | + * possible language. |
| 369 | + * |
| 370 | + * @param $key String: lookup key for the message, usually |
| 371 | + * defined in languages/Language.php |
| 372 | + */ |
| 373 | +function wfMsgForContent( $key ) { |
| 374 | + global $wgForceUIMsgAsContentMsg; |
| 375 | + $args = func_get_args(); |
| 376 | + array_shift( $args ); |
| 377 | + $forcontent = true; |
| 378 | + if( is_array( $wgForceUIMsgAsContentMsg ) && |
| 379 | + in_array( $key, $wgForceUIMsgAsContentMsg ) ) |
| 380 | + $forcontent = false; |
| 381 | + return wfMsgReal( $key, $args, true, $forcontent ); |
| 382 | +} |
| 383 | + |
| 384 | +/** |
| 385 | + * Same as above except doesn't transform the message |
| 386 | + */ |
| 387 | +function wfMsgForContentNoTrans( $key ) { |
| 388 | + global $wgForceUIMsgAsContentMsg; |
| 389 | + $args = func_get_args(); |
| 390 | + array_shift( $args ); |
| 391 | + $forcontent = true; |
| 392 | + if( is_array( $wgForceUIMsgAsContentMsg ) && |
| 393 | + in_array( $key, $wgForceUIMsgAsContentMsg ) ) |
| 394 | + $forcontent = false; |
| 395 | + return wfMsgReal( $key, $args, true, $forcontent, false ); |
| 396 | +} |
| 397 | + |
| 398 | +/** |
| 399 | + * Get a message from the language file, for the UI elements |
| 400 | + */ |
| 401 | +function wfMsgNoDB( $key ) { |
| 402 | + $args = func_get_args(); |
| 403 | + array_shift( $args ); |
| 404 | + return wfMsgReal( $key, $args, false ); |
| 405 | +} |
| 406 | + |
| 407 | +/** |
| 408 | + * Get a message from the language file, for the content |
| 409 | + */ |
| 410 | +function wfMsgNoDBForContent( $key ) { |
| 411 | + global $wgForceUIMsgAsContentMsg; |
| 412 | + $args = func_get_args(); |
| 413 | + array_shift( $args ); |
| 414 | + $forcontent = true; |
| 415 | + if( is_array( $wgForceUIMsgAsContentMsg ) && |
| 416 | + in_array( $key, $wgForceUIMsgAsContentMsg ) ) |
| 417 | + $forcontent = false; |
| 418 | + return wfMsgReal( $key, $args, false, $forcontent ); |
| 419 | +} |
| 420 | + |
| 421 | + |
| 422 | +/** |
| 423 | + * Really get a message |
| 424 | + * @param $key String: key to get. |
| 425 | + * @param $args |
| 426 | + * @param $useDB Boolean |
| 427 | + * @param $transform Boolean: Whether or not to transform the message. |
| 428 | + * @param $forContent Boolean |
| 429 | + * @return String: the requested message. |
| 430 | + */ |
| 431 | +function wfMsgReal( $key, $args, $useDB = true, $forContent=false, $transform = true ) { |
| 432 | + wfProfileIn( __METHOD__ ); |
| 433 | + $message = wfMsgGetKey( $key, $useDB, $forContent, $transform ); |
| 434 | + $message = wfMsgReplaceArgs( $message, $args ); |
| 435 | + wfProfileOut( __METHOD__ ); |
| 436 | + return $message; |
| 437 | +} |
| 438 | + |
| 439 | +/** |
| 440 | + * This function provides the message source for messages to be edited which are *not* stored in the database. |
| 441 | + * @param $key String: |
| 442 | + */ |
| 443 | +function wfMsgWeirdKey ( $key ) { |
| 444 | + $source = wfMsgGetKey( $key, false, true, false ); |
| 445 | + if ( wfEmptyMsg( $key, $source ) ) |
| 446 | + return ""; |
| 447 | + else |
| 448 | + return $source; |
| 449 | +} |
| 450 | + |
| 451 | +/** |
| 452 | + * Fetch a message string value, but don't replace any keys yet. |
| 453 | + * @param string $key |
| 454 | + * @param bool $useDB |
| 455 | + * @param string $langcode Code of the language to get the message for, or |
| 456 | + * behaves as a content language switch if it is a |
| 457 | + * boolean. |
| 458 | + * @return string |
| 459 | + * @private |
| 460 | + */ |
| 461 | +function wfMsgGetKey( $key, $useDB, $langCode = false, $transform = true ) { |
| 462 | + global $wgParser, $wgContLang, $wgMessageCache, $wgLang; |
| 463 | + |
| 464 | + wfRunHooks('NormalizeMessageKey', array(&$key, &$useDB, &$langCode, &$transform)); |
| 465 | + |
| 466 | + # If $wgMessageCache isn't initialised yet, try to return something sensible. |
| 467 | + if( is_object( $wgMessageCache ) ) { |
| 468 | + $message = $wgMessageCache->get( $key, $useDB, $langCode ); |
| 469 | + if ( $transform ) { |
| 470 | + $message = $wgMessageCache->transform( $message ); |
| 471 | + } |
| 472 | + } else { |
| 473 | + if( $langCode === true ) { |
| 474 | + $lang = &$wgContLang; |
| 475 | + } elseif( $langCode === false ) { |
| 476 | + $lang = &$wgLang; |
| 477 | + } else { |
| 478 | + $validCodes = array_keys( Language::getLanguageNames() ); |
| 479 | + if( in_array( $langCode, $validCodes ) ) { |
| 480 | + # $langcode corresponds to a valid language. |
| 481 | + $lang = Language::factory( $langCode ); |
| 482 | + } else { |
| 483 | + # $langcode is a string, but not a valid language code; use content language. |
| 484 | + $lang =& $wgContLang; |
| 485 | + wfDebug( 'Invalid language code passed to wfMsgGetKey, falling back to content language.' ); |
| 486 | + } |
| 487 | + } |
| 488 | + |
| 489 | + # MessageCache::get() does this already, Language::getMessage() doesn't |
| 490 | + # ISSUE: Should we try to handle "message/lang" here too? |
| 491 | + $key = str_replace( ' ' , '_' , $wgContLang->lcfirst( $key ) ); |
| 492 | + |
| 493 | + if( is_object( $lang ) ) { |
| 494 | + $message = $lang->getMessage( $key ); |
| 495 | + } else { |
| 496 | + $message = false; |
| 497 | + } |
| 498 | + } |
| 499 | + |
| 500 | + return $message; |
| 501 | +} |
| 502 | + |
| 503 | +/** |
| 504 | + * Replace message parameter keys on the given formatted output. |
| 505 | + * |
| 506 | + * @param string $message |
| 507 | + * @param array $args |
| 508 | + * @return string |
| 509 | + * @private |
| 510 | + */ |
| 511 | +function wfMsgReplaceArgs( $message, $args ) { |
| 512 | + # Fix windows line-endings |
| 513 | + # Some messages are split with explode("\n", $msg) |
| 514 | + $message = str_replace( "\r", '', $message ); |
| 515 | + |
| 516 | + // Replace arguments |
| 517 | + if ( count( $args ) ) { |
| 518 | + if ( is_array( $args[0] ) ) { |
| 519 | + $args = array_values( $args[0] ); |
| 520 | + } |
| 521 | + $replacementKeys = array(); |
| 522 | + foreach( $args as $n => $param ) { |
| 523 | + $replacementKeys['$' . ($n + 1)] = $param; |
| 524 | + } |
| 525 | + $message = strtr( $message, $replacementKeys ); |
| 526 | + } |
| 527 | + |
| 528 | + return $message; |
| 529 | +} |
| 530 | + |
| 531 | +/** |
| 532 | + * Return an HTML-escaped version of a message. |
| 533 | + * Parameter replacements, if any, are done *after* the HTML-escaping, |
| 534 | + * so parameters may contain HTML (eg links or form controls). Be sure |
| 535 | + * to pre-escape them if you really do want plaintext, or just wrap |
| 536 | + * the whole thing in htmlspecialchars(). |
| 537 | + * |
| 538 | + * @param string $key |
| 539 | + * @param string ... parameters |
| 540 | + * @return string |
| 541 | + */ |
| 542 | +function wfMsgHtml( $key ) { |
| 543 | + $args = func_get_args(); |
| 544 | + array_shift( $args ); |
| 545 | + return wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $key, true ) ), $args ); |
| 546 | +} |
| 547 | + |
| 548 | +/** |
| 549 | + * Return an HTML version of message |
| 550 | + * Parameter replacements, if any, are done *after* parsing the wiki-text message, |
| 551 | + * so parameters may contain HTML (eg links or form controls). Be sure |
| 552 | + * to pre-escape them if you really do want plaintext, or just wrap |
| 553 | + * the whole thing in htmlspecialchars(). |
| 554 | + * |
| 555 | + * @param string $key |
| 556 | + * @param string ... parameters |
| 557 | + * @return string |
| 558 | + */ |
| 559 | +function wfMsgWikiHtml( $key ) { |
| 560 | + global $wgOut; |
| 561 | + $args = func_get_args(); |
| 562 | + array_shift( $args ); |
| 563 | + return wfMsgReplaceArgs( $wgOut->parse( wfMsgGetKey( $key, true ), /* can't be set to false */ true ), $args ); |
| 564 | +} |
| 565 | + |
| 566 | +/** |
| 567 | + * Returns message in the requested format |
| 568 | + * @param string $key Key of the message |
| 569 | + * @param array $options Processing rules: |
| 570 | + * <i>parse</i>: parses wikitext to html |
| 571 | + * <i>parseinline</i>: parses wikitext to html and removes the surrounding p's added by parser or tidy |
| 572 | + * <i>escape</i>: filters message through htmlspecialchars |
| 573 | + * <i>escapenoentities</i>: same, but allows entity references like through |
| 574 | + * <i>replaceafter</i>: parameters are substituted after parsing or escaping |
| 575 | + * <i>parsemag</i>: transform the message using magic phrases |
| 576 | + * <i>content</i>: fetch message for content language instead of interface |
| 577 | + * <i>language</i>: language code to fetch message for (overriden by <i>content</i>), its behaviour |
| 578 | + * with parser, parseinline and parsemag is undefined. |
| 579 | + * Behavior for conflicting options (e.g., parse+parseinline) is undefined. |
| 580 | + */ |
| 581 | +function wfMsgExt( $key, $options ) { |
| 582 | + global $wgOut, $wgParser; |
| 583 | + |
| 584 | + $args = func_get_args(); |
| 585 | + array_shift( $args ); |
| 586 | + array_shift( $args ); |
| 587 | + |
| 588 | + if( !is_array($options) ) { |
| 589 | + $options = array($options); |
| 590 | + } |
| 591 | + |
| 592 | + if( in_array('content', $options) ) { |
| 593 | + $forContent = true; |
| 594 | + $langCode = true; |
| 595 | + } elseif( array_key_exists('language', $options) ) { |
| 596 | + $forContent = false; |
| 597 | + $langCode = $options['language']; |
| 598 | + $validCodes = array_keys( Language::getLanguageNames() ); |
| 599 | + if( !in_array($options['language'], $validCodes) ) { |
| 600 | + # Fallback to en, instead of whatever interface language we might have |
| 601 | + $langCode = 'en'; |
| 602 | + } |
| 603 | + } else { |
| 604 | + $forContent = false; |
| 605 | + $langCode = false; |
| 606 | + } |
| 607 | + |
| 608 | + $string = wfMsgGetKey( $key, /*DB*/true, $langCode, /*Transform*/false ); |
| 609 | + |
| 610 | + if( !in_array('replaceafter', $options) ) { |
| 611 | + $string = wfMsgReplaceArgs( $string, $args ); |
| 612 | + } |
| 613 | + |
| 614 | + if( in_array('parse', $options) ) { |
| 615 | + $string = $wgOut->parse( $string, true, !$forContent ); |
| 616 | + } elseif ( in_array('parseinline', $options) ) { |
| 617 | + $string = $wgOut->parse( $string, true, !$forContent ); |
| 618 | + $m = array(); |
| 619 | + if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) { |
| 620 | + $string = $m[1]; |
| 621 | + } |
| 622 | + } elseif ( in_array('parsemag', $options) ) { |
| 623 | + global $wgMessageCache; |
| 624 | + if ( isset( $wgMessageCache ) ) { |
| 625 | + $string = $wgMessageCache->transform( $string, !$forContent ); |
| 626 | + } |
| 627 | + } |
| 628 | + |
| 629 | + if ( in_array('escape', $options) ) { |
| 630 | + $string = htmlspecialchars ( $string ); |
| 631 | + } elseif ( in_array( 'escapenoentities', $options ) ) { |
| 632 | + $string = htmlspecialchars( $string ); |
| 633 | + $string = str_replace( '&', '&', $string ); |
| 634 | + $string = Sanitizer::normalizeCharReferences( $string ); |
| 635 | + } |
| 636 | + |
| 637 | + if( in_array('replaceafter', $options) ) { |
| 638 | + $string = wfMsgReplaceArgs( $string, $args ); |
| 639 | + } |
| 640 | + |
| 641 | + return $string; |
| 642 | +} |
| 643 | + |
| 644 | + |
| 645 | +/** |
325 | 646 | * Just like exit() but makes a note of it. |
326 | 647 | * Commits open transactions except if the error parameter is set |
327 | 648 | * |