6 if( !defined( 'MEDIAWIKI' ) ) {
7 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
12 global $wgLanguageNames;
13 require_once( dirname(__FILE__
) . '/Names.php' ) ;
15 global $wgInputEncoding, $wgOutputEncoding;
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 */
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 #--------------------------------------------------------------------------
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',
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',
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',
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 ) {
121 static $recursionLevel = 0;
123 if ( $code == 'en' ) {
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 );
143 $lang = Language
::factory( $fallback );
145 $lang->setCode( $code );
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' ) {
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() {}
173 function getDefaultUserOptions() {
174 trigger_error( 'Use of ' . __METHOD__
. ' is deprecated', E_USER_NOTICE
);
175 return User
::getDefaultOptions();
178 function getFallbackLanguageCode() {
179 return self
::getFallbackFor( $this->mCode
);
183 * Exports $wgBookstoreListEn
186 function getBookstoreList() {
188 return $this->bookstoreList
;
194 function getNamespaces() {
196 return $this->namespaceNames
;
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.
207 function getFormattedNamespaces() {
208 $ns = $this->getNamespaces();
209 foreach($ns as $k => $v) {
210 $ns[$k] = strtr($v, '_', ' ');
216 * Get a namespace value by key
218 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
219 * echo $mw_ns; // prints 'MediaWiki'
222 * @param int $index the array key of the namespace to return
223 * @return mixed, string if the namespace value exists, otherwise false
225 function getNsText( $index ) {
226 $ns = $this->getNamespaces();
227 return isset( $ns[$index] ) ?
$ns[$index] : false;
231 * A convenience function that returns the same thing as
232 * getNsText() except with '_' changed to ' ', useful for
237 function getFormattedNsText( $index ) {
238 $ns = $this->getNsText( $index );
239 return strtr($ns, '_', ' ');
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.
247 * @param string $text
248 * @return mixed An integer if $text is a valid value otherwise false
250 function getLocalNsIndex( $text ) {
252 $lctext = $this->lc($text);
253 return isset( $this->mNamespaceIds
[$lctext] ) ?
$this->mNamespaceIds
[$lctext] : false;
257 * Get a namespace key by value, case insensitive. Canonical namespace
258 * names override custom ones defined for the current language.
260 * @param string $text
261 * @return mixed An integer if $text is a valid value otherwise false
263 function getNsIndex( $text ) {
265 $lctext = $this->lc($text);
266 if( ( $ns = MWNamespace
::getCanonicalIndex( $lctext ) ) !== null ) return $ns;
267 return isset( $this->mNamespaceIds
[$lctext] ) ?
$this->mNamespaceIds
[$lctext] : false;
271 * short names for language variants used for language conversion links.
273 * @param string $code
276 function getVariantname( $code ) {
277 return $this->getMessageFromDB( "variantname-$code" );
280 function specialPage( $name ) {
281 $aliases = $this->getSpecialPageAliases();
282 if ( isset( $aliases[$name][0] ) ) {
283 $name = $aliases[$name][0];
285 return $this->getNsText(NS_SPECIAL
) . ':' . $name;
288 function getQuickbarSettings() {
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' )
298 function getSkinNames() {
300 return $this->skinNames
;
303 function getMathNames() {
305 return $this->mathNames
;
308 function getDatePreferences() {
310 return $this->datePreferences
;
313 function getDateFormats() {
315 return $this->dateFormats
;
318 function getDefaultDateFormat() {
320 return $this->defaultDateFormat
;
323 function getDatePreferenceMigrationMap() {
325 return $this->datePreferenceMigrationMap
;
328 function getDefaultUserOptionOverrides() {
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
;
338 function getExtraUserToggles() {
340 return $this->extraUserToggles
;
343 function getUserToggle( $tog ) {
344 return $this->getMessageFromDB( "tog-$tog" );
348 * Get language names, indexed by code.
349 * If $customisedOnly is true, only returns codes with a messages file
351 public static function getLanguageNames( $customisedOnly = false ) {
352 global $wgLanguageNames, $wgExtraLanguageNames;
353 $allNames = $wgExtraLanguageNames +
$wgLanguageNames;
354 if ( !$customisedOnly ) {
360 $dir = opendir( "$IP/languages/messages" );
361 while( false !== ( $file = readdir( $dir ) ) ) {
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];
375 * Ugly hack to get a message maybe from the MediaWiki namespace, if this
376 * language object is the content or user language.
378 function getMessageFromDB( $msg ) {
379 global $wgContLang, $wgLang;
380 if ( $wgContLang->getCode() == $this->getCode() ) {
382 return wfMsgForContent( $msg );
383 } elseif ( $wgLang->getCode() == $this->getCode() ) {
385 return wfMsg( $msg );
387 # Neither, get from localisation
388 return $this->getMessage( $msg );
392 function getLanguageName( $code ) {
393 $names = self
::getLanguageNames();
394 if ( !array_key_exists( $code, $names ) ) {
397 return $names[$code];
400 function getMonthName( $key ) {
401 return $this->getMessageFromDB( self
::$mMonthMsgs[$key-1] );
404 function getMonthNameGen( $key ) {
405 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key-1] );
408 function getMonthAbbreviation( $key ) {
409 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key-1] );
412 function getWeekdayName( $key ) {
413 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key-1] );
416 function getWeekdayAbbreviation( $key ) {
417 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key-1] );
420 function getIranianCalendarMonthName( $key ) {
421 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key-1] );
424 function getHebrewCalendarMonthName( $key ) {
425 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key-1] );
428 function getHebrewCalendarMonthNameGen( $key ) {
429 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key-1] );
434 * Used by date() and time() to adjust the time output.
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)
441 function userAdjust( $ts, $tz = false ) {
442 global $wgUser, $wgLocalTZoffset;
445 $tz = $wgUser->getOption( 'timecorrection' );
448 # minutes and hours differences:
453 # Global offset in minutes.
454 if( isset($wgLocalTZoffset) ) {
455 if( $wgLocalTZoffset >= 0 ) {
456 $hrDiff = floor($wgLocalTZoffset / 60);
458 $hrDiff = ceil($wgLocalTZoffset / 60);
460 $minDiff = $wgLocalTZoffset %
60;
462 } elseif ( strpos( $tz, ':' ) !== false ) {
463 $tzArray = explode( ':', $tz );
464 $hrDiff = intval($tzArray[0]);
465 $minDiff = intval($hrDiff < 0 ?
-$tzArray[1] : $tzArray[1]);
467 $hrDiff = intval( $tz );
470 # No difference ? Return time unchanged
471 if ( 0 == $hrDiff && 0 == $minDiff ) { return $ts; }
473 wfSuppressWarnings(); // E_STRICT system time bitching
474 # Generate an adjusted date
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
483 $date = date( 'YmdHis', $t );
490 * This is a workalike of PHP's date() function, but with better
491 * internationalisation, a reduced set of format characters, and a better
494 * Supported format characters are dDjlNwzWFmMntLYyaAgGhHiscrU. See the
495 * PHP manual for definitions. There are a number of extensions, which
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
503 * xg Genitive month name
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
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
517 * xkY Y (full year) in Thai solar calendar. Months and days are
518 * identical to the Gregorian calendar
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:
524 * "The month is" F => The month is January
527 * Backslash escaping is also supported.
529 * Input timestamp is assumed to be pre-normalized to the desired local
532 * @param string $format
533 * @param string $ts 14-character timestamp
537 function sprintfDate( $format, $ts ) {
547 for ( $p = 0; $p < strlen( $format ); $p++
) {
550 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
551 $code .= $format[++
$p];
554 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ) && $p < strlen( $format ) - 1 ) {
555 $code .= $format[++
$p];
566 $rawToggle = !$rawToggle;
575 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
578 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
579 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
582 $num = substr( $ts, 6, 2 );
585 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
586 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) +
1 );
589 $num = intval( substr( $ts, 6, 2 ) );
592 if ( !$iranian ) $iranian = self
::tsToIranian( $ts );
596 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
600 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
601 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) +
1 );
604 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
605 $w = gmdate( 'w', $unix );
609 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
610 $num = gmdate( 'w', $unix );
613 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
614 $num = gmdate( 'z', $unix );
617 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
618 $num = gmdate( 'W', $unix );
621 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
624 if ( !$iranian ) $iranian = self
::tsToIranian( $ts );
625 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
628 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
629 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
632 $num = substr( $ts, 4, 2 );
635 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
638 $num = intval( substr( $ts, 4, 2 ) );
641 if ( !$iranian ) $iranian = self
::tsToIranian( $ts );
645 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
649 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
650 $num = gmdate( 't', $unix );
653 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
657 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
658 $num = gmdate( 'L', $unix );
661 $num = substr( $ts, 0, 4 );
664 if ( !$iranian ) $iranian = self
::tsToIranian( $ts );
668 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
672 if ( !$thai ) $thai = self
::tsToThai( $ts );
676 $num = substr( $ts, 2, 2 );
679 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
682 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
685 $h = substr( $ts, 8, 2 );
686 $num = $h %
12 ?
$h %
12 : 12;
689 $num = intval( substr( $ts, 8, 2 ) );
692 $h = substr( $ts, 8, 2 );
693 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
696 $num = substr( $ts, 8, 2 );
699 $num = substr( $ts, 10, 2 );
702 $num = substr( $ts, 12, 2 );
705 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
706 $s .= gmdate( 'c', $unix );
709 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
710 $s .= gmdate( 'r', $unix );
713 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
718 if ( $p < strlen( $format ) - 1 ) {
726 if ( $p < strlen( $format ) - 1 ) {
727 $endQuote = strpos( $format, '"', $p +
1 );
728 if ( $endQuote === false ) {
729 # No terminating quote, assume literal "
732 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
736 # Quote at end of string, assume literal "
743 if ( $num !== false ) {
744 if ( $rawToggle ||
$raw ) {
747 } elseif ( $roman ) {
748 $s .= self
::romanNumeral( $num );
750 } elseif( $hebrewNum ) {
751 $s .= self
::hebrewNumeral( $num );
754 $s .= $this->formatNum( $num, true );
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 );
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.
770 * Link: http://www.farsiweb.info/jalali/jalali.c
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;
777 # Days passed from the beginning (including leap years)
780 - floor(($gy+
99) / 100)
781 +
floor(($gy+
399) / 400);
784 // Add days of the past months of this year
785 for( $i = 0; $i < $gm; $i++
) {
786 $gDayNo +
= self
::$GREG_DAYS[$i];
790 if ( $gm > 1 && (($gy%4
===0 && $gy%100
!==0 ||
($gy%400
==0)))) {
794 // Days passed in current month
797 $jDayNo = $gDayNo - 79;
799 $jNp = floor($jDayNo / 12053);
802 $jy = 979 +
33*$jNp +
4*floor($jDayNo/1461);
805 if ( $jDayNo >= 366 ) {
806 $jy +
= floor(($jDayNo-1)/365);
807 $jDayNo = floor(($jDayNo-1)%365
);
810 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
811 $jDayNo -= self
::$IRANIAN_DAYS[$i];
817 return array($jy, $jm, $jd);
821 * Converting Gregorian dates to Hebrew dates.
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
828 private static function tsToHebrew( $ts ) {
830 $year = substr( $ts, 0, 4 );
831 $month = substr( $ts, 4, 2 );
832 $day = substr( $ts, 6, 2 );
834 # Calculate Hebrew year
835 $hebrewYear = $year +
3760;
837 # Month number when September = 1, August = 12
846 # Calculate day of year from 1 September
848 for( $i = 1; $i < $month; $i++
) {
852 # Check if the year is leap
853 if( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
856 } elseif( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
863 # Calculate the start of the Hebrew year
864 $start = self
::hebrewYearStart( $hebrewYear );
866 # Calculate next year's start
867 if( $dayOfYear <= $start ) {
868 # Day is before the start of the year - it is the previous year
874 # Add days since previous year's 1 September
876 if( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
880 # Start of the new (previous) year
881 $start = self
::hebrewYearStart( $hebrewYear );
884 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
887 # Calculate Hebrew day of year
888 $hebrewDayOfYear = $dayOfYear - $start;
890 # Difference between year's days
891 $diff = $nextStart - $start;
892 # Add 12 (or 13 for leap years) days to ignore the difference between
893 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
894 # difference is only about the year type
895 if( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
901 # Check the year pattern, and is leap year
902 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
903 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
905 $yearPattern = $diff %
30;
907 $isLeap = $diff >= 30;
909 # Calculate day in the month from number of day in the Hebrew year
910 # Don't check Adar - if the day is not in Adar, we will stop before;
911 # if it is in Adar, we will use it to check if it is Adar I or Adar II
912 $hebrewDay = $hebrewDayOfYear;
915 while( $hebrewMonth <= 12 ) {
916 # Calculate days in this month
917 if( $isLeap && $hebrewMonth == 6 ) {
918 # Adar in a leap year
920 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
922 if( $hebrewDay <= $days ) {
926 # Subtract the days of Adar I
930 if( $hebrewDay <= $days ) {
936 } elseif( $hebrewMonth == 2 && $yearPattern == 2 ) {
937 # Cheshvan in a complete year (otherwise as the rule below)
939 } elseif( $hebrewMonth == 3 && $yearPattern == 0 ) {
940 # Kislev in an incomplete year (otherwise as the rule below)
943 # Odd months have 30 days, even have 29
944 $days = 30 - ( $hebrewMonth - 1 ) %
2;
946 if( $hebrewDay <= $days ) {
947 # In the current month
950 # Subtract the days of the current month
952 # Try in the next month
957 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
961 * This calculates the Hebrew year start, as days since 1 September.
962 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
963 * Used for Hebrew date.
965 private static function hebrewYearStart( $year ) {
966 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
967 $b = intval( ( $year - 1 ) %
4 );
968 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
978 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7);
979 if( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
981 } else if( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
983 } else if( $c == 2 ||
$c == 4 ||
$c == 6 ) {
987 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
992 * Algorithm to convert Gregorian dates to Thai solar dates.
994 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
996 * @param string $ts 14-character timestamp
997 * @return array converted year, month, day
999 private static function tsToThai( $ts ) {
1000 $gy = substr( $ts, 0, 4 );
1001 $gm = substr( $ts, 4, 2 );
1002 $gd = substr( $ts, 6, 2 );
1004 # Add 543 years to the Gregorian calendar
1005 # Months and days are identical
1006 $gy_thai = $gy +
543;
1008 return array( $gy_thai, $gm, $gd );
1013 * Roman number formatting up to 3000
1015 static function romanNumeral( $num ) {
1016 static $table = array(
1017 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1018 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1019 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1020 array( '', 'M', 'MM', 'MMM' )
1023 $num = intval( $num );
1024 if ( $num > 3000 ||
$num <= 0 ) {
1029 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1030 if ( $num >= $pow10 ) {
1031 $s .= $table[$i][floor($num / $pow10)];
1033 $num = $num %
$pow10;
1039 * Hebrew Gematria number formatting up to 9999
1041 static function hebrewNumeral( $num ) {
1042 static $table = array(
1043 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1044 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1045 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1046 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1049 $num = intval( $num );
1050 if ( $num > 9999 ||
$num <= 0 ) {
1055 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1056 if ( $num >= $pow10 ) {
1057 if ( $num == 15 ||
$num == 16 ) {
1058 $s .= $table[0][9] . $table[0][$num - 9];
1061 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1062 if( $pow10 == 1000 ) {
1067 $num = $num %
$pow10;
1069 if( strlen( $s ) == 2 ) {
1072 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1073 $str .= substr( $s, strlen( $s ) - 2, 2 );
1075 $start = substr( $str, 0, strlen( $str ) - 2 );
1076 $end = substr( $str, strlen( $str ) - 2 );
1079 $str = $start . 'ך';
1082 $str = $start . 'ם';
1085 $str = $start . 'ן';
1088 $str = $start . 'ף';
1091 $str = $start . 'ץ';
1098 * This is meant to be used by time(), date(), and timeanddate() to get
1099 * the date preference they're supposed to use, it should be used in
1103 * function timeanddate([...], $format = true) {
1104 * $datePreference = $this->dateFormat($format);
1109 * @param mixed $usePrefs: if true, the user's preference is used
1110 * if false, the site/language default is used
1111 * if int/string, assumed to be a format.
1114 function dateFormat( $usePrefs = true ) {
1117 if( is_bool( $usePrefs ) ) {
1119 $datePreference = $wgUser->getDatePreference();
1121 $options = User
::getDefaultOptions();
1122 $datePreference = (string)$options['date'];
1125 $datePreference = (string)$usePrefs;
1129 if( $datePreference == '' ) {
1133 return $datePreference;
1138 * @param mixed $ts the time format which needs to be turned into a
1139 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1140 * @param bool $adj whether to adjust the time output according to the
1141 * user configured offset ($timecorrection)
1142 * @param mixed $format true to use user's date format preference
1143 * @param string $timecorrection the time offset as returned by
1144 * validateTimeZone() in Special:Preferences
1147 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1150 $ts = $this->userAdjust( $ts, $timecorrection );
1153 $pref = $this->dateFormat( $format );
1154 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref date"] ) ) {
1155 $pref = $this->defaultDateFormat
;
1157 return $this->sprintfDate( $this->dateFormats
["$pref date"], $ts );
1162 * @param mixed $ts the time format which needs to be turned into a
1163 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1164 * @param bool $adj whether to adjust the time output according to the
1165 * user configured offset ($timecorrection)
1166 * @param mixed $format true to use user's date format preference
1167 * @param string $timecorrection the time offset as returned by
1168 * validateTimeZone() in Special:Preferences
1171 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1174 $ts = $this->userAdjust( $ts, $timecorrection );
1177 $pref = $this->dateFormat( $format );
1178 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref time"] ) ) {
1179 $pref = $this->defaultDateFormat
;
1181 return $this->sprintfDate( $this->dateFormats
["$pref time"], $ts );
1186 * @param mixed $ts the time format which needs to be turned into a
1187 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1188 * @param bool $adj whether to adjust the time output according to the
1189 * user configured offset ($timecorrection)
1191 * @param mixed $format what format to return, if it's false output the
1192 * default one (default true)
1193 * @param string $timecorrection the time offset as returned by
1194 * validateTimeZone() in Special:Preferences
1197 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false) {
1200 $ts = wfTimestamp( TS_MW
, $ts );
1203 $ts = $this->userAdjust( $ts, $timecorrection );
1206 $pref = $this->dateFormat( $format );
1207 if( $pref == 'default' ||
!isset( $this->dateFormats
["$pref both"] ) ) {
1208 $pref = $this->defaultDateFormat
;
1211 return $this->sprintfDate( $this->dateFormats
["$pref both"], $ts );
1214 function getMessage( $key ) {
1216 return isset( $this->messages
[$key] ) ?
$this->messages
[$key] : null;
1219 function getAllMessages() {
1221 return $this->messages
;
1224 function iconv( $in, $out, $string ) {
1225 # For most languages, this is a wrapper for iconv
1226 return iconv( $in, $out . '//IGNORE', $string );
1229 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
1230 function ucwordbreaksCallbackAscii($matches){
1231 return $this->ucfirst($matches[1]);
1234 function ucwordbreaksCallbackMB($matches){
1235 return mb_strtoupper($matches[0]);
1238 function ucCallback($matches){
1239 list( $wikiUpperChars ) = self
::getCaseMaps();
1240 return strtr( $matches[1], $wikiUpperChars );
1243 function lcCallback($matches){
1244 list( , $wikiLowerChars ) = self
::getCaseMaps();
1245 return strtr( $matches[1], $wikiLowerChars );
1248 function ucwordsCallbackMB($matches){
1249 return mb_strtoupper($matches[0]);
1252 function ucwordsCallbackWiki($matches){
1253 list( $wikiUpperChars ) = self
::getCaseMaps();
1254 return strtr( $matches[0], $wikiUpperChars );
1257 function ucfirst( $str ) {
1258 if ( empty($str) ) return $str;
1259 if ( ord($str[0]) < 128 ) return ucfirst($str);
1260 else return self
::uc($str,true); // fall back to more complex logic in case of multibyte strings
1263 function uc( $str, $first = false ) {
1264 if ( function_exists( 'mb_strtoupper' ) ) {
1266 if ( self
::isMultibyte( $str ) ) {
1267 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1269 return ucfirst( $str );
1272 return self
::isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
1275 if ( self
::isMultibyte( $str ) ) {
1276 list( $wikiUpperChars ) = $this->getCaseMaps();
1277 $x = $first ?
'^' : '';
1278 return preg_replace_callback(
1279 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1280 array($this,"ucCallback"),
1284 return $first ?
ucfirst( $str ) : strtoupper( $str );
1289 function lcfirst( $str ) {
1290 if ( empty($str) ) return $str;
1291 if ( is_string( $str ) && ord($str[0]) < 128 ) {
1292 // editing string in place = cool
1293 $str[0]=strtolower($str[0]);
1296 else return self
::lc( $str, true );
1299 function lc( $str, $first = false ) {
1300 if ( function_exists( 'mb_strtolower' ) )
1302 if ( self
::isMultibyte( $str ) )
1303 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1305 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
1307 return self
::isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
1309 if ( self
::isMultibyte( $str ) ) {
1310 list( , $wikiLowerChars ) = self
::getCaseMaps();
1311 $x = $first ?
'^' : '';
1312 return preg_replace_callback(
1313 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1314 array($this,"lcCallback"),
1318 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
1321 function isMultibyte( $str ) {
1322 return (bool)preg_match( '/[\x80-\xff]/', $str );
1325 function ucwords($str) {
1326 if ( self
::isMultibyte( $str ) ) {
1327 $str = self
::lc($str);
1329 // regexp to find first letter in each word (i.e. after each space)
1330 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1332 // function to use to capitalize a single char
1333 if ( function_exists( 'mb_strtoupper' ) )
1334 return preg_replace_callback(
1336 array($this,"ucwordsCallbackMB"),
1340 return preg_replace_callback(
1342 array($this,"ucwordsCallbackWiki"),
1347 return ucwords( strtolower( $str ) );
1350 # capitalize words at word breaks
1351 function ucwordbreaks($str){
1352 if (self
::isMultibyte( $str ) ) {
1353 $str = self
::lc($str);
1355 // since \b doesn't work for UTF-8, we explicitely define word break chars
1356 $breaks= "[ \-\(\)\}\{\.,\?!]";
1358 // find first letter after word break
1359 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1361 if ( function_exists( 'mb_strtoupper' ) )
1362 return preg_replace_callback(
1364 array($this,"ucwordbreaksCallbackMB"),
1368 return preg_replace_callback(
1370 array($this,"ucwordsCallbackWiki"),
1375 return preg_replace_callback(
1376 '/\b([\w\x80-\xff]+)\b/',
1377 array($this,"ucwordbreaksCallbackAscii"),
1382 * Return a case-folded representation of $s
1384 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
1385 * and $s2 are the same except for the case of their characters. It is not
1386 * necessary for the value returned to make sense when displayed.
1388 * Do *not* perform any other normalisation in this function. If a caller
1389 * uses this function when it should be using a more general normalisation
1390 * function, then fix the caller.
1392 function caseFold( $s ) {
1393 return $this->uc( $s );
1396 function checkTitleEncoding( $s ) {
1397 if( is_array( $s ) ) {
1398 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
1400 # Check for non-UTF-8 URLs
1401 $ishigh = preg_match( '/[\x80-\xff]/', $s);
1402 if(!$ishigh) return $s;
1404 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1405 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
1406 if( $isutf8 ) return $s;
1408 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
1411 function fallback8bitEncoding() {
1413 return $this->fallback8bitEncoding
;
1417 * Some languages have special punctuation to strip out
1418 * or characters which need to be converted for MySQL's
1419 * indexing to grok it correctly. Make such changes here.
1424 function stripForSearch( $string ) {
1426 if ( $wgDBtype != 'mysql' ) {
1430 # MySQL fulltext index doesn't grok utf-8, so we
1431 # need to fold cases and convert to hex
1433 wfProfileIn( __METHOD__
);
1434 if( function_exists( 'mb_strtolower' ) ) {
1435 $out = preg_replace(
1436 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1437 "'U8' . bin2hex( \"$1\" )",
1438 mb_strtolower( $string ) );
1440 list( , $wikiLowerChars ) = self
::getCaseMaps();
1441 $out = preg_replace(
1442 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
1443 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
1446 wfProfileOut( __METHOD__
);
1450 function convertForSearchResult( $termsArray ) {
1451 # some languages, e.g. Chinese, need to do a conversion
1452 # in order for search results to be displayed correctly
1457 * Get the first character of a string.
1462 function firstChar( $s ) {
1464 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1465 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
1467 if ( isset( $matches[1] ) ) {
1468 if ( strlen( $matches[1] ) != 3 ) {
1472 // Break down Hangul syllables to grab the first jamo
1473 $code = utf8ToCodepoint( $matches[1] );
1474 if ( $code < 0xac00 ||
0xd7a4 <= $code) {
1476 } elseif ( $code < 0xb098 ) {
1477 return "\xe3\x84\xb1";
1478 } elseif ( $code < 0xb2e4 ) {
1479 return "\xe3\x84\xb4";
1480 } elseif ( $code < 0xb77c ) {
1481 return "\xe3\x84\xb7";
1482 } elseif ( $code < 0xb9c8 ) {
1483 return "\xe3\x84\xb9";
1484 } elseif ( $code < 0xbc14 ) {
1485 return "\xe3\x85\x81";
1486 } elseif ( $code < 0xc0ac ) {
1487 return "\xe3\x85\x82";
1488 } elseif ( $code < 0xc544 ) {
1489 return "\xe3\x85\x85";
1490 } elseif ( $code < 0xc790 ) {
1491 return "\xe3\x85\x87";
1492 } elseif ( $code < 0xcc28 ) {
1493 return "\xe3\x85\x88";
1494 } elseif ( $code < 0xce74 ) {
1495 return "\xe3\x85\x8a";
1496 } elseif ( $code < 0xd0c0 ) {
1497 return "\xe3\x85\x8b";
1498 } elseif ( $code < 0xd30c ) {
1499 return "\xe3\x85\x8c";
1500 } elseif ( $code < 0xd558 ) {
1501 return "\xe3\x85\x8d";
1503 return "\xe3\x85\x8e";
1510 function initEncoding() {
1511 # Some languages may have an alternate char encoding option
1512 # (Esperanto X-coding, Japanese furigana conversion, etc)
1513 # If this language is used as the primary content language,
1514 # an override to the defaults can be set here on startup.
1517 function recodeForEdit( $s ) {
1518 # For some languages we'll want to explicitly specify
1519 # which characters make it into the edit box raw
1520 # or are converted in some way or another.
1521 # Note that if wgOutputEncoding is different from
1522 # wgInputEncoding, this text will be further converted
1523 # to wgOutputEncoding.
1524 global $wgEditEncoding;
1525 if( $wgEditEncoding == '' or
1526 $wgEditEncoding == 'UTF-8' ) {
1529 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
1533 function recodeInput( $s ) {
1534 # Take the previous into account.
1535 global $wgEditEncoding;
1536 if($wgEditEncoding != "") {
1537 $enc = $wgEditEncoding;
1541 if( $enc == 'UTF-8' ) {
1544 return $this->iconv( $enc, 'UTF-8', $s );
1549 * For right-to-left language support
1559 * A hidden direction mark (LRM or RLM), depending on the language direction
1563 function getDirMark() {
1564 return $this->isRTL() ?
"\xE2\x80\x8F" : "\xE2\x80\x8E";
1568 * An arrow, depending on the language direction
1572 function getArrow() {
1573 return $this->isRTL() ?
'←' : '→';
1577 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
1581 function linkPrefixExtension() {
1583 return $this->linkPrefixExtension
;
1586 function &getMagicWords() {
1588 return $this->magicWords
;
1591 # Fill a MagicWord object with data from here
1592 function getMagic( &$mw ) {
1593 if ( !$this->mMagicHookDone
) {
1594 $this->mMagicHookDone
= true;
1595 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
1597 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
1598 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
1600 $magicWords =& $this->getMagicWords();
1601 if ( isset( $magicWords[$mw->mId
] ) ) {
1602 $rawEntry = $magicWords[$mw->mId
];
1604 # Fall back to English if local list is incomplete
1605 $magicWords =& Language
::getMagicWords();
1606 if ( !isset($magicWords[$mw->mId
]) ) { throw new MWException("Magic word not found" ); }
1607 $rawEntry = $magicWords[$mw->mId
];
1611 if( !is_array( $rawEntry ) ) {
1612 error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
1614 $mw->mCaseSensitive
= $rawEntry[0];
1615 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
1620 * Add magic words to the extension array
1622 function addMagicWordsByLang( $newWords ) {
1623 $code = $this->getCode();
1624 $fallbackChain = array();
1625 while ( $code && !in_array( $code, $fallbackChain ) ) {
1626 $fallbackChain[] = $code;
1627 $code = self
::getFallbackFor( $code );
1629 if ( !in_array( 'en', $fallbackChain ) ) {
1630 $fallbackChain[] = 'en';
1632 $fallbackChain = array_reverse( $fallbackChain );
1633 foreach ( $fallbackChain as $code ) {
1634 if ( isset( $newWords[$code] ) ) {
1635 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
1641 * Get special page names, as an associative array
1642 * case folded alias => real name
1644 function getSpecialPageAliases() {
1646 if ( !isset( $this->mExtendedSpecialPageAliases
) ) {
1647 $this->mExtendedSpecialPageAliases
= $this->specialPageAliases
;
1648 wfRunHooks( 'LanguageGetSpecialPageAliases',
1649 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
1651 return $this->mExtendedSpecialPageAliases
;
1655 * Italic is unsuitable for some languages
1659 * @param string $text The text to be emphasized.
1662 function emphasize( $text ) {
1663 return "<em>$text</em>";
1667 * Normally we output all numbers in plain en_US style, that is
1668 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
1669 * point twohundredthirtyfive. However this is not sutable for all
1670 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
1671 * Icelandic just want to use commas instead of dots, and dots instead
1672 * of commas like "293.291,235".
1674 * An example of this function being called:
1676 * wfMsg( 'message', $wgLang->formatNum( $num ) )
1679 * See LanguageGu.php for the Gujarati implementation and
1680 * LanguageIs.php for the , => . and . => , implementation.
1682 * @todo check if it's viable to use localeconv() for the decimal
1685 * @param mixed $number the string to be formatted, should be an integer or
1686 * a floating point number.
1687 * @param bool $nocommafy Set to true for special numbers like dates
1690 function formatNum( $number, $nocommafy = false ) {
1691 global $wgTranslateNumerals;
1693 $number = $this->commafy($number);
1694 $s = $this->separatorTransformTable();
1695 if (!is_null($s)) { $number = strtr($number, $s); }
1698 if ($wgTranslateNumerals) {
1699 $s = $this->digitTransformTable();
1700 if (!is_null($s)) { $number = strtr($number, $s); }
1706 function parseFormattedNumber( $number ) {
1707 $s = $this->digitTransformTable();
1708 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1710 $s = $this->separatorTransformTable();
1711 if (!is_null($s)) { $number = strtr($number, array_flip($s)); }
1713 $number = strtr( $number, array (',' => '') );
1718 * Adds commas to a given number
1723 function commafy($_) {
1724 return strrev((string)preg_replace('/(\d{3})(?=\d)(?!\d*\.)/','$1,',strrev($_)));
1727 function digitTransformTable() {
1729 return $this->digitTransformTable
;
1732 function separatorTransformTable() {
1734 return $this->separatorTransformTable
;
1739 * For the credit list in includes/Credits.php (action=credits)
1744 function listToText( $l ) {
1747 for ($i = $m; $i >= 0; $i--) {
1750 } else if ($i == $m - 1) {
1751 $s = $l[$i] . ' ' . $this->getMessageFromDB( 'and' ) . ' ' . $s;
1753 $s = $l[$i] . ', ' . $s;
1760 * Truncate a string to a specified length in bytes, appending an optional
1761 * string (e.g. for ellipses)
1763 * The database offers limited byte lengths for some columns in the database;
1764 * multi-byte character sets mean we need to ensure that only whole characters
1765 * are included, otherwise broken characters can be passed to the user
1767 * If $length is negative, the string will be truncated from the beginning
1769 * @param string $string String to truncate
1770 * @param int $length Maximum length (excluding ellipses)
1771 * @param string $ellipses String to append to the truncated text
1774 function truncate( $string, $length, $ellipsis = "" ) {
1775 if( $length == 0 ) {
1778 if ( strlen( $string ) <= abs( $length ) ) {
1782 $string = substr( $string, 0, $length );
1783 $char = ord( $string[strlen( $string ) - 1] );
1785 if ($char >= 0xc0) {
1786 # We got the first byte only of a multibyte char; remove it.
1787 $string = substr( $string, 0, -1 );
1788 } elseif( $char >= 0x80 &&
1789 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
1790 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
1791 # We chopped in the middle of a character; remove it
1794 return $string . $ellipsis;
1796 $string = substr( $string, $length );
1797 $char = ord( $string[0] );
1798 if( $char >= 0x80 && $char < 0xc0 ) {
1799 # We chopped in the middle of a character; remove the whole thing
1800 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
1802 return $ellipsis . $string;
1807 * Grammatical transformations, needed for inflected languages
1808 * Invoked by putting {{grammar:case|word}} in a message
1810 * @param string $word
1811 * @param string $case
1814 function convertGrammar( $word, $case ) {
1815 global $wgGrammarForms;
1816 if ( isset($wgGrammarForms[$this->getCode()][$case][$word]) ) {
1817 return $wgGrammarForms[$this->getCode()][$case][$word];
1823 * Plural form transformations, needed for some languages.
1824 * For example, there are 3 form of plural in Russian and Polish,
1825 * depending on "count mod 10". See [[w:Plural]]
1826 * For English it is pretty simple.
1828 * Invoked by putting {{plural:count|wordform1|wordform2}}
1829 * or {{plural:count|wordform1|wordform2|wordform3}}
1831 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
1833 * @param integer $count Non-localized number
1834 * @param array $forms Different plural forms
1835 * @return string Correct form of plural for $count in this language
1837 function convertPlural( $count, $forms ) {
1838 if ( !count($forms) ) { return ''; }
1839 $forms = $this->preConvertPlural( $forms, 2 );
1841 return ( abs($count) == 1 ) ?
$forms[0] : $forms[1];
1845 * Checks that convertPlural was given an array and pads it to requested
1846 * amound of forms by copying the last one.
1848 * @param integer $count How many forms should there be at least
1849 * @param array $forms Array of forms given to convertPlural
1850 * @return array Padded array of forms or an exception if not an array
1852 protected function preConvertPlural( /* Array */ $forms, $count ) {
1853 while ( count($forms) < $count ) {
1854 $forms[] = $forms[count($forms)-1];
1860 * For translaing of expiry times
1861 * @param string The validated block time in English
1862 * @return Somehow translated block time
1863 * @see LanguageFi.php for example implementation
1865 function translateBlockExpiry( $str ) {
1867 $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
1869 if ( $scBlockExpiryOptions == '-') {
1873 foreach (explode(',', $scBlockExpiryOptions) as $option) {
1874 if ( strpos($option, ":") === false )
1876 list($show, $value) = explode(":", $option);
1877 if ( strcmp ( $str, $value) == 0 ) {
1878 return htmlspecialchars( trim( $show ) );
1886 * languages like Chinese need to be segmented in order for the diff
1889 * @param string $text
1892 function segmentForDiff( $text ) {
1897 * and unsegment to show the result
1899 * @param string $text
1902 function unsegmentForDiff( $text ) {
1906 # convert text to different variants of a language.
1907 function convert( $text, $isTitle = false) {
1908 return $this->mConverter
->convert($text, $isTitle);
1911 # Convert text from within Parser
1912 function parserConvert( $text, &$parser ) {
1913 return $this->mConverter
->parserConvert( $text, $parser );
1916 # Check if this is a language with variants
1917 function hasVariants(){
1918 return sizeof($this->getVariants())>1;
1921 # Put custom tags (e.g. -{ }-) around math to prevent conversion
1922 function armourMath($text){
1923 return $this->mConverter
->armourMath($text);
1928 * Perform output conversion on a string, and encode for safe HTML output.
1929 * @param string $text
1930 * @param bool $isTitle -- wtf?
1932 * @todo this should get integrated somewhere sane
1934 function convertHtml( $text, $isTitle = false ) {
1935 return htmlspecialchars( $this->convert( $text, $isTitle ) );
1938 function convertCategoryKey( $key ) {
1939 return $this->mConverter
->convertCategoryKey( $key );
1943 * get the list of variants supported by this langauge
1944 * see sample implementation in LanguageZh.php
1946 * @return array an array of language codes
1948 function getVariants() {
1949 return $this->mConverter
->getVariants();
1953 function getPreferredVariant( $fromUser = true ) {
1954 return $this->mConverter
->getPreferredVariant( $fromUser );
1958 * if a language supports multiple variants, it is
1959 * possible that non-existing link in one variant
1960 * actually exists in another variant. this function
1961 * tries to find it. See e.g. LanguageZh.php
1963 * @param string $link the name of the link
1964 * @param mixed $nt the title object of the link
1965 * @return null the input parameters may be modified upon return
1967 function findVariantLink( &$link, &$nt ) {
1968 $this->mConverter
->findVariantLink($link, $nt);
1972 * If a language supports multiple variants, converts text
1973 * into an array of all possible variants of the text:
1974 * 'variant' => text in that variant
1977 function convertLinkToAllVariants($text){
1978 return $this->mConverter
->convertLinkToAllVariants($text);
1983 * returns language specific options used by User::getPageRenderHash()
1984 * for example, the preferred language variant
1989 function getExtraHashOptions() {
1990 return $this->mConverter
->getExtraHashOptions();
1994 * for languages that support multiple variants, the title of an
1995 * article may be displayed differently in different variants. this
1996 * function returns the apporiate title defined in the body of the article.
2000 function getParsedTitle() {
2001 return $this->mConverter
->getParsedTitle();
2005 * Enclose a string with the "no conversion" tag. This is used by
2006 * various functions in the Parser
2008 * @param string $text text to be tagged for no conversion
2009 * @return string the tagged text
2011 function markNoConversion( $text, $noParse=false ) {
2012 return $this->mConverter
->markNoConversion( $text, $noParse );
2016 * A regular expression to match legal word-trailing characters
2017 * which should be merged onto a link of the form [[foo]]bar.
2022 function linkTrail() {
2024 return $this->linkTrail
;
2027 function getLangObj() {
2032 * Get the RFC 3066 code for this language object
2034 function getCode() {
2035 return $this->mCode
;
2038 function setCode( $code ) {
2039 $this->mCode
= $code;
2042 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
2043 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
2046 static function getMessagesFileName( $code ) {
2048 return self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
2051 static function getClassFileName( $code ) {
2053 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
2056 static function getLocalisationArray( $code, $disableCache = false ) {
2057 self
::loadLocalisation( $code, $disableCache );
2058 return self
::$mLocalisationCache[$code];
2062 * Load localisation data for a given code into the static cache
2064 * @return array Dependencies, map of filenames to mtimes
2066 static function loadLocalisation( $code, $disableCache = false ) {
2067 static $recursionGuard = array();
2068 global $wgMemc, $wgCheckSerialized;
2071 throw new MWException( "Invalid language code requested" );
2074 if ( !$disableCache ) {
2075 # Try the per-process cache
2076 if ( isset( self
::$mLocalisationCache[$code] ) ) {
2077 return self
::$mLocalisationCache[$code]['deps'];
2080 wfProfileIn( __METHOD__
);
2082 # Try the serialized directory
2083 $cache = wfGetPrecompiledData( self
::getFileName( "Messages", $code, '.ser' ) );
2085 if ( $wgCheckSerialized && self
::isLocalisationOutOfDate( $cache ) ) {
2087 wfDebug( "Language::loadLocalisation(): precompiled data file for $code is out of date\n" );
2089 self
::$mLocalisationCache[$code] = $cache;
2090 wfDebug( "Language::loadLocalisation(): got localisation for $code from precompiled data file\n" );
2091 wfProfileOut( __METHOD__
);
2092 return self
::$mLocalisationCache[$code]['deps'];
2096 # Try the global cache
2097 $memcKey = wfMemcKey('localisation', $code );
2098 $fbMemcKey = wfMemcKey('fallback', $cache['fallback'] );
2099 $cache = $wgMemc->get( $memcKey );
2101 if ( self
::isLocalisationOutOfDate( $cache ) ) {
2102 $wgMemc->delete( $memcKey );
2103 $wgMemc->delete( $fbMemcKey );
2105 wfDebug( "Language::loadLocalisation(): localisation cache for $code had expired\n" );
2107 self
::$mLocalisationCache[$code] = $cache;
2108 wfDebug( "Language::loadLocalisation(): got localisation for $code from cache\n" );
2109 wfProfileOut( __METHOD__
);
2110 return $cache['deps'];
2114 wfProfileIn( __METHOD__
);
2117 # Default fallback, may be overridden when the messages file is included
2118 if ( $code != 'en' ) {
2124 # Load the primary localisation from the source file
2125 $filename = self
::getMessagesFileName( $code );
2126 if ( !file_exists( $filename ) ) {
2127 wfDebug( "Language::loadLocalisation(): no localisation file for $code, using implicit fallback to en\n" );
2131 $deps = array( $filename => filemtime( $filename ) );
2132 require( $filename );
2133 $cache = compact( self
::$mLocalisationKeys );
2134 wfDebug( "Language::loadLocalisation(): got localisation for $code from source\n" );
2137 if ( !empty( $fallback ) ) {
2138 # Load the fallback localisation, with a circular reference guard
2139 if ( isset( $recursionGuard[$code] ) ) {
2140 throw new MWException( "Error: Circular fallback reference in language code $code" );
2142 $recursionGuard[$code] = true;
2143 $newDeps = self
::loadLocalisation( $fallback, $disableCache );
2144 unset( $recursionGuard[$code] );
2146 $secondary = self
::$mLocalisationCache[$fallback];
2147 $deps = array_merge( $deps, $newDeps );
2149 # Merge the fallback localisation with the current localisation
2150 foreach ( self
::$mLocalisationKeys as $key ) {
2151 if ( isset( $cache[$key] ) ) {
2152 if ( isset( $secondary[$key] ) ) {
2153 if ( in_array( $key, self
::$mMergeableMapKeys ) ) {
2154 $cache[$key] = $cache[$key] +
$secondary[$key];
2155 } elseif ( in_array( $key, self
::$mMergeableListKeys ) ) {
2156 $cache[$key] = array_merge( $secondary[$key], $cache[$key] );
2157 } elseif ( in_array( $key, self
::$mMergeableAliasListKeys ) ) {
2158 $cache[$key] = array_merge_recursive( $cache[$key], $secondary[$key] );
2162 $cache[$key] = $secondary[$key];
2166 # Merge bookstore lists if requested
2167 if ( !empty( $cache['bookstoreList']['inherit'] ) ) {
2168 $cache['bookstoreList'] = array_merge( $cache['bookstoreList'], $secondary['bookstoreList'] );
2170 if ( isset( $cache['bookstoreList']['inherit'] ) ) {
2171 unset( $cache['bookstoreList']['inherit'] );
2175 # Add dependencies to the cache entry
2176 $cache['deps'] = $deps;
2178 # Replace spaces with underscores in namespace names
2179 $cache['namespaceNames'] = str_replace( ' ', '_', $cache['namespaceNames'] );
2181 # And do the same for specialpage aliases. $page is an array.
2182 foreach ( $cache['specialPageAliases'] as &$page ) {
2183 $page = str_replace( ' ', '_', $page );
2185 # Decouple the reference to prevent accidental damage
2188 # Save to both caches
2189 self
::$mLocalisationCache[$code] = $cache;
2190 if ( !$disableCache ) {
2191 $wgMemc->set( $memcKey, $cache );
2192 $wgMemc->set( $fbMemcKey, (string) $cache['fallback'] );
2195 wfProfileOut( __METHOD__
);
2200 * Test if a given localisation cache is out of date with respect to the
2201 * source Messages files. This is done automatically for the global cache
2202 * in $wgMemc, but is only done on certain occasions for the serialized
2205 * @param $cache mixed Either a language code or a cache array
2207 static function isLocalisationOutOfDate( $cache ) {
2208 if ( !is_array( $cache ) ) {
2209 self
::loadLocalisation( $cache );
2210 $cache = self
::$mLocalisationCache[$cache];
2213 foreach ( $cache['deps'] as $file => $mtime ) {
2214 if ( !file_exists( $file ) ||
filemtime( $file ) > $mtime ) {
2223 * Get the fallback for a given language
2225 static function getFallbackFor( $code ) {
2227 if ( $code === 'en' ) return false;
2230 static $cache = array();
2232 if ( isset($cache[$code]) ) return $cache[$code];
2236 $memcKey = wfMemcKey( 'fallback', $code );
2237 $fbcode = $wgMemc->get( $memcKey );
2239 if ( is_string($fbcode) ) {
2240 // False is stored as a string to detect failures in memcache properly
2241 if ( $fbcode === '' ) $fbcode = false;
2243 // Update local cache and return
2244 $cache[$code] = $fbcode;
2248 // Nothing in caches, load and and update both caches
2249 self
::loadLocalisation( $code );
2250 $fbcode = self
::$mLocalisationCache[$code]['fallback'];
2252 $cache[$code] = $fbcode;
2253 $wgMemc->set( $memcKey, (string) $fbcode );
2259 * Get all messages for a given language
2261 static function getMessagesFor( $code ) {
2262 self
::loadLocalisation( $code );
2263 return self
::$mLocalisationCache[$code]['messages'];
2267 * Get a message for a given language
2269 static function getMessageFor( $key, $code ) {
2270 self
::loadLocalisation( $code );
2271 return isset( self
::$mLocalisationCache[$code]['messages'][$key] ) ? self
::$mLocalisationCache[$code]['messages'][$key] : null;
2275 * Load localisation data for this object
2278 if ( !$this->mLoaded
) {
2279 self
::loadLocalisation( $this->getCode() );
2280 $cache =& self
::$mLocalisationCache[$this->getCode()];
2281 foreach ( self
::$mLocalisationKeys as $key ) {
2282 $this->$key = $cache[$key];
2284 $this->mLoaded
= true;
2286 $this->fixUpSettings();
2291 * Do any necessary post-cache-load settings adjustment
2293 function fixUpSettings() {
2294 global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk,
2295 $wgNamespaceAliases, $wgAmericanDates;
2296 wfProfileIn( __METHOD__
);
2297 if ( $wgExtraNamespaces ) {
2298 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames
;
2301 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
2302 if ( $wgMetaNamespaceTalk ) {
2303 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
2305 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
2306 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
2308 # Allow grammar transformations
2309 # Allowing full message-style parsing would make simple requests
2310 # such as action=raw much more expensive than they need to be.
2311 # This will hopefully cover most cases.
2312 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
2313 array( &$this, 'replaceGrammarInNamespace' ), $talk );
2314 $talk = str_replace( ' ', '_', $talk );
2315 $this->namespaceNames
[NS_PROJECT_TALK
] = $talk;
2318 # The above mixing may leave namespaces out of canonical order.
2319 # Re-order by namespace ID number...
2320 ksort( $this->namespaceNames
);
2322 # Put namespace names and aliases into a hashtable.
2323 # If this is too slow, then we should arrange it so that it is done
2324 # before caching. The catch is that at pre-cache time, the above
2325 # class-specific fixup hasn't been done.
2326 $this->mNamespaceIds
= array();
2327 foreach ( $this->namespaceNames
as $index => $name ) {
2328 $this->mNamespaceIds
[$this->lc($name)] = $index;
2330 if ( $this->namespaceAliases
) {
2331 foreach ( $this->namespaceAliases
as $name => $index ) {
2332 $this->mNamespaceIds
[$this->lc($name)] = $index;
2335 if ( $wgNamespaceAliases ) {
2336 foreach ( $wgNamespaceAliases as $name => $index ) {
2337 $this->mNamespaceIds
[$this->lc($name)] = $index;
2341 if ( $this->defaultDateFormat
== 'dmy or mdy' ) {
2342 $this->defaultDateFormat
= $wgAmericanDates ?
'mdy' : 'dmy';
2344 wfProfileOut( __METHOD__
);
2347 function replaceGrammarInNamespace( $m ) {
2348 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
2351 static function getCaseMaps() {
2352 static $wikiUpperChars, $wikiLowerChars;
2353 if ( isset( $wikiUpperChars ) ) {
2354 return array( $wikiUpperChars, $wikiLowerChars );
2357 wfProfileIn( __METHOD__
);
2358 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
2359 if ( $arr === false ) {
2360 throw new MWException(
2361 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
2364 wfProfileOut( __METHOD__
);
2365 return array( $wikiUpperChars, $wikiLowerChars );
2368 function formatTimePeriod( $seconds ) {
2369 if ( $seconds < 10 ) {
2370 return $this->formatNum( sprintf( "%.1f", $seconds ) ) . wfMsg( 'seconds-abbrev' );
2371 } elseif ( $seconds < 60 ) {
2372 return $this->formatNum( round( $seconds ) ) . wfMsg( 'seconds-abbrev' );
2373 } elseif ( $seconds < 3600 ) {
2374 return $this->formatNum( floor( $seconds / 60 ) ) . wfMsg( 'minutes-abbrev' ) .
2375 $this->formatNum( round( fmod( $seconds, 60 ) ) ) . wfMsg( 'seconds-abbrev' );
2377 $hours = floor( $seconds / 3600 );
2378 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
2379 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
2380 return $this->formatNum( $hours ) . wfMsg( 'hours-abbrev' ) .
2381 $this->formatNum( $minutes ) . wfMsg( 'minutes-abbrev' ) .
2382 $this->formatNum( $secondsPart ) . wfMsg( 'seconds-abbrev' );
2386 function formatBitrate( $bps ) {
2387 $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
2389 return $this->formatNum( $bps ) . $units[0];
2391 $unitIndex = floor( log10( $bps ) / 3 );
2392 $mantissa = $bps / pow( 1000, $unitIndex );
2393 if ( $mantissa < 10 ) {
2394 $mantissa = round( $mantissa, 1 );
2396 $mantissa = round( $mantissa );
2398 return $this->formatNum( $mantissa ) . $units[$unitIndex];
2402 * Format a size in bytes for output, using an appropriate
2403 * unit (B, KB, MB or GB) according to the magnitude in question
2405 * @param $size Size to format
2406 * @return string Plain text (not HTML)
2408 function formatSize( $size ) {
2409 // For small sizes no decimal places necessary
2411 if( $size > 1024 ) {
2412 $size = $size / 1024;
2413 if( $size > 1024 ) {
2414 $size = $size / 1024;
2415 // For MB and bigger two decimal places are smarter
2417 if( $size > 1024 ) {
2418 $size = $size / 1024;
2419 $msg = 'size-gigabytes';
2421 $msg = 'size-megabytes';
2424 $msg = 'size-kilobytes';
2427 $msg = 'size-bytes';
2429 $size = round( $size, $round );
2430 $text = $this->getMessageFromDB( $msg );
2431 return str_replace( '$1', $this->formatNum( $size ), $text );