* Add 'editsection-brackets' message to allow localization (or removal) of the bracke...
[lhc/web/wiklou.git] / languages / Language.php
1 <?php
2 /**
3 * @addtogroup Language
4 */
5
6 if( !defined( 'MEDIAWIKI' ) ) {
7 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
8 exit( 1 );
9 }
10
11 #
12 # In general you should not make customizations in these language files
13 # directly, but should use the MediaWiki: special namespace to customize
14 # user interface messages through the wiki.
15 # See http://meta.wikipedia.org/wiki/MediaWiki_namespace
16 #
17 # NOTE TO TRANSLATORS: Do not copy this whole file when making translations!
18 # A lot of common constants and a base class with inheritable methods are
19 # defined here, which should not be redefined. See the other LanguageXx.php
20 # files for examples.
21 #
22
23 # Read language names
24 global $wgLanguageNames;
25 require_once( dirname(__FILE__) . '/Names.php' ) ;
26
27 global $wgInputEncoding, $wgOutputEncoding;
28
29 /**
30 * These are always UTF-8, they exist only for backwards compatibility
31 */
32 $wgInputEncoding = "UTF-8";
33 $wgOutputEncoding = "UTF-8";
34
35 if( function_exists( 'mb_strtoupper' ) ) {
36 mb_internal_encoding('UTF-8');
37 }
38
39 /* a fake language converter */
40 class FakeConverter {
41 var $mLang;
42 function FakeConverter($langobj) {$this->mLang = $langobj;}
43 function convert($t, $i) {return $t;}
44 function parserConvert($t, $p) {return $t;}
45 function getVariants() { return array( $this->mLang->getCode() ); }
46 function getPreferredVariant() {return $this->mLang->getCode(); }
47 function findVariantLink(&$l, &$n) {}
48 function getExtraHashOptions() {return '';}
49 function getParsedTitle() {return '';}
50 function markNoConversion($text, $noParse=false) {return $text;}
51 function convertCategoryKey( $key ) {return $key; }
52 function convertLinkToAllVariants($text){ return array( $this->mLang->getCode() => $text); }
53 function armourMath($text){ return $text; }
54 }
55
56 #--------------------------------------------------------------------------
57 # Internationalisation code
58 #--------------------------------------------------------------------------
59
60 class Language {
61 var $mConverter, $mVariants, $mCode, $mLoaded = false;
62
63 static public $mLocalisationKeys = array( 'fallback', 'namespaceNames',
64 'skinNames', 'mathNames',
65 'bookstoreList', 'magicWords', 'messages', 'rtl', 'digitTransformTable',
66 'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
67 'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
68 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
69 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases' );
70
71 static public $mMergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
72 'dateFormats', 'defaultUserOptionOverrides', 'magicWords' );
73
74 static public $mMergeableListKeys = array( 'extraUserToggles' );
75
76 static public $mMergeableAliasListKeys = array( 'specialPageAliases' );
77
78 static public $mLocalisationCache = array();
79
80 static public $mWeekdayMsgs = array(
81 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
82 'friday', 'saturday'
83 );
84
85 static public $mWeekdayAbbrevMsgs = array(
86 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
87 );
88
89 static public $mMonthMsgs = array(
90 'january', 'february', 'march', 'april', 'may_long', 'june',
91 'july', 'august', 'september', 'october', 'november',
92 'december'
93 );
94 static public $mMonthGenMsgs = array(
95 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
96 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
97 'december-gen'
98 );
99 static public $mMonthAbbrevMsgs = array(
100 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
101 'sep', 'oct', 'nov', 'dec'
102 );
103
104 /**
105 * Create a language object for a given language code
106 */
107 static function factory( $code ) {
108 global $IP;
109 static $recursionLevel = 0;
110
111 if ( $code == 'en' ) {
112 $class = 'Language';
113 } else {
114 $class = 'Language' . str_replace( '-', '_', ucfirst( $code ) );
115 // Preload base classes to work around APC/PHP5 bug
116 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
117 include_once("$IP/languages/classes/$class.deps.php");
118 }
119 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
120 include_once("$IP/languages/classes/$class.php");
121 }
122 }
123
124 if ( $recursionLevel > 5 ) {
125 throw new MWException( "Language fallback loop detected when creating class $class\n" );
126 }
127
128 if( ! class_exists( $class ) ) {
129 $fallback = Language::getFallbackFor( $code );
130 ++$recursionLevel;
131 $lang = Language::factory( $fallback );
132 --$recursionLevel;
133 $lang->setCode( $code );
134 } else {
135 $lang = new $class;
136 }
137
138 return $lang;
139 }
140
141 function __construct() {
142 $this->mConverter = new FakeConverter($this);
143 // Set the code to the name of the descendant
144 if ( get_class( $this ) == 'Language' ) {
145 $this->mCode = 'en';
146 } else {
147 $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
148 }
149 }
150
151 /**
152 * Hook which will be called if this is the content language.
153 * Descendants can use this to register hook functions or modify globals
154 */
155 function initContLang() {}
156
157 /**
158 * @deprecated
159 * @return array
160 */
161 function getDefaultUserOptions() {
162 return User::getDefaultOptions();
163 }
164
165 function getFallbackLanguageCode() {
166 $this->load();
167 return $this->fallback;
168 }
169
170 /**
171 * Exports $wgBookstoreListEn
172 * @return array
173 */
174 function getBookstoreList() {
175 $this->load();
176 return $this->bookstoreList;
177 }
178
179 /**
180 * @return array
181 */
182 function getNamespaces() {
183 $this->load();
184 return $this->namespaceNames;
185 }
186
187 /**
188 * A convenience function that returns the same thing as
189 * getNamespaces() except with the array values changed to ' '
190 * where it found '_', useful for producing output to be displayed
191 * e.g. in <select> forms.
192 *
193 * @return array
194 */
195 function getFormattedNamespaces() {
196 $ns = $this->getNamespaces();
197 foreach($ns as $k => $v) {
198 $ns[$k] = strtr($v, '_', ' ');
199 }
200 return $ns;
201 }
202
203 /**
204 * Get a namespace value by key
205 * <code>
206 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
207 * echo $mw_ns; // prints 'MediaWiki'
208 * </code>
209 *
210 * @param int $index the array key of the namespace to return
211 * @return mixed, string if the namespace value exists, otherwise false
212 */
213 function getNsText( $index ) {
214 $ns = $this->getNamespaces();
215 return isset( $ns[$index] ) ? $ns[$index] : false;
216 }
217
218 /**
219 * A convenience function that returns the same thing as
220 * getNsText() except with '_' changed to ' ', useful for
221 * producing output.
222 *
223 * @return array
224 */
225 function getFormattedNsText( $index ) {
226 $ns = $this->getNsText( $index );
227 return strtr($ns, '_', ' ');
228 }
229
230 /**
231 * Get a namespace key by value, case insensitive.
232 * Only matches namespace names for the current language, not the
233 * canonical ones defined in Namespace.php.
234 *
235 * @param string $text
236 * @return mixed An integer if $text is a valid value otherwise false
237 */
238 function getLocalNsIndex( $text ) {
239 $this->load();
240 $lctext = $this->lc($text);
241 return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
242 }
243
244 /**
245 * Get a namespace key by value, case insensitive. Canonical namespace
246 * names override custom ones defined for the current language.
247 *
248 * @param string $text
249 * @return mixed An integer if $text is a valid value otherwise false
250 */
251 function getNsIndex( $text ) {
252 $this->load();
253 $lctext = $this->lc($text);
254 if( ( $ns = Namespace::getCanonicalIndex( $lctext ) ) !== null ) return $ns;
255 return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
256 }
257
258 /**
259 * short names for language variants used for language conversion links.
260 *
261 * @param string $code
262 * @return string
263 */
264 function getVariantname( $code ) {
265 return $this->getMessageFromDB( "variantname-$code" );
266 }
267
268 function specialPage( $name ) {
269 $aliases = $this->getSpecialPageAliases();
270 if ( isset( $aliases[$name][0] ) ) {
271 $name = $aliases[$name][0];
272 }
273 return $this->getNsText(NS_SPECIAL) . ':' . $name;
274 }
275
276 function getQuickbarSettings() {
277 return array(
278 $this->getMessage( 'qbsettings-none' ),
279 $this->getMessage( 'qbsettings-fixedleft' ),
280 $this->getMessage( 'qbsettings-fixedright' ),
281 $this->getMessage( 'qbsettings-floatingleft' ),
282 $this->getMessage( 'qbsettings-floatingright' )
283 );
284 }
285
286 function getSkinNames() {
287 $this->load();
288 return $this->skinNames;
289 }
290
291 function getMathNames() {
292 $this->load();
293 return $this->mathNames;
294 }
295
296 function getDatePreferences() {
297 $this->load();
298 return $this->datePreferences;
299 }
300
301 function getDateFormats() {
302 $this->load();
303 return $this->dateFormats;
304 }
305
306 function getDefaultDateFormat() {
307 $this->load();
308 return $this->defaultDateFormat;
309 }
310
311 function getDatePreferenceMigrationMap() {
312 $this->load();
313 return $this->datePreferenceMigrationMap;
314 }
315
316 function getDefaultUserOptionOverrides() {
317 $this->load();
318 return $this->defaultUserOptionOverrides;
319 }
320
321 function getExtraUserToggles() {
322 $this->load();
323 return $this->extraUserToggles;
324 }
325
326 function getUserToggle( $tog ) {
327 return $this->getMessageFromDB( "tog-$tog" );
328 }
329
330 /**
331 * Get language names, indexed by code.
332 * If $customisedOnly is true, only returns codes with a messages file
333 */
334 public static function getLanguageNames( $customisedOnly = false ) {
335 global $wgLanguageNames;
336 if ( !$customisedOnly ) {
337 return $wgLanguageNames;
338 }
339
340 global $IP;
341 $messageFiles = glob( "$IP/languages/messages/Messages*.php" );
342 $names = array();
343 foreach ( $messageFiles as $file ) {
344 $m = array();
345 if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
346 $code = str_replace( '_', '-', strtolower( $m[1] ) );
347 if ( isset( $wgLanguageNames[$code] ) ) {
348 $names[$code] = $wgLanguageNames[$code];
349 }
350 }
351 }
352 return $names;
353 }
354
355 /**
356 * Ugly hack to get a message maybe from the MediaWiki namespace, if this
357 * language object is the content or user language.
358 */
359 function getMessageFromDB( $msg ) {
360 global $wgContLang, $wgLang;
361 if ( $wgContLang->getCode() == $this->getCode() ) {
362 # Content language
363 return wfMsgForContent( $msg );
364 } elseif ( $wgLang->getCode() == $this->getCode() ) {
365 # User language
366 return wfMsg( $msg );
367 } else {
368 # Neither, get from localisation
369 return $this->getMessage( $msg );
370 }
371 }
372
373 function getLanguageName( $code ) {
374 global $wgLanguageNames;
375 if ( ! array_key_exists( $code, $wgLanguageNames ) ) {
376 return '';
377 }
378 return $wgLanguageNames[$code];
379 }
380
381 function getMonthName( $key ) {
382 return $this->getMessageFromDB( self::$mMonthMsgs[$key-1] );
383 }
384
385 function getMonthNameGen( $key ) {
386 return $this->getMessageFromDB( self::$mMonthGenMsgs[$key-1] );
387 }
388
389 function getMonthAbbreviation( $key ) {
390 return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key-1] );
391 }
392
393 function getWeekdayName( $key ) {
394 return $this->getMessageFromDB( self::$mWeekdayMsgs[$key-1] );
395 }
396
397 function getWeekdayAbbreviation( $key ) {
398 return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key-1] );
399 }
400
401 /**
402 * Used by date() and time() to adjust the time output.
403 * @public
404 * @param int $ts the time in date('YmdHis') format
405 * @param mixed $tz adjust the time by this amount (default false,
406 * mean we get user timecorrection setting)
407 * @return int
408 */
409 function userAdjust( $ts, $tz = false ) {
410 global $wgUser, $wgLocalTZoffset;
411
412 if (!$tz) {
413 $tz = $wgUser->getOption( 'timecorrection' );
414 }
415
416 # minutes and hours differences:
417 $minDiff = 0;
418 $hrDiff = 0;
419
420 if ( $tz === '' ) {
421 # Global offset in minutes.
422 if( isset($wgLocalTZoffset) ) {
423 if( $wgLocalTZoffset >= 0 ) {
424 $hrDiff = floor($wgLocalTZoffset / 60);
425 } else {
426 $hrDiff = ceil($wgLocalTZoffset / 60);
427 }
428 $minDiff = $wgLocalTZoffset % 60;
429 }
430 } elseif ( strpos( $tz, ':' ) !== false ) {
431 $tzArray = explode( ':', $tz );
432 $hrDiff = intval($tzArray[0]);
433 $minDiff = intval($hrDiff < 0 ? -$tzArray[1] : $tzArray[1]);
434 } else {
435 $hrDiff = intval( $tz );
436 }
437
438 # No difference ? Return time unchanged
439 if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
440
441 wfSuppressWarnings(); // E_STRICT system time bitching
442 # Generate an adjusted date
443 $t = mktime( (
444 (int)substr( $ts, 8, 2) ) + $hrDiff, # Hours
445 (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
446 (int)substr( $ts, 12, 2 ), # Seconds
447 (int)substr( $ts, 4, 2 ), # Month
448 (int)substr( $ts, 6, 2 ), # Day
449 (int)substr( $ts, 0, 4 ) ); #Year
450
451 $date = date( 'YmdHis', $t );
452 wfRestoreWarnings();
453
454 return $date;
455 }
456
457 /**
458 * This is a workalike of PHP's date() function, but with better
459 * internationalisation, a reduced set of format characters, and a better
460 * escaping format.
461 *
462 * Supported format characters are dDjlNwzWFmMntLYyaAgGhHiscrU. See the
463 * PHP manual for definitions. There are a number of extensions, which
464 * start with "x":
465 *
466 * xn Do not translate digits of the next numeric format character
467 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
468 * xr Use roman numerals for the next numeric format character
469 * xx Literal x
470 * xg Genitive month name
471 *
472 * Characters enclosed in double quotes will be considered literal (with
473 * the quotes themselves removed). Unmatched quotes will be considered
474 * literal quotes. Example:
475 *
476 * "The month is" F => The month is January
477 * i's" => 20'11"
478 *
479 * Backslash escaping is also supported.
480 *
481 * Input timestamp is assumed to be pre-normalized to the desired local
482 * time zone, if any.
483 *
484 * @param string $format
485 * @param string $ts 14-character timestamp
486 * YYYYMMDDHHMMSS
487 * 01234567890123
488 */
489 function sprintfDate( $format, $ts ) {
490 $s = '';
491 $raw = false;
492 $roman = false;
493 $unix = false;
494 $rawToggle = false;
495 for ( $p = 0; $p < strlen( $format ); $p++ ) {
496 $num = false;
497 $code = $format[$p];
498 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
499 $code .= $format[++$p];
500 }
501
502 switch ( $code ) {
503 case 'xx':
504 $s .= 'x';
505 break;
506 case 'xn':
507 $raw = true;
508 break;
509 case 'xN':
510 $rawToggle = !$rawToggle;
511 break;
512 case 'xr':
513 $roman = true;
514 break;
515 case 'xg':
516 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
517 break;
518 case 'd':
519 $num = substr( $ts, 6, 2 );
520 break;
521 case 'D':
522 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
523 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) + 1 );
524 break;
525 case 'j':
526 $num = intval( substr( $ts, 6, 2 ) );
527 break;
528 case 'l':
529 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
530 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 );
531 break;
532 case 'N':
533 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
534 $w = gmdate( 'w', $unix );
535 $num = $w ? $w : 7;
536 break;
537 case 'w':
538 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
539 $num = gmdate( 'w', $unix );
540 break;
541 case 'z':
542 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
543 $num = gmdate( 'z', $unix );
544 break;
545 case 'W':
546 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
547 $num = gmdate( 'W', $unix );
548 break;
549 case 'F':
550 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
551 break;
552 case 'm':
553 $num = substr( $ts, 4, 2 );
554 break;
555 case 'M':
556 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
557 break;
558 case 'n':
559 $num = intval( substr( $ts, 4, 2 ) );
560 break;
561 case 't':
562 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
563 $num = gmdate( 't', $unix );
564 break;
565 case 'L':
566 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
567 $num = gmdate( 'L', $unix );
568 break;
569 case 'Y':
570 $num = substr( $ts, 0, 4 );
571 break;
572 case 'y':
573 $num = substr( $ts, 2, 2 );
574 break;
575 case 'a':
576 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
577 break;
578 case 'A':
579 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
580 break;
581 case 'g':
582 $h = substr( $ts, 8, 2 );
583 $num = $h % 12 ? $h % 12 : 12;
584 break;
585 case 'G':
586 $num = intval( substr( $ts, 8, 2 ) );
587 break;
588 case 'h':
589 $h = substr( $ts, 8, 2 );
590 $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
591 break;
592 case 'H':
593 $num = substr( $ts, 8, 2 );
594 break;
595 case 'i':
596 $num = substr( $ts, 10, 2 );
597 break;
598 case 's':
599 $num = substr( $ts, 12, 2 );
600 break;
601 case 'c':
602 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
603 $s .= gmdate( 'c', $unix );
604 break;
605 case 'r':
606 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
607 $s .= gmdate( 'r', $unix );
608 break;
609 case 'U':
610 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
611 $num = $unix;
612 break;
613 case '\\':
614 # Backslash escaping
615 if ( $p < strlen( $format ) - 1 ) {
616 $s .= $format[++$p];
617 } else {
618 $s .= '\\';
619 }
620 break;
621 case '"':
622 # Quoted literal
623 if ( $p < strlen( $format ) - 1 ) {
624 $endQuote = strpos( $format, '"', $p + 1 );
625 if ( $endQuote === false ) {
626 # No terminating quote, assume literal "
627 $s .= '"';
628 } else {
629 $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
630 $p = $endQuote;
631 }
632 } else {
633 # Quote at end of string, assume literal "
634 $s .= '"';
635 }
636 break;
637 default:
638 $s .= $format[$p];
639 }
640 if ( $num !== false ) {
641 if ( $rawToggle || $raw ) {
642 $s .= $num;
643 $raw = false;
644 } elseif ( $roman ) {
645 $s .= self::romanNumeral( $num );
646 $roman = false;
647 } else {
648 $s .= $this->formatNum( $num, true );
649 }
650 $num = false;
651 }
652 }
653 return $s;
654 }
655
656 /**
657 * Roman number formatting up to 3000
658 */
659 static function romanNumeral( $num ) {
660 static $table = array(
661 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
662 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
663 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
664 array( '', 'M', 'MM', 'MMM' )
665 );
666
667 $num = intval( $num );
668 if ( $num > 3000 || $num <= 0 ) {
669 return $num;
670 }
671
672 $s = '';
673 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
674 if ( $num >= $pow10 ) {
675 $s .= $table[$i][floor($num / $pow10)];
676 }
677 $num = $num % $pow10;
678 }
679 return $s;
680 }
681
682 /**
683 * This is meant to be used by time(), date(), and timeanddate() to get
684 * the date preference they're supposed to use, it should be used in
685 * all children.
686 *
687 *<code>
688 * function timeanddate([...], $format = true) {
689 * $datePreference = $this->dateFormat($format);
690 * [...]
691 * }
692 *</code>
693 *
694 * @param mixed $usePrefs: if true, the user's preference is used
695 * if false, the site/language default is used
696 * if int/string, assumed to be a format.
697 * @return string
698 */
699 function dateFormat( $usePrefs = true ) {
700 global $wgUser;
701
702 if( is_bool( $usePrefs ) ) {
703 if( $usePrefs ) {
704 $datePreference = $wgUser->getDatePreference();
705 } else {
706 $options = User::getDefaultOptions();
707 $datePreference = (string)$options['date'];
708 }
709 } else {
710 $datePreference = (string)$usePrefs;
711 }
712
713 // return int
714 if( $datePreference == '' ) {
715 return 'default';
716 }
717
718 return $datePreference;
719 }
720
721 /**
722 * @public
723 * @param mixed $ts the time format which needs to be turned into a
724 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
725 * @param bool $adj whether to adjust the time output according to the
726 * user configured offset ($timecorrection)
727 * @param mixed $format true to use user's date format preference
728 * @param string $timecorrection the time offset as returned by
729 * validateTimeZone() in Special:Preferences
730 * @return string
731 */
732 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
733 $this->load();
734 if ( $adj ) {
735 $ts = $this->userAdjust( $ts, $timecorrection );
736 }
737
738 $pref = $this->dateFormat( $format );
739 if( $pref == 'default' || !isset( $this->dateFormats["$pref date"] ) ) {
740 $pref = $this->defaultDateFormat;
741 }
742 return $this->sprintfDate( $this->dateFormats["$pref date"], $ts );
743 }
744
745 /**
746 * @public
747 * @param mixed $ts the time format which needs to be turned into a
748 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
749 * @param bool $adj whether to adjust the time output according to the
750 * user configured offset ($timecorrection)
751 * @param mixed $format true to use user's date format preference
752 * @param string $timecorrection the time offset as returned by
753 * validateTimeZone() in Special:Preferences
754 * @return string
755 */
756 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
757 $this->load();
758 if ( $adj ) {
759 $ts = $this->userAdjust( $ts, $timecorrection );
760 }
761
762 $pref = $this->dateFormat( $format );
763 if( $pref == 'default' || !isset( $this->dateFormats["$pref time"] ) ) {
764 $pref = $this->defaultDateFormat;
765 }
766 return $this->sprintfDate( $this->dateFormats["$pref time"], $ts );
767 }
768
769 /**
770 * @public
771 * @param mixed $ts the time format which needs to be turned into a
772 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
773 * @param bool $adj whether to adjust the time output according to the
774 * user configured offset ($timecorrection)
775
776 * @param mixed $format what format to return, if it's false output the
777 * default one (default true)
778 * @param string $timecorrection the time offset as returned by
779 * validateTimeZone() in Special:Preferences
780 * @return string
781 */
782 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
783 $this->load();
784
785 $ts = wfTimestamp( TS_MW, $ts );
786
787 if ( $adj ) {
788 $ts = $this->userAdjust( $ts, $timecorrection );
789 }
790
791 $pref = $this->dateFormat( $format );
792 if( $pref == 'default' || !isset( $this->dateFormats["$pref both"] ) ) {
793 $pref = $this->defaultDateFormat;
794 }
795
796 return $this->sprintfDate( $this->dateFormats["$pref both"], $ts );
797 }
798
799 function getMessage( $key ) {
800 $this->load();
801 return isset( $this->messages[$key] ) ? $this->messages[$key] : null;
802 }
803
804 function getAllMessages() {
805 $this->load();
806 return $this->messages;
807 }
808
809 function iconv( $in, $out, $string ) {
810 # For most languages, this is a wrapper for iconv
811 return iconv( $in, $out . '//IGNORE', $string );
812 }
813
814 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
815 function ucwordbreaksCallbackAscii($matches){
816 return $this->ucfirst($matches[1]);
817 }
818
819 function ucwordbreaksCallbackMB($matches){
820 return mb_strtoupper($matches[0]);
821 }
822
823 function ucCallback($matches){
824 list( $wikiUpperChars ) = self::getCaseMaps();
825 return strtr( $matches[1], $wikiUpperChars );
826 }
827
828 function lcCallback($matches){
829 list( , $wikiLowerChars ) = self::getCaseMaps();
830 return strtr( $matches[1], $wikiLowerChars );
831 }
832
833 function ucwordsCallbackMB($matches){
834 return mb_strtoupper($matches[0]);
835 }
836
837 function ucwordsCallbackWiki($matches){
838 list( $wikiUpperChars ) = self::getCaseMaps();
839 return strtr( $matches[0], $wikiUpperChars );
840 }
841
842 function ucfirst( $str ) {
843 return self::uc( $str, true );
844 }
845
846 function uc( $str, $first = false ) {
847 if ( function_exists( 'mb_strtoupper' ) ) {
848 if ( $first ) {
849 if ( self::isMultibyte( $str ) ) {
850 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
851 } else {
852 return ucfirst( $str );
853 }
854 } else {
855 return self::isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
856 }
857 } else {
858 if ( self::isMultibyte( $str ) ) {
859 list( $wikiUpperChars ) = $this->getCaseMaps();
860 $x = $first ? '^' : '';
861 return preg_replace_callback(
862 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
863 array($this,"ucCallback"),
864 $str
865 );
866 } else {
867 return $first ? ucfirst( $str ) : strtoupper( $str );
868 }
869 }
870 }
871
872 function lcfirst( $str ) {
873 return self::lc( $str, true );
874 }
875
876 function lc( $str, $first = false ) {
877 if ( function_exists( 'mb_strtolower' ) )
878 if ( $first )
879 if ( self::isMultibyte( $str ) )
880 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
881 else
882 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
883 else
884 return self::isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
885 else
886 if ( self::isMultibyte( $str ) ) {
887 list( , $wikiLowerChars ) = self::getCaseMaps();
888 $x = $first ? '^' : '';
889 return preg_replace_callback(
890 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
891 array($this,"lcCallback"),
892 $str
893 );
894 } else
895 return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
896 }
897
898 function isMultibyte( $str ) {
899 return (bool)preg_match( '/[\x80-\xff]/', $str );
900 }
901
902 function ucwords($str) {
903 if ( self::isMultibyte( $str ) ) {
904 $str = self::lc($str);
905
906 // regexp to find first letter in each word (i.e. after each space)
907 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
908
909 // function to use to capitalize a single char
910 if ( function_exists( 'mb_strtoupper' ) )
911 return preg_replace_callback(
912 $replaceRegexp,
913 array($this,"ucwordsCallbackMB"),
914 $str
915 );
916 else
917 return preg_replace_callback(
918 $replaceRegexp,
919 array($this,"ucwordsCallbackWiki"),
920 $str
921 );
922 }
923 else
924 return ucwords( strtolower( $str ) );
925 }
926
927 # capitalize words at word breaks
928 function ucwordbreaks($str){
929 if (self::isMultibyte( $str ) ) {
930 $str = self::lc($str);
931
932 // since \b doesn't work for UTF-8, we explicitely define word break chars
933 $breaks= "[ \-\(\)\}\{\.,\?!]";
934
935 // find first letter after word break
936 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
937
938 if ( function_exists( 'mb_strtoupper' ) )
939 return preg_replace_callback(
940 $replaceRegexp,
941 array($this,"ucwordbreaksCallbackMB"),
942 $str
943 );
944 else
945 return preg_replace_callback(
946 $replaceRegexp,
947 array($this,"ucwordsCallbackWiki"),
948 $str
949 );
950 }
951 else
952 return preg_replace_callback(
953 '/\b([\w\x80-\xff]+)\b/',
954 array($this,"ucwordbreaksCallbackAscii"),
955 $str );
956 }
957
958 /**
959 * Return a case-folded representation of $s
960 *
961 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
962 * and $s2 are the same except for the case of their characters. It is not
963 * necessary for the value returned to make sense when displayed.
964 *
965 * Do *not* perform any other normalisation in this function. If a caller
966 * uses this function when it should be using a more general normalisation
967 * function, then fix the caller.
968 */
969 function caseFold( $s ) {
970 return $this->uc( $s );
971 }
972
973 function checkTitleEncoding( $s ) {
974 if( is_array( $s ) ) {
975 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
976 }
977 # Check for non-UTF-8 URLs
978 $ishigh = preg_match( '/[\x80-\xff]/', $s);
979 if(!$ishigh) return $s;
980
981 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
982 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
983 if( $isutf8 ) return $s;
984
985 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
986 }
987
988 function fallback8bitEncoding() {
989 $this->load();
990 return $this->fallback8bitEncoding;
991 }
992
993 /**
994 * Some languages have special punctuation to strip out
995 * or characters which need to be converted for MySQL's
996 * indexing to grok it correctly. Make such changes here.
997 *
998 * @param string $in
999 * @return string
1000 */
1001 function stripForSearch( $string ) {
1002 global $wgDBtype;
1003 if ( $wgDBtype != 'mysql' ) {
1004 return $string;
1005 }
1006
1007 # MySQL fulltext index doesn't grok utf-8, so we
1008 # need to fold cases and convert to hex
1009
1010 wfProfileIn( __METHOD__ );
1011 if( function_exists( 'mb_strtolower' ) ) {
1012 $out = preg_replace(
1013 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1014 "'U8' . bin2hex( \"$1\" )",
1015 mb_strtolower( $string ) );
1016 } else {
1017 list( , $wikiLowerChars ) = self::getCaseMaps();
1018 $out = preg_replace(
1019 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1020 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
1021 $string );
1022 }
1023 wfProfileOut( __METHOD__ );
1024 return $out;
1025 }
1026
1027 function convertForSearchResult( $termsArray ) {
1028 # some languages, e.g. Chinese, need to do a conversion
1029 # in order for search results to be displayed correctly
1030 return $termsArray;
1031 }
1032
1033 /**
1034 * Get the first character of a string.
1035 *
1036 * @param string $s
1037 * @return string
1038 */
1039 function firstChar( $s ) {
1040 $matches = array();
1041 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1042 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
1043
1044 return isset( $matches[1] ) ? $matches[1] : "";
1045 }
1046
1047 function initEncoding() {
1048 # Some languages may have an alternate char encoding option
1049 # (Esperanto X-coding, Japanese furigana conversion, etc)
1050 # If this language is used as the primary content language,
1051 # an override to the defaults can be set here on startup.
1052 }
1053
1054 function recodeForEdit( $s ) {
1055 # For some languages we'll want to explicitly specify
1056 # which characters make it into the edit box raw
1057 # or are converted in some way or another.
1058 # Note that if wgOutputEncoding is different from
1059 # wgInputEncoding, this text will be further converted
1060 # to wgOutputEncoding.
1061 global $wgEditEncoding;
1062 if( $wgEditEncoding == '' or
1063 $wgEditEncoding == 'UTF-8' ) {
1064 return $s;
1065 } else {
1066 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
1067 }
1068 }
1069
1070 function recodeInput( $s ) {
1071 # Take the previous into account.
1072 global $wgEditEncoding;
1073 if($wgEditEncoding != "") {
1074 $enc = $wgEditEncoding;
1075 } else {
1076 $enc = 'UTF-8';
1077 }
1078 if( $enc == 'UTF-8' ) {
1079 return $s;
1080 } else {
1081 return $this->iconv( $enc, 'UTF-8', $s );
1082 }
1083 }
1084
1085 /**
1086 * For right-to-left language support
1087 *
1088 * @return bool
1089 */
1090 function isRTL() {
1091 $this->load();
1092 return $this->rtl;
1093 }
1094
1095 /**
1096 * A hidden direction mark (LRM or RLM), depending on the language direction
1097 *
1098 * @return string
1099 */
1100 function getDirMark() {
1101 return $this->isRTL() ? "\xE2\x80\x8F" : "\xE2\x80\x8E";
1102 }
1103
1104 /**
1105 * An arrow, depending on the language direction
1106 *
1107 * @return string
1108 */
1109 function getArrow() {
1110 return $this->isRTL() ? '←' : '→';
1111 }
1112
1113 /**
1114 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
1115 *
1116 * @return bool
1117 */
1118 function linkPrefixExtension() {
1119 $this->load();
1120 return $this->linkPrefixExtension;
1121 }
1122
1123 function &getMagicWords() {
1124 $this->load();
1125 return $this->magicWords;
1126 }
1127
1128 # Fill a MagicWord object with data from here
1129 function getMagic( &$mw ) {
1130 if ( !isset( $this->mMagicExtensions ) ) {
1131 $this->mMagicExtensions = array();
1132 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
1133 }
1134 if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
1135 $rawEntry = $this->mMagicExtensions[$mw->mId];
1136 } else {
1137 $magicWords =& $this->getMagicWords();
1138 if ( isset( $magicWords[$mw->mId] ) ) {
1139 $rawEntry = $magicWords[$mw->mId];
1140 } else {
1141 # Fall back to English if local list is incomplete
1142 $magicWords =& Language::getMagicWords();
1143 $rawEntry = $magicWords[$mw->mId];
1144 }
1145 }
1146
1147 if( !is_array( $rawEntry ) ) {
1148 error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
1149 }
1150 $mw->mCaseSensitive = $rawEntry[0];
1151 $mw->mSynonyms = array_slice( $rawEntry, 1 );
1152 }
1153
1154 /**
1155 * Get special page names, as an associative array
1156 * case folded alias => real name
1157 */
1158 function getSpecialPageAliases() {
1159 $this->load();
1160 if ( !isset( $this->mExtendedSpecialPageAliases ) ) {
1161 $this->mExtendedSpecialPageAliases = $this->specialPageAliases;
1162 wfRunHooks( 'LangugeGetSpecialPageAliases',
1163 array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
1164 }
1165 return $this->mExtendedSpecialPageAliases;
1166 }
1167
1168 /**
1169 * Italic is unsuitable for some languages
1170 *
1171 * @public
1172 *
1173 * @param string $text The text to be emphasized.
1174 * @return string
1175 */
1176 function emphasize( $text ) {
1177 return "<em>$text</em>";
1178 }
1179
1180 /**
1181 * Normally we output all numbers in plain en_US style, that is
1182 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
1183 * point twohundredthirtyfive. However this is not sutable for all
1184 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
1185 * Icelandic just want to use commas instead of dots, and dots instead
1186 * of commas like "293.291,235".
1187 *
1188 * An example of this function being called:
1189 * <code>
1190 * wfMsg( 'message', $wgLang->formatNum( $num ) )
1191 * </code>
1192 *
1193 * See LanguageGu.php for the Gujarati implementation and
1194 * LanguageIs.php for the , => . and . => , implementation.
1195 *
1196 * @todo check if it's viable to use localeconv() for the decimal
1197 * seperator thing.
1198 * @public
1199 * @param mixed $number the string to be formatted, should be an integer or
1200 * a floating point number.
1201 * @param bool $nocommafy Set to true for special numbers like dates
1202 * @return string
1203 */
1204 function formatNum( $number, $nocommafy = false ) {
1205 global $wgTranslateNumerals;
1206 if (!$nocommafy) {
1207 $number = $this->commafy($number);
1208 $s = $this->separatorTransformTable();
1209 if (!is_null($s)) { $number = strtr($number, $s); }
1210 }
1211
1212 if ($wgTranslateNumerals) {
1213 $s = $this->digitTransformTable();
1214 if (!is_null($s)) { $number = strtr($number, $s); }
1215 }
1216
1217 return $number;
1218 }
1219
1220 function parseFormattedNumber( $number ) {
1221 $s = $this->digitTransformTable();
1222 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1223
1224 $s = $this->separatorTransformTable();
1225 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1226
1227 $number = strtr( $number, array (',' => '') );
1228 return $number;
1229 }
1230
1231 /**
1232 * Adds commas to a given number
1233 *
1234 * @param mixed $_
1235 * @return string
1236 */
1237 function commafy($_) {
1238 return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
1239 }
1240
1241 function digitTransformTable() {
1242 $this->load();
1243 return $this->digitTransformTable;
1244 }
1245
1246 function separatorTransformTable() {
1247 $this->load();
1248 return $this->separatorTransformTable;
1249 }
1250
1251
1252 /**
1253 * For the credit list in includes/Credits.php (action=credits)
1254 *
1255 * @param array $l
1256 * @return string
1257 */
1258 function listToText( $l ) {
1259 $s = '';
1260 $m = count($l) - 1;
1261 for ($i = $m; $i >= 0; $i--) {
1262 if ($i == $m) {
1263 $s = $l[$i];
1264 } else if ($i == $m - 1) {
1265 $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
1266 } else {
1267 $s = $l[$i] . ', ' . $s;
1268 }
1269 }
1270 return $s;
1271 }
1272
1273 # Crop a string from the beginning or end to a certain number of bytes.
1274 # (Bytes are used because our storage has limited byte lengths for some
1275 # columns in the database.) Multibyte charsets will need to make sure that
1276 # only whole characters are included!
1277 #
1278 # $length does not include the optional ellipsis.
1279 # If $length is negative, snip from the beginning
1280 function truncate( $string, $length, $ellipsis = "" ) {
1281 if( $length == 0 ) {
1282 return $ellipsis;
1283 }
1284 if ( strlen( $string ) <= abs( $length ) ) {
1285 return $string;
1286 }
1287 if( $length > 0 ) {
1288 $string = substr( $string, 0, $length );
1289 $char = ord( $string[strlen( $string ) - 1] );
1290 $m = array();
1291 if ($char >= 0xc0) {
1292 # We got the first byte only of a multibyte char; remove it.
1293 $string = substr( $string, 0, -1 );
1294 } elseif( $char >= 0x80 &&
1295 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
1296 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
1297 # We chopped in the middle of a character; remove it
1298 $string = $m[1];
1299 }
1300 return $string . $ellipsis;
1301 } else {
1302 $string = substr( $string, $length );
1303 $char = ord( $string[0] );
1304 if( $char >= 0x80 && $char < 0xc0 ) {
1305 # We chopped in the middle of a character; remove the whole thing
1306 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
1307 }
1308 return $ellipsis . $string;
1309 }
1310 }
1311
1312 /**
1313 * Grammatical transformations, needed for inflected languages
1314 * Invoked by putting {{grammar:case|word}} in a message
1315 *
1316 * @param string $word
1317 * @param string $case
1318 * @return string
1319 */
1320 function convertGrammar( $word, $case ) {
1321 global $wgGrammarForms;
1322 if ( isset($wgGrammarForms['en'][$case][$word]) ) {
1323 return $wgGrammarForms['en'][$case][$word];
1324 }
1325 return $word;
1326 }
1327
1328 /**
1329 * Plural form transformations, needed for some languages.
1330 * For example, where are 3 form of plural in Russian and Polish,
1331 * depending on "count mod 10". See [[w:Plural]]
1332 * For English it is pretty simple.
1333 *
1334 * Invoked by putting {{plural:count|wordform1|wordform2}}
1335 * or {{plural:count|wordform1|wordform2|wordform3}}
1336 *
1337 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
1338 *
1339 * @param integer $count
1340 * @param string $wordform1
1341 * @param string $wordform2
1342 * @param string $wordform3 (optional)
1343 * @param string $wordform4 (optional)
1344 * @param string $wordform5 (optional)
1345 * @return string
1346 */
1347 function convertPlural( $count, $w1, $w2, $w3, $w4, $w5) {
1348 return ( $count == '1' || $count == '-1' ) ? $w1 : $w2;
1349 }
1350
1351 /**
1352 * For translaing of expiry times
1353 * @param string The validated block time in English
1354 * @param $forContent, avoid html?
1355 * @return Somehow translated block time
1356 * @see LanguageFi.php for example implementation
1357 */
1358 function translateBlockExpiry( $str, $forContent=false ) {
1359
1360 $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
1361
1362 if ( $scBlockExpiryOptions == '-') {
1363 return $str;
1364 }
1365
1366 foreach (explode(',', $scBlockExpiryOptions) as $option) {
1367 if ( strpos($option, ":") === false )
1368 continue;
1369 list($show, $value) = explode(":", $option);
1370 if ( strcmp ( $str, $value) == 0 ) {
1371 if ( $forContent )
1372 return htmlspecialchars($str) . htmlspecialchars( trim( $show ) );
1373 else
1374 return '<span title="' . htmlspecialchars($str). '">' . htmlspecialchars( trim( $show ) ) . '</span>';
1375 }
1376 }
1377
1378 return $str;
1379 }
1380
1381 /**
1382 * languages like Chinese need to be segmented in order for the diff
1383 * to be of any use
1384 *
1385 * @param string $text
1386 * @return string
1387 */
1388 function segmentForDiff( $text ) {
1389 return $text;
1390 }
1391
1392 /**
1393 * and unsegment to show the result
1394 *
1395 * @param string $text
1396 * @return string
1397 */
1398 function unsegmentForDiff( $text ) {
1399 return $text;
1400 }
1401
1402 # convert text to different variants of a language.
1403 function convert( $text, $isTitle = false) {
1404 return $this->mConverter->convert($text, $isTitle);
1405 }
1406
1407 # Convert text from within Parser
1408 function parserConvert( $text, &$parser ) {
1409 return $this->mConverter->parserConvert( $text, $parser );
1410 }
1411
1412 # Check if this is a language with variants
1413 function hasVariants(){
1414 return sizeof($this->getVariants())>1;
1415 }
1416
1417 # Put custom tags (e.g. -{ }-) around math to prevent conversion
1418 function armourMath($text){
1419 return $this->mConverter->armourMath($text);
1420 }
1421
1422
1423 /**
1424 * Perform output conversion on a string, and encode for safe HTML output.
1425 * @param string $text
1426 * @param bool $isTitle -- wtf?
1427 * @return string
1428 * @todo this should get integrated somewhere sane
1429 */
1430 function convertHtml( $text, $isTitle = false ) {
1431 return htmlspecialchars( $this->convert( $text, $isTitle ) );
1432 }
1433
1434 function convertCategoryKey( $key ) {
1435 return $this->mConverter->convertCategoryKey( $key );
1436 }
1437
1438 /**
1439 * get the list of variants supported by this langauge
1440 * see sample implementation in LanguageZh.php
1441 *
1442 * @return array an array of language codes
1443 */
1444 function getVariants() {
1445 return $this->mConverter->getVariants();
1446 }
1447
1448
1449 function getPreferredVariant( $fromUser = true ) {
1450 return $this->mConverter->getPreferredVariant( $fromUser );
1451 }
1452
1453 /**
1454 * if a language supports multiple variants, it is
1455 * possible that non-existing link in one variant
1456 * actually exists in another variant. this function
1457 * tries to find it. See e.g. LanguageZh.php
1458 *
1459 * @param string $link the name of the link
1460 * @param mixed $nt the title object of the link
1461 * @return null the input parameters may be modified upon return
1462 */
1463 function findVariantLink( &$link, &$nt ) {
1464 $this->mConverter->findVariantLink($link, $nt);
1465 }
1466
1467 /**
1468 * If a language supports multiple variants, converts text
1469 * into an array of all possible variants of the text:
1470 * 'variant' => text in that variant
1471 */
1472
1473 function convertLinkToAllVariants($text){
1474 return $this->mConverter->convertLinkToAllVariants($text);
1475 }
1476
1477
1478 /**
1479 * returns language specific options used by User::getPageRenderHash()
1480 * for example, the preferred language variant
1481 *
1482 * @return string
1483 * @public
1484 */
1485 function getExtraHashOptions() {
1486 return $this->mConverter->getExtraHashOptions();
1487 }
1488
1489 /**
1490 * for languages that support multiple variants, the title of an
1491 * article may be displayed differently in different variants. this
1492 * function returns the apporiate title defined in the body of the article.
1493 *
1494 * @return string
1495 */
1496 function getParsedTitle() {
1497 return $this->mConverter->getParsedTitle();
1498 }
1499
1500 /**
1501 * Enclose a string with the "no conversion" tag. This is used by
1502 * various functions in the Parser
1503 *
1504 * @param string $text text to be tagged for no conversion
1505 * @return string the tagged text
1506 */
1507 function markNoConversion( $text, $noParse=false ) {
1508 return $this->mConverter->markNoConversion( $text, $noParse );
1509 }
1510
1511 /**
1512 * A regular expression to match legal word-trailing characters
1513 * which should be merged onto a link of the form [[foo]]bar.
1514 *
1515 * @return string
1516 * @public
1517 */
1518 function linkTrail() {
1519 $this->load();
1520 return $this->linkTrail;
1521 }
1522
1523 function getLangObj() {
1524 return $this;
1525 }
1526
1527 /**
1528 * Get the RFC 3066 code for this language object
1529 */
1530 function getCode() {
1531 return $this->mCode;
1532 }
1533
1534 function setCode( $code ) {
1535 $this->mCode = $code;
1536 }
1537
1538 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
1539 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
1540 }
1541
1542 static function getMessagesFileName( $code ) {
1543 global $IP;
1544 return self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
1545 }
1546
1547 static function getClassFileName( $code ) {
1548 global $IP;
1549 return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
1550 }
1551
1552 static function getLocalisationArray( $code, $disableCache = false ) {
1553 self::loadLocalisation( $code, $disableCache );
1554 return self::$mLocalisationCache[$code];
1555 }
1556
1557 /**
1558 * Load localisation data for a given code into the static cache
1559 *
1560 * @return array Dependencies, map of filenames to mtimes
1561 */
1562 static function loadLocalisation( $code, $disableCache = false ) {
1563 static $recursionGuard = array();
1564 global $wgMemc;
1565
1566 if ( !$code ) {
1567 throw new MWException( "Invalid language code requested" );
1568 }
1569
1570 if ( !$disableCache ) {
1571 # Try the per-process cache
1572 if ( isset( self::$mLocalisationCache[$code] ) ) {
1573 return self::$mLocalisationCache[$code]['deps'];
1574 }
1575
1576 wfProfileIn( __METHOD__ );
1577
1578 # Try the serialized directory
1579 $cache = wfGetPrecompiledData( self::getFileName( "Messages", $code, '.ser' ) );
1580 if ( $cache ) {
1581 self::$mLocalisationCache[$code] = $cache;
1582 wfDebug( "Language::loadLocalisation(): got localisation for $code from precompiled data file\n" );
1583 wfProfileOut( __METHOD__ );
1584 return self::$mLocalisationCache[$code]['deps'];
1585 }
1586
1587 # Try the global cache
1588 $memcKey = wfMemcKey('localisation', $code );
1589 $cache = $wgMemc->get( $memcKey );
1590 if ( $cache ) {
1591 # Check file modification times
1592 foreach ( $cache['deps'] as $file => $mtime ) {
1593 if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
1594 break;
1595 }
1596 }
1597 if ( self::isLocalisationOutOfDate( $cache ) ) {
1598 $wgMemc->delete( $memcKey );
1599 $cache = false;
1600 wfDebug( "Language::loadLocalisation(): localisation cache for $code had expired due to update of $file\n" );
1601 } else {
1602 self::$mLocalisationCache[$code] = $cache;
1603 wfDebug( "Language::loadLocalisation(): got localisation for $code from cache\n" );
1604 wfProfileOut( __METHOD__ );
1605 return $cache['deps'];
1606 }
1607 }
1608 } else {
1609 wfProfileIn( __METHOD__ );
1610 }
1611
1612 # Default fallback, may be overridden when the messages file is included
1613 if ( $code != 'en' ) {
1614 $fallback = 'en';
1615 } else {
1616 $fallback = false;
1617 }
1618
1619 # Load the primary localisation from the source file
1620 $filename = self::getMessagesFileName( $code );
1621 if ( !file_exists( $filename ) ) {
1622 wfDebug( "Language::loadLocalisation(): no localisation file for $code, using implicit fallback to en\n" );
1623 $cache = array();
1624 $deps = array();
1625 } else {
1626 $deps = array( $filename => filemtime( $filename ) );
1627 require( $filename );
1628 $cache = compact( self::$mLocalisationKeys );
1629 wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
1630 }
1631
1632 if ( !empty( $fallback ) ) {
1633 # Load the fallback localisation, with a circular reference guard
1634 if ( isset( $recursionGuard[$code] ) ) {
1635 throw new MWException( "Error: Circular fallback reference in language code $code" );
1636 }
1637 $recursionGuard[$code] = true;
1638 $newDeps = self::loadLocalisation( $fallback, $disableCache );
1639 unset( $recursionGuard[$code] );
1640
1641 $secondary = self::$mLocalisationCache[$fallback];
1642 $deps = array_merge( $deps, $newDeps );
1643
1644 # Merge the fallback localisation with the current localisation
1645 foreach ( self::$mLocalisationKeys as $key ) {
1646 if ( isset( $cache[$key] ) ) {
1647 if ( isset( $secondary[$key] ) ) {
1648 if ( in_array( $key, self::$mMergeableMapKeys ) ) {
1649 $cache[$key] = $cache[$key] + $secondary[$key];
1650 } elseif ( in_array( $key, self::$mMergeableListKeys ) ) {
1651 $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
1652 } elseif ( in_array( $key, self::$mMergeableAliasListKeys ) ) {
1653 $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
1654 }
1655 }
1656 } else {
1657 $cache[$key] = $secondary[$key];
1658 }
1659 }
1660
1661 # Merge bookstore lists if requested
1662 if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
1663 $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
1664 }
1665 if ( isset( $cache['bookstoreList']['inherit'] ) ) {
1666 unset( $cache['bookstoreList']['inherit'] );
1667 }
1668 }
1669
1670 # Add dependencies to the cache entry
1671 $cache['deps'] = $deps;
1672
1673 # Replace spaces with underscores in namespace names
1674 $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
1675
1676 # Save to both caches
1677 self::$mLocalisationCache[$code] = $cache;
1678 if ( !$disableCache ) {
1679 $wgMemc->set( $memcKey, $cache );
1680 }
1681
1682 wfProfileOut( __METHOD__ );
1683 return $deps;
1684 }
1685
1686 /**
1687 * Test if a given localisation cache is out of date with respect to the
1688 * source Messages files. This is done automatically for the global cache
1689 * in $wgMemc, but is only done on certain occasions for the serialized
1690 * data file.
1691 *
1692 * @param $cache mixed Either a language code or a cache array
1693 */
1694 static function isLocalisationOutOfDate( $cache ) {
1695 if ( !is_array( $cache ) ) {
1696 self::loadLocalisation( $cache );
1697 $cache = self::$mLocalisationCache[$cache];
1698 }
1699 $expired = false;
1700 foreach ( $cache['deps'] as $file => $mtime ) {
1701 if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
1702 $expired = true;
1703 break;
1704 }
1705 }
1706 return $expired;
1707 }
1708
1709 /**
1710 * Get the fallback for a given language
1711 */
1712 static function getFallbackFor( $code ) {
1713 self::loadLocalisation( $code );
1714 return self::$mLocalisationCache[$code]['fallback'];
1715 }
1716
1717 /**
1718 * Get all messages for a given language
1719 */
1720 static function getMessagesFor( $code ) {
1721 self::loadLocalisation( $code );
1722 return self::$mLocalisationCache[$code]['messages'];
1723 }
1724
1725 /**
1726 * Get a message for a given language
1727 */
1728 static function getMessageFor( $key, $code ) {
1729 self::loadLocalisation( $code );
1730 return isset( self::$mLocalisationCache[$code]['messages'][$key] ) ? self::$mLocalisationCache[$code]['messages'][$key] : null;
1731 }
1732
1733 /**
1734 * Load localisation data for this object
1735 */
1736 function load() {
1737 if ( !$this->mLoaded ) {
1738 self::loadLocalisation( $this->getCode() );
1739 $cache =& self::$mLocalisationCache[$this->getCode()];
1740 foreach ( self::$mLocalisationKeys as $key ) {
1741 $this->$key = $cache[$key];
1742 }
1743 $this->mLoaded = true;
1744
1745 $this->fixUpSettings();
1746 }
1747 }
1748
1749 /**
1750 * Do any necessary post-cache-load settings adjustment
1751 */
1752 function fixUpSettings() {
1753 global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk,
1754 $wgNamespaceAliases, $wgAmericanDates;
1755 wfProfileIn( __METHOD__ );
1756 if ( $wgExtraNamespaces ) {
1757 $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames;
1758 }
1759
1760 $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
1761 if ( $wgMetaNamespaceTalk ) {
1762 $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
1763 } else {
1764 $talk = $this->namespaceNames[NS_PROJECT_TALK];
1765 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
1766
1767 # Allow grammar transformations
1768 # Allowing full message-style parsing would make simple requests
1769 # such as action=raw much more expensive than they need to be.
1770 # This will hopefully cover most cases.
1771 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
1772 array( &$this, 'replaceGrammarInNamespace' ), $talk );
1773 $talk = str_replace( ' ', '_', $talk );
1774 $this->namespaceNames[NS_PROJECT_TALK] = $talk;
1775 }
1776
1777 # The above mixing may leave namespaces out of canonical order.
1778 # Re-order by namespace ID number...
1779 ksort( $this->namespaceNames );
1780
1781 # Put namespace names and aliases into a hashtable.
1782 # If this is too slow, then we should arrange it so that it is done
1783 # before caching. The catch is that at pre-cache time, the above
1784 # class-specific fixup hasn't been done.
1785 $this->mNamespaceIds = array();
1786 foreach ( $this->namespaceNames as $index => $name ) {
1787 $this->mNamespaceIds[$this->lc($name)] = $index;
1788 }
1789 if ( $this->namespaceAliases ) {
1790 foreach ( $this->namespaceAliases as $name => $index ) {
1791 $this->mNamespaceIds[$this->lc($name)] = $index;
1792 }
1793 }
1794 if ( $wgNamespaceAliases ) {
1795 foreach ( $wgNamespaceAliases as $name => $index ) {
1796 $this->mNamespaceIds[$this->lc($name)] = $index;
1797 }
1798 }
1799
1800 if ( $this->defaultDateFormat == 'dmy or mdy' ) {
1801 $this->defaultDateFormat = $wgAmericanDates ? 'mdy' : 'dmy';
1802 }
1803 wfProfileOut( __METHOD__ );
1804 }
1805
1806 function replaceGrammarInNamespace( $m ) {
1807 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
1808 }
1809
1810 static function getCaseMaps() {
1811 static $wikiUpperChars, $wikiLowerChars;
1812 if ( isset( $wikiUpperChars ) ) {
1813 return array( $wikiUpperChars, $wikiLowerChars );
1814 }
1815
1816 wfProfileIn( __METHOD__ );
1817 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
1818 if ( $arr === false ) {
1819 throw new MWException(
1820 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
1821 }
1822 extract( $arr );
1823 wfProfileOut( __METHOD__ );
1824 return array( $wikiUpperChars, $wikiLowerChars );
1825 }
1826 }
1827
1828