Add comment
[mediawiki.git] / languages / Language.php
blob66cb762534298d30856107db338cb54598310bde
1 <?php
2 /**
3 * @addtogroup Language
4 */
6 if( !defined( 'MEDIAWIKI' ) ) {
7 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
8 exit( 1 );
11 # Read language names
12 global $wgLanguageNames;
13 require_once( dirname(__FILE__) . '/Names.php' ) ;
15 global $wgInputEncoding, $wgOutputEncoding;
17 /**
18 * These are always UTF-8, they exist only for backwards compatibility
20 $wgInputEncoding = "UTF-8";
21 $wgOutputEncoding = "UTF-8";
23 if( function_exists( 'mb_strtoupper' ) ) {
24 mb_internal_encoding('UTF-8');
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; }
44 #--------------------------------------------------------------------------
45 # Internationalisation code
46 #--------------------------------------------------------------------------
48 class Language {
49 var $mConverter, $mVariants, $mCode, $mLoaded = false;
50 var $mMagicExtensions = array(), $mMagicHookDone = false;
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' );
60 static public $mMergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
61 'dateFormats', 'defaultUserOptionOverrides', 'magicWords' );
63 static public $mMergeableListKeys = array( 'extraUserToggles' );
65 static public $mMergeableAliasListKeys = array( 'specialPageAliases' );
67 static public $mLocalisationCache = array();
69 static public $mWeekdayMsgs = array(
70 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
71 'friday', 'saturday'
74 static public $mWeekdayAbbrevMsgs = array(
75 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
78 static public $mMonthMsgs = array(
79 'january', 'february', 'march', 'april', 'may_long', 'june',
80 'july', 'august', 'september', 'october', 'november',
81 'december'
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'
88 static public $mMonthAbbrevMsgs = array(
89 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
90 'sep', 'oct', 'nov', 'dec'
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'
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'
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'
117 * Create a language object for a given language code
119 static function factory( $code ) {
120 global $IP;
121 static $recursionLevel = 0;
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");
131 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
132 include_once("$IP/languages/classes/$class.php");
136 if ( $recursionLevel > 5 ) {
137 throw new MWException( "Language fallback loop detected when creating class $class\n" );
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;
150 return $lang;
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 ) ) );
164 * Hook which will be called if this is the content language.
165 * Descendants can use this to register hook functions or modify globals
167 function initContLang() {}
170 * @deprecated
171 * @return array
173 function getDefaultUserOptions() {
174 trigger_error( 'Use of ' . __METHOD__ . ' is deprecated', E_USER_NOTICE );
175 return User::getDefaultOptions();
178 function getFallbackLanguageCode() {
179 $this->load();
180 return $this->fallback;
184 * Exports $wgBookstoreListEn
185 * @return array
187 function getBookstoreList() {
188 $this->load();
189 return $this->bookstoreList;
193 * @return array
195 function getNamespaces() {
196 $this->load();
197 return $this->namespaceNames;
201 * A convenience function that returns the same thing as
202 * getNamespaces() except with the array values changed to ' '
203 * where it found '_', useful for producing output to be displayed
204 * e.g. in <select> forms.
206 * @return array
208 function getFormattedNamespaces() {
209 $ns = $this->getNamespaces();
210 foreach($ns as $k => $v) {
211 $ns[$k] = strtr($v, '_', ' ');
213 return $ns;
217 * Get a namespace value by key
218 * <code>
219 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
220 * echo $mw_ns; // prints 'MediaWiki'
221 * </code>
223 * @param int $index the array key of the namespace to return
224 * @return mixed, string if the namespace value exists, otherwise false
226 function getNsText( $index ) {
227 $ns = $this->getNamespaces();
228 return isset( $ns[$index] ) ? $ns[$index] : false;
232 * A convenience function that returns the same thing as
233 * getNsText() except with '_' changed to ' ', useful for
234 * producing output.
236 * @return array
238 function getFormattedNsText( $index ) {
239 $ns = $this->getNsText( $index );
240 return strtr($ns, '_', ' ');
244 * Get a namespace key by value, case insensitive.
245 * Only matches namespace names for the current language, not the
246 * canonical ones defined in Namespace.php.
248 * @param string $text
249 * @return mixed An integer if $text is a valid value otherwise false
251 function getLocalNsIndex( $text ) {
252 $this->load();
253 $lctext = $this->lc($text);
254 return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
258 * Get a namespace key by value, case insensitive. Canonical namespace
259 * names override custom ones defined for the current language.
261 * @param string $text
262 * @return mixed An integer if $text is a valid value otherwise false
264 function getNsIndex( $text ) {
265 $this->load();
266 $lctext = $this->lc($text);
267 if( ( $ns = MWNamespace::getCanonicalIndex( $lctext ) ) !== null ) return $ns;
268 return isset( $this->mNamespaceIds[$lctext] ) ? $this->mNamespaceIds[$lctext] : false;
272 * short names for language variants used for language conversion links.
274 * @param string $code
275 * @return string
277 function getVariantname( $code ) {
278 return $this->getMessageFromDB( "variantname-$code" );
281 function specialPage( $name ) {
282 $aliases = $this->getSpecialPageAliases();
283 if ( isset( $aliases[$name][0] ) ) {
284 $name = $aliases[$name][0];
286 return $this->getNsText(NS_SPECIAL) . ':' . $name;
289 function getQuickbarSettings() {
290 return array(
291 $this->getMessage( 'qbsettings-none' ),
292 $this->getMessage( 'qbsettings-fixedleft' ),
293 $this->getMessage( 'qbsettings-fixedright' ),
294 $this->getMessage( 'qbsettings-floatingleft' ),
295 $this->getMessage( 'qbsettings-floatingright' )
299 function getSkinNames() {
300 $this->load();
301 return $this->skinNames;
304 function getMathNames() {
305 $this->load();
306 return $this->mathNames;
309 function getDatePreferences() {
310 $this->load();
311 return $this->datePreferences;
314 function getDateFormats() {
315 $this->load();
316 return $this->dateFormats;
319 function getDefaultDateFormat() {
320 $this->load();
321 return $this->defaultDateFormat;
324 function getDatePreferenceMigrationMap() {
325 $this->load();
326 return $this->datePreferenceMigrationMap;
329 function getDefaultUserOptionOverrides() {
330 $this->load();
331 # XXX - apparently some languageas get empty arrays, didn't get to it yet -- midom
332 if (is_array($this->defaultUserOptionOverrides)) {
333 return $this->defaultUserOptionOverrides;
334 } else {
335 return array();
339 function getExtraUserToggles() {
340 $this->load();
341 return $this->extraUserToggles;
344 function getUserToggle( $tog ) {
345 return $this->getMessageFromDB( "tog-$tog" );
349 * Get language names, indexed by code.
350 * If $customisedOnly is true, only returns codes with a messages file
352 public static function getLanguageNames( $customisedOnly = false ) {
353 global $wgLanguageNames, $wgExtraLanguageNames;
354 $allNames = $wgExtraLanguageNames + $wgLanguageNames;
355 if ( !$customisedOnly ) {
356 return $allNames;
359 global $IP;
360 $names = array();
361 $dir = opendir( "$IP/languages/messages" );
362 while( false !== ( $file = readdir( $dir ) ) ) {
363 $m = array();
364 if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
365 $code = str_replace( '_', '-', strtolower( $m[1] ) );
366 if ( isset( $allNames[$code] ) ) {
367 $names[$code] = $allNames[$code];
371 closedir( $dir );
372 return $names;
376 * Ugly hack to get a message maybe from the MediaWiki namespace, if this
377 * language object is the content or user language.
379 function getMessageFromDB( $msg ) {
380 global $wgContLang, $wgLang;
381 if ( $wgContLang->getCode() == $this->getCode() ) {
382 # Content language
383 return wfMsgForContent( $msg );
384 } elseif ( $wgLang->getCode() == $this->getCode() ) {
385 # User language
386 return wfMsg( $msg );
387 } else {
388 # Neither, get from localisation
389 return $this->getMessage( $msg );
393 function getLanguageName( $code ) {
394 $names = self::getLanguageNames();
395 if ( !array_key_exists( $code, $names ) ) {
396 return '';
398 return $names[$code];
401 function getMonthName( $key ) {
402 return $this->getMessageFromDB( self::$mMonthMsgs[$key-1] );
405 function getMonthNameGen( $key ) {
406 return $this->getMessageFromDB( self::$mMonthGenMsgs[$key-1] );
409 function getMonthAbbreviation( $key ) {
410 return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key-1] );
413 function getWeekdayName( $key ) {
414 return $this->getMessageFromDB( self::$mWeekdayMsgs[$key-1] );
417 function getWeekdayAbbreviation( $key ) {
418 return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key-1] );
421 function getIranianCalendarMonthName( $key ) {
422 return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key-1] );
425 function getHebrewCalendarMonthName( $key ) {
426 return $this->getMessageFromDB( self::$mHebrewCalendarMonthMsgs[$key-1] );
429 function getHebrewCalendarMonthNameGen( $key ) {
430 return $this->getMessageFromDB( self::$mHebrewCalendarMonthGenMsgs[$key-1] );
435 * Used by date() and time() to adjust the time output.
436 * @public
437 * @param int $ts the time in date('YmdHis') format
438 * @param mixed $tz adjust the time by this amount (default false,
439 * mean we get user timecorrection setting)
440 * @return int
442 function userAdjust( $ts, $tz = false ) {
443 global $wgUser, $wgLocalTZoffset;
445 if (!$tz) {
446 $tz = $wgUser->getOption( 'timecorrection' );
449 # minutes and hours differences:
450 $minDiff = 0;
451 $hrDiff = 0;
453 if ( $tz === '' ) {
454 # Global offset in minutes.
455 if( isset($wgLocalTZoffset) ) {
456 if( $wgLocalTZoffset >= 0 ) {
457 $hrDiff = floor($wgLocalTZoffset / 60);
458 } else {
459 $hrDiff = ceil($wgLocalTZoffset / 60);
461 $minDiff = $wgLocalTZoffset % 60;
463 } elseif ( strpos( $tz, ':' ) !== false ) {
464 $tzArray = explode( ':', $tz );
465 $hrDiff = intval($tzArray[0]);
466 $minDiff = intval($hrDiff < 0 ? -$tzArray[1] : $tzArray[1]);
467 } else {
468 $hrDiff = intval( $tz );
471 # No difference ? Return time unchanged
472 if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
474 wfSuppressWarnings(); // E_STRICT system time bitching
475 # Generate an adjusted date
476 $t = mktime( (
477 (int)substr( $ts, 8, 2) ) + $hrDiff, # Hours
478 (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
479 (int)substr( $ts, 12, 2 ), # Seconds
480 (int)substr( $ts, 4, 2 ), # Month
481 (int)substr( $ts, 6, 2 ), # Day
482 (int)substr( $ts, 0, 4 ) ); #Year
484 $date = date( 'YmdHis', $t );
485 wfRestoreWarnings();
487 return $date;
491 * This is a workalike of PHP's date() function, but with better
492 * internationalisation, a reduced set of format characters, and a better
493 * escaping format.
495 * Supported format characters are dDjlNwzWFmMntLYyaAgGhHiscrU. See the
496 * PHP manual for definitions. There are a number of extensions, which
497 * start with "x":
499 * xn Do not translate digits of the next numeric format character
500 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
501 * xr Use roman numerals for the next numeric format character
502 * xh Use hebrew numerals for the next numeric format character
503 * xx Literal x
504 * xg Genitive month name
506 * xij j (day number) in Iranian calendar
507 * xiF F (month name) in Iranian calendar
508 * xin n (month number) in Iranian calendar
509 * xiY Y (full year) in Iranian calendar
511 * xjj j (day number) in Hebrew calendar
512 * xjF F (month name) in Hebrew calendar
513 * xjt t (days in month) in Hebrew calendar
514 * xjx xg (genitive month name) in Hebrew calendar
515 * xjn n (month number) in Hebrew calendar
516 * xjY Y (full year) in Hebrew calendar
518 * xkY Y (full year) in Thai solar calendar. Months and days are
519 * identical to the Gregorian calendar
521 * Characters enclosed in double quotes will be considered literal (with
522 * the quotes themselves removed). Unmatched quotes will be considered
523 * literal quotes. Example:
525 * "The month is" F => The month is January
526 * i's" => 20'11"
528 * Backslash escaping is also supported.
530 * Input timestamp is assumed to be pre-normalized to the desired local
531 * time zone, if any.
533 * @param string $format
534 * @param string $ts 14-character timestamp
535 * YYYYMMDDHHMMSS
536 * 01234567890123
538 function sprintfDate( $format, $ts ) {
539 $s = '';
540 $raw = false;
541 $roman = false;
542 $hebrewNum = false;
543 $unix = false;
544 $rawToggle = false;
545 $iranian = false;
546 $hebrew = false;
547 $thai = false;
548 for ( $p = 0; $p < strlen( $format ); $p++ ) {
549 $num = false;
550 $code = $format[$p];
551 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
552 $code .= $format[++$p];
555 if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' ) && $p < strlen( $format ) - 1 ) {
556 $code .= $format[++$p];
559 switch ( $code ) {
560 case 'xx':
561 $s .= 'x';
562 break;
563 case 'xn':
564 $raw = true;
565 break;
566 case 'xN':
567 $rawToggle = !$rawToggle;
568 break;
569 case 'xr':
570 $roman = true;
571 break;
572 case 'xh':
573 $hebrewNum = true;
574 break;
575 case 'xg':
576 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
577 break;
578 case 'xjx':
579 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
580 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
581 break;
582 case 'd':
583 $num = substr( $ts, 6, 2 );
584 break;
585 case 'D':
586 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
587 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) + 1 );
588 break;
589 case 'j':
590 $num = intval( substr( $ts, 6, 2 ) );
591 break;
592 case 'xij':
593 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
594 $num = $iranian[2];
595 break;
596 case 'xjj':
597 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
598 $num = $hebrew[2];
599 break;
600 case 'l':
601 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
602 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 );
603 break;
604 case 'N':
605 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
606 $w = gmdate( 'w', $unix );
607 $num = $w ? $w : 7;
608 break;
609 case 'w':
610 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
611 $num = gmdate( 'w', $unix );
612 break;
613 case 'z':
614 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
615 $num = gmdate( 'z', $unix );
616 break;
617 case 'W':
618 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
619 $num = gmdate( 'W', $unix );
620 break;
621 case 'F':
622 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
623 break;
624 case 'xiF':
625 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
626 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
627 break;
628 case 'xjF':
629 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
630 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
631 break;
632 case 'm':
633 $num = substr( $ts, 4, 2 );
634 break;
635 case 'M':
636 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
637 break;
638 case 'n':
639 $num = intval( substr( $ts, 4, 2 ) );
640 break;
641 case 'xin':
642 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
643 $num = $iranian[1];
644 break;
645 case 'xjn':
646 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
647 $num = $hebrew[1];
648 break;
649 case 't':
650 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
651 $num = gmdate( 't', $unix );
652 break;
653 case 'xjt':
654 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
655 $num = $hebrew[3];
656 break;
657 case 'L':
658 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
659 $num = gmdate( 'L', $unix );
660 break;
661 case 'Y':
662 $num = substr( $ts, 0, 4 );
663 break;
664 case 'xiY':
665 if ( !$iranian ) $iranian = self::tsToIranian( $ts );
666 $num = $iranian[0];
667 break;
668 case 'xjY':
669 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
670 $num = $hebrew[0];
671 break;
672 case 'xkY':
673 if ( !$thai ) $thai = self::tsToThai( $ts );
674 $num = $thai[0];
675 break;
676 case 'y':
677 $num = substr( $ts, 2, 2 );
678 break;
679 case 'a':
680 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
681 break;
682 case 'A':
683 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
684 break;
685 case 'g':
686 $h = substr( $ts, 8, 2 );
687 $num = $h % 12 ? $h % 12 : 12;
688 break;
689 case 'G':
690 $num = intval( substr( $ts, 8, 2 ) );
691 break;
692 case 'h':
693 $h = substr( $ts, 8, 2 );
694 $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
695 break;
696 case 'H':
697 $num = substr( $ts, 8, 2 );
698 break;
699 case 'i':
700 $num = substr( $ts, 10, 2 );
701 break;
702 case 's':
703 $num = substr( $ts, 12, 2 );
704 break;
705 case 'c':
706 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
707 $s .= gmdate( 'c', $unix );
708 break;
709 case 'r':
710 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
711 $s .= gmdate( 'r', $unix );
712 break;
713 case 'U':
714 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
715 $num = $unix;
716 break;
717 case '\\':
718 # Backslash escaping
719 if ( $p < strlen( $format ) - 1 ) {
720 $s .= $format[++$p];
721 } else {
722 $s .= '\\';
724 break;
725 case '"':
726 # Quoted literal
727 if ( $p < strlen( $format ) - 1 ) {
728 $endQuote = strpos( $format, '"', $p + 1 );
729 if ( $endQuote === false ) {
730 # No terminating quote, assume literal "
731 $s .= '"';
732 } else {
733 $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
734 $p = $endQuote;
736 } else {
737 # Quote at end of string, assume literal "
738 $s .= '"';
740 break;
741 default:
742 $s .= $format[$p];
744 if ( $num !== false ) {
745 if ( $rawToggle || $raw ) {
746 $s .= $num;
747 $raw = false;
748 } elseif ( $roman ) {
749 $s .= self::romanNumeral( $num );
750 $roman = false;
751 } elseif( $hebrewNum ) {
752 $s .= self::hebrewNumeral( $num );
753 $hebrewNum = false;
754 } else {
755 $s .= $this->formatNum( $num, true );
757 $num = false;
760 return $s;
763 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
764 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
766 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
767 * Gregorian dates to Iranian dates. Originally written in C, it
768 * is released under the terms of GNU Lesser General Public
769 * License. Conversion to PHP was performed by Niklas Laxström.
771 * Link: http://www.farsiweb.info/jalali/jalali.c
773 private static function tsToIranian( $ts ) {
774 $gy = substr( $ts, 0, 4 ) -1600;
775 $gm = substr( $ts, 4, 2 ) -1;
776 $gd = substr( $ts, 6, 2 ) -1;
778 # Days passed from the beginning (including leap years)
779 $gDayNo = 365*$gy
780 + floor(($gy+3) / 4)
781 - floor(($gy+99) / 100)
782 + floor(($gy+399) / 400);
785 // Add days of the past months of this year
786 for( $i = 0; $i < $gm; $i++ ) {
787 $gDayNo += self::$GREG_DAYS[$i];
790 // Leap years
791 if ( $gm > 1 && (($gy%4===0 && $gy%100!==0 || ($gy%400==0)))) {
792 $gDayNo++;
795 // Days passed in current month
796 $gDayNo += $gd;
798 $jDayNo = $gDayNo - 79;
800 $jNp = floor($jDayNo / 12053);
801 $jDayNo %= 12053;
803 $jy = 979 + 33*$jNp + 4*floor($jDayNo/1461);
804 $jDayNo %= 1461;
806 if ( $jDayNo >= 366 ) {
807 $jy += floor(($jDayNo-1)/365);
808 $jDayNo = floor(($jDayNo-1)%365);
811 for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
812 $jDayNo -= self::$IRANIAN_DAYS[$i];
815 $jm= $i+1;
816 $jd= $jDayNo+1;
818 return array($jy, $jm, $jd);
822 * Converting Gregorian dates to Hebrew dates.
824 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
825 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
826 * to translate the relevant functions into PHP and release them under
827 * GNU GPL.
829 private static function tsToHebrew( $ts ) {
830 # Parse date
831 $year = substr( $ts, 0, 4 );
832 $month = substr( $ts, 4, 2 );
833 $day = substr( $ts, 6, 2 );
835 # Calculate Hebrew year
836 $hebrewYear = $year + 3760;
838 # Month number when September = 1, August = 12
839 $month += 4;
840 if( $month > 12 ) {
841 # Next year
842 $month -= 12;
843 $year++;
844 $hebrewYear++;
847 # Calculate day of year from 1 September
848 $dayOfYear = $day;
849 for( $i = 1; $i < $month; $i++ ) {
850 if( $i == 6 ) {
851 # February
852 $dayOfYear += 28;
853 # Check if the year is leap
854 if( $year % 400 == 0 || ( $year % 4 == 0 && $year % 100 > 0 ) ) {
855 $dayOfYear++;
857 } elseif( $i == 8 || $i == 10 || $i == 1 || $i == 3 ) {
858 $dayOfYear += 30;
859 } else {
860 $dayOfYear += 31;
864 # Calculate the start of the Hebrew year
865 $start = self::hebrewYearStart( $hebrewYear );
867 # Calculate next year's start
868 if( $dayOfYear <= $start ) {
869 # Day is before the start of the year - it is the previous year
870 # Next year's start
871 $nextStart = $start;
872 # Previous year
873 $year--;
874 $hebrewYear--;
875 # Add days since previous year's 1 September
876 $dayOfYear += 365;
877 if( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
878 # Leap year
879 $dayOfYear++;
881 # Start of the new (previous) year
882 $start = self::hebrewYearStart( $hebrewYear );
883 } else {
884 # Next year's start
885 $nextStart = self::hebrewYearStart( $hebrewYear + 1 );
888 # Calculate Hebrew day of year
889 $hebrewDayOfYear = $dayOfYear - $start;
891 # Difference between year's days
892 $diff = $nextStart - $start;
893 # Add 12 (or 13 for leap years) days to ignore the difference between
894 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
895 # difference is only about the year type
896 if( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
897 $diff += 13;
898 } else {
899 $diff += 12;
902 # Check the year pattern, and is leap year
903 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
904 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
905 # and non-leap years
906 $yearPattern = $diff % 30;
907 # Check if leap year
908 $isLeap = $diff >= 30;
910 # Calculate day in the month from number of day in the Hebrew year
911 # Don't check Adar - if the day is not in Adar, we will stop before;
912 # if it is in Adar, we will use it to check if it is Adar I or Adar II
913 $hebrewDay = $hebrewDayOfYear;
914 $hebrewMonth = 1;
915 $days = 0;
916 while( $hebrewMonth <= 12 ) {
917 # Calculate days in this month
918 if( $isLeap && $hebrewMonth == 6 ) {
919 # Adar in a leap year
920 if( $isLeap ) {
921 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
922 $days = 30;
923 if( $hebrewDay <= $days ) {
924 # Day in Adar I
925 $hebrewMonth = 13;
926 } else {
927 # Subtract the days of Adar I
928 $hebrewDay -= $days;
929 # Try Adar II
930 $days = 29;
931 if( $hebrewDay <= $days ) {
932 # Day in Adar II
933 $hebrewMonth = 14;
937 } elseif( $hebrewMonth == 2 && $yearPattern == 2 ) {
938 # Cheshvan in a complete year (otherwise as the rule below)
939 $days = 30;
940 } elseif( $hebrewMonth == 3 && $yearPattern == 0 ) {
941 # Kislev in an incomplete year (otherwise as the rule below)
942 $days = 29;
943 } else {
944 # Odd months have 30 days, even have 29
945 $days = 30 - ( $hebrewMonth - 1 ) % 2;
947 if( $hebrewDay <= $days ) {
948 # In the current month
949 break;
950 } else {
951 # Subtract the days of the current month
952 $hebrewDay -= $days;
953 # Try in the next month
954 $hebrewMonth++;
958 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
962 * This calculates the Hebrew year start, as days since 1 September.
963 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
964 * Used for Hebrew date.
966 private static function hebrewYearStart( $year ) {
967 $a = intval( ( 12 * ( $year - 1 ) + 17 ) % 19 );
968 $b = intval( ( $year - 1 ) % 4 );
969 $m = 32.044093161144 + 1.5542417966212 * $a + $b / 4.0 - 0.0031777940220923 * ( $year - 1 );
970 if( $m < 0 ) {
971 $m--;
973 $Mar = intval( $m );
974 if( $m < 0 ) {
975 $m++;
977 $m -= $Mar;
979 $c = intval( ( $Mar + 3 * ( $year - 1 ) + 5 * $b + 5 ) % 7);
980 if( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
981 $Mar++;
982 } else if( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
983 $Mar += 2;
984 } else if( $c == 2 || $c == 4 || $c == 6 ) {
985 $Mar++;
988 $Mar += intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
989 return $Mar;
993 * Algorithm to convert Gregorian dates to Thai solar dates.
995 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
997 * @param string $ts 14-character timestamp
998 * @return array converted year, month, day
1000 private static function tsToThai( $ts ) {
1001 $gy = substr( $ts, 0, 4 );
1002 $gm = substr( $ts, 4, 2 );
1003 $gd = substr( $ts, 6, 2 );
1005 # Add 543 years to the Gregorian calendar
1006 # Months and days are identical
1007 $gy_thai = $gy + 543;
1009 return array( $gy_thai, $gm, $gd );
1014 * Roman number formatting up to 3000
1016 static function romanNumeral( $num ) {
1017 static $table = array(
1018 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1019 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1020 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1021 array( '', 'M', 'MM', 'MMM' )
1024 $num = intval( $num );
1025 if ( $num > 3000 || $num <= 0 ) {
1026 return $num;
1029 $s = '';
1030 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1031 if ( $num >= $pow10 ) {
1032 $s .= $table[$i][floor($num / $pow10)];
1034 $num = $num % $pow10;
1036 return $s;
1040 * Hebrew Gematria number formatting up to 9999
1042 static function hebrewNumeral( $num ) {
1043 static $table = array(
1044 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1045 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1046 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1047 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1050 $num = intval( $num );
1051 if ( $num > 9999 || $num <= 0 ) {
1052 return $num;
1055 $s = '';
1056 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1057 if ( $num >= $pow10 ) {
1058 if ( $num == 15 || $num == 16 ) {
1059 $s .= $table[0][9] . $table[0][$num - 9];
1060 $num = 0;
1061 } else {
1062 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1063 if( $pow10 == 1000 ) {
1064 $s .= "'";
1068 $num = $num % $pow10;
1070 if( strlen( $s ) == 2 ) {
1071 $str = $s . "'";
1072 } else {
1073 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1074 $str .= substr( $s, strlen( $s ) - 2, 2 );
1076 $start = substr( $str, 0, strlen( $str ) - 2 );
1077 $end = substr( $str, strlen( $str ) - 2 );
1078 switch( $end ) {
1079 case 'כ':
1080 $str = $start . 'ך';
1081 break;
1082 case 'מ':
1083 $str = $start . 'ם';
1084 break;
1085 case 'נ':
1086 $str = $start . 'ן';
1087 break;
1088 case 'פ':
1089 $str = $start . 'ף';
1090 break;
1091 case 'צ':
1092 $str = $start . 'ץ';
1093 break;
1095 return $str;
1099 * This is meant to be used by time(), date(), and timeanddate() to get
1100 * the date preference they're supposed to use, it should be used in
1101 * all children.
1103 *<code>
1104 * function timeanddate([...], $format = true) {
1105 * $datePreference = $this->dateFormat($format);
1106 * [...]
1108 *</code>
1110 * @param mixed $usePrefs: if true, the user's preference is used
1111 * if false, the site/language default is used
1112 * if int/string, assumed to be a format.
1113 * @return string
1115 function dateFormat( $usePrefs = true ) {
1116 global $wgUser;
1118 if( is_bool( $usePrefs ) ) {
1119 if( $usePrefs ) {
1120 $datePreference = $wgUser->getDatePreference();
1121 } else {
1122 $options = User::getDefaultOptions();
1123 $datePreference = (string)$options['date'];
1125 } else {
1126 $datePreference = (string)$usePrefs;
1129 // return int
1130 if( $datePreference == '' ) {
1131 return 'default';
1134 return $datePreference;
1138 * @public
1139 * @param mixed $ts the time format which needs to be turned into a
1140 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1141 * @param bool $adj whether to adjust the time output according to the
1142 * user configured offset ($timecorrection)
1143 * @param mixed $format true to use user's date format preference
1144 * @param string $timecorrection the time offset as returned by
1145 * validateTimeZone() in Special:Preferences
1146 * @return string
1148 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1149 $this->load();
1150 if ( $adj ) {
1151 $ts = $this->userAdjust( $ts, $timecorrection );
1154 $pref = $this->dateFormat( $format );
1155 if( $pref == 'default' || !isset( $this->dateFormats["$pref date"] ) ) {
1156 $pref = $this->defaultDateFormat;
1158 return $this->sprintfDate( $this->dateFormats["$pref date"], $ts );
1162 * @public
1163 * @param mixed $ts the time format which needs to be turned into a
1164 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1165 * @param bool $adj whether to adjust the time output according to the
1166 * user configured offset ($timecorrection)
1167 * @param mixed $format true to use user's date format preference
1168 * @param string $timecorrection the time offset as returned by
1169 * validateTimeZone() in Special:Preferences
1170 * @return string
1172 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1173 $this->load();
1174 if ( $adj ) {
1175 $ts = $this->userAdjust( $ts, $timecorrection );
1178 $pref = $this->dateFormat( $format );
1179 if( $pref == 'default' || !isset( $this->dateFormats["$pref time"] ) ) {
1180 $pref = $this->defaultDateFormat;
1182 return $this->sprintfDate( $this->dateFormats["$pref time"], $ts );
1186 * @public
1187 * @param mixed $ts the time format which needs to be turned into a
1188 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1189 * @param bool $adj whether to adjust the time output according to the
1190 * user configured offset ($timecorrection)
1192 * @param mixed $format what format to return, if it's false output the
1193 * default one (default true)
1194 * @param string $timecorrection the time offset as returned by
1195 * validateTimeZone() in Special:Preferences
1196 * @return string
1198 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
1199 $this->load();
1201 $ts = wfTimestamp( TS_MW, $ts );
1203 if ( $adj ) {
1204 $ts = $this->userAdjust( $ts, $timecorrection );
1207 $pref = $this->dateFormat( $format );
1208 if( $pref == 'default' || !isset( $this->dateFormats["$pref both"] ) ) {
1209 $pref = $this->defaultDateFormat;
1212 return $this->sprintfDate( $this->dateFormats["$pref both"], $ts );
1215 function getMessage( $key ) {
1216 $this->load();
1217 return isset( $this->messages[$key] ) ? $this->messages[$key] : null;
1220 function getAllMessages() {
1221 $this->load();
1222 return $this->messages;
1225 function iconv( $in, $out, $string ) {
1226 # For most languages, this is a wrapper for iconv
1227 return iconv( $in, $out . '//IGNORE', $string );
1230 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
1231 function ucwordbreaksCallbackAscii($matches){
1232 return $this->ucfirst($matches[1]);
1235 function ucwordbreaksCallbackMB($matches){
1236 return mb_strtoupper($matches[0]);
1239 function ucCallback($matches){
1240 list( $wikiUpperChars ) = self::getCaseMaps();
1241 return strtr( $matches[1], $wikiUpperChars );
1244 function lcCallback($matches){
1245 list( , $wikiLowerChars ) = self::getCaseMaps();
1246 return strtr( $matches[1], $wikiLowerChars );
1249 function ucwordsCallbackMB($matches){
1250 return mb_strtoupper($matches[0]);
1253 function ucwordsCallbackWiki($matches){
1254 list( $wikiUpperChars ) = self::getCaseMaps();
1255 return strtr( $matches[0], $wikiUpperChars );
1258 function ucfirst( $str ) {
1259 if ( empty($str) ) return $str;
1260 if ( ord($str[0]) < 128 ) return ucfirst($str);
1261 else return self::uc($str,true); // fall back to more complex logic in case of multibyte strings
1264 function uc( $str, $first = false ) {
1265 if ( function_exists( 'mb_strtoupper' ) ) {
1266 if ( $first ) {
1267 if ( self::isMultibyte( $str ) ) {
1268 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1269 } else {
1270 return ucfirst( $str );
1272 } else {
1273 return self::isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
1275 } else {
1276 if ( self::isMultibyte( $str ) ) {
1277 list( $wikiUpperChars ) = $this->getCaseMaps();
1278 $x = $first ? '^' : '';
1279 return preg_replace_callback(
1280 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1281 array($this,"ucCallback"),
1282 $str
1284 } else {
1285 return $first ? ucfirst( $str ) : strtoupper( $str );
1290 function lcfirst( $str ) {
1291 if ( empty($str) ) return $str;
1292 if ( is_string( $str ) && ord($str[0]) < 128 ) {
1293 // editing string in place = cool
1294 $str[0]=strtolower($str[0]);
1295 return $str;
1297 else return self::lc( $str, true );
1300 function lc( $str, $first = false ) {
1301 if ( function_exists( 'mb_strtolower' ) )
1302 if ( $first )
1303 if ( self::isMultibyte( $str ) )
1304 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1305 else
1306 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
1307 else
1308 return self::isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
1309 else
1310 if ( self::isMultibyte( $str ) ) {
1311 list( , $wikiLowerChars ) = self::getCaseMaps();
1312 $x = $first ? '^' : '';
1313 return preg_replace_callback(
1314 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1315 array($this,"lcCallback"),
1316 $str
1318 } else
1319 return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
1322 function isMultibyte( $str ) {
1323 return (bool)preg_match( '/[\x80-\xff]/', $str );
1326 function ucwords($str) {
1327 if ( self::isMultibyte( $str ) ) {
1328 $str = self::lc($str);
1330 // regexp to find first letter in each word (i.e. after each space)
1331 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1333 // function to use to capitalize a single char
1334 if ( function_exists( 'mb_strtoupper' ) )
1335 return preg_replace_callback(
1336 $replaceRegexp,
1337 array($this,"ucwordsCallbackMB"),
1338 $str
1340 else
1341 return preg_replace_callback(
1342 $replaceRegexp,
1343 array($this,"ucwordsCallbackWiki"),
1344 $str
1347 else
1348 return ucwords( strtolower( $str ) );
1351 # capitalize words at word breaks
1352 function ucwordbreaks($str){
1353 if (self::isMultibyte( $str ) ) {
1354 $str = self::lc($str);
1356 // since \b doesn't work for UTF-8, we explicitely define word break chars
1357 $breaks= "[ \-\(\)\}\{\.,\?!]";
1359 // find first letter after word break
1360 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1362 if ( function_exists( 'mb_strtoupper' ) )
1363 return preg_replace_callback(
1364 $replaceRegexp,
1365 array($this,"ucwordbreaksCallbackMB"),
1366 $str
1368 else
1369 return preg_replace_callback(
1370 $replaceRegexp,
1371 array($this,"ucwordsCallbackWiki"),
1372 $str
1375 else
1376 return preg_replace_callback(
1377 '/\b([\w\x80-\xff]+)\b/',
1378 array($this,"ucwordbreaksCallbackAscii"),
1379 $str );
1383 * Return a case-folded representation of $s
1385 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
1386 * and $s2 are the same except for the case of their characters. It is not
1387 * necessary for the value returned to make sense when displayed.
1389 * Do *not* perform any other normalisation in this function. If a caller
1390 * uses this function when it should be using a more general normalisation
1391 * function, then fix the caller.
1393 function caseFold( $s ) {
1394 return $this->uc( $s );
1397 function checkTitleEncoding( $s ) {
1398 if( is_array( $s ) ) {
1399 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
1401 # Check for non-UTF-8 URLs
1402 $ishigh = preg_match( '/[\x80-\xff]/', $s);
1403 if(!$ishigh) return $s;
1405 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1406 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
1407 if( $isutf8 ) return $s;
1409 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
1412 function fallback8bitEncoding() {
1413 $this->load();
1414 return $this->fallback8bitEncoding;
1418 * Some languages have special punctuation to strip out
1419 * or characters which need to be converted for MySQL's
1420 * indexing to grok it correctly. Make such changes here.
1422 * @param string $in
1423 * @return string
1425 function stripForSearch( $string ) {
1426 global $wgDBtype;
1427 if ( $wgDBtype != 'mysql' ) {
1428 return $string;
1431 # MySQL fulltext index doesn't grok utf-8, so we
1432 # need to fold cases and convert to hex
1434 wfProfileIn( __METHOD__ );
1435 if( function_exists( 'mb_strtolower' ) ) {
1436 $out = preg_replace(
1437 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1438 "'U8' . bin2hex( \"$1\" )",
1439 mb_strtolower( $string ) );
1440 } else {
1441 list( , $wikiLowerChars ) = self::getCaseMaps();
1442 $out = preg_replace(
1443 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1444 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
1445 $string );
1447 wfProfileOut( __METHOD__ );
1448 return $out;
1451 function convertForSearchResult( $termsArray ) {
1452 # some languages, e.g. Chinese, need to do a conversion
1453 # in order for search results to be displayed correctly
1454 return $termsArray;
1458 * Get the first character of a string.
1460 * @param string $s
1461 * @return string
1463 function firstChar( $s ) {
1464 $matches = array();
1465 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1466 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
1468 return isset( $matches[1] ) ? $matches[1] : "";
1471 function initEncoding() {
1472 # Some languages may have an alternate char encoding option
1473 # (Esperanto X-coding, Japanese furigana conversion, etc)
1474 # If this language is used as the primary content language,
1475 # an override to the defaults can be set here on startup.
1478 function recodeForEdit( $s ) {
1479 # For some languages we'll want to explicitly specify
1480 # which characters make it into the edit box raw
1481 # or are converted in some way or another.
1482 # Note that if wgOutputEncoding is different from
1483 # wgInputEncoding, this text will be further converted
1484 # to wgOutputEncoding.
1485 global $wgEditEncoding;
1486 if( $wgEditEncoding == '' or
1487 $wgEditEncoding == 'UTF-8' ) {
1488 return $s;
1489 } else {
1490 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
1494 function recodeInput( $s ) {
1495 # Take the previous into account.
1496 global $wgEditEncoding;
1497 if($wgEditEncoding != "") {
1498 $enc = $wgEditEncoding;
1499 } else {
1500 $enc = 'UTF-8';
1502 if( $enc == 'UTF-8' ) {
1503 return $s;
1504 } else {
1505 return $this->iconv( $enc, 'UTF-8', $s );
1510 * For right-to-left language support
1512 * @return bool
1514 function isRTL() {
1515 $this->load();
1516 return $this->rtl;
1520 * A hidden direction mark (LRM or RLM), depending on the language direction
1522 * @return string
1524 function getDirMark() {
1525 return $this->isRTL() ? "\xE2\x80\x8F" : "\xE2\x80\x8E";
1529 * An arrow, depending on the language direction
1531 * @return string
1533 function getArrow() {
1534 return $this->isRTL() ? '←' : '→';
1538 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
1540 * @return bool
1542 function linkPrefixExtension() {
1543 $this->load();
1544 return $this->linkPrefixExtension;
1547 function &getMagicWords() {
1548 $this->load();
1549 return $this->magicWords;
1552 # Fill a MagicWord object with data from here
1553 function getMagic( &$mw ) {
1554 if ( !$this->mMagicHookDone ) {
1555 $this->mMagicHookDone = true;
1556 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
1558 if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
1559 $rawEntry = $this->mMagicExtensions[$mw->mId];
1560 } else {
1561 $magicWords =& $this->getMagicWords();
1562 if ( isset( $magicWords[$mw->mId] ) ) {
1563 $rawEntry = $magicWords[$mw->mId];
1564 } else {
1565 # Fall back to English if local list is incomplete
1566 $magicWords =& Language::getMagicWords();
1567 $rawEntry = $magicWords[$mw->mId];
1571 if( !is_array( $rawEntry ) ) {
1572 error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
1573 } else {
1574 $mw->mCaseSensitive = $rawEntry[0];
1575 $mw->mSynonyms = array_slice( $rawEntry, 1 );
1580 * Add magic words to the extension array
1582 function addMagicWordsByLang( $newWords ) {
1583 $code = $this->getCode();
1584 $fallbackChain = array();
1585 while ( $code && !in_array( $code, $fallbackChain ) ) {
1586 $fallbackChain[] = $code;
1587 $code = self::getFallbackFor( $code );
1589 if ( !in_array( 'en', $fallbackChain ) ) {
1590 $fallbackChain[] = 'en';
1592 $fallbackChain = array_reverse( $fallbackChain );
1593 foreach ( $fallbackChain as $code ) {
1594 if ( isset( $newWords[$code] ) ) {
1595 $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
1601 * Get special page names, as an associative array
1602 * case folded alias => real name
1604 function getSpecialPageAliases() {
1605 $this->load();
1606 if ( !isset( $this->mExtendedSpecialPageAliases ) ) {
1607 $this->mExtendedSpecialPageAliases = $this->specialPageAliases;
1608 wfRunHooks( 'LanguageGetSpecialPageAliases',
1609 array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
1611 return $this->mExtendedSpecialPageAliases;
1615 * Italic is unsuitable for some languages
1617 * @public
1619 * @param string $text The text to be emphasized.
1620 * @return string
1622 function emphasize( $text ) {
1623 return "<em>$text</em>";
1627 * Normally we output all numbers in plain en_US style, that is
1628 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
1629 * point twohundredthirtyfive. However this is not sutable for all
1630 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
1631 * Icelandic just want to use commas instead of dots, and dots instead
1632 * of commas like "293.291,235".
1634 * An example of this function being called:
1635 * <code>
1636 * wfMsg( 'message', $wgLang->formatNum( $num ) )
1637 * </code>
1639 * See LanguageGu.php for the Gujarati implementation and
1640 * LanguageIs.php for the , => . and . => , implementation.
1642 * @todo check if it's viable to use localeconv() for the decimal
1643 * seperator thing.
1644 * @public
1645 * @param mixed $number the string to be formatted, should be an integer or
1646 * a floating point number.
1647 * @param bool $nocommafy Set to true for special numbers like dates
1648 * @return string
1650 function formatNum( $number, $nocommafy = false ) {
1651 global $wgTranslateNumerals;
1652 if (!$nocommafy) {
1653 $number = $this->commafy($number);
1654 $s = $this->separatorTransformTable();
1655 if (!is_null($s)) { $number = strtr($number, $s); }
1658 if ($wgTranslateNumerals) {
1659 $s = $this->digitTransformTable();
1660 if (!is_null($s)) { $number = strtr($number, $s); }
1663 return $number;
1666 function parseFormattedNumber( $number ) {
1667 $s = $this->digitTransformTable();
1668 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1670 $s = $this->separatorTransformTable();
1671 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1673 $number = strtr( $number, array (',' => '') );
1674 return $number;
1678 * Adds commas to a given number
1680 * @param mixed $_
1681 * @return string
1683 function commafy($_) {
1684 return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
1687 function digitTransformTable() {
1688 $this->load();
1689 return $this->digitTransformTable;
1692 function separatorTransformTable() {
1693 $this->load();
1694 return $this->separatorTransformTable;
1699 * For the credit list in includes/Credits.php (action=credits)
1701 * @param array $l
1702 * @return string
1704 function listToText( $l ) {
1705 $s = '';
1706 $m = count($l) - 1;
1707 for ($i = $m; $i >= 0; $i--) {
1708 if ($i == $m) {
1709 $s = $l[$i];
1710 } else if ($i == $m - 1) {
1711 $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
1712 } else {
1713 $s = $l[$i] . ', ' . $s;
1716 return $s;
1720 * Truncate a string to a specified length in bytes, appending an optional
1721 * string (e.g. for ellipses)
1723 * The database offers limited byte lengths for some columns in the database;
1724 * multi-byte character sets mean we need to ensure that only whole characters
1725 * are included, otherwise broken characters can be passed to the user
1727 * If $length is negative, the string will be truncated from the beginning
1729 * @param string $string String to truncate
1730 * @param int $length Maximum length (excluding ellipses)
1731 * @param string $ellipses String to append to the truncated text
1732 * @return string
1734 function truncate( $string, $length, $ellipsis = "" ) {
1735 if( $length == 0 ) {
1736 return $ellipsis;
1738 if ( strlen( $string ) <= abs( $length ) ) {
1739 return $string;
1741 if( $length > 0 ) {
1742 $string = substr( $string, 0, $length );
1743 $char = ord( $string[strlen( $string ) - 1] );
1744 $m = array();
1745 if ($char >= 0xc0) {
1746 # We got the first byte only of a multibyte char; remove it.
1747 $string = substr( $string, 0, -1 );
1748 } elseif( $char >= 0x80 &&
1749 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
1750 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
1751 # We chopped in the middle of a character; remove it
1752 $string = $m[1];
1754 return $string . $ellipsis;
1755 } else {
1756 $string = substr( $string, $length );
1757 $char = ord( $string[0] );
1758 if( $char >= 0x80 && $char < 0xc0 ) {
1759 # We chopped in the middle of a character; remove the whole thing
1760 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
1762 return $ellipsis . $string;
1767 * Grammatical transformations, needed for inflected languages
1768 * Invoked by putting {{grammar:case|word}} in a message
1770 * @param string $word
1771 * @param string $case
1772 * @return string
1774 function convertGrammar( $word, $case ) {
1775 global $wgGrammarForms;
1776 if ( isset($wgGrammarForms[$this->getCode()][$case][$word]) ) {
1777 return $wgGrammarForms[$this->getCode()][$case][$word];
1779 return $word;
1783 * Plural form transformations, needed for some languages.
1784 * For example, there are 3 form of plural in Russian and Polish,
1785 * depending on "count mod 10". See [[w:Plural]]
1786 * For English it is pretty simple.
1788 * Invoked by putting {{plural:count|wordform1|wordform2}}
1789 * or {{plural:count|wordform1|wordform2|wordform3}}
1791 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
1793 * @param integer $count Non-localized number
1794 * @param array $forms Different plural forms
1795 * @return string Correct form of plural for $count in this language
1797 function convertPlural( $count, $forms ) {
1798 if ( !count($forms) ) { return ''; }
1799 $forms = $this->preConvertPlural( $forms, 2 );
1801 return ( abs($count) == 1 ) ? $forms[0] : $forms[1];
1805 * Checks that convertPlural was given an array and pads it to requested
1806 * amound of forms by copying the last one.
1808 * @param integer $count How many forms should there be at least
1809 * @param array $forms Array of forms given to convertPlural
1810 * @return array Padded array of forms or an exception if not an array
1812 protected function preConvertPlural( /* Array */ $forms, $count ) {
1813 while ( count($forms) < $count ) {
1814 $forms[] = $forms[count($forms)-1];
1816 return $forms;
1820 * For translaing of expiry times
1821 * @param string The validated block time in English
1822 * @return Somehow translated block time
1823 * @see LanguageFi.php for example implementation
1825 function translateBlockExpiry( $str ) {
1827 $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
1829 if ( $scBlockExpiryOptions == '-') {
1830 return $str;
1833 foreach (explode(',', $scBlockExpiryOptions) as $option) {
1834 if ( strpos($option, ":") === false )
1835 continue;
1836 list($show, $value) = explode(":", $option);
1837 if ( strcmp ( $str, $value) == 0 ) {
1838 return htmlspecialchars( trim( $show ) );
1842 return $str;
1846 * languages like Chinese need to be segmented in order for the diff
1847 * to be of any use
1849 * @param string $text
1850 * @return string
1852 function segmentForDiff( $text ) {
1853 return $text;
1857 * and unsegment to show the result
1859 * @param string $text
1860 * @return string
1862 function unsegmentForDiff( $text ) {
1863 return $text;
1866 # convert text to different variants of a language.
1867 function convert( $text, $isTitle = false) {
1868 return $this->mConverter->convert($text, $isTitle);
1871 # Convert text from within Parser
1872 function parserConvert( $text, &$parser ) {
1873 return $this->mConverter->parserConvert( $text, $parser );
1876 # Check if this is a language with variants
1877 function hasVariants(){
1878 return sizeof($this->getVariants())>1;
1881 # Put custom tags (e.g. -{ }-) around math to prevent conversion
1882 function armourMath($text){
1883 return $this->mConverter->armourMath($text);
1888 * Perform output conversion on a string, and encode for safe HTML output.
1889 * @param string $text
1890 * @param bool $isTitle -- wtf?
1891 * @return string
1892 * @todo this should get integrated somewhere sane
1894 function convertHtml( $text, $isTitle = false ) {
1895 return htmlspecialchars( $this->convert( $text, $isTitle ) );
1898 function convertCategoryKey( $key ) {
1899 return $this->mConverter->convertCategoryKey( $key );
1903 * get the list of variants supported by this langauge
1904 * see sample implementation in LanguageZh.php
1906 * @return array an array of language codes
1908 function getVariants() {
1909 return $this->mConverter->getVariants();
1913 function getPreferredVariant( $fromUser = true ) {
1914 return $this->mConverter->getPreferredVariant( $fromUser );
1918 * if a language supports multiple variants, it is
1919 * possible that non-existing link in one variant
1920 * actually exists in another variant. this function
1921 * tries to find it. See e.g. LanguageZh.php
1923 * @param string $link the name of the link
1924 * @param mixed $nt the title object of the link
1925 * @return null the input parameters may be modified upon return
1927 function findVariantLink( &$link, &$nt ) {
1928 $this->mConverter->findVariantLink($link, $nt);
1932 * If a language supports multiple variants, converts text
1933 * into an array of all possible variants of the text:
1934 * 'variant' => text in that variant
1937 function convertLinkToAllVariants($text){
1938 return $this->mConverter->convertLinkToAllVariants($text);
1943 * returns language specific options used by User::getPageRenderHash()
1944 * for example, the preferred language variant
1946 * @return string
1947 * @public
1949 function getExtraHashOptions() {
1950 return $this->mConverter->getExtraHashOptions();
1954 * for languages that support multiple variants, the title of an
1955 * article may be displayed differently in different variants. this
1956 * function returns the apporiate title defined in the body of the article.
1958 * @return string
1960 function getParsedTitle() {
1961 return $this->mConverter->getParsedTitle();
1965 * Enclose a string with the "no conversion" tag. This is used by
1966 * various functions in the Parser
1968 * @param string $text text to be tagged for no conversion
1969 * @return string the tagged text
1971 function markNoConversion( $text, $noParse=false ) {
1972 return $this->mConverter->markNoConversion( $text, $noParse );
1976 * A regular expression to match legal word-trailing characters
1977 * which should be merged onto a link of the form [[foo]]bar.
1979 * @return string
1980 * @public
1982 function linkTrail() {
1983 $this->load();
1984 return $this->linkTrail;
1987 function getLangObj() {
1988 return $this;
1992 * Get the RFC 3066 code for this language object
1994 function getCode() {
1995 return $this->mCode;
1998 function setCode( $code ) {
1999 $this->mCode = $code;
2002 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
2003 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
2006 static function getMessagesFileName( $code ) {
2007 global $IP;
2008 return self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
2011 static function getClassFileName( $code ) {
2012 global $IP;
2013 return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
2016 static function getLocalisationArray( $code, $disableCache = false ) {
2017 self::loadLocalisation( $code, $disableCache );
2018 return self::$mLocalisationCache[$code];
2022 * Load localisation data for a given code into the static cache
2024 * @return array Dependencies, map of filenames to mtimes
2026 static function loadLocalisation( $code, $disableCache = false ) {
2027 static $recursionGuard = array();
2028 global $wgMemc, $wgCheckSerialized;
2030 if ( !$code ) {
2031 throw new MWException( "Invalid language code requested" );
2034 if ( !$disableCache ) {
2035 # Try the per-process cache
2036 if ( isset( self::$mLocalisationCache[$code] ) ) {
2037 return self::$mLocalisationCache[$code]['deps'];
2040 wfProfileIn( __METHOD__ );
2042 # Try the serialized directory
2043 $cache = wfGetPrecompiledData( self::getFileName( "Messages", $code, '.ser' ) );
2044 if ( $cache ) {
2045 if ( $wgCheckSerialized && self::isLocalisationOutOfDate( $cache ) ) {
2046 $cache = false;
2047 wfDebug( "Language::loadLocalisation(): precompiled data file for $code is out of date\n" );
2048 } else {
2049 self::$mLocalisationCache[$code] = $cache;
2050 wfDebug( "Language::loadLocalisation(): got localisation for $code from precompiled data file\n" );
2051 wfProfileOut( __METHOD__ );
2052 return self::$mLocalisationCache[$code]['deps'];
2056 # Try the global cache
2057 $memcKey = wfMemcKey('localisation', $code );
2058 $cache = $wgMemc->get( $memcKey );
2059 if ( $cache ) {
2060 if ( self::isLocalisationOutOfDate( $cache ) ) {
2061 $wgMemc->delete( $memcKey );
2062 $cache = false;
2063 wfDebug( "Language::loadLocalisation(): localisation cache for $code had expired\n" );
2064 } else {
2065 self::$mLocalisationCache[$code] = $cache;
2066 wfDebug( "Language::loadLocalisation(): got localisation for $code from cache\n" );
2067 wfProfileOut( __METHOD__ );
2068 return $cache['deps'];
2071 } else {
2072 wfProfileIn( __METHOD__ );
2075 # Default fallback, may be overridden when the messages file is included
2076 if ( $code != 'en' ) {
2077 $fallback = 'en';
2078 } else {
2079 $fallback = false;
2082 # Load the primary localisation from the source file
2083 $filename = self::getMessagesFileName( $code );
2084 if ( !file_exists( $filename ) ) {
2085 wfDebug( "Language::loadLocalisation(): no localisation file for $code, using implicit fallback to en\n" );
2086 $cache = array();
2087 $deps = array();
2088 } else {
2089 $deps = array( $filename => filemtime( $filename ) );
2090 require( $filename );
2091 $cache = compact( self::$mLocalisationKeys );
2092 wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
2095 if ( !empty( $fallback ) ) {
2096 # Load the fallback localisation, with a circular reference guard
2097 if ( isset( $recursionGuard[$code] ) ) {
2098 throw new MWException( "Error: Circular fallback reference in language code $code" );
2100 $recursionGuard[$code] = true;
2101 $newDeps = self::loadLocalisation( $fallback, $disableCache );
2102 unset( $recursionGuard[$code] );
2104 $secondary = self::$mLocalisationCache[$fallback];
2105 $deps = array_merge( $deps, $newDeps );
2107 # Merge the fallback localisation with the current localisation
2108 foreach ( self::$mLocalisationKeys as $key ) {
2109 if ( isset( $cache[$key] ) ) {
2110 if ( isset( $secondary[$key] ) ) {
2111 if ( in_array( $key, self::$mMergeableMapKeys ) ) {
2112 $cache[$key] = $cache[$key] + $secondary[$key];
2113 } elseif ( in_array( $key, self::$mMergeableListKeys ) ) {
2114 $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
2115 } elseif ( in_array( $key, self::$mMergeableAliasListKeys ) ) {
2116 $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
2119 } else {
2120 $cache[$key] = $secondary[$key];
2124 # Merge bookstore lists if requested
2125 if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
2126 $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
2128 if ( isset( $cache['bookstoreList']['inherit'] ) ) {
2129 unset( $cache['bookstoreList']['inherit'] );
2133 # Add dependencies to the cache entry
2134 $cache['deps'] = $deps;
2136 # Replace spaces with underscores in namespace names
2137 $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
2139 # And do the same for specialpage aliases. $page is an array.
2140 foreach ( $cache['specialPageAliases'] as &$page ) {
2141 $page = str_replace( ' ', '_', $page );
2143 # Decouple the reference to prevent accidental damage
2144 unset($page);
2146 # Save to both caches
2147 self::$mLocalisationCache[$code] = $cache;
2148 if ( !$disableCache ) {
2149 $wgMemc->set( $memcKey, $cache );
2152 wfProfileOut( __METHOD__ );
2153 return $deps;
2157 * Test if a given localisation cache is out of date with respect to the
2158 * source Messages files. This is done automatically for the global cache
2159 * in $wgMemc, but is only done on certain occasions for the serialized
2160 * data file.
2162 * @param $cache mixed Either a language code or a cache array
2164 static function isLocalisationOutOfDate( $cache ) {
2165 if ( !is_array( $cache ) ) {
2166 self::loadLocalisation( $cache );
2167 $cache = self::$mLocalisationCache[$cache];
2169 $expired = false;
2170 foreach ( $cache['deps'] as $file => $mtime ) {
2171 if ( !file_exists( $file ) || filemtime( $file ) > $mtime ) {
2172 $expired = true;
2173 break;
2176 return $expired;
2180 * Get the fallback for a given language
2182 static function getFallbackFor( $code ) {
2183 self::loadLocalisation( $code );
2184 return self::$mLocalisationCache[$code]['fallback'];
2187 /**
2188 * Get all messages for a given language
2190 static function getMessagesFor( $code ) {
2191 self::loadLocalisation( $code );
2192 return self::$mLocalisationCache[$code]['messages'];
2195 /**
2196 * Get a message for a given language
2198 static function getMessageFor( $key, $code ) {
2199 self::loadLocalisation( $code );
2200 return isset( self::$mLocalisationCache[$code]['messages'][$key] ) ? self::$mLocalisationCache[$code]['messages'][$key] : null;
2204 * Load localisation data for this object
2206 function load() {
2207 if ( !$this->mLoaded ) {
2208 self::loadLocalisation( $this->getCode() );
2209 $cache =& self::$mLocalisationCache[$this->getCode()];
2210 foreach ( self::$mLocalisationKeys as $key ) {
2211 $this->$key = $cache[$key];
2213 $this->mLoaded = true;
2215 $this->fixUpSettings();
2220 * Do any necessary post-cache-load settings adjustment
2222 function fixUpSettings() {
2223 global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk,
2224 $wgNamespaceAliases, $wgAmericanDates;
2225 wfProfileIn( __METHOD__ );
2226 if ( $wgExtraNamespaces ) {
2227 $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames;
2230 $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
2231 if ( $wgMetaNamespaceTalk ) {
2232 $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
2233 } else {
2234 $talk = $this->namespaceNames[NS_PROJECT_TALK];
2235 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
2237 # Allow grammar transformations
2238 # Allowing full message-style parsing would make simple requests
2239 # such as action=raw much more expensive than they need to be.
2240 # This will hopefully cover most cases.
2241 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
2242 array( &$this, 'replaceGrammarInNamespace' ), $talk );
2243 $talk = str_replace( ' ', '_', $talk );
2244 $this->namespaceNames[NS_PROJECT_TALK] = $talk;
2247 # The above mixing may leave namespaces out of canonical order.
2248 # Re-order by namespace ID number...
2249 ksort( $this->namespaceNames );
2251 # Put namespace names and aliases into a hashtable.
2252 # If this is too slow, then we should arrange it so that it is done
2253 # before caching. The catch is that at pre-cache time, the above
2254 # class-specific fixup hasn't been done.
2255 $this->mNamespaceIds = array();
2256 foreach ( $this->namespaceNames as $index => $name ) {
2257 $this->mNamespaceIds[$this->lc($name)] = $index;
2259 if ( $this->namespaceAliases ) {
2260 foreach ( $this->namespaceAliases as $name => $index ) {
2261 $this->mNamespaceIds[$this->lc($name)] = $index;
2264 if ( $wgNamespaceAliases ) {
2265 foreach ( $wgNamespaceAliases as $name => $index ) {
2266 $this->mNamespaceIds[$this->lc($name)] = $index;
2270 if ( $this->defaultDateFormat == 'dmy or mdy' ) {
2271 $this->defaultDateFormat = $wgAmericanDates ? 'mdy' : 'dmy';
2273 wfProfileOut( __METHOD__ );
2276 function replaceGrammarInNamespace( $m ) {
2277 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
2280 static function getCaseMaps() {
2281 static $wikiUpperChars, $wikiLowerChars;
2282 if ( isset( $wikiUpperChars ) ) {
2283 return array( $wikiUpperChars, $wikiLowerChars );
2286 wfProfileIn( __METHOD__ );
2287 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
2288 if ( $arr === false ) {
2289 throw new MWException(
2290 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
2292 extract( $arr );
2293 wfProfileOut( __METHOD__ );
2294 return array( $wikiUpperChars, $wikiLowerChars );
2297 function formatTimePeriod( $seconds ) {
2298 if ( $seconds < 10 ) {
2299 return $this->formatNum( sprintf( "%.1f", $seconds ) ) . wfMsg( 'seconds-abbrev' );
2300 } elseif ( $seconds < 60 ) {
2301 return $this->formatNum( round( $seconds ) ) . wfMsg( 'seconds-abbrev' );
2302 } elseif ( $seconds < 3600 ) {
2303 return $this->formatNum( floor( $seconds / 60 ) ) . wfMsg( 'minutes-abbrev' ) .
2304 $this->formatNum( round( fmod( $seconds, 60 ) ) ) . wfMsg( 'seconds-abbrev' );
2305 } else {
2306 $hours = floor( $seconds / 3600 );
2307 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
2308 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
2309 return $this->formatNum( $hours ) . wfMsg( 'hours-abbrev' ) .
2310 $this->formatNum( $minutes ) . wfMsg( 'minutes-abbrev' ) .
2311 $this->formatNum( $secondsPart ) . wfMsg( 'seconds-abbrev' );
2315 function formatBitrate( $bps ) {
2316 $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
2317 if ( $bps <= 0 ) {
2318 return $this->formatNum( $bps ) . $units[0];
2320 $unitIndex = floor( log10( $bps ) / 3 );
2321 $mantissa = $bps / pow( 1000, $unitIndex );
2322 if ( $mantissa < 10 ) {
2323 $mantissa = round( $mantissa, 1 );
2324 } else {
2325 $mantissa = round( $mantissa );
2327 return $this->formatNum( $mantissa ) . $units[$unitIndex];
2331 * Format a size in bytes for output, using an appropriate
2332 * unit (B, KB, MB or GB) according to the magnitude in question
2334 * @param $size Size to format
2335 * @return string Plain text (not HTML)
2337 function formatSize( $size ) {
2338 // For small sizes no decimal places necessary
2339 $round = 0;
2340 if( $size > 1024 ) {
2341 $size = $size / 1024;
2342 if( $size > 1024 ) {
2343 $size = $size / 1024;
2344 // For MB and bigger two decimal places are smarter
2345 $round = 2;
2346 if( $size > 1024 ) {
2347 $size = $size / 1024;
2348 $msg = 'size-gigabytes';
2349 } else {
2350 $msg = 'size-megabytes';
2352 } else {
2353 $msg = 'size-kilobytes';
2355 } else {
2356 $msg = 'size-bytes';
2358 $size = round( $size, $round );
2359 $text = $this->getMessageFromDB( $msg );
2360 return str_replace( '$1', $this->formatNum( $size ), $text );