Adding xjt (to check the number of days in the Hebrew month), fixing methods order...
[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 # Read language names
12 global $wgLanguageNames;
13 require_once( dirname(__FILE__) . '/Names.php' ) ;
14
15 global $wgInputEncoding, $wgOutputEncoding;
16
17 /**
18 * These are always UTF-8, they exist only for backwards compatibility
19 */
20 $wgInputEncoding = "UTF-8";
21 $wgOutputEncoding = "UTF-8";
22
23 if( function_exists( 'mb_strtoupper' ) ) {
24 mb_internal_encoding('UTF-8');
25 }
26
27 /* a fake language converter */
28 class FakeConverter {
29 var $mLang;
30 function FakeConverter($langobj) {$this->mLang = $langobj;}
31 function convert($t, $i) {return $t;}
32 function parserConvert($t, $p) {return $t;}
33 function getVariants() { return array( $this->mLang->getCode() ); }
34 function getPreferredVariant() {return $this->mLang->getCode(); }
35 function findVariantLink(&$l, &$n) {}
36 function getExtraHashOptions() {return '';}
37 function getParsedTitle() {return '';}
38 function markNoConversion($text, $noParse=false) {return $text;}
39 function convertCategoryKey( $key ) {return $key; }
40 function convertLinkToAllVariants($text){ return array( $this->mLang->getCode() => $text); }
41 function armourMath($text){ return $text; }
42 }
43
44 #--------------------------------------------------------------------------
45 # Internationalisation code
46 #--------------------------------------------------------------------------
47
48 class Language {
49 var $mConverter, $mVariants, $mCode, $mLoaded = false;
50 var $mMagicExtensions = array(), $mMagicHookDone = false;
51
52 static public $mLocalisationKeys = array( 'fallback', 'namespaceNames',
53 'skinNames', 'mathNames',
54 'bookstoreList', 'magicWords', 'messages', 'rtl', 'digitTransformTable',
55 'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
56 'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
57 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
58 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases' );
59
60 static public $mMergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
61 'dateFormats', 'defaultUserOptionOverrides', 'magicWords' );
62
63 static public $mMergeableListKeys = array( 'extraUserToggles' );
64
65 static public $mMergeableAliasListKeys = array( 'specialPageAliases' );
66
67 static public $mLocalisationCache = array();
68
69 static public $mWeekdayMsgs = array(
70 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
71 'friday', 'saturday'
72 );
73
74 static public $mWeekdayAbbrevMsgs = array(
75 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
76 );
77
78 static public $mMonthMsgs = array(
79 'january', 'february', 'march', 'april', 'may_long', 'june',
80 'july', 'august', 'september', 'october', 'november',
81 'december'
82 );
83 static public $mMonthGenMsgs = array(
84 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
85 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
86 'december-gen'
87 );
88 static public $mMonthAbbrevMsgs = array(
89 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
90 'sep', 'oct', 'nov', 'dec'
91 );
92
93 static public $mIranianCalendarMonthMsgs = array(
94 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
95 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
96 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
97 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
98 );
99
100 static public $mHebrewCalendarMonthMsgs = array(
101 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
102 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
103 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
104 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
105 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
106 );
107
108 static public $mHebrewCalendarMonthGenMsgs = array(
109 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
110 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
111 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
112 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
113 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
114 );
115
116 /**
117 * Create a language object for a given language code
118 */
119 static function factory( $code ) {
120 global $IP;
121 static $recursionLevel = 0;
122
123 if ( $code == 'en' ) {
124 $class = 'Language';
125 } else {
126 $class = 'Language' . str_replace( '-', '_', ucfirst( $code ) );
127 // Preload base classes to work around APC/PHP5 bug
128 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
129 include_once("$IP/languages/classes/$class.deps.php");
130 }
131 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
132 include_once("$IP/languages/classes/$class.php");
133 }
134 }
135
136 if ( $recursionLevel > 5 ) {
137 throw new MWException( "Language fallback loop detected when creating class $class\n" );
138 }
139
140 if( ! class_exists( $class ) ) {
141 $fallback = Language::getFallbackFor( $code );
142 ++$recursionLevel;
143 $lang = Language::factory( $fallback );
144 --$recursionLevel;
145 $lang->setCode( $code );
146 } else {
147 $lang = new $class;
148 }
149
150 return $lang;
151 }
152
153 function __construct() {
154 $this->mConverter = new FakeConverter($this);
155 // Set the code to the name of the descendant
156 if ( get_class( $this ) == 'Language' ) {
157 $this->mCode = 'en';
158 } else {
159 $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
160 }
161 }
162
163 /**
164 * Hook which will be called if this is the content language.
165 * Descendants can use this to register hook functions or modify globals
166 */
167 function initContLang() {}
168
169 /**
170 * @deprecated
171 * @return array
172 */
173 function getDefaultUserOptions() {
174 return User::getDefaultOptions();
175 }
176
177 function getFallbackLanguageCode() {
178 $this->load();
179 return $this->fallback;
180 }
181
182 /**
183 * Exports $wgBookstoreListEn
184 * @return array
185 */
186 function getBookstoreList() {
187 $this->load();
188 return $this->bookstoreList;
189 }
190
191 /**
192 * @return array
193 */
194 function getNamespaces() {
195 $this->load();
196 return $this->namespaceNames;
197 }
198
199 /**
200 * A convenience function that returns the same thing as
201 * getNamespaces() except with the array values changed to ' '
202 * where it found '_', useful for producing output to be displayed
203 * e.g. in <select> forms.
204 *
205 * @return array
206 */
207 function getFormattedNamespaces() {
208 $ns = $this->getNamespaces();
209 foreach($ns as $k => $v) {
210 $ns[$k] = strtr($v, '_', ' ');
211 }
212 return $ns;
213 }
214
215 /**
216 * Get a namespace value by key
217 * <code>
218 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
219 * echo $mw_ns; // prints 'MediaWiki'
220 * </code>
221 *
222 * @param int $index the array key of the namespace to return
223 * @return mixed, string if the namespace value exists, otherwise false
224 */
225 function getNsText( $index ) {
226 $ns = $this->getNamespaces();
227 return isset( $ns[$index] ) ? $ns[$index] : false;
228 }
229
230 /**
231 * A convenience function that returns the same thing as
232 * getNsText() except with '_' changed to ' ', useful for
233 * producing output.
234 *
235 * @return array
236 */
237 function getFormattedNsText( $index ) {
238 $ns = $this->getNsText( $index );
239 return strtr($ns, '_', ' ');
240 }
241
242 /**
243 * Get a namespace key by value, case insensitive.
244 * Only matches namespace names for the current language, not the
245 * canonical ones defined in Namespace.php.
246 *
247 * @param string $text
248 * @return mixed An integer if $text is a valid value otherwise false
249 */
250 function getLocalNsIndex( $text ) {
251 $this->load();
252 $lctext = $this->lc($text);
253 return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
254 }
255
256 /**
257 * Get a namespace key by value, case insensitive. Canonical namespace
258 * names override custom ones defined for the current language.
259 *
260 * @param string $text
261 * @return mixed An integer if $text is a valid value otherwise false
262 */
263 function getNsIndex( $text ) {
264 $this->load();
265 $lctext = $this->lc($text);
266 if( ( $ns = Namespace::getCanonicalIndex( $lctext ) ) !== null ) return $ns;
267 return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
268 }
269
270 /**
271 * short names for language variants used for language conversion links.
272 *
273 * @param string $code
274 * @return string
275 */
276 function getVariantname( $code ) {
277 return $this->getMessageFromDB( "variantname-$code" );
278 }
279
280 function specialPage( $name ) {
281 $aliases = $this->getSpecialPageAliases();
282 if ( isset( $aliases[$name][0] ) ) {
283 $name = $aliases[$name][0];
284 }
285 return $this->getNsText(NS_SPECIAL) . ':' . $name;
286 }
287
288 function getQuickbarSettings() {
289 return array(
290 $this->getMessage( 'qbsettings-none' ),
291 $this->getMessage( 'qbsettings-fixedleft' ),
292 $this->getMessage( 'qbsettings-fixedright' ),
293 $this->getMessage( 'qbsettings-floatingleft' ),
294 $this->getMessage( 'qbsettings-floatingright' )
295 );
296 }
297
298 function getSkinNames() {
299 $this->load();
300 return $this->skinNames;
301 }
302
303 function getMathNames() {
304 $this->load();
305 return $this->mathNames;
306 }
307
308 function getDatePreferences() {
309 $this->load();
310 return $this->datePreferences;
311 }
312
313 function getDateFormats() {
314 $this->load();
315 return $this->dateFormats;
316 }
317
318 function getDefaultDateFormat() {
319 $this->load();
320 return $this->defaultDateFormat;
321 }
322
323 function getDatePreferenceMigrationMap() {
324 $this->load();
325 return $this->datePreferenceMigrationMap;
326 }
327
328 function getDefaultUserOptionOverrides() {
329 $this->load();
330 # XXX - apparently some languageas get empty arrays, didn't get to it yet -- midom
331 if (is_array($this->defaultUserOptionOverrides)) {
332 return $this->defaultUserOptionOverrides;
333 } else {
334 return array();
335 }
336 }
337
338 function getExtraUserToggles() {
339 $this->load();
340 return $this->extraUserToggles;
341 }
342
343 function getUserToggle( $tog ) {
344 return $this->getMessageFromDB( "tog-$tog" );
345 }
346
347 /**
348 * Get language names, indexed by code.
349 * If $customisedOnly is true, only returns codes with a messages file
350 */
351 public static function getLanguageNames( $customisedOnly = false ) {
352 global $wgLanguageNames, $wgExtraLanguageNames;
353 $allNames = $wgExtraLanguageNames + $wgLanguageNames;
354 if ( !$customisedOnly ) {
355 return $allNames;
356 }
357
358 global $IP;
359 $names = array();
360 $dir = opendir( "$IP/languages/messages" );
361 while( false !== ( $file = readdir( $dir ) ) ) {
362 $m = array();
363 if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
364 $code = str_replace( '_', '-', strtolower( $m[1] ) );
365 if ( isset( $allNames[$code] ) ) {
366 $names[$code] = $allNames[$code];
367 }
368 }
369 }
370 closedir( $dir );
371 return $names;
372 }
373
374 /**
375 * Ugly hack to get a message maybe from the MediaWiki namespace, if this
376 * language object is the content or user language.
377 */
378 function getMessageFromDB( $msg ) {
379 global $wgContLang, $wgLang;
380 if ( $wgContLang->getCode() == $this->getCode() ) {
381 # Content language
382 return wfMsgForContent( $msg );
383 } elseif ( $wgLang->getCode() == $this->getCode() ) {
384 # User language
385 return wfMsg( $msg );
386 } else {
387 # Neither, get from localisation
388 return $this->getMessage( $msg );
389 }
390 }
391
392 function getLanguageName( $code ) {
393 $names = self::getLanguageNames();
394 if ( !array_key_exists( $code, $names ) ) {
395 return '';
396 }
397 return $names[$code];
398 }
399
400 function getMonthName( $key ) {
401 return $this->getMessageFromDB( self::$mMonthMsgs[$key-1] );
402 }
403
404 function getMonthNameGen( $key ) {
405 return $this->getMessageFromDB( self::$mMonthGenMsgs[$key-1] );
406 }
407
408 function getMonthAbbreviation( $key ) {
409 return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key-1] );
410 }
411
412 function getWeekdayName( $key ) {
413 return $this->getMessageFromDB( self::$mWeekdayMsgs[$key-1] );
414 }
415
416 function getWeekdayAbbreviation( $key ) {
417 return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key-1] );
418 }
419
420 function getIranianCalendarMonthName( $key ) {
421 return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key-1] );
422 }
423
424 function getHebrewCalendarMonthName( $key ) {
425 return $this->getMessageFromDB( self::$mHebrewCalendarMonthMsgs[$key-1] );
426 }
427
428 function getHebrewCalendarMonthNameGen( $key ) {
429 return $this->getMessageFromDB( self::$mHebrewCalendarMonthGenMsgs[$key-1] );
430 }
431
432
433 /**
434 * Used by date() and time() to adjust the time output.
435 * @public
436 * @param int $ts the time in date('YmdHis') format
437 * @param mixed $tz adjust the time by this amount (default false,
438 * mean we get user timecorrection setting)
439 * @return int
440 */
441 function userAdjust( $ts, $tz = false ) {
442 global $wgUser, $wgLocalTZoffset;
443
444 if (!$tz) {
445 $tz = $wgUser->getOption( 'timecorrection' );
446 }
447
448 # minutes and hours differences:
449 $minDiff = 0;
450 $hrDiff = 0;
451
452 if ( $tz === '' ) {
453 # Global offset in minutes.
454 if( isset($wgLocalTZoffset) ) {
455 if( $wgLocalTZoffset >= 0 ) {
456 $hrDiff = floor($wgLocalTZoffset / 60);
457 } else {
458 $hrDiff = ceil($wgLocalTZoffset / 60);
459 }
460 $minDiff = $wgLocalTZoffset % 60;
461 }
462 } elseif ( strpos( $tz, ':' ) !== false ) {
463 $tzArray = explode( ':', $tz );
464 $hrDiff = intval($tzArray[0]);
465 $minDiff = intval($hrDiff < 0 ? -$tzArray[1] : $tzArray[1]);
466 } else {
467 $hrDiff = intval( $tz );
468 }
469
470 # No difference ? Return time unchanged
471 if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
472
473 wfSuppressWarnings(); // E_STRICT system time bitching
474 # Generate an adjusted date
475 $t = mktime( (
476 (int)substr( $ts, 8, 2) ) + $hrDiff, # Hours
477 (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
478 (int)substr( $ts, 12, 2 ), # Seconds
479 (int)substr( $ts, 4, 2 ), # Month
480 (int)substr( $ts, 6, 2 ), # Day
481 (int)substr( $ts, 0, 4 ) ); #Year
482
483 $date = date( 'YmdHis', $t );
484 wfRestoreWarnings();
485
486 return $date;
487 }
488
489 /**
490 * This is a workalike of PHP's date() function, but with better
491 * internationalisation, a reduced set of format characters, and a better
492 * escaping format.
493 *
494 * Supported format characters are dDjlNwzWFmMntLYyaAgGhHiscrU. See the
495 * PHP manual for definitions. There are a number of extensions, which
496 * start with "x":
497 *
498 * xn Do not translate digits of the next numeric format character
499 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
500 * xr Use roman numerals for the next numeric format character
501 * xh Use hebrew numerals for the next numeric format character
502 * xx Literal x
503 * xg Genitive month name
504 *
505 * xij j (day number) in Iranian calendar
506 * xiF F (month name) in Iranian calendar
507 * xin n (month number) in Iranian calendar
508 * xiY Y (full year) in Iranian calendar
509 *
510 * xjj j (day number) in Hebrew calendar
511 * xjF F (month name) in Hebrew calendar
512 * xjt t (days in month) in Hebrew calendar
513 * xjx xg (genitive month name) in Hebrew calendar
514 * xjn n (month number) in Hebrew calendar
515 * xjY Y (full year) in Hebrew calendar
516 *
517 * xkY Y (full year) in Thai solar calendar. Months and days are
518 * identical to the Gregorian calendar
519 *
520 * Characters enclosed in double quotes will be considered literal (with
521 * the quotes themselves removed). Unmatched quotes will be considered
522 * literal quotes. Example:
523 *
524 * "The month is" F => The month is January
525 * i's" => 20'11"
526 *
527 * Backslash escaping is also supported.
528 *
529 * Input timestamp is assumed to be pre-normalized to the desired local
530 * time zone, if any.
531 *
532 * @param string $format
533 * @param string $ts 14-character timestamp
534 * YYYYMMDDHHMMSS
535 * 01234567890123
536 */
537 function sprintfDate( $format, $ts ) {
538 $s = '';
539 $raw = false;
540 $roman = false;
541 $hebrewNum = false;
542 $unix = false;
543 $rawToggle = false;
544 $iranian = false;
545 $hebrew = false;
546 $thai = false;
547 for ( $p = 0; $p < strlen( $format ); $p++ ) {
548 $num = false;
549 $code = $format[$p];
550 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
551 $code .= $format[++$p];
552 }
553
554 if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' ) && $p < strlen( $format ) - 1 ) {
555 $code .= $format[++$p];
556 }
557
558 switch ( $code ) {
559 case 'xx':
560 $s .= 'x';
561 break;
562 case 'xn':
563 $raw = true;
564 break;
565 case 'xN':
566 $rawToggle = !$rawToggle;
567 break;
568 case 'xr':
569 $roman = true;
570 break;
571 case 'xh':
572 $hebrewNum = true;
573 break;
574 case 'xg':
575 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
576 break;
577 case 'xjx':
578 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
579 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
580 break;
581 case 'd':
582 $num = substr( $ts, 6, 2 );
583 break;
584 case 'D':
585 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
586 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) + 1 );
587 break;
588 case 'j':
589 $num = intval( substr( $ts, 6, 2 ) );
590 break;
591 case 'xij':
592 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
593 $num = $iranian[2];
594 break;
595 case 'xjj':
596 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
597 $num = $hebrew[2];
598 break;
599 case 'l':
600 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
601 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 );
602 break;
603 case 'N':
604 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
605 $w = gmdate( 'w', $unix );
606 $num = $w ? $w : 7;
607 break;
608 case 'w':
609 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
610 $num = gmdate( 'w', $unix );
611 break;
612 case 'z':
613 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
614 $num = gmdate( 'z', $unix );
615 break;
616 case 'W':
617 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
618 $num = gmdate( 'W', $unix );
619 break;
620 case 'F':
621 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
622 break;
623 case 'xiF':
624 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
625 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
626 break;
627 case 'xjF':
628 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
629 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
630 break;
631 case 'm':
632 $num = substr( $ts, 4, 2 );
633 break;
634 case 'M':
635 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
636 break;
637 case 'n':
638 $num = intval( substr( $ts, 4, 2 ) );
639 break;
640 case 'xin':
641 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
642 $num = $iranian[1];
643 break;
644 case 'xjn':
645 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
646 $num = $hebrew[1];
647 break;
648 case 't':
649 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
650 $num = gmdate( 't', $unix );
651 break;
652 case 'xjt':
653 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
654 $num = $hebrew[3];
655 break;
656 case 'L':
657 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
658 $num = gmdate( 'L', $unix );
659 break;
660 case 'Y':
661 $num = substr( $ts, 0, 4 );
662 break;
663 case 'xiY':
664 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
665 $num = $iranian[0];
666 break;
667 case 'xjY':
668 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
669 $num = $hebrew[0];
670 break;
671 case 'xkY':
672 if ( !$thai ) $thai = self::tsToThai( $ts );
673 $num = $thai[0];
674 break;
675 case 'y':
676 $num = substr( $ts, 2, 2 );
677 break;
678 case 'a':
679 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
680 break;
681 case 'A':
682 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
683 break;
684 case 'g':
685 $h = substr( $ts, 8, 2 );
686 $num = $h % 12 ? $h % 12 : 12;
687 break;
688 case 'G':
689 $num = intval( substr( $ts, 8, 2 ) );
690 break;
691 case 'h':
692 $h = substr( $ts, 8, 2 );
693 $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
694 break;
695 case 'H':
696 $num = substr( $ts, 8, 2 );
697 break;
698 case 'i':
699 $num = substr( $ts, 10, 2 );
700 break;
701 case 's':
702 $num = substr( $ts, 12, 2 );
703 break;
704 case 'c':
705 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
706 $s .= gmdate( 'c', $unix );
707 break;
708 case 'r':
709 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
710 $s .= gmdate( 'r', $unix );
711 break;
712 case 'U':
713 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
714 $num = $unix;
715 break;
716 case '\\':
717 # Backslash escaping
718 if ( $p < strlen( $format ) - 1 ) {
719 $s .= $format[++$p];
720 } else {
721 $s .= '\\';
722 }
723 break;
724 case '"':
725 # Quoted literal
726 if ( $p < strlen( $format ) - 1 ) {
727 $endQuote = strpos( $format, '"', $p + 1 );
728 if ( $endQuote === false ) {
729 # No terminating quote, assume literal "
730 $s .= '"';
731 } else {
732 $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
733 $p = $endQuote;
734 }
735 } else {
736 # Quote at end of string, assume literal "
737 $s .= '"';
738 }
739 break;
740 default:
741 $s .= $format[$p];
742 }
743 if ( $num !== false ) {
744 if ( $rawToggle || $raw ) {
745 $s .= $num;
746 $raw = false;
747 } elseif ( $roman ) {
748 $s .= self::romanNumeral( $num );
749 $roman = false;
750 } elseif( $hebrewNum ) {
751 $s .= self::hebrewNumeral( $num );
752 $hebrewNum = false;
753 } else {
754 $s .= $this->formatNum( $num, true );
755 }
756 $num = false;
757 }
758 }
759 return $s;
760 }
761
762 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
763 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
764 /**
765 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
766 * Gregorian dates to Iranian dates. Originally written in C, it
767 * is released under the terms of GNU Lesser General Public
768 * License. Conversion to PHP was performed by Niklas Laxström.
769 *
770 * Link: http://www.farsiweb.info/jalali/jalali.c
771 */
772 private static function tsToIranian( $ts ) {
773 $gy = substr( $ts, 0, 4 ) -1600;
774 $gm = substr( $ts, 4, 2 ) -1;
775 $gd = substr( $ts, 6, 2 ) -1;
776
777 # Days passed from the beginning (including leap years)
778 $gDayNo = 365*$gy
779 + floor(($gy+3) / 4)
780 - floor(($gy+99) / 100)
781 + floor(($gy+399) / 400);
782
783
784 // Add days of the past months of this year
785 for( $i = 0; $i < $gm; $i++ ) {
786 $gDayNo += self::$GREG_DAYS[$i];
787 }
788
789 // Leap years
790 if ( $gm > 1 && (($gy%4===0 && $gy%100!==0 || ($gy%400==0)))) {
791 $gDayNo++;
792 }
793
794 // Days passed in current month
795 $gDayNo += $gd;
796
797 $jDayNo = $gDayNo - 79;
798
799 $jNp = floor($jDayNo / 12053);
800 $jDayNo %= 12053;
801
802 $jy = 979 + 33*$jNp + 4*floor($jDayNo/1461);
803 $jDayNo %= 1461;
804
805 if ( $jDayNo >= 366 ) {
806 $jy += floor(($jDayNo-1)/365);
807 $jDayNo = floor(($jDayNo-1)%365);
808 }
809
810 for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
811 $jDayNo -= self::$IRANIAN_DAYS[$i];
812 }
813
814 $jm= $i+1;
815 $jd= $jDayNo+1;
816
817 return array($jy, $jm, $jd);
818 }
819
820 /**
821 * Converting Gregorian dates to Hebrew dates.
822 *
823 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
824 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
825 * to translate the relevant functions into PHP and release them under
826 * GNU GPL.
827 */
828 private static function tsToHebrew( $ts ) {
829 # Parse date
830 $year = substr( $ts, 0, 4 );
831 $month = substr( $ts, 4, 2 );
832 $day = substr( $ts, 6, 2 );
833
834 # Month number when March = 1, February = 12
835 $month -= 2;
836 if( $month <= 0 ) {
837 # January or February
838 $month += 12;
839 $year--;
840 }
841
842 # Days since 1 March - calculating 30 days a month,
843 # and then adding the missing number of days
844 $dayOfYear = $day + intval( 7 * $month / 12 + 30 * ( $month - 1 ) );
845 # Calculate Hebrew year for days after 1 Nisan
846 $hebrewYear = $year + 3760;
847 # Passover date for this year (as days since 1 March)
848 $passover = self::passoverDate( $hebrewYear );
849 if( $dayOfYear <= $passover - 15 ) {
850 # Day is before 1 Nisan (passover is 15 Nisan) - it is the previous year
851 # Next year's passover (as days since 1 March)
852 $anchor = $passover;
853 # Add days since previous year's 1 March
854 $dayOfYear += 365;
855 if( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
856 # Leap year
857 $dayOfYear++;
858 }
859 # Previous year
860 $year--;
861 $hebrewYear--;
862 # Passover date for the new year (as days since 1 March)
863 $passover = self::passoverDate( $hebrewYear );
864 } else {
865 # Next year's passover (as days since 1 March)
866 $anchor = self::passoverDate( $hebrewYear + 1 );
867 }
868
869 # Days since 1 Nisan
870 $dayOfYear -= $passover - 15;
871 # Difference between this year's passover date by gregorian calendar,
872 # and the next year's one + 12 days. This should be 1 days for a regular year,
873 # but 0 for incomplete one, 2 for complete, and those + 30 days of Adar I
874 # for a leap year.
875 $anchor -= $passover - 12;
876 $nextYear = $year + 1;
877 if( ( $nextYear % 400 == 0 ) || ( $nextYear % 100 != 0 && $nextYear % 4 == 0 ) ) {
878 # Next year is a leap year - difference is growing
879 $anchor++;
880 }
881
882 # Check the year pattern
883 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
884 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
885 # and non-leap years
886 $yearPattern = $anchor % 30;
887
888 # Calculate day in the month from number of days sine 1 Nisan
889 # Don't check Adar - if the day is not in Adar, we will stop before;
890 # if it is in Adar, we will use it to check if it is Adar I or Adar II
891 $day = $dayOfYear;
892 for( $month = 0; $month < 11; $month++ ) {
893 # Calculate days in this month
894 if( $month == 7 && $yearPattern == 2 ) {
895 # Cheshvan in a complete year (otherwise as the rule below)
896 $days = 30;
897 } else if( $month == 8 && $yearPattern == 0 ) {
898 # Kislev in an incomplete year (otherwise as the rule below)
899 $days = 29;
900 } else {
901 # Even months have 30 days, odd have 29
902 $days = 30 - $month % 2;
903 }
904 if( $day <= $days ) {
905 # In this month
906 break;
907 }
908 # Try in next months
909 $day -= $days;
910 }
911
912 # Now we move to a year from Tishrei
913 if( $month >= 6 ) {
914 # After Tishrei, use next year
915 $hebrewYear++;
916 }
917 # Recalculate month number so that we start from Tishrei
918 $month = ( $month + 6 ) % 12 + 1;
919
920 # Get Adar type and number of days
921 if( $month == 6 ) {
922 # Adar
923 if( $anchor >= 30 ) {
924 # Leap year - check type and set number of days
925 if( $day > 30 ) {
926 # Adar II - 29 days
927 $month = 14;
928 $day -= 30;
929 $days = 29;
930 } else {
931 # Adar I - 30 days
932 $month = 13;
933 $days = 30;
934 }
935 } else {
936 # Non-leap year - just set number of days (29)
937 $days = 29;
938 }
939 }
940
941 return array( $hebrewYear, $month, $day, $days );
942 }
943
944 /**
945 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
946 * Used for Hebrew date.
947 */
948 private static function passoverDate( $year ) {
949 $a = intval( ( 12 * $year + 17 ) % 19 );
950 $b = intval( $year % 4 );
951 $m = 32.044093161144 + 1.5542417966212 * $a + $b / 4.0 - 0.0031777940220923 * $year;
952 if( $m < 0 ) {
953 $m--;
954 }
955 $Mar = intval( $m );
956 if( $m < 0 ) {
957 $m++;
958 }
959 $m -= $Mar;
960
961 $c = intval( ( $Mar + 3 * $year + 5 * $b + 5 ) % 7);
962 if( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
963 $Mar++;
964 } else if( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
965 $Mar += 2;
966 } else if( $c == 2 || $c == 4 || $c == 6 ) {
967 $Mar++;
968 }
969
970 $Mar += intval( ( $year - 3760 ) / 100 ) - intval( ( $year - 3760 ) / 400 ) - 2;
971 return $Mar;
972 }
973
974 /**
975 * Algorithm to convert Gregorian dates to Thai solar dates.
976 *
977 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
978 *
979 * @param string $ts 14-character timestamp
980 * @return array converted year, month, day
981 */
982 private static function tsToThai( $ts ) {
983 $gy = substr( $ts, 0, 4 );
984 $gm = substr( $ts, 4, 2 );
985 $gd = substr( $ts, 6, 2 );
986
987 # Add 543 years to the Gregorian calendar
988 # Months and days are identical
989 $gy_thai = $gy + 543;
990
991 return array( $gy_thai, $gm, $gd );
992 }
993
994
995 /**
996 * Roman number formatting up to 3000
997 */
998 static function romanNumeral( $num ) {
999 static $table = array(
1000 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1001 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1002 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1003 array( '', 'M', 'MM', 'MMM' )
1004 );
1005
1006 $num = intval( $num );
1007 if ( $num > 3000 || $num <= 0 ) {
1008 return $num;
1009 }
1010
1011 $s = '';
1012 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1013 if ( $num >= $pow10 ) {
1014 $s .= $table[$i][floor($num / $pow10)];
1015 }
1016 $num = $num % $pow10;
1017 }
1018 return $s;
1019 }
1020
1021 /**
1022 * Hebrew Gematria number formatting up to 9999
1023 */
1024 static function hebrewNumeral( $num ) {
1025 static $table = array(
1026 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1027 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1028 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1029 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1030 );
1031
1032 $num = intval( $num );
1033 if ( $num > 9999 || $num <= 0 ) {
1034 return $num;
1035 }
1036
1037 $s = '';
1038 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1039 if ( $num >= $pow10 ) {
1040 if ( $num == 15 || $num == 16 ) {
1041 $s .= $table[0][9] . $table[0][$num - 9];
1042 $num = 0;
1043 } else {
1044 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1045 if( $pow10 == 1000 ) {
1046 $s .= "'";
1047 }
1048 }
1049 }
1050 $num = $num % $pow10;
1051 }
1052 if( strlen( $s ) == 2 ) {
1053 $str = $s . "'";
1054 } else {
1055 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1056 $str .= substr( $s, strlen( $s ) - 2, 2 );
1057 }
1058 $start = substr( $str, 0, strlen( $str ) - 2 );
1059 $end = substr( $str, strlen( $str ) - 2 );
1060 switch( $end ) {
1061 case 'כ':
1062 $str = $start . 'ך';
1063 break;
1064 case 'מ':
1065 $str = $start . 'ם';
1066 break;
1067 case 'נ':
1068 $str = $start . 'ן';
1069 break;
1070 case 'פ':
1071 $str = $start . 'ף';
1072 break;
1073 case 'צ':
1074 $str = $start . 'ץ';
1075 break;
1076 }
1077 return $str;
1078 }
1079
1080 /**
1081 * This is meant to be used by time(), date(), and timeanddate() to get
1082 * the date preference they're supposed to use, it should be used in
1083 * all children.
1084 *
1085 *<code>
1086 * function timeanddate([...], $format = true) {
1087 * $datePreference = $this->dateFormat($format);
1088 * [...]
1089 * }
1090 *</code>
1091 *
1092 * @param mixed $usePrefs: if true, the user's preference is used
1093 * if false, the site/language default is used
1094 * if int/string, assumed to be a format.
1095 * @return string
1096 */
1097 function dateFormat( $usePrefs = true ) {
1098 global $wgUser;
1099
1100 if( is_bool( $usePrefs ) ) {
1101 if( $usePrefs ) {
1102 $datePreference = $wgUser->getDatePreference();
1103 } else {
1104 $options = User::getDefaultOptions();
1105 $datePreference = (string)$options['date'];
1106 }
1107 } else {
1108 $datePreference = (string)$usePrefs;
1109 }
1110
1111 // return int
1112 if( $datePreference == '' ) {
1113 return 'default';
1114 }
1115
1116 return $datePreference;
1117 }
1118
1119 /**
1120 * @public
1121 * @param mixed $ts the time format which needs to be turned into a
1122 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1123 * @param bool $adj whether to adjust the time output according to the
1124 * user configured offset ($timecorrection)
1125 * @param mixed $format true to use user's date format preference
1126 * @param string $timecorrection the time offset as returned by
1127 * validateTimeZone() in Special:Preferences
1128 * @return string
1129 */
1130 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1131 $this->load();
1132 if ( $adj ) {
1133 $ts = $this->userAdjust( $ts, $timecorrection );
1134 }
1135
1136 $pref = $this->dateFormat( $format );
1137 if( $pref == 'default' || !isset( $this->dateFormats["$pref date"] ) ) {
1138 $pref = $this->defaultDateFormat;
1139 }
1140 return $this->sprintfDate( $this->dateFormats["$pref date"], $ts );
1141 }
1142
1143 /**
1144 * @public
1145 * @param mixed $ts the time format which needs to be turned into a
1146 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1147 * @param bool $adj whether to adjust the time output according to the
1148 * user configured offset ($timecorrection)
1149 * @param mixed $format true to use user's date format preference
1150 * @param string $timecorrection the time offset as returned by
1151 * validateTimeZone() in Special:Preferences
1152 * @return string
1153 */
1154 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1155 $this->load();
1156 if ( $adj ) {
1157 $ts = $this->userAdjust( $ts, $timecorrection );
1158 }
1159
1160 $pref = $this->dateFormat( $format );
1161 if( $pref == 'default' || !isset( $this->dateFormats["$pref time"] ) ) {
1162 $pref = $this->defaultDateFormat;
1163 }
1164 return $this->sprintfDate( $this->dateFormats["$pref time"], $ts );
1165 }
1166
1167 /**
1168 * @public
1169 * @param mixed $ts the time format which needs to be turned into a
1170 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1171 * @param bool $adj whether to adjust the time output according to the
1172 * user configured offset ($timecorrection)
1173
1174 * @param mixed $format what format to return, if it's false output the
1175 * default one (default true)
1176 * @param string $timecorrection the time offset as returned by
1177 * validateTimeZone() in Special:Preferences
1178 * @return string
1179 */
1180 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
1181 $this->load();
1182
1183 $ts = wfTimestamp( TS_MW, $ts );
1184
1185 if ( $adj ) {
1186 $ts = $this->userAdjust( $ts, $timecorrection );
1187 }
1188
1189 $pref = $this->dateFormat( $format );
1190 if( $pref == 'default' || !isset( $this->dateFormats["$pref both"] ) ) {
1191 $pref = $this->defaultDateFormat;
1192 }
1193
1194 return $this->sprintfDate( $this->dateFormats["$pref both"], $ts );
1195 }
1196
1197 function getMessage( $key ) {
1198 $this->load();
1199 return isset( $this->messages[$key] ) ? $this->messages[$key] : null;
1200 }
1201
1202 function getAllMessages() {
1203 $this->load();
1204 return $this->messages;
1205 }
1206
1207 function iconv( $in, $out, $string ) {
1208 # For most languages, this is a wrapper for iconv
1209 return iconv( $in, $out . '//IGNORE', $string );
1210 }
1211
1212 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
1213 function ucwordbreaksCallbackAscii($matches){
1214 return $this->ucfirst($matches[1]);
1215 }
1216
1217 function ucwordbreaksCallbackMB($matches){
1218 return mb_strtoupper($matches[0]);
1219 }
1220
1221 function ucCallback($matches){
1222 list( $wikiUpperChars ) = self::getCaseMaps();
1223 return strtr( $matches[1], $wikiUpperChars );
1224 }
1225
1226 function lcCallback($matches){
1227 list( , $wikiLowerChars ) = self::getCaseMaps();
1228 return strtr( $matches[1], $wikiLowerChars );
1229 }
1230
1231 function ucwordsCallbackMB($matches){
1232 return mb_strtoupper($matches[0]);
1233 }
1234
1235 function ucwordsCallbackWiki($matches){
1236 list( $wikiUpperChars ) = self::getCaseMaps();
1237 return strtr( $matches[0], $wikiUpperChars );
1238 }
1239
1240 function ucfirst( $str ) {
1241 if ( empty($str) ) return $str;
1242 if ( ord($str[0]) < 128 ) return ucfirst($str);
1243 else return self::uc($str,true); // fall back to more complex logic in case of multibyte strings
1244 }
1245
1246 function uc( $str, $first = false ) {
1247 if ( function_exists( 'mb_strtoupper' ) ) {
1248 if ( $first ) {
1249 if ( self::isMultibyte( $str ) ) {
1250 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1251 } else {
1252 return ucfirst( $str );
1253 }
1254 } else {
1255 return self::isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
1256 }
1257 } else {
1258 if ( self::isMultibyte( $str ) ) {
1259 list( $wikiUpperChars ) = $this->getCaseMaps();
1260 $x = $first ? '^' : '';
1261 return preg_replace_callback(
1262 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1263 array($this,"ucCallback"),
1264 $str
1265 );
1266 } else {
1267 return $first ? ucfirst( $str ) : strtoupper( $str );
1268 }
1269 }
1270 }
1271
1272 function lcfirst( $str ) {
1273 if ( empty($str) ) return $str;
1274 if ( is_string( $str ) && ord($str[0]) < 128 ) {
1275 // editing string in place = cool
1276 $str[0]=strtolower($str[0]);
1277 return $str;
1278 }
1279 else return self::lc( $str, true );
1280 }
1281
1282 function lc( $str, $first = false ) {
1283 if ( function_exists( 'mb_strtolower' ) )
1284 if ( $first )
1285 if ( self::isMultibyte( $str ) )
1286 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1287 else
1288 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
1289 else
1290 return self::isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
1291 else
1292 if ( self::isMultibyte( $str ) ) {
1293 list( , $wikiLowerChars ) = self::getCaseMaps();
1294 $x = $first ? '^' : '';
1295 return preg_replace_callback(
1296 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1297 array($this,"lcCallback"),
1298 $str
1299 );
1300 } else
1301 return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
1302 }
1303
1304 function isMultibyte( $str ) {
1305 return (bool)preg_match( '/[\x80-\xff]/', $str );
1306 }
1307
1308 function ucwords($str) {
1309 if ( self::isMultibyte( $str ) ) {
1310 $str = self::lc($str);
1311
1312 // regexp to find first letter in each word (i.e. after each space)
1313 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1314
1315 // function to use to capitalize a single char
1316 if ( function_exists( 'mb_strtoupper' ) )
1317 return preg_replace_callback(
1318 $replaceRegexp,
1319 array($this,"ucwordsCallbackMB"),
1320 $str
1321 );
1322 else
1323 return preg_replace_callback(
1324 $replaceRegexp,
1325 array($this,"ucwordsCallbackWiki"),
1326 $str
1327 );
1328 }
1329 else
1330 return ucwords( strtolower( $str ) );
1331 }
1332
1333 # capitalize words at word breaks
1334 function ucwordbreaks($str){
1335 if (self::isMultibyte( $str ) ) {
1336 $str = self::lc($str);
1337
1338 // since \b doesn't work for UTF-8, we explicitely define word break chars
1339 $breaks= "[ \-\(\)\}\{\.,\?!]";
1340
1341 // find first letter after word break
1342 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1343
1344 if ( function_exists( 'mb_strtoupper' ) )
1345 return preg_replace_callback(
1346 $replaceRegexp,
1347 array($this,"ucwordbreaksCallbackMB"),
1348 $str
1349 );
1350 else
1351 return preg_replace_callback(
1352 $replaceRegexp,
1353 array($this,"ucwordsCallbackWiki"),
1354 $str
1355 );
1356 }
1357 else
1358 return preg_replace_callback(
1359 '/\b([\w\x80-\xff]+)\b/',
1360 array($this,"ucwordbreaksCallbackAscii"),
1361 $str );
1362 }
1363
1364 /**
1365 * Return a case-folded representation of $s
1366 *
1367 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
1368 * and $s2 are the same except for the case of their characters. It is not
1369 * necessary for the value returned to make sense when displayed.
1370 *
1371 * Do *not* perform any other normalisation in this function. If a caller
1372 * uses this function when it should be using a more general normalisation
1373 * function, then fix the caller.
1374 */
1375 function caseFold( $s ) {
1376 return $this->uc( $s );
1377 }
1378
1379 function checkTitleEncoding( $s ) {
1380 if( is_array( $s ) ) {
1381 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
1382 }
1383 # Check for non-UTF-8 URLs
1384 $ishigh = preg_match( '/[\x80-\xff]/', $s);
1385 if(!$ishigh) return $s;
1386
1387 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1388 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
1389 if( $isutf8 ) return $s;
1390
1391 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
1392 }
1393
1394 function fallback8bitEncoding() {
1395 $this->load();
1396 return $this->fallback8bitEncoding;
1397 }
1398
1399 /**
1400 * Some languages have special punctuation to strip out
1401 * or characters which need to be converted for MySQL's
1402 * indexing to grok it correctly. Make such changes here.
1403 *
1404 * @param string $in
1405 * @return string
1406 */
1407 function stripForSearch( $string ) {
1408 global $wgDBtype;
1409 if ( $wgDBtype != 'mysql' ) {
1410 return $string;
1411 }
1412
1413 # MySQL fulltext index doesn't grok utf-8, so we
1414 # need to fold cases and convert to hex
1415
1416 wfProfileIn( __METHOD__ );
1417 if( function_exists( 'mb_strtolower' ) ) {
1418 $out = preg_replace(
1419 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1420 "'U8' . bin2hex( \"$1\" )",
1421 mb_strtolower( $string ) );
1422 } else {
1423 list( , $wikiLowerChars ) = self::getCaseMaps();
1424 $out = preg_replace(
1425 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1426 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
1427 $string );
1428 }
1429 wfProfileOut( __METHOD__ );
1430 return $out;
1431 }
1432
1433 function convertForSearchResult( $termsArray ) {
1434 # some languages, e.g. Chinese, need to do a conversion
1435 # in order for search results to be displayed correctly
1436 return $termsArray;
1437 }
1438
1439 /**
1440 * Get the first character of a string.
1441 *
1442 * @param string $s
1443 * @return string
1444 */
1445 function firstChar( $s ) {
1446 $matches = array();
1447 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1448 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
1449
1450 return isset( $matches[1] ) ? $matches[1] : "";
1451 }
1452
1453 function initEncoding() {
1454 # Some languages may have an alternate char encoding option
1455 # (Esperanto X-coding, Japanese furigana conversion, etc)
1456 # If this language is used as the primary content language,
1457 # an override to the defaults can be set here on startup.
1458 }
1459
1460 function recodeForEdit( $s ) {
1461 # For some languages we'll want to explicitly specify
1462 # which characters make it into the edit box raw
1463 # or are converted in some way or another.
1464 # Note that if wgOutputEncoding is different from
1465 # wgInputEncoding, this text will be further converted
1466 # to wgOutputEncoding.
1467 global $wgEditEncoding;
1468 if( $wgEditEncoding == '' or
1469 $wgEditEncoding == 'UTF-8' ) {
1470 return $s;
1471 } else {
1472 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
1473 }
1474 }
1475
1476 function recodeInput( $s ) {
1477 # Take the previous into account.
1478 global $wgEditEncoding;
1479 if($wgEditEncoding != "") {
1480 $enc = $wgEditEncoding;
1481 } else {
1482 $enc = 'UTF-8';
1483 }
1484 if( $enc == 'UTF-8' ) {
1485 return $s;
1486 } else {
1487 return $this->iconv( $enc, 'UTF-8', $s );
1488 }
1489 }
1490
1491 /**
1492 * For right-to-left language support
1493 *
1494 * @return bool
1495 */
1496 function isRTL() {
1497 $this->load();
1498 return $this->rtl;
1499 }
1500
1501 /**
1502 * A hidden direction mark (LRM or RLM), depending on the language direction
1503 *
1504 * @return string
1505 */
1506 function getDirMark() {
1507 return $this->isRTL() ? "\xE2\x80\x8F" : "\xE2\x80\x8E";
1508 }
1509
1510 /**
1511 * An arrow, depending on the language direction
1512 *
1513 * @return string
1514 */
1515 function getArrow() {
1516 return $this->isRTL() ? '←' : '→';
1517 }
1518
1519 /**
1520 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
1521 *
1522 * @return bool
1523 */
1524 function linkPrefixExtension() {
1525 $this->load();
1526 return $this->linkPrefixExtension;
1527 }
1528
1529 function &getMagicWords() {
1530 $this->load();
1531 return $this->magicWords;
1532 }
1533
1534 # Fill a MagicWord object with data from here
1535 function getMagic( &$mw ) {
1536 if ( !$this->mMagicHookDone ) {
1537 $this->mMagicHookDone = true;
1538 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
1539 }
1540 if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
1541 $rawEntry = $this->mMagicExtensions[$mw->mId];
1542 } else {
1543 $magicWords =& $this->getMagicWords();
1544 if ( isset( $magicWords[$mw->mId] ) ) {
1545 $rawEntry = $magicWords[$mw->mId];
1546 } else {
1547 # Fall back to English if local list is incomplete
1548 $magicWords =& Language::getMagicWords();
1549 $rawEntry = $magicWords[$mw->mId];
1550 }
1551 }
1552
1553 if( !is_array( $rawEntry ) ) {
1554 error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
1555 } else {
1556 $mw->mCaseSensitive = $rawEntry[0];
1557 $mw->mSynonyms = array_slice( $rawEntry, 1 );
1558 }
1559 }
1560
1561 /**
1562 * Add magic words to the extension array
1563 */
1564 function addMagicWordsByLang( $newWords ) {
1565 $code = $this->getCode();
1566 $fallbackChain = array();
1567 while ( $code && !in_array( $code, $fallbackChain ) ) {
1568 $fallbackChain[] = $code;
1569 $code = self::getFallbackFor( $code );
1570 }
1571 if ( !in_array( 'en', $fallbackChain ) ) {
1572 $fallbackChain[] = 'en';
1573 }
1574 $fallbackChain = array_reverse( $fallbackChain );
1575 foreach ( $fallbackChain as $code ) {
1576 if ( isset( $newWords[$code] ) ) {
1577 $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
1578 }
1579 }
1580 }
1581
1582 /**
1583 * Get special page names, as an associative array
1584 * case folded alias => real name
1585 */
1586 function getSpecialPageAliases() {
1587 $this->load();
1588 if ( !isset( $this->mExtendedSpecialPageAliases ) ) {
1589 $this->mExtendedSpecialPageAliases = $this->specialPageAliases;
1590 wfRunHooks( 'LanguageGetSpecialPageAliases',
1591 array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
1592 }
1593 return $this->mExtendedSpecialPageAliases;
1594 }
1595
1596 /**
1597 * Italic is unsuitable for some languages
1598 *
1599 * @public
1600 *
1601 * @param string $text The text to be emphasized.
1602 * @return string
1603 */
1604 function emphasize( $text ) {
1605 return "<em>$text</em>";
1606 }
1607
1608 /**
1609 * Normally we output all numbers in plain en_US style, that is
1610 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
1611 * point twohundredthirtyfive. However this is not sutable for all
1612 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
1613 * Icelandic just want to use commas instead of dots, and dots instead
1614 * of commas like "293.291,235".
1615 *
1616 * An example of this function being called:
1617 * <code>
1618 * wfMsg( 'message', $wgLang->formatNum( $num ) )
1619 * </code>
1620 *
1621 * See LanguageGu.php for the Gujarati implementation and
1622 * LanguageIs.php for the , => . and . => , implementation.
1623 *
1624 * @todo check if it's viable to use localeconv() for the decimal
1625 * seperator thing.
1626 * @public
1627 * @param mixed $number the string to be formatted, should be an integer or
1628 * a floating point number.
1629 * @param bool $nocommafy Set to true for special numbers like dates
1630 * @return string
1631 */
1632 function formatNum( $number, $nocommafy = false ) {
1633 global $wgTranslateNumerals;
1634 if (!$nocommafy) {
1635 $number = $this->commafy($number);
1636 $s = $this->separatorTransformTable();
1637 if (!is_null($s)) { $number = strtr($number, $s); }
1638 }
1639
1640 if ($wgTranslateNumerals) {
1641 $s = $this->digitTransformTable();
1642 if (!is_null($s)) { $number = strtr($number, $s); }
1643 }
1644
1645 return $number;
1646 }
1647
1648 function parseFormattedNumber( $number ) {
1649 $s = $this->digitTransformTable();
1650 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1651
1652 $s = $this->separatorTransformTable();
1653 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1654
1655 $number = strtr( $number, array (',' => '') );
1656 return $number;
1657 }
1658
1659 /**
1660 * Adds commas to a given number
1661 *
1662 * @param mixed $_
1663 * @return string
1664 */
1665 function commafy($_) {
1666 return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
1667 }
1668
1669 function digitTransformTable() {
1670 $this->load();
1671 return $this->digitTransformTable;
1672 }
1673
1674 function separatorTransformTable() {
1675 $this->load();
1676 return $this->separatorTransformTable;
1677 }
1678
1679
1680 /**
1681 * For the credit list in includes/Credits.php (action=credits)
1682 *
1683 * @param array $l
1684 * @return string
1685 */
1686 function listToText( $l ) {
1687 $s = '';
1688 $m = count($l) - 1;
1689 for ($i = $m; $i >= 0; $i--) {
1690 if ($i == $m) {
1691 $s = $l[$i];
1692 } else if ($i == $m - 1) {
1693 $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
1694 } else {
1695 $s = $l[$i] . ', ' . $s;
1696 }
1697 }
1698 return $s;
1699 }
1700
1701 /**
1702 * Truncate a string to a specified length in bytes, appending an optional
1703 * string (e.g. for ellipses)
1704 *
1705 * The database offers limited byte lengths for some columns in the database;
1706 * multi-byte character sets mean we need to ensure that only whole characters
1707 * are included, otherwise broken characters can be passed to the user
1708 *
1709 * If $length is negative, the string will be truncated from the beginning
1710 *
1711 * @param string $string String to truncate
1712 * @param int $length Maximum length (excluding ellipses)
1713 * @param string $ellipses String to append to the truncated text
1714 * @return string
1715 */
1716 function truncate( $string, $length, $ellipsis = "" ) {
1717 if( $length == 0 ) {
1718 return $ellipsis;
1719 }
1720 if ( strlen( $string ) <= abs( $length ) ) {
1721 return $string;
1722 }
1723 if( $length > 0 ) {
1724 $string = substr( $string, 0, $length );
1725 $char = ord( $string[strlen( $string ) - 1] );
1726 $m = array();
1727 if ($char >= 0xc0) {
1728 # We got the first byte only of a multibyte char; remove it.
1729 $string = substr( $string, 0, -1 );
1730 } elseif( $char >= 0x80 &&
1731 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
1732 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
1733 # We chopped in the middle of a character; remove it
1734 $string = $m[1];
1735 }
1736 return $string . $ellipsis;
1737 } else {
1738 $string = substr( $string, $length );
1739 $char = ord( $string[0] );
1740 if( $char >= 0x80 && $char < 0xc0 ) {
1741 # We chopped in the middle of a character; remove the whole thing
1742 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
1743 }
1744 return $ellipsis . $string;
1745 }
1746 }
1747
1748 /**
1749 * Grammatical transformations, needed for inflected languages
1750 * Invoked by putting {{grammar:case|word}} in a message
1751 *
1752 * @param string $word
1753 * @param string $case
1754 * @return string
1755 */
1756 function convertGrammar( $word, $case ) {
1757 global $wgGrammarForms;
1758 if ( isset($wgGrammarForms['en'][$case][$word]) ) {
1759 return $wgGrammarForms['en'][$case][$word];
1760 }
1761 return $word;
1762 }
1763
1764 /**
1765 * Plural form transformations, needed for some languages.
1766 * For example, there are 3 form of plural in Russian and Polish,
1767 * depending on "count mod 10". See [[w:Plural]]
1768 * For English it is pretty simple.
1769 *
1770 * Invoked by putting {{plural:count|wordform1|wordform2}}
1771 * or {{plural:count|wordform1|wordform2|wordform3}}
1772 *
1773 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
1774 *
1775 * @param integer $count Non-localized number
1776 * @param array $forms Different plural forms
1777 * @return string Correct form of plural for $count in this language
1778 */
1779 function convertPlural( $count, $forms ) {
1780 if ( !count($forms) ) { return ''; }
1781 $forms = $this->preConvertPlural( $forms, 2 );
1782
1783 return ( abs($count) == 1 ) ? $forms[0] : $forms[1];
1784 }
1785
1786 /**
1787 * Checks that convertPlural was given an array and pads it to requested
1788 * amound of forms by copying the last one.
1789 *
1790 * @param integer $count How many forms should there be at least
1791 * @param array $forms Array of forms given to convertPlural
1792 * @return array Padded array of forms or an exception if not an array
1793 */
1794 protected function preConvertPlural( Array $forms, $count ) {
1795 while ( count($forms) < $count ) {
1796 $forms[] = $forms[count($forms)-1];
1797 }
1798 return $forms;
1799 }
1800
1801 /**
1802 * For translaing of expiry times
1803 * @param string The validated block time in English
1804 * @return Somehow translated block time
1805 * @see LanguageFi.php for example implementation
1806 */
1807 function translateBlockExpiry( $str ) {
1808
1809 $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
1810
1811 if ( $scBlockExpiryOptions == '-') {
1812 return $str;
1813 }
1814
1815 foreach (explode(',', $scBlockExpiryOptions) as $option) {
1816 if ( strpos($option, ":") === false )
1817 continue;
1818 list($show, $value) = explode(":", $option);
1819 if ( strcmp ( $str, $value) == 0 ) {
1820 return htmlspecialchars( trim( $show ) );
1821 }
1822 }
1823
1824 return $str;
1825 }
1826
1827 /**
1828 * languages like Chinese need to be segmented in order for the diff
1829 * to be of any use
1830 *
1831 * @param string $text
1832 * @return string
1833 */
1834 function segmentForDiff( $text ) {
1835 return $text;
1836 }
1837
1838 /**
1839 * and unsegment to show the result
1840 *
1841 * @param string $text
1842 * @return string
1843 */
1844 function unsegmentForDiff( $text ) {
1845 return $text;
1846 }
1847
1848 # convert text to different variants of a language.
1849 function convert( $text, $isTitle = false) {
1850 return $this->mConverter->convert($text, $isTitle);
1851 }
1852
1853 # Convert text from within Parser
1854 function parserConvert( $text, &$parser ) {
1855 return $this->mConverter->parserConvert( $text, $parser );
1856 }
1857
1858 # Check if this is a language with variants
1859 function hasVariants(){
1860 return sizeof($this->getVariants())>1;
1861 }
1862
1863 # Put custom tags (e.g. -{ }-) around math to prevent conversion
1864 function armourMath($text){
1865 return $this->mConverter->armourMath($text);
1866 }
1867
1868
1869 /**
1870 * Perform output conversion on a string, and encode for safe HTML output.
1871 * @param string $text
1872 * @param bool $isTitle -- wtf?
1873 * @return string
1874 * @todo this should get integrated somewhere sane
1875 */
1876 function convertHtml( $text, $isTitle = false ) {
1877 return htmlspecialchars( $this->convert( $text, $isTitle ) );
1878 }
1879
1880 function convertCategoryKey( $key ) {
1881 return $this->mConverter->convertCategoryKey( $key );
1882 }
1883
1884 /**
1885 * get the list of variants supported by this langauge
1886 * see sample implementation in LanguageZh.php
1887 *
1888 * @return array an array of language codes
1889 */
1890 function getVariants() {
1891 return $this->mConverter->getVariants();
1892 }
1893
1894
1895 function getPreferredVariant( $fromUser = true ) {
1896 return $this->mConverter->getPreferredVariant( $fromUser );
1897 }
1898
1899 /**
1900 * if a language supports multiple variants, it is
1901 * possible that non-existing link in one variant
1902 * actually exists in another variant. this function
1903 * tries to find it. See e.g. LanguageZh.php
1904 *
1905 * @param string $link the name of the link
1906 * @param mixed $nt the title object of the link
1907 * @return null the input parameters may be modified upon return
1908 */
1909 function findVariantLink( &$link, &$nt ) {
1910 $this->mConverter->findVariantLink($link, $nt);
1911 }
1912
1913 /**
1914 * If a language supports multiple variants, converts text
1915 * into an array of all possible variants of the text:
1916 * 'variant' => text in that variant
1917 */
1918
1919 function convertLinkToAllVariants($text){
1920 return $this->mConverter->convertLinkToAllVariants($text);
1921 }
1922
1923
1924 /**
1925 * returns language specific options used by User::getPageRenderHash()
1926 * for example, the preferred language variant
1927 *
1928 * @return string
1929 * @public
1930 */
1931 function getExtraHashOptions() {
1932 return $this->mConverter->getExtraHashOptions();
1933 }
1934
1935 /**
1936 * for languages that support multiple variants, the title of an
1937 * article may be displayed differently in different variants. this
1938 * function returns the apporiate title defined in the body of the article.
1939 *
1940 * @return string
1941 */
1942 function getParsedTitle() {
1943 return $this->mConverter->getParsedTitle();
1944 }
1945
1946 /**
1947 * Enclose a string with the "no conversion" tag. This is used by
1948 * various functions in the Parser
1949 *
1950 * @param string $text text to be tagged for no conversion
1951 * @return string the tagged text
1952 */
1953 function markNoConversion( $text, $noParse=false ) {
1954 return $this->mConverter->markNoConversion( $text, $noParse );
1955 }
1956
1957 /**
1958 * A regular expression to match legal word-trailing characters
1959 * which should be merged onto a link of the form [[foo]]bar.
1960 *
1961 * @return string
1962 * @public
1963 */
1964 function linkTrail() {
1965 $this->load();
1966 return $this->linkTrail;
1967 }
1968
1969 function getLangObj() {
1970 return $this;
1971 }
1972
1973 /**
1974 * Get the RFC 3066 code for this language object
1975 */
1976 function getCode() {
1977 return $this->mCode;
1978 }
1979
1980 function setCode( $code ) {
1981 $this->mCode = $code;
1982 }
1983
1984 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
1985 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
1986 }
1987
1988 static function getMessagesFileName( $code ) {
1989 global $IP;
1990 return self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
1991 }
1992
1993 static function getClassFileName( $code ) {
1994 global $IP;
1995 return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
1996 }
1997
1998 static function getLocalisationArray( $code, $disableCache = false ) {
1999 self::loadLocalisation( $code, $disableCache );
2000 return self::$mLocalisationCache[$code];
2001 }
2002
2003 /**
2004 * Load localisation data for a given code into the static cache
2005 *
2006 * @return array Dependencies, map of filenames to mtimes
2007 */
2008 static function loadLocalisation( $code, $disableCache = false ) {
2009 static $recursionGuard = array();
2010 global $wgMemc, $wgCheckSerialized;
2011
2012 if ( !$code ) {
2013 throw new MWException( "Invalid language code requested" );
2014 }
2015
2016 if ( !$disableCache ) {
2017 # Try the per-process cache
2018 if ( isset( self::$mLocalisationCache[$code] ) ) {
2019 return self::$mLocalisationCache[$code]['deps'];
2020 }
2021
2022 wfProfileIn( __METHOD__ );
2023
2024 # Try the serialized directory
2025 $cache = wfGetPrecompiledData( self::getFileName( "Messages", $code, '.ser' ) );
2026 if ( $cache ) {
2027 if ( $wgCheckSerialized && self::isLocalisationOutOfDate( $cache ) ) {
2028 $cache = false;
2029 wfDebug( "Language::loadLocalisation(): precompiled data file for $code is out of date\n" );
2030 } else {
2031 self::$mLocalisationCache[$code] = $cache;
2032 wfDebug( "Language::loadLocalisation(): got localisation for $code from precompiled data file\n" );
2033 wfProfileOut( __METHOD__ );
2034 return self::$mLocalisationCache[$code]['deps'];
2035 }
2036 }
2037
2038 # Try the global cache
2039 $memcKey = wfMemcKey('localisation', $code );
2040 $cache = $wgMemc->get( $memcKey );
2041 if ( $cache ) {
2042 if ( self::isLocalisationOutOfDate( $cache ) ) {
2043 $wgMemc->delete( $memcKey );
2044 $cache = false;
2045 wfDebug( "Language::loadLocalisation(): localisation cache for $code had expired\n" );
2046 } else {
2047 self::$mLocalisationCache[$code] = $cache;
2048 wfDebug( "Language::loadLocalisation(): got localisation for $code from cache\n" );
2049 wfProfileOut( __METHOD__ );
2050 return $cache['deps'];
2051 }
2052 }
2053 } else {
2054 wfProfileIn( __METHOD__ );
2055 }
2056
2057 # Default fallback, may be overridden when the messages file is included
2058 if ( $code != 'en' ) {
2059 $fallback = 'en';
2060 } else {
2061 $fallback = false;
2062 }
2063
2064 # Load the primary localisation from the source file
2065 $filename = self::getMessagesFileName( $code );
2066 if ( !file_exists( $filename ) ) {
2067 wfDebug( "Language::loadLocalisation(): no localisation file for $code, using implicit fallback to en\n" );
2068 $cache = array();
2069 $deps = array();
2070 } else {
2071 $deps = array( $filename => filemtime( $filename ) );
2072 require( $filename );
2073 $cache = compact( self::$mLocalisationKeys );
2074 wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
2075 }
2076
2077 if ( !empty( $fallback ) ) {
2078 # Load the fallback localisation, with a circular reference guard
2079 if ( isset( $recursionGuard[$code] ) ) {
2080 throw new MWException( "Error: Circular fallback reference in language code $code" );
2081 }
2082 $recursionGuard[$code] = true;
2083 $newDeps = self::loadLocalisation( $fallback, $disableCache );
2084 unset( $recursionGuard[$code] );
2085
2086 $secondary = self::$mLocalisationCache[$fallback];
2087 $deps = array_merge( $deps, $newDeps );
2088
2089 # Merge the fallback localisation with the current localisation
2090 foreach ( self::$mLocalisationKeys as $key ) {
2091 if ( isset( $cache[$key] ) ) {
2092 if ( isset( $secondary[$key] ) ) {
2093 if ( in_array( $key, self::$mMergeableMapKeys ) ) {
2094 $cache[$key] = $cache[$key] + $secondary[$key];
2095 } elseif ( in_array( $key, self::$mMergeableListKeys ) ) {
2096 $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
2097 } elseif ( in_array( $key, self::$mMergeableAliasListKeys ) ) {
2098 $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
2099 }
2100 }
2101 } else {
2102 $cache[$key] = $secondary[$key];
2103 }
2104 }
2105
2106 # Merge bookstore lists if requested
2107 if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
2108 $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
2109 }
2110 if ( isset( $cache['bookstoreList']['inherit'] ) ) {
2111 unset( $cache['bookstoreList']['inherit'] );
2112 }
2113 }
2114
2115 # Add dependencies to the cache entry
2116 $cache['deps'] = $deps;
2117
2118 # Replace spaces with underscores in namespace names
2119 $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
2120
2121 # Save to both caches
2122 self::$mLocalisationCache[$code] = $cache;
2123 if ( !$disableCache ) {
2124 $wgMemc->set( $memcKey, $cache );
2125 }
2126
2127 wfProfileOut( __METHOD__ );
2128 return $deps;
2129 }
2130
2131 /**
2132 * Test if a given localisation cache is out of date with respect to the
2133 * source Messages files. This is done automatically for the global cache
2134 * in $wgMemc, but is only done on certain occasions for the serialized
2135 * data file.
2136 *
2137 * @param $cache mixed Either a language code or a cache array
2138 */
2139 static function isLocalisationOutOfDate( $cache ) {
2140 if ( !is_array( $cache ) ) {
2141 self::loadLocalisation( $cache );
2142 $cache = self::$mLocalisationCache[$cache];
2143 }
2144 $expired = false;
2145 foreach ( $cache['deps'] as $file => $mtime ) {
2146 if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
2147 $expired = true;
2148 break;
2149 }
2150 }
2151 return $expired;
2152 }
2153
2154 /**
2155 * Get the fallback for a given language
2156 */
2157 static function getFallbackFor( $code ) {
2158 self::loadLocalisation( $code );
2159 return self::$mLocalisationCache[$code]['fallback'];
2160 }
2161
2162 /**
2163 * Get all messages for a given language
2164 */
2165 static function getMessagesFor( $code ) {
2166 self::loadLocalisation( $code );
2167 return self::$mLocalisationCache[$code]['messages'];
2168 }
2169
2170 /**
2171 * Get a message for a given language
2172 */
2173 static function getMessageFor( $key, $code ) {
2174 self::loadLocalisation( $code );
2175 return isset( self::$mLocalisationCache[$code]['messages'][$key] ) ? self::$mLocalisationCache[$code]['messages'][$key] : null;
2176 }
2177
2178 /**
2179 * Load localisation data for this object
2180 */
2181 function load() {
2182 if ( !$this->mLoaded ) {
2183 self::loadLocalisation( $this->getCode() );
2184 $cache =& self::$mLocalisationCache[$this->getCode()];
2185 foreach ( self::$mLocalisationKeys as $key ) {
2186 $this->$key = $cache[$key];
2187 }
2188 $this->mLoaded = true;
2189
2190 $this->fixUpSettings();
2191 }
2192 }
2193
2194 /**
2195 * Do any necessary post-cache-load settings adjustment
2196 */
2197 function fixUpSettings() {
2198 global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk,
2199 $wgNamespaceAliases, $wgAmericanDates;
2200 wfProfileIn( __METHOD__ );
2201 if ( $wgExtraNamespaces ) {
2202 $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames;
2203 }
2204
2205 $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
2206 if ( $wgMetaNamespaceTalk ) {
2207 $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
2208 } else {
2209 $talk = $this->namespaceNames[NS_PROJECT_TALK];
2210 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
2211
2212 # Allow grammar transformations
2213 # Allowing full message-style parsing would make simple requests
2214 # such as action=raw much more expensive than they need to be.
2215 # This will hopefully cover most cases.
2216 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
2217 array( &$this, 'replaceGrammarInNamespace' ), $talk );
2218 $talk = str_replace( ' ', '_', $talk );
2219 $this->namespaceNames[NS_PROJECT_TALK] = $talk;
2220 }
2221
2222 # The above mixing may leave namespaces out of canonical order.
2223 # Re-order by namespace ID number...
2224 ksort( $this->namespaceNames );
2225
2226 # Put namespace names and aliases into a hashtable.
2227 # If this is too slow, then we should arrange it so that it is done
2228 # before caching. The catch is that at pre-cache time, the above
2229 # class-specific fixup hasn't been done.
2230 $this->mNamespaceIds = array();
2231 foreach ( $this->namespaceNames as $index => $name ) {
2232 $this->mNamespaceIds[$this->lc($name)] = $index;
2233 }
2234 if ( $this->namespaceAliases ) {
2235 foreach ( $this->namespaceAliases as $name => $index ) {
2236 $this->mNamespaceIds[$this->lc($name)] = $index;
2237 }
2238 }
2239 if ( $wgNamespaceAliases ) {
2240 foreach ( $wgNamespaceAliases as $name => $index ) {
2241 $this->mNamespaceIds[$this->lc($name)] = $index;
2242 }
2243 }
2244
2245 if ( $this->defaultDateFormat == 'dmy or mdy' ) {
2246 $this->defaultDateFormat = $wgAmericanDates ? 'mdy' : 'dmy';
2247 }
2248 wfProfileOut( __METHOD__ );
2249 }
2250
2251 function replaceGrammarInNamespace( $m ) {
2252 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
2253 }
2254
2255 static function getCaseMaps() {
2256 static $wikiUpperChars, $wikiLowerChars;
2257 if ( isset( $wikiUpperChars ) ) {
2258 return array( $wikiUpperChars, $wikiLowerChars );
2259 }
2260
2261 wfProfileIn( __METHOD__ );
2262 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
2263 if ( $arr === false ) {
2264 throw new MWException(
2265 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
2266 }
2267 extract( $arr );
2268 wfProfileOut( __METHOD__ );
2269 return array( $wikiUpperChars, $wikiLowerChars );
2270 }
2271
2272 function formatTimePeriod( $seconds ) {
2273 if ( $seconds < 10 ) {
2274 return $this->formatNum( sprintf( "%.1f", $seconds ) ) . wfMsg( 'seconds-abbrev' );
2275 } elseif ( $seconds < 60 ) {
2276 return $this->formatNum( round( $seconds ) ) . wfMsg( 'seconds-abbrev' );
2277 } elseif ( $seconds < 3600 ) {
2278 return $this->formatNum( floor( $seconds / 60 ) ) . wfMsg( 'minutes-abbrev' ) .
2279 $this->formatNum( round( fmod( $seconds, 60 ) ) ) . wfMsg( 'seconds-abbrev' );
2280 } else {
2281 $hours = floor( $seconds / 3600 );
2282 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
2283 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
2284 return $this->formatNum( $hours ) . wfMsg( 'hours-abbrev' ) .
2285 $this->formatNum( $minutes ) . wfMsg( 'minutes-abbrev' ) .
2286 $this->formatNum( $secondsPart ) . wfMsg( 'seconds-abbrev' );
2287 }
2288 }
2289
2290 function formatBitrate( $bps ) {
2291 $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
2292 if ( $bps <= 0 ) {
2293 return $this->formatNum( $bps ) . $units[0];
2294 }
2295 $unitIndex = floor( log10( $bps ) / 3 );
2296 $mantissa = $bps / pow( 1000, $unitIndex );
2297 if ( $mantissa < 10 ) {
2298 $mantissa = round( $mantissa, 1 );
2299 } else {
2300 $mantissa = round( $mantissa );
2301 }
2302 return $this->formatNum( $mantissa ) . $units[$unitIndex];
2303 }
2304
2305 /**
2306 * Format a size in bytes for output, using an appropriate
2307 * unit (B, KB, MB or GB) according to the magnitude in question
2308 *
2309 * @param $size Size to format
2310 * @return string Plain text (not HTML)
2311 */
2312 function formatSize( $size ) {
2313 // For small sizes no decimal places necessary
2314 $round = 0;
2315 if( $size > 1024 ) {
2316 $size = $size / 1024;
2317 if( $size > 1024 ) {
2318 $size = $size / 1024;
2319 // For MB and bigger two decimal places are smarter
2320 $round = 2;
2321 if( $size > 1024 ) {
2322 $size = $size / 1024;
2323 $msg = 'size-gigabytes';
2324 } else {
2325 $msg = 'size-megabytes';
2326 }
2327 } else {
2328 $msg = 'size-kilobytes';
2329 }
2330 } else {
2331 $msg = 'size-bytes';
2332 }
2333 $size = round( $size, $round );
2334 $text = $this->getMessageFromDB( $msg );
2335 return str_replace( '$1', $this->formatNum( $size ), $text );
2336 }
2337 }