3 * Internationalisation code
10 * @defgroup Language Language
13 if ( !defined( 'MEDIAWIKI' ) ) {
14 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
19 global $wgLanguageNames;
20 require_once( dirname( __FILE__
) . '/Names.php' );
22 if ( function_exists( 'mb_strtoupper' ) ) {
23 mb_internal_encoding( 'UTF-8' );
27 * a fake language converter
33 function __construct( $langobj ) { $this->mLang
= $langobj; }
34 function autoConvertToAllVariants( $text ) { return array( $this->mLang
->getCode() => $text ); }
35 function convert( $t ) { return $t; }
36 function convertTitle( $t ) { return $t->getPrefixedText(); }
37 function getVariants() { return array( $this->mLang
->getCode() ); }
38 function getPreferredVariant() { return $this->mLang
->getCode(); }
39 function getDefaultVariant() { return $this->mLang
->getCode(); }
40 function getURLVariant() { return ''; }
41 function getConvRuleTitle() { return false; }
42 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
43 function getExtraHashOptions() { return ''; }
44 function getParsedTitle() { return ''; }
45 function markNoConversion( $text, $noParse = false ) { return $text; }
46 function convertCategoryKey( $key ) { return $key; }
47 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
48 function armourMath( $text ) { return $text; }
52 * Internationalisation code
56 var $mConverter, $mVariants, $mCode, $mLoaded = false;
57 var $mMagicExtensions = array(), $mMagicHookDone = false;
59 var $mNamespaceIds, $namespaceNames, $namespaceAliases;
60 var $dateFormatStrings = array();
61 var $mExtendedSpecialPageAliases;
64 * ReplacementArray object caches
66 var $transformData = array();
69 * @var LocalisationCache
71 static public $dataCache;
73 static public $mLangObjCache = array();
75 static public $mWeekdayMsgs = array(
76 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
80 static public $mWeekdayAbbrevMsgs = array(
81 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
84 static public $mMonthMsgs = array(
85 'january', 'february', 'march', 'april', 'may_long', 'june',
86 'july', 'august', 'september', 'october', 'november',
89 static public $mMonthGenMsgs = array(
90 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
91 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
94 static public $mMonthAbbrevMsgs = array(
95 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
96 'sep', 'oct', 'nov', 'dec'
99 static public $mIranianCalendarMonthMsgs = array(
100 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
101 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
102 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
103 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
106 static public $mHebrewCalendarMonthMsgs = array(
107 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
108 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
109 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
110 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
111 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
114 static public $mHebrewCalendarMonthGenMsgs = array(
115 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
116 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
117 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
118 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
119 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
122 static public $mHijriCalendarMonthMsgs = array(
123 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
124 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
125 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
126 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
130 * Get a cached language object for a given language code
131 * @param $code String
134 static function factory( $code ) {
135 if ( !isset( self
::$mLangObjCache[$code] ) ) {
136 if ( count( self
::$mLangObjCache ) > 10 ) {
137 // Don't keep a billion objects around, that's stupid.
138 self
::$mLangObjCache = array();
140 self
::$mLangObjCache[$code] = self
::newFromCode( $code );
142 return self
::$mLangObjCache[$code];
146 * Create a language object for a given language code
147 * @param $code String
150 protected static function newFromCode( $code ) {
151 // Protect against path traversal below
152 if ( !Language
::isValidCode( $code )
153 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
155 throw new MWException( "Invalid language code \"$code\"" );
158 if ( !Language
::isValidBuiltInCode( $code ) ) {
159 // It's not possible to customise this code with class files, so
160 // just return a Language object. This is to support uselang= hacks.
161 $lang = new Language
;
162 $lang->setCode( $code );
166 // Check if there is a language class for the code
167 $class = self
::classFromCode( $code );
168 self
::preloadLanguageClass( $class );
169 if ( MWInit
::classExists( $class ) ) {
174 // Keep trying the fallback list until we find an existing class
175 $fallbacks = Language
::getFallbacksFor( $code );
176 foreach ( $fallbacks as $fallbackCode ) {
177 if ( !Language
::isValidBuiltInCode( $fallbackCode ) ) {
178 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
181 $class = self
::classFromCode( $fallbackCode );
182 self
::preloadLanguageClass( $class );
183 if ( MWInit
::classExists( $class ) ) {
184 $lang = Language
::newFromCode( $fallbackCode );
185 $lang->setCode( $code );
190 throw new MWException( "Invalid fallback sequence for language '$code'" );
194 * Returns true if a language code string is of a valid form, whether or
195 * not it exists. This includes codes which are used solely for
196 * customisation via the MediaWiki namespace.
198 * @param $code string
202 public static function isValidCode( $code ) {
204 strcspn( $code, ":/\\\000" ) === strlen( $code )
205 && !preg_match( Title
::getTitleInvalidRegex(), $code );
209 * Returns true if a language code is of a valid form for the purposes of
210 * internal customisation of MediaWiki, via Messages*.php.
212 * @param $code string
217 public static function isValidBuiltInCode( $code ) {
218 return preg_match( '/^[a-z0-9-]+$/i', $code );
221 public static function classFromCode( $code ) {
222 if ( $code == 'en' ) {
225 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
229 public static function preloadLanguageClass( $class ) {
232 if ( $class === 'Language' ) {
236 if ( !defined( 'MW_COMPILED' ) ) {
237 // Preload base classes to work around APC/PHP5 bug
238 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
239 include_once( "$IP/languages/classes/$class.deps.php" );
241 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
242 include_once( "$IP/languages/classes/$class.php" );
248 * Get the LocalisationCache instance
250 * @return LocalisationCache
252 public static function getLocalisationCache() {
253 if ( is_null( self
::$dataCache ) ) {
254 global $wgLocalisationCacheConf;
255 $class = $wgLocalisationCacheConf['class'];
256 self
::$dataCache = new $class( $wgLocalisationCacheConf );
258 return self
::$dataCache;
261 function __construct() {
262 $this->mConverter
= new FakeConverter( $this );
263 // Set the code to the name of the descendant
264 if ( get_class( $this ) == 'Language' ) {
267 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
269 self
::getLocalisationCache();
273 * Reduce memory usage
275 function __destruct() {
276 foreach ( $this as $name => $value ) {
277 unset( $this->$name );
282 * Hook which will be called if this is the content language.
283 * Descendants can use this to register hook functions or modify globals
285 function initContLang() { }
288 * Same as getFallbacksFor for current language.
290 * @deprecated in 1.19
292 function getFallbackLanguageCode() {
293 wfDeprecated( __METHOD__
);
294 return self
::getFallbackFor( $this->mCode
);
301 function getFallbackLanguages() {
302 return self
::getFallbacksFor( $this->mCode
);
306 * Exports $wgBookstoreListEn
309 function getBookstoreList() {
310 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
316 function getNamespaces() {
317 if ( is_null( $this->namespaceNames
) ) {
318 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
320 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
321 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
323 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
325 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
326 if ( $wgMetaNamespaceTalk ) {
327 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
329 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
330 $this->namespaceNames
[NS_PROJECT_TALK
] =
331 $this->fixVariableInNamespace( $talk );
334 # Sometimes a language will be localised but not actually exist on this wiki.
335 foreach( $this->namespaceNames
as $key => $text ) {
336 if ( !isset( $validNamespaces[$key] ) ) {
337 unset( $this->namespaceNames
[$key] );
341 # The above mixing may leave namespaces out of canonical order.
342 # Re-order by namespace ID number...
343 ksort( $this->namespaceNames
);
345 return $this->namespaceNames
;
349 * A convenience function that returns the same thing as
350 * getNamespaces() except with the array values changed to ' '
351 * where it found '_', useful for producing output to be displayed
352 * e.g. in <select> forms.
356 function getFormattedNamespaces() {
357 $ns = $this->getNamespaces();
358 foreach ( $ns as $k => $v ) {
359 $ns[$k] = strtr( $v, '_', ' ' );
365 * Get a namespace value by key
367 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
368 * echo $mw_ns; // prints 'MediaWiki'
371 * @param $index Int: the array key of the namespace to return
372 * @return mixed, string if the namespace value exists, otherwise false
374 function getNsText( $index ) {
375 $ns = $this->getNamespaces();
376 return isset( $ns[$index] ) ?
$ns[$index] : false;
380 * A convenience function that returns the same thing as
381 * getNsText() except with '_' changed to ' ', useful for
384 * @param $index string
388 function getFormattedNsText( $index ) {
389 $ns = $this->getNsText( $index );
390 return strtr( $ns, '_', ' ' );
394 * Returns gender-dependent namespace alias if available.
395 * @param $index Int: namespace index
396 * @param $gender String: gender key (male, female... )
400 function getGenderNsText( $index, $gender ) {
401 $ns = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
402 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
406 * Whether this language makes distinguishes genders for example in
411 function needsGenderDistinction() {
412 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
413 return count( $aliases ) > 0;
417 * Get a namespace key by value, case insensitive.
418 * Only matches namespace names for the current language, not the
419 * canonical ones defined in Namespace.php.
421 * @param $text String
422 * @return mixed An integer if $text is a valid value otherwise false
424 function getLocalNsIndex( $text ) {
425 $lctext = $this->lc( $text );
426 $ids = $this->getNamespaceIds();
427 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
433 function getNamespaceAliases() {
434 if ( is_null( $this->namespaceAliases
) ) {
435 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
439 foreach ( $aliases as $name => $index ) {
440 if ( $index === NS_PROJECT_TALK
) {
441 unset( $aliases[$name] );
442 $name = $this->fixVariableInNamespace( $name );
443 $aliases[$name] = $index;
448 $genders = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
449 foreach ( $genders as $index => $forms ) {
450 foreach ( $forms as $alias ) {
451 $aliases[$alias] = $index;
455 $this->namespaceAliases
= $aliases;
457 return $this->namespaceAliases
;
463 function getNamespaceIds() {
464 if ( is_null( $this->mNamespaceIds
) ) {
465 global $wgNamespaceAliases;
466 # Put namespace names and aliases into a hashtable.
467 # If this is too slow, then we should arrange it so that it is done
468 # before caching. The catch is that at pre-cache time, the above
469 # class-specific fixup hasn't been done.
470 $this->mNamespaceIds
= array();
471 foreach ( $this->getNamespaces() as $index => $name ) {
472 $this->mNamespaceIds
[$this->lc( $name )] = $index;
474 foreach ( $this->getNamespaceAliases() as $name => $index ) {
475 $this->mNamespaceIds
[$this->lc( $name )] = $index;
477 if ( $wgNamespaceAliases ) {
478 foreach ( $wgNamespaceAliases as $name => $index ) {
479 $this->mNamespaceIds
[$this->lc( $name )] = $index;
483 return $this->mNamespaceIds
;
488 * Get a namespace key by value, case insensitive. Canonical namespace
489 * names override custom ones defined for the current language.
491 * @param $text String
492 * @return mixed An integer if $text is a valid value otherwise false
494 function getNsIndex( $text ) {
495 $lctext = $this->lc( $text );
496 if ( ( $ns = MWNamespace
::getCanonicalIndex( $lctext ) ) !== null ) {
499 $ids = $this->getNamespaceIds();
500 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
504 * short names for language variants used for language conversion links.
506 * @param $code String
509 function getVariantname( $code ) {
510 return $this->getMessageFromDB( "variantname-$code" );
514 * @param $name string
517 function specialPage( $name ) {
518 $aliases = $this->getSpecialPageAliases();
519 if ( isset( $aliases[$name][0] ) ) {
520 $name = $aliases[$name][0];
522 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
528 function getQuickbarSettings() {
530 $this->getMessage( 'qbsettings-none' ),
531 $this->getMessage( 'qbsettings-fixedleft' ),
532 $this->getMessage( 'qbsettings-fixedright' ),
533 $this->getMessage( 'qbsettings-floatingleft' ),
534 $this->getMessage( 'qbsettings-floatingright' ),
535 $this->getMessage( 'qbsettings-directionality' )
542 function getDatePreferences() {
543 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
549 function getDateFormats() {
550 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
554 * @return array|string
556 function getDefaultDateFormat() {
557 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
558 if ( $df === 'dmy or mdy' ) {
559 global $wgAmericanDates;
560 return $wgAmericanDates ?
'mdy' : 'dmy';
569 function getDatePreferenceMigrationMap() {
570 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
577 function getImageFile( $image ) {
578 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
584 function getExtraUserToggles() {
585 return self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
592 function getUserToggle( $tog ) {
593 return $this->getMessageFromDB( "tog-$tog" );
597 * Get language names, indexed by code.
598 * If $customisedOnly is true, only returns codes with a messages file
600 * @param $customisedOnly bool
604 public static function getLanguageNames( $customisedOnly = false ) {
605 global $wgExtraLanguageNames;
606 static $coreLanguageNames;
608 if ( $coreLanguageNames === null ) {
609 include( MWInit
::compiledPath( 'languages/Names.php' ) );
612 $allNames = $wgExtraLanguageNames +
$coreLanguageNames;
613 if ( !$customisedOnly ) {
619 $dir = opendir( "$IP/languages/messages" );
620 while ( false !== ( $file = readdir( $dir ) ) ) {
621 $code = self
::getCodeFromFileName( $file, 'Messages' );
622 if ( $code && isset( $allNames[$code] ) ) {
623 $names[$code] = $allNames[$code];
631 * Get translated language names. This is done on best effort and
632 * by default this is exactly the same as Language::getLanguageNames.
633 * The CLDR extension provides translated names.
634 * @param $code String Language code.
635 * @return Array language code => language name
638 public static function getTranslatedLanguageNames( $code ) {
640 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
642 foreach ( self
::getLanguageNames() as $code => $name ) {
643 if ( !isset( $names[$code] ) ) $names[$code] = $name;
650 * Get a message from the MediaWiki namespace.
652 * @param $msg String: message name
655 function getMessageFromDB( $msg ) {
656 return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
660 * @param $code string
663 function getLanguageName( $code ) {
664 $names = self
::getLanguageNames();
665 if ( !array_key_exists( $code, $names ) ) {
668 return $names[$code];
675 function getMonthName( $key ) {
676 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
682 function getMonthNamesArray() {
683 $monthNames = array( '' );
684 for ( $i=1; $i < 13; $i++
) {
685 $monthNames[] = $this->getMonthName( $i );
694 function getMonthNameGen( $key ) {
695 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
702 function getMonthAbbreviation( $key ) {
703 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
709 function getMonthAbbreviationsArray() {
710 $monthNames = array( '' );
711 for ( $i=1; $i < 13; $i++
) {
712 $monthNames[] = $this->getMonthAbbreviation( $i );
721 function getWeekdayName( $key ) {
722 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
729 function getWeekdayAbbreviation( $key ) {
730 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
737 function getIranianCalendarMonthName( $key ) {
738 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
745 function getHebrewCalendarMonthName( $key ) {
746 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
753 function getHebrewCalendarMonthNameGen( $key ) {
754 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
761 function getHijriCalendarMonthName( $key ) {
762 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
766 * Used by date() and time() to adjust the time output.
768 * @param $ts Int the time in date('YmdHis') format
769 * @param $tz Mixed: adjust the time by this amount (default false, mean we
770 * get user timecorrection setting)
773 function userAdjust( $ts, $tz = false ) {
774 global $wgUser, $wgLocalTZoffset;
776 if ( $tz === false ) {
777 $tz = $wgUser->getOption( 'timecorrection' );
780 $data = explode( '|', $tz, 3 );
782 if ( $data[0] == 'ZoneInfo' ) {
783 wfSuppressWarnings();
784 $userTZ = timezone_open( $data[2] );
786 if ( $userTZ !== false ) {
787 $date = date_create( $ts, timezone_open( 'UTC' ) );
788 date_timezone_set( $date, $userTZ );
789 $date = date_format( $date, 'YmdHis' );
792 # Unrecognized timezone, default to 'Offset' with the stored offset.
797 if ( $data[0] == 'System' ||
$tz == '' ) {
798 # Global offset in minutes.
799 if ( isset( $wgLocalTZoffset ) ) {
800 $minDiff = $wgLocalTZoffset;
802 } elseif ( $data[0] == 'Offset' ) {
803 $minDiff = intval( $data[1] );
805 $data = explode( ':', $tz );
806 if ( count( $data ) == 2 ) {
807 $data[0] = intval( $data[0] );
808 $data[1] = intval( $data[1] );
809 $minDiff = abs( $data[0] ) * 60 +
$data[1];
810 if ( $data[0] < 0 ) {
811 $minDiff = -$minDiff;
814 $minDiff = intval( $data[0] ) * 60;
818 # No difference ? Return time unchanged
819 if ( 0 == $minDiff ) {
823 wfSuppressWarnings(); // E_STRICT system time bitching
824 # Generate an adjusted date; take advantage of the fact that mktime
825 # will normalize out-of-range values so we don't have to split $minDiff
826 # into hours and minutes.
828 (int)substr( $ts, 8, 2 ) ), # Hours
829 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
830 (int)substr( $ts, 12, 2 ), # Seconds
831 (int)substr( $ts, 4, 2 ), # Month
832 (int)substr( $ts, 6, 2 ), # Day
833 (int)substr( $ts, 0, 4 ) ); # Year
835 $date = date( 'YmdHis', $t );
842 * This is a workalike of PHP's date() function, but with better
843 * internationalisation, a reduced set of format characters, and a better
846 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
847 * PHP manual for definitions. There are a number of extensions, which
850 * xn Do not translate digits of the next numeric format character
851 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
852 * xr Use roman numerals for the next numeric format character
853 * xh Use hebrew numerals for the next numeric format character
855 * xg Genitive month name
857 * xij j (day number) in Iranian calendar
858 * xiF F (month name) in Iranian calendar
859 * xin n (month number) in Iranian calendar
860 * xiY Y (full year) in Iranian calendar
862 * xjj j (day number) in Hebrew calendar
863 * xjF F (month name) in Hebrew calendar
864 * xjt t (days in month) in Hebrew calendar
865 * xjx xg (genitive month name) in Hebrew calendar
866 * xjn n (month number) in Hebrew calendar
867 * xjY Y (full year) in Hebrew calendar
869 * xmj j (day number) in Hijri calendar
870 * xmF F (month name) in Hijri calendar
871 * xmn n (month number) in Hijri calendar
872 * xmY Y (full year) in Hijri calendar
874 * xkY Y (full year) in Thai solar calendar. Months and days are
875 * identical to the Gregorian calendar
876 * xoY Y (full year) in Minguo calendar or Juche year.
877 * Months and days are identical to the
879 * xtY Y (full year) in Japanese nengo. Months and days are
880 * identical to the Gregorian calendar
882 * Characters enclosed in double quotes will be considered literal (with
883 * the quotes themselves removed). Unmatched quotes will be considered
884 * literal quotes. Example:
886 * "The month is" F => The month is January
889 * Backslash escaping is also supported.
891 * Input timestamp is assumed to be pre-normalized to the desired local
894 * @param $format String
895 * @param $ts String: 14-character timestamp
898 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
902 function sprintfDate( $format, $ts ) {
915 for ( $p = 0; $p < strlen( $format ); $p++
) {
918 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
919 $code .= $format[++
$p];
922 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
923 $code .= $format[++
$p];
934 $rawToggle = !$rawToggle;
943 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
946 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
947 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
950 $num = substr( $ts, 6, 2 );
953 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
954 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) +
1 );
957 $num = intval( substr( $ts, 6, 2 ) );
961 $iranian = self
::tsToIranian( $ts );
967 $hijri = self
::tsToHijri( $ts );
973 $hebrew = self
::tsToHebrew( $ts );
979 $unix = wfTimestamp( TS_UNIX
, $ts );
981 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) +
1 );
985 $unix = wfTimestamp( TS_UNIX
, $ts );
987 $w = gmdate( 'w', $unix );
992 $unix = wfTimestamp( TS_UNIX
, $ts );
994 $num = gmdate( 'w', $unix );
998 $unix = wfTimestamp( TS_UNIX
, $ts );
1000 $num = gmdate( 'z', $unix );
1004 $unix = wfTimestamp( TS_UNIX
, $ts );
1006 $num = gmdate( 'W', $unix );
1009 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1013 $iranian = self
::tsToIranian( $ts );
1015 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1019 $hijri = self
::tsToHijri( $ts );
1021 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1025 $hebrew = self
::tsToHebrew( $ts );
1027 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1030 $num = substr( $ts, 4, 2 );
1033 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1036 $num = intval( substr( $ts, 4, 2 ) );
1040 $iranian = self
::tsToIranian( $ts );
1046 $hijri = self
::tsToHijri ( $ts );
1052 $hebrew = self
::tsToHebrew( $ts );
1058 $unix = wfTimestamp( TS_UNIX
, $ts );
1060 $num = gmdate( 't', $unix );
1064 $hebrew = self
::tsToHebrew( $ts );
1070 $unix = wfTimestamp( TS_UNIX
, $ts );
1072 $num = gmdate( 'L', $unix );
1076 $unix = wfTimestamp( TS_UNIX
, $ts );
1078 $num = date( 'o', $unix );
1081 $num = substr( $ts, 0, 4 );
1085 $iranian = self
::tsToIranian( $ts );
1091 $hijri = self
::tsToHijri( $ts );
1097 $hebrew = self
::tsToHebrew( $ts );
1103 $thai = self
::tsToYear( $ts, 'thai' );
1109 $minguo = self
::tsToYear( $ts, 'minguo' );
1115 $tenno = self
::tsToYear( $ts, 'tenno' );
1120 $num = substr( $ts, 2, 2 );
1123 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
1126 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
1129 $h = substr( $ts, 8, 2 );
1130 $num = $h %
12 ?
$h %
12 : 12;
1133 $num = intval( substr( $ts, 8, 2 ) );
1136 $h = substr( $ts, 8, 2 );
1137 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
1140 $num = substr( $ts, 8, 2 );
1143 $num = substr( $ts, 10, 2 );
1146 $num = substr( $ts, 12, 2 );
1150 $unix = wfTimestamp( TS_UNIX
, $ts );
1152 $s .= gmdate( 'c', $unix );
1156 $unix = wfTimestamp( TS_UNIX
, $ts );
1158 $s .= gmdate( 'r', $unix );
1162 $unix = wfTimestamp( TS_UNIX
, $ts );
1167 # Backslash escaping
1168 if ( $p < strlen( $format ) - 1 ) {
1169 $s .= $format[++
$p];
1176 if ( $p < strlen( $format ) - 1 ) {
1177 $endQuote = strpos( $format, '"', $p +
1 );
1178 if ( $endQuote === false ) {
1179 # No terminating quote, assume literal "
1182 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1186 # Quote at end of string, assume literal "
1193 if ( $num !== false ) {
1194 if ( $rawToggle ||
$raw ) {
1197 } elseif ( $roman ) {
1198 $s .= self
::romanNumeral( $num );
1200 } elseif ( $hebrewNum ) {
1201 $s .= self
::hebrewNumeral( $num );
1204 $s .= $this->formatNum( $num, true );
1211 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1212 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1215 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1216 * Gregorian dates to Iranian dates. Originally written in C, it
1217 * is released under the terms of GNU Lesser General Public
1218 * License. Conversion to PHP was performed by Niklas Laxström.
1220 * Link: http://www.farsiweb.info/jalali/jalali.c
1226 private static function tsToIranian( $ts ) {
1227 $gy = substr( $ts, 0, 4 ) -1600;
1228 $gm = substr( $ts, 4, 2 ) -1;
1229 $gd = substr( $ts, 6, 2 ) -1;
1231 # Days passed from the beginning (including leap years)
1233 +
floor( ( $gy +
3 ) / 4 )
1234 - floor( ( $gy +
99 ) / 100 )
1235 +
floor( ( $gy +
399 ) / 400 );
1238 // Add days of the past months of this year
1239 for ( $i = 0; $i < $gm; $i++
) {
1240 $gDayNo +
= self
::$GREG_DAYS[$i];
1244 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1248 // Days passed in current month
1251 $jDayNo = $gDayNo - 79;
1253 $jNp = floor( $jDayNo / 12053 );
1256 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1259 if ( $jDayNo >= 366 ) {
1260 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1261 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1264 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1265 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1271 return array( $jy, $jm, $jd );
1275 * Converting Gregorian dates to Hijri dates.
1277 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1279 * @link http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1285 private static function tsToHijri( $ts ) {
1286 $year = substr( $ts, 0, 4 );
1287 $month = substr( $ts, 4, 2 );
1288 $day = substr( $ts, 6, 2 );
1296 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1297 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1300 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1301 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1302 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1305 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1306 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1309 $zl = $zjd -1948440 +
10632;
1310 $zn = (int)( ( $zl - 1 ) / 10631 );
1311 $zl = $zl - 10631 * $zn +
354;
1312 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1313 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1314 $zm = (int)( ( 24 * $zl ) / 709 );
1315 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1316 $zy = 30 * $zn +
$zj - 30;
1318 return array( $zy, $zm, $zd );
1322 * Converting Gregorian dates to Hebrew dates.
1324 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1325 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1326 * to translate the relevant functions into PHP and release them under
1329 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1330 * and Adar II is 14. In a non-leap year, Adar is 6.
1336 private static function tsToHebrew( $ts ) {
1338 $year = substr( $ts, 0, 4 );
1339 $month = substr( $ts, 4, 2 );
1340 $day = substr( $ts, 6, 2 );
1342 # Calculate Hebrew year
1343 $hebrewYear = $year +
3760;
1345 # Month number when September = 1, August = 12
1347 if ( $month > 12 ) {
1354 # Calculate day of year from 1 September
1356 for ( $i = 1; $i < $month; $i++
) {
1360 # Check if the year is leap
1361 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1364 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1371 # Calculate the start of the Hebrew year
1372 $start = self
::hebrewYearStart( $hebrewYear );
1374 # Calculate next year's start
1375 if ( $dayOfYear <= $start ) {
1376 # Day is before the start of the year - it is the previous year
1378 $nextStart = $start;
1382 # Add days since previous year's 1 September
1384 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1388 # Start of the new (previous) year
1389 $start = self
::hebrewYearStart( $hebrewYear );
1392 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1395 # Calculate Hebrew day of year
1396 $hebrewDayOfYear = $dayOfYear - $start;
1398 # Difference between year's days
1399 $diff = $nextStart - $start;
1400 # Add 12 (or 13 for leap years) days to ignore the difference between
1401 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1402 # difference is only about the year type
1403 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1409 # Check the year pattern, and is leap year
1410 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1411 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1412 # and non-leap years
1413 $yearPattern = $diff %
30;
1414 # Check if leap year
1415 $isLeap = $diff >= 30;
1417 # Calculate day in the month from number of day in the Hebrew year
1418 # Don't check Adar - if the day is not in Adar, we will stop before;
1419 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1420 $hebrewDay = $hebrewDayOfYear;
1423 while ( $hebrewMonth <= 12 ) {
1424 # Calculate days in this month
1425 if ( $isLeap && $hebrewMonth == 6 ) {
1426 # Adar in a leap year
1428 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1430 if ( $hebrewDay <= $days ) {
1434 # Subtract the days of Adar I
1435 $hebrewDay -= $days;
1438 if ( $hebrewDay <= $days ) {
1444 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1445 # Cheshvan in a complete year (otherwise as the rule below)
1447 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1448 # Kislev in an incomplete year (otherwise as the rule below)
1451 # Odd months have 30 days, even have 29
1452 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1454 if ( $hebrewDay <= $days ) {
1455 # In the current month
1458 # Subtract the days of the current month
1459 $hebrewDay -= $days;
1460 # Try in the next month
1465 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1469 * This calculates the Hebrew year start, as days since 1 September.
1470 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1471 * Used for Hebrew date.
1477 private static function hebrewYearStart( $year ) {
1478 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1479 $b = intval( ( $year - 1 ) %
4 );
1480 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1484 $Mar = intval( $m );
1490 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1491 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1493 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1495 } elseif ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1499 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1504 * Algorithm to convert Gregorian dates to Thai solar dates,
1505 * Minguo dates or Minguo dates.
1507 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1508 * http://en.wikipedia.org/wiki/Minguo_calendar
1509 * http://en.wikipedia.org/wiki/Japanese_era_name
1511 * @param $ts String: 14-character timestamp
1512 * @param $cName String: calender name
1513 * @return Array: converted year, month, day
1515 private static function tsToYear( $ts, $cName ) {
1516 $gy = substr( $ts, 0, 4 );
1517 $gm = substr( $ts, 4, 2 );
1518 $gd = substr( $ts, 6, 2 );
1520 if ( !strcmp( $cName, 'thai' ) ) {
1522 # Add 543 years to the Gregorian calendar
1523 # Months and days are identical
1524 $gy_offset = $gy +
543;
1525 } elseif ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1527 # Deduct 1911 years from the Gregorian calendar
1528 # Months and days are identical
1529 $gy_offset = $gy - 1911;
1530 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1531 # Nengō dates up to Meiji period
1532 # Deduct years from the Gregorian calendar
1533 # depending on the nengo periods
1534 # Months and days are identical
1535 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1537 $gy_gannen = $gy - 1868 +
1;
1538 $gy_offset = $gy_gannen;
1539 if ( $gy_gannen == 1 ) {
1542 $gy_offset = '明治' . $gy_offset;
1544 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1545 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1546 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1547 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1548 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1552 $gy_gannen = $gy - 1912 +
1;
1553 $gy_offset = $gy_gannen;
1554 if ( $gy_gannen == 1 ) {
1557 $gy_offset = '大正' . $gy_offset;
1559 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1560 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1561 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1565 $gy_gannen = $gy - 1926 +
1;
1566 $gy_offset = $gy_gannen;
1567 if ( $gy_gannen == 1 ) {
1570 $gy_offset = '昭和' . $gy_offset;
1573 $gy_gannen = $gy - 1989 +
1;
1574 $gy_offset = $gy_gannen;
1575 if ( $gy_gannen == 1 ) {
1578 $gy_offset = '平成' . $gy_offset;
1584 return array( $gy_offset, $gm, $gd );
1588 * Roman number formatting up to 3000
1594 static function romanNumeral( $num ) {
1595 static $table = array(
1596 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1597 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1598 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1599 array( '', 'M', 'MM', 'MMM' )
1602 $num = intval( $num );
1603 if ( $num > 3000 ||
$num <= 0 ) {
1608 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1609 if ( $num >= $pow10 ) {
1610 $s .= $table[$i][floor( $num / $pow10 )];
1612 $num = $num %
$pow10;
1618 * Hebrew Gematria number formatting up to 9999
1624 static function hebrewNumeral( $num ) {
1625 static $table = array(
1626 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1627 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1628 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1629 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1632 $num = intval( $num );
1633 if ( $num > 9999 ||
$num <= 0 ) {
1638 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1639 if ( $num >= $pow10 ) {
1640 if ( $num == 15 ||
$num == 16 ) {
1641 $s .= $table[0][9] . $table[0][$num - 9];
1644 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1645 if ( $pow10 == 1000 ) {
1650 $num = $num %
$pow10;
1652 if ( strlen( $s ) == 2 ) {
1655 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1656 $str .= substr( $s, strlen( $s ) - 2, 2 );
1658 $start = substr( $str, 0, strlen( $str ) - 2 );
1659 $end = substr( $str, strlen( $str ) - 2 );
1662 $str = $start . 'ך';
1665 $str = $start . 'ם';
1668 $str = $start . 'ן';
1671 $str = $start . 'ף';
1674 $str = $start . 'ץ';
1681 * This is meant to be used by time(), date(), and timeanddate() to get
1682 * the date preference they're supposed to use, it should be used in
1686 * function timeanddate([...], $format = true) {
1687 * $datePreference = $this->dateFormat($format);
1692 * @param $usePrefs Mixed: if true, the user's preference is used
1693 * if false, the site/language default is used
1694 * if int/string, assumed to be a format.
1697 function dateFormat( $usePrefs = true ) {
1700 if ( is_bool( $usePrefs ) ) {
1702 $datePreference = $wgUser->getDatePreference();
1704 $datePreference = (string)User
::getDefaultOption( 'date' );
1707 $datePreference = (string)$usePrefs;
1711 if ( $datePreference == '' ) {
1715 return $datePreference;
1719 * Get a format string for a given type and preference
1720 * @param $type string May be date, time or both
1721 * @param $pref string The format name as it appears in Messages*.php
1725 function getDateFormatString( $type, $pref ) {
1726 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
1727 if ( $pref == 'default' ) {
1728 $pref = $this->getDefaultDateFormat();
1729 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1731 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1732 if ( is_null( $df ) ) {
1733 $pref = $this->getDefaultDateFormat();
1734 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1737 $this->dateFormatStrings
[$type][$pref] = $df;
1739 return $this->dateFormatStrings
[$type][$pref];
1743 * @param $ts Mixed: the time format which needs to be turned into a
1744 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1745 * @param $adj Bool: whether to adjust the time output according to the
1746 * user configured offset ($timecorrection)
1747 * @param $format Mixed: true to use user's date format preference
1748 * @param $timecorrection String|bool the time offset as returned by
1749 * validateTimeZone() in Special:Preferences
1752 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1753 $ts = wfTimestamp( TS_MW
, $ts );
1755 $ts = $this->userAdjust( $ts, $timecorrection );
1757 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1758 return $this->sprintfDate( $df, $ts );
1762 * @param $ts Mixed: the time format which needs to be turned into a
1763 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1764 * @param $adj Bool: whether to adjust the time output according to the
1765 * user configured offset ($timecorrection)
1766 * @param $format Mixed: true to use user's date format preference
1767 * @param $timecorrection String|bool the time offset as returned by
1768 * validateTimeZone() in Special:Preferences
1771 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1772 $ts = wfTimestamp( TS_MW
, $ts );
1774 $ts = $this->userAdjust( $ts, $timecorrection );
1776 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1777 return $this->sprintfDate( $df, $ts );
1781 * @param $ts Mixed: the time format which needs to be turned into a
1782 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1783 * @param $adj Bool: whether to adjust the time output according to the
1784 * user configured offset ($timecorrection)
1785 * @param $format Mixed: what format to return, if it's false output the
1786 * default one (default true)
1787 * @param $timecorrection String|bool the time offset as returned by
1788 * validateTimeZone() in Special:Preferences
1791 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1792 $ts = wfTimestamp( TS_MW
, $ts );
1794 $ts = $this->userAdjust( $ts, $timecorrection );
1796 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1797 return $this->sprintfDate( $df, $ts );
1801 * @param $key string
1802 * @return array|null
1804 function getMessage( $key ) {
1805 return self
::$dataCache->getSubitem( $this->mCode
, 'messages', $key );
1811 function getAllMessages() {
1812 return self
::$dataCache->getItem( $this->mCode
, 'messages' );
1821 function iconv( $in, $out, $string ) {
1822 # This is a wrapper for iconv in all languages except esperanto,
1823 # which does some nasty x-conversions beforehand
1825 # Even with //IGNORE iconv can whine about illegal characters in
1826 # *input* string. We just ignore those too.
1827 # REF: http://bugs.php.net/bug.php?id=37166
1828 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
1829 wfSuppressWarnings();
1830 $text = iconv( $in, $out . '//IGNORE', $string );
1831 wfRestoreWarnings();
1835 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
1838 * @param $matches array
1839 * @return mixed|string
1841 function ucwordbreaksCallbackAscii( $matches ) {
1842 return $this->ucfirst( $matches[1] );
1846 * @param $matches array
1849 function ucwordbreaksCallbackMB( $matches ) {
1850 return mb_strtoupper( $matches[0] );
1854 * @param $matches array
1857 function ucCallback( $matches ) {
1858 list( $wikiUpperChars ) = self
::getCaseMaps();
1859 return strtr( $matches[1], $wikiUpperChars );
1863 * @param $matches array
1866 function lcCallback( $matches ) {
1867 list( , $wikiLowerChars ) = self
::getCaseMaps();
1868 return strtr( $matches[1], $wikiLowerChars );
1872 * @param $matches array
1875 function ucwordsCallbackMB( $matches ) {
1876 return mb_strtoupper( $matches[0] );
1880 * @param $matches array
1883 function ucwordsCallbackWiki( $matches ) {
1884 list( $wikiUpperChars ) = self
::getCaseMaps();
1885 return strtr( $matches[0], $wikiUpperChars );
1889 * Make a string's first character uppercase
1891 * @param $str string
1895 function ucfirst( $str ) {
1897 if ( $o < 96 ) { // if already uppercase...
1899 } elseif ( $o < 128 ) {
1900 return ucfirst( $str ); // use PHP's ucfirst()
1902 // fall back to more complex logic in case of multibyte strings
1903 return $this->uc( $str, true );
1908 * Convert a string to uppercase
1910 * @param $str string
1911 * @param $first bool
1915 function uc( $str, $first = false ) {
1916 if ( function_exists( 'mb_strtoupper' ) ) {
1918 if ( $this->isMultibyte( $str ) ) {
1919 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1921 return ucfirst( $str );
1924 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
1927 if ( $this->isMultibyte( $str ) ) {
1928 $x = $first ?
'^' : '';
1929 return preg_replace_callback(
1930 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1931 array( $this, 'ucCallback' ),
1935 return $first ?
ucfirst( $str ) : strtoupper( $str );
1941 * @param $str string
1942 * @return mixed|string
1944 function lcfirst( $str ) {
1947 return strval( $str );
1948 } elseif ( $o >= 128 ) {
1949 return $this->lc( $str, true );
1950 } elseif ( $o > 96 ) {
1953 $str[0] = strtolower( $str[0] );
1959 * @param $str string
1960 * @param $first bool
1961 * @return mixed|string
1963 function lc( $str, $first = false ) {
1964 if ( function_exists( 'mb_strtolower' ) ) {
1966 if ( $this->isMultibyte( $str ) ) {
1967 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1969 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
1972 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
1975 if ( $this->isMultibyte( $str ) ) {
1976 $x = $first ?
'^' : '';
1977 return preg_replace_callback(
1978 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1979 array( $this, 'lcCallback' ),
1983 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
1989 * @param $str string
1992 function isMultibyte( $str ) {
1993 return (bool)preg_match( '/[\x80-\xff]/', $str );
1997 * @param $str string
1998 * @return mixed|string
2000 function ucwords( $str ) {
2001 if ( $this->isMultibyte( $str ) ) {
2002 $str = $this->lc( $str );
2004 // regexp to find first letter in each word (i.e. after each space)
2005 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2007 // function to use to capitalize a single char
2008 if ( function_exists( 'mb_strtoupper' ) ) {
2009 return preg_replace_callback(
2011 array( $this, 'ucwordsCallbackMB' ),
2015 return preg_replace_callback(
2017 array( $this, 'ucwordsCallbackWiki' ),
2022 return ucwords( strtolower( $str ) );
2027 * capitalize words at word breaks
2029 * @param $str string
2032 function ucwordbreaks( $str ) {
2033 if ( $this->isMultibyte( $str ) ) {
2034 $str = $this->lc( $str );
2036 // since \b doesn't work for UTF-8, we explicitely define word break chars
2037 $breaks = "[ \-\(\)\}\{\.,\?!]";
2039 // find first letter after word break
2040 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2042 if ( function_exists( 'mb_strtoupper' ) ) {
2043 return preg_replace_callback(
2045 array( $this, 'ucwordbreaksCallbackMB' ),
2049 return preg_replace_callback(
2051 array( $this, 'ucwordsCallbackWiki' ),
2056 return preg_replace_callback(
2057 '/\b([\w\x80-\xff]+)\b/',
2058 array( $this, 'ucwordbreaksCallbackAscii' ),
2065 * Return a case-folded representation of $s
2067 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2068 * and $s2 are the same except for the case of their characters. It is not
2069 * necessary for the value returned to make sense when displayed.
2071 * Do *not* perform any other normalisation in this function. If a caller
2072 * uses this function when it should be using a more general normalisation
2073 * function, then fix the caller.
2079 function caseFold( $s ) {
2080 return $this->uc( $s );
2087 function checkTitleEncoding( $s ) {
2088 if ( is_array( $s ) ) {
2089 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2091 # Check for non-UTF-8 URLs
2092 $ishigh = preg_match( '/[\x80-\xff]/', $s );
2097 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2098 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
2103 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2109 function fallback8bitEncoding() {
2110 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
2114 * Most writing systems use whitespace to break up words.
2115 * Some languages such as Chinese don't conventionally do this,
2116 * which requires special handling when breaking up words for
2121 function hasWordBreaks() {
2126 * Some languages such as Chinese require word segmentation,
2127 * Specify such segmentation when overridden in derived class.
2129 * @param $string String
2132 function segmentByWord( $string ) {
2137 * Some languages have special punctuation need to be normalized.
2138 * Make such changes here.
2140 * @param $string String
2143 function normalizeForSearch( $string ) {
2144 return self
::convertDoubleWidth( $string );
2148 * convert double-width roman characters to single-width.
2149 * range: ff00-ff5f ~= 0020-007f
2151 * @param $string string
2155 protected static function convertDoubleWidth( $string ) {
2156 static $full = null;
2157 static $half = null;
2159 if ( $full === null ) {
2160 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2161 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2162 $full = str_split( $fullWidth, 3 );
2163 $half = str_split( $halfWidth );
2166 $string = str_replace( $full, $half, $string );
2171 * @param $string string
2172 * @param $pattern string
2175 protected static function insertSpace( $string, $pattern ) {
2176 $string = preg_replace( $pattern, " $1 ", $string );
2177 $string = preg_replace( '/ +/', ' ', $string );
2182 * @param $termsArray array
2185 function convertForSearchResult( $termsArray ) {
2186 # some languages, e.g. Chinese, need to do a conversion
2187 # in order for search results to be displayed correctly
2192 * Get the first character of a string.
2197 function firstChar( $s ) {
2200 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2201 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2206 if ( isset( $matches[1] ) ) {
2207 if ( strlen( $matches[1] ) != 3 ) {
2211 // Break down Hangul syllables to grab the first jamo
2212 $code = utf8ToCodepoint( $matches[1] );
2213 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
2215 } elseif ( $code < 0xb098 ) {
2216 return "\xe3\x84\xb1";
2217 } elseif ( $code < 0xb2e4 ) {
2218 return "\xe3\x84\xb4";
2219 } elseif ( $code < 0xb77c ) {
2220 return "\xe3\x84\xb7";
2221 } elseif ( $code < 0xb9c8 ) {
2222 return "\xe3\x84\xb9";
2223 } elseif ( $code < 0xbc14 ) {
2224 return "\xe3\x85\x81";
2225 } elseif ( $code < 0xc0ac ) {
2226 return "\xe3\x85\x82";
2227 } elseif ( $code < 0xc544 ) {
2228 return "\xe3\x85\x85";
2229 } elseif ( $code < 0xc790 ) {
2230 return "\xe3\x85\x87";
2231 } elseif ( $code < 0xcc28 ) {
2232 return "\xe3\x85\x88";
2233 } elseif ( $code < 0xce74 ) {
2234 return "\xe3\x85\x8a";
2235 } elseif ( $code < 0xd0c0 ) {
2236 return "\xe3\x85\x8b";
2237 } elseif ( $code < 0xd30c ) {
2238 return "\xe3\x85\x8c";
2239 } elseif ( $code < 0xd558 ) {
2240 return "\xe3\x85\x8d";
2242 return "\xe3\x85\x8e";
2249 function initEncoding() {
2250 # Some languages may have an alternate char encoding option
2251 # (Esperanto X-coding, Japanese furigana conversion, etc)
2252 # If this language is used as the primary content language,
2253 # an override to the defaults can be set here on startup.
2260 function recodeForEdit( $s ) {
2261 # For some languages we'll want to explicitly specify
2262 # which characters make it into the edit box raw
2263 # or are converted in some way or another.
2264 global $wgEditEncoding;
2265 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
2268 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2276 function recodeInput( $s ) {
2277 # Take the previous into account.
2278 global $wgEditEncoding;
2279 if ( $wgEditEncoding != '' ) {
2280 $enc = $wgEditEncoding;
2284 if ( $enc == 'UTF-8' ) {
2287 return $this->iconv( $enc, 'UTF-8', $s );
2292 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2293 * also cleans up certain backwards-compatible sequences, converting them
2294 * to the modern Unicode equivalent.
2296 * This is language-specific for performance reasons only.
2302 function normalize( $s ) {
2303 global $wgAllUnicodeFixes;
2304 $s = UtfNormal
::cleanUp( $s );
2305 if ( $wgAllUnicodeFixes ) {
2306 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2307 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2314 * Transform a string using serialized data stored in the given file (which
2315 * must be in the serialized subdirectory of $IP). The file contains pairs
2316 * mapping source characters to destination characters.
2318 * The data is cached in process memory. This will go faster if you have the
2319 * FastStringSearch extension.
2321 * @param $file string
2322 * @param $string string
2326 function transformUsingPairFile( $file, $string ) {
2327 if ( !isset( $this->transformData
[$file] ) ) {
2328 $data = wfGetPrecompiledData( $file );
2329 if ( $data === false ) {
2330 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2332 $this->transformData
[$file] = new ReplacementArray( $data );
2334 return $this->transformData
[$file]->replace( $string );
2338 * For right-to-left language support
2343 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2347 * Return the correct HTML 'dir' attribute value for this language.
2351 return $this->isRTL() ?
'rtl' : 'ltr';
2355 * Return 'left' or 'right' as appropriate alignment for line-start
2356 * for this language's text direction.
2358 * Should be equivalent to CSS3 'start' text-align value....
2362 function alignStart() {
2363 return $this->isRTL() ?
'right' : 'left';
2367 * Return 'right' or 'left' as appropriate alignment for line-end
2368 * for this language's text direction.
2370 * Should be equivalent to CSS3 'end' text-align value....
2374 function alignEnd() {
2375 return $this->isRTL() ?
'left' : 'right';
2379 * A hidden direction mark (LRM or RLM), depending on the language direction
2381 * @param $opposite Boolean Get the direction mark opposite to your language
2384 function getDirMark( $opposite = false ) {
2385 $rtl = "\xE2\x80\x8F";
2386 $ltr = "\xE2\x80\x8E";
2387 if( $opposite ) { return $this->isRTL() ?
$ltr : $rtl; }
2388 return $this->isRTL() ?
$rtl : $ltr;
2394 function capitalizeAllNouns() {
2395 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2399 * An arrow, depending on the language direction
2403 function getArrow() {
2404 return $this->isRTL() ?
'←' : '→';
2408 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2412 function linkPrefixExtension() {
2413 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2419 function getMagicWords() {
2420 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2423 protected function doMagicHook() {
2424 if ( $this->mMagicHookDone
) {
2427 $this->mMagicHookDone
= true;
2428 wfProfileIn( 'LanguageGetMagic' );
2429 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2430 wfProfileOut( 'LanguageGetMagic' );
2434 * Fill a MagicWord object with data from here
2438 function getMagic( $mw ) {
2439 $this->doMagicHook();
2441 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
2442 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
2444 $magicWords = $this->getMagicWords();
2445 if ( isset( $magicWords[$mw->mId
] ) ) {
2446 $rawEntry = $magicWords[$mw->mId
];
2452 if ( !is_array( $rawEntry ) ) {
2453 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
2455 $mw->mCaseSensitive
= $rawEntry[0];
2456 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
2461 * Add magic words to the extension array
2463 * @param $newWords array
2465 function addMagicWordsByLang( $newWords ) {
2466 $fallbackChain = $this->getFallbackLanguages();
2467 $fallbackChain = array_reverse( $fallbackChain );
2468 foreach ( $fallbackChain as $code ) {
2469 if ( isset( $newWords[$code] ) ) {
2470 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
2476 * Get special page names, as an associative array
2477 * case folded alias => real name
2479 function getSpecialPageAliases() {
2480 // Cache aliases because it may be slow to load them
2481 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
2483 $this->mExtendedSpecialPageAliases
=
2484 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
2485 wfRunHooks( 'LanguageGetSpecialPageAliases',
2486 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
2489 return $this->mExtendedSpecialPageAliases
;
2493 * Italic is unsuitable for some languages
2495 * @param $text String: the text to be emphasized.
2498 function emphasize( $text ) {
2499 return "<em>$text</em>";
2503 * Normally we output all numbers in plain en_US style, that is
2504 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2505 * point twohundredthirtyfive. However this is not sutable for all
2506 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2507 * Icelandic just want to use commas instead of dots, and dots instead
2508 * of commas like "293.291,235".
2510 * An example of this function being called:
2512 * wfMsg( 'message', $wgLang->formatNum( $num ) )
2515 * See LanguageGu.php for the Gujarati implementation and
2516 * $separatorTransformTable on MessageIs.php for
2517 * the , => . and . => , implementation.
2519 * @todo check if it's viable to use localeconv() for the decimal
2521 * @param $number Mixed: the string to be formatted, should be an integer
2522 * or a floating point number.
2523 * @param $nocommafy Bool: set to true for special numbers like dates
2526 function formatNum( $number, $nocommafy = false ) {
2527 global $wgTranslateNumerals;
2528 if ( !$nocommafy ) {
2529 $number = $this->commafy( $number );
2530 $s = $this->separatorTransformTable();
2532 $number = strtr( $number, $s );
2536 if ( $wgTranslateNumerals ) {
2537 $s = $this->digitTransformTable();
2539 $number = strtr( $number, $s );
2547 * @param $number string
2550 function parseFormattedNumber( $number ) {
2551 $s = $this->digitTransformTable();
2553 $number = strtr( $number, array_flip( $s ) );
2556 $s = $this->separatorTransformTable();
2558 $number = strtr( $number, array_flip( $s ) );
2561 $number = strtr( $number, array( ',' => '' ) );
2566 * Adds commas to a given number
2571 function commafy( $_ ) {
2572 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2578 function digitTransformTable() {
2579 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
2585 function separatorTransformTable() {
2586 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
2590 * Take a list of strings and build a locale-friendly comma-separated
2591 * list, using the local comma-separator message.
2592 * The last two strings are chained with an "and".
2597 function listToText( $l ) {
2599 $m = count( $l ) - 1;
2601 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
2603 for ( $i = $m; $i >= 0; $i-- ) {
2606 } elseif ( $i == $m - 1 ) {
2607 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
2609 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
2617 * Take a list of strings and build a locale-friendly comma-separated
2618 * list, using the local comma-separator message.
2619 * @param $list array of strings to put in a comma list
2622 function commaList( $list ) {
2627 array( 'parsemag', 'escapenoentities', 'language' => $this )
2633 * Take a list of strings and build a locale-friendly semicolon-separated
2634 * list, using the local semicolon-separator message.
2635 * @param $list array of strings to put in a semicolon list
2638 function semicolonList( $list ) {
2642 'semicolon-separator',
2643 array( 'parsemag', 'escapenoentities', 'language' => $this )
2649 * Same as commaList, but separate it with the pipe instead.
2650 * @param $list array of strings to put in a pipe list
2653 function pipeList( $list ) {
2658 array( 'escapenoentities', 'language' => $this )
2664 * Truncate a string to a specified length in bytes, appending an optional
2665 * string (e.g. for ellipses)
2667 * The database offers limited byte lengths for some columns in the database;
2668 * multi-byte character sets mean we need to ensure that only whole characters
2669 * are included, otherwise broken characters can be passed to the user
2671 * If $length is negative, the string will be truncated from the beginning
2673 * @param $string String to truncate
2674 * @param $length Int: maximum length (including ellipses)
2675 * @param $ellipsis String to append to the truncated text
2676 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
2677 * $adjustLength was introduced in 1.18, before that behaved as if false.
2680 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
2681 # Use the localized ellipsis character
2682 if ( $ellipsis == '...' ) {
2683 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
2685 # Check if there is no need to truncate
2686 if ( $length == 0 ) {
2687 return $ellipsis; // convention
2688 } elseif ( strlen( $string ) <= abs( $length ) ) {
2689 return $string; // no need to truncate
2691 $stringOriginal = $string;
2692 # If ellipsis length is >= $length then we can't apply $adjustLength
2693 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
2694 $string = $ellipsis; // this can be slightly unexpected
2695 # Otherwise, truncate and add ellipsis...
2697 $eLength = $adjustLength ?
strlen( $ellipsis ) : 0;
2698 if ( $length > 0 ) {
2699 $length -= $eLength;
2700 $string = substr( $string, 0, $length ); // xyz...
2701 $string = $this->removeBadCharLast( $string );
2702 $string = $string . $ellipsis;
2704 $length +
= $eLength;
2705 $string = substr( $string, $length ); // ...xyz
2706 $string = $this->removeBadCharFirst( $string );
2707 $string = $ellipsis . $string;
2710 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
2711 # This check is *not* redundant if $adjustLength, due to the single case where
2712 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
2713 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
2716 return $stringOriginal;
2721 * Remove bytes that represent an incomplete Unicode character
2722 * at the end of string (e.g. bytes of the char are missing)
2724 * @param $string String
2727 protected function removeBadCharLast( $string ) {
2728 if ( $string != '' ) {
2729 $char = ord( $string[strlen( $string ) - 1] );
2731 if ( $char >= 0xc0 ) {
2732 # We got the first byte only of a multibyte char; remove it.
2733 $string = substr( $string, 0, -1 );
2734 } elseif ( $char >= 0x80 &&
2735 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
2736 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
2738 # We chopped in the middle of a character; remove it
2746 * Remove bytes that represent an incomplete Unicode character
2747 * at the start of string (e.g. bytes of the char are missing)
2749 * @param $string String
2752 protected function removeBadCharFirst( $string ) {
2753 if ( $string != '' ) {
2754 $char = ord( $string[0] );
2755 if ( $char >= 0x80 && $char < 0xc0 ) {
2756 # We chopped in the middle of a character; remove the whole thing
2757 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
2764 * Truncate a string of valid HTML to a specified length in bytes,
2765 * appending an optional string (e.g. for ellipses), and return valid HTML
2767 * This is only intended for styled/linked text, such as HTML with
2768 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
2769 * Also, this will not detect things like "display:none" CSS.
2771 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
2773 * @param string $text HTML string to truncate
2774 * @param int $length (zero/positive) Maximum length (including ellipses)
2775 * @param string $ellipsis String to append to the truncated text
2778 function truncateHtml( $text, $length, $ellipsis = '...' ) {
2779 # Use the localized ellipsis character
2780 if ( $ellipsis == '...' ) {
2781 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
2783 # Check if there is clearly no need to truncate
2784 if ( $length <= 0 ) {
2785 return $ellipsis; // no text shown, nothing to format (convention)
2786 } elseif ( strlen( $text ) <= $length ) {
2787 return $text; // string short enough even *with* HTML (short-circuit)
2790 $dispLen = 0; // innerHTML legth so far
2791 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
2792 $tagType = 0; // 0-open, 1-close
2793 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
2794 $entityState = 0; // 0-not entity, 1-entity
2795 $tag = $ret = ''; // accumulated tag name, accumulated result string
2796 $openTags = array(); // open tag stack
2797 $maybeState = null; // possible truncation state
2799 $textLen = strlen( $text );
2800 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
2801 for ( $pos = 0; true; ++
$pos ) {
2802 # Consider truncation once the display length has reached the maximim.
2803 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
2804 # Check that we're not in the middle of a bracket/entity...
2805 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
2806 if ( !$testingEllipsis ) {
2807 $testingEllipsis = true;
2808 # Save where we are; we will truncate here unless there turn out to
2809 # be so few remaining characters that truncation is not necessary.
2810 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
2811 $maybeState = array( $ret, $openTags ); // save state
2813 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
2814 # String in fact does need truncation, the truncation point was OK.
2815 list( $ret, $openTags ) = $maybeState; // reload state
2816 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
2817 $ret .= $ellipsis; // add ellipsis
2821 if ( $pos >= $textLen ) break; // extra iteration just for above checks
2823 # Read the next char...
2825 $lastCh = $pos ?
$text[$pos - 1] : '';
2826 $ret .= $ch; // add to result string
2828 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
2829 $entityState = 0; // for bad HTML
2830 $bracketState = 1; // tag started (checking for backslash)
2831 } elseif ( $ch == '>' ) {
2832 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
2833 $entityState = 0; // for bad HTML
2834 $bracketState = 0; // out of brackets
2835 } elseif ( $bracketState == 1 ) {
2837 $tagType = 1; // close tag (e.g. "</span>")
2839 $tagType = 0; // open tag (e.g. "<span>")
2842 $bracketState = 2; // building tag name
2843 } elseif ( $bracketState == 2 ) {
2847 // Name found (e.g. "<a href=..."), add on tag attributes...
2848 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
2850 } elseif ( $bracketState == 0 ) {
2851 if ( $entityState ) {
2854 $dispLen++
; // entity is one displayed char
2857 if ( $neLength == 0 && !$maybeState ) {
2858 // Save state without $ch. We want to *hit* the first
2859 // display char (to get tags) but not *use* it if truncating.
2860 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
2863 $entityState = 1; // entity found, (e.g. " ")
2865 $dispLen++
; // this char is displayed
2866 // Add the next $max display text chars after this in one swoop...
2867 $max = ( $testingEllipsis ?
$length : $neLength ) - $dispLen;
2868 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos +
1, $max );
2869 $dispLen +
= $skipped;
2875 // Close the last tag if left unclosed by bad HTML
2876 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
2877 while ( count( $openTags ) > 0 ) {
2878 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
2884 * truncateHtml() helper function
2885 * like strcspn() but adds the skipped chars to $ret
2894 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
2895 if ( $len === null ) {
2896 $len = -1; // -1 means "no limit" for strcspn
2897 } elseif ( $len < 0 ) {
2901 if ( $start < strlen( $text ) ) {
2902 $skipCount = strcspn( $text, $search, $start, $len );
2903 $ret .= substr( $text, $start, $skipCount );
2909 * truncateHtml() helper function
2910 * (a) push or pop $tag from $openTags as needed
2911 * (b) clear $tag value
2912 * @param String &$tag Current HTML tag name we are looking at
2913 * @param int $tagType (0-open tag, 1-close tag)
2914 * @param char $lastCh Character before the '>' that ended this tag
2915 * @param array &$openTags Open tag stack (not accounting for $tag)
2917 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
2918 $tag = ltrim( $tag );
2920 if ( $tagType == 0 && $lastCh != '/' ) {
2921 $openTags[] = $tag; // tag opened (didn't close itself)
2922 } elseif ( $tagType == 1 ) {
2923 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
2924 array_pop( $openTags ); // tag closed
2932 * Grammatical transformations, needed for inflected languages
2933 * Invoked by putting {{grammar:case|word}} in a message
2935 * @param $word string
2936 * @param $case string
2939 function convertGrammar( $word, $case ) {
2940 global $wgGrammarForms;
2941 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
2942 return $wgGrammarForms[$this->getCode()][$case][$word];
2948 * Provides an alternative text depending on specified gender.
2949 * Usage {{gender:username|masculine|feminine|neutral}}.
2950 * username is optional, in which case the gender of current user is used,
2951 * but only in (some) interface messages; otherwise default gender is used.
2952 * If second or third parameter are not specified, masculine is used.
2953 * These details may be overriden per language.
2955 * @param $gender string
2956 * @param $forms array
2960 function gender( $gender, $forms ) {
2961 if ( !count( $forms ) ) {
2964 $forms = $this->preConvertPlural( $forms, 2 );
2965 if ( $gender === 'male' ) {
2968 if ( $gender === 'female' ) {
2971 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
2975 * Plural form transformations, needed for some languages.
2976 * For example, there are 3 form of plural in Russian and Polish,
2977 * depending on "count mod 10". See [[w:Plural]]
2978 * For English it is pretty simple.
2980 * Invoked by putting {{plural:count|wordform1|wordform2}}
2981 * or {{plural:count|wordform1|wordform2|wordform3}}
2983 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
2985 * @param $count Integer: non-localized number
2986 * @param $forms Array: different plural forms
2987 * @return string Correct form of plural for $count in this language
2989 function convertPlural( $count, $forms ) {
2990 if ( !count( $forms ) ) {
2993 $forms = $this->preConvertPlural( $forms, 2 );
2995 return ( $count == 1 ) ?
$forms[0] : $forms[1];
2999 * Checks that convertPlural was given an array and pads it to requested
3000 * amount of forms by copying the last one.
3002 * @param $count Integer: How many forms should there be at least
3003 * @param $forms Array of forms given to convertPlural
3004 * @return array Padded array of forms or an exception if not an array
3006 protected function preConvertPlural( /* Array */ $forms, $count ) {
3007 while ( count( $forms ) < $count ) {
3008 $forms[] = $forms[count( $forms ) - 1];
3014 * This translates the duration ("1 week", "4 days", etc)
3015 * as well as the expiry time (which is an absolute timestamp).
3016 * @param $str String: the validated block duration in English
3017 * @return Somehow translated block duration
3018 * @see LanguageFi.php for example implementation
3020 function translateBlockExpiry( $str ) {
3021 $duration = SpecialBlock
::getSuggestedDurations( $this );
3022 foreach( $duration as $show => $value ){
3023 if ( strcmp( $str, $value ) == 0 ) {
3024 return htmlspecialchars( trim( $show ) );
3028 // Since usually only infinite or indefinite is only on list, so try
3029 // equivalents if still here.
3030 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3031 if ( in_array( $str, $indefs ) ) {
3032 foreach( $indefs as $val ) {
3033 $show = array_search( $val, $duration, true );
3034 if ( $show !== false ) {
3035 return htmlspecialchars( trim( $show ) );
3039 // If no duration is given, but a timestamp, display that
3040 return ( strtotime( $str ) ?
$this->timeanddate( strtotime( $str ) ) : $str );
3044 * languages like Chinese need to be segmented in order for the diff
3047 * @param $text String
3050 function segmentForDiff( $text ) {
3055 * and unsegment to show the result
3057 * @param $text String
3060 function unsegmentForDiff( $text ) {
3065 * convert text to all supported variants
3067 * @param $text string
3070 function autoConvertToAllVariants( $text ) {
3071 return $this->mConverter
->autoConvertToAllVariants( $text );
3075 * convert text to different variants of a language.
3077 * @param $text string
3080 function convert( $text ) {
3081 return $this->mConverter
->convert( $text );
3086 * Convert a Title object to a string in the preferred variant
3088 * @param $title Title
3091 function convertTitle( $title ) {
3092 return $this->mConverter
->convertTitle( $title );
3096 * Check if this is a language with variants
3100 function hasVariants() {
3101 return sizeof( $this->getVariants() ) > 1;
3105 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3107 * @param $text string
3110 function armourMath( $text ) {
3111 return $this->mConverter
->armourMath( $text );
3115 * Perform output conversion on a string, and encode for safe HTML output.
3116 * @param $text String text to be converted
3117 * @param $isTitle Bool whether this conversion is for the article title
3119 * @todo this should get integrated somewhere sane
3121 function convertHtml( $text, $isTitle = false ) {
3122 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3126 * @param $key string
3129 function convertCategoryKey( $key ) {
3130 return $this->mConverter
->convertCategoryKey( $key );
3134 * Get the list of variants supported by this language
3135 * see sample implementation in LanguageZh.php
3137 * @return array an array of language codes
3139 function getVariants() {
3140 return $this->mConverter
->getVariants();
3146 function getPreferredVariant() {
3147 return $this->mConverter
->getPreferredVariant();
3153 function getDefaultVariant() {
3154 return $this->mConverter
->getDefaultVariant();
3160 function getURLVariant() {
3161 return $this->mConverter
->getURLVariant();
3165 * If a language supports multiple variants, it is
3166 * possible that non-existing link in one variant
3167 * actually exists in another variant. this function
3168 * tries to find it. See e.g. LanguageZh.php
3170 * @param $link String: the name of the link
3171 * @param $nt Mixed: the title object of the link
3172 * @param $ignoreOtherCond Boolean: to disable other conditions when
3173 * we need to transclude a template or update a category's link
3174 * @return null the input parameters may be modified upon return
3176 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3177 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
3181 * If a language supports multiple variants, converts text
3182 * into an array of all possible variants of the text:
3183 * 'variant' => text in that variant
3185 * @deprecated since 1.17 Use autoConvertToAllVariants()
3187 * @param $text string
3191 function convertLinkToAllVariants( $text ) {
3192 return $this->mConverter
->convertLinkToAllVariants( $text );
3196 * returns language specific options used by User::getPageRenderHash()
3197 * for example, the preferred language variant
3201 function getExtraHashOptions() {
3202 return $this->mConverter
->getExtraHashOptions();
3206 * For languages that support multiple variants, the title of an
3207 * article may be displayed differently in different variants. this
3208 * function returns the apporiate title defined in the body of the article.
3212 function getParsedTitle() {
3213 return $this->mConverter
->getParsedTitle();
3217 * Enclose a string with the "no conversion" tag. This is used by
3218 * various functions in the Parser
3220 * @param $text String: text to be tagged for no conversion
3221 * @param $noParse bool
3222 * @return string the tagged text
3224 function markNoConversion( $text, $noParse = false ) {
3225 return $this->mConverter
->markNoConversion( $text, $noParse );
3229 * A regular expression to match legal word-trailing characters
3230 * which should be merged onto a link of the form [[foo]]bar.
3234 function linkTrail() {
3235 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
3241 function getLangObj() {
3246 * Get the RFC 3066 code for this language object
3250 function getCode() {
3251 return $this->mCode
;
3255 * @param $code string
3257 function setCode( $code ) {
3258 $this->mCode
= $code;
3262 * Get the name of a file for a certain language code
3263 * @param $prefix string Prepend this to the filename
3264 * @param $code string Language code
3265 * @param $suffix string Append this to the filename
3266 * @return string $prefix . $mangledCode . $suffix
3268 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
3269 // Protect against path traversal
3270 if ( !Language
::isValidCode( $code )
3271 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
3273 throw new MWException( "Invalid language code \"$code\"" );
3276 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
3280 * Get the language code from a file name. Inverse of getFileName()
3281 * @param $filename string $prefix . $languageCode . $suffix
3282 * @param $prefix string Prefix before the language code
3283 * @param $suffix string Suffix after the language code
3284 * @return string Language code, or false if $prefix or $suffix isn't found
3286 static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
3288 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
3289 preg_quote( $suffix, '/' ) . '/', $filename, $m );
3290 if ( !count( $m ) ) {
3293 return str_replace( '_', '-', strtolower( $m[1] ) );
3297 * @param $code string
3300 static function getMessagesFileName( $code ) {
3302 return self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
3306 * @param $code string
3309 static function getClassFileName( $code ) {
3311 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
3315 * Get the first fallback for a given language
3317 * @param $code string
3319 * @return false|string
3321 static function getFallbackFor( $code ) {
3322 if ( $code === 'en' ) {
3325 $fallbacks = self
::getFallbacksFor( $code );
3326 $first = array_shift( $fallbacks );
3332 * Get the ordered list of fallback languages.
3335 * @param $code string Language code
3338 static function getFallbacksFor( $code ) {
3339 if ( $code === 'en' ) {
3342 $v = self
::getLocalisationCache()->getItem( $code, 'fallback' );
3343 $v = array_map( 'trim', explode( ',', $v ) );
3344 if ( $v[count( $v ) - 1] !== 'en' ) {
3352 * Get all messages for a given language
3353 * WARNING: this may take a long time
3355 * @param $code string
3359 static function getMessagesFor( $code ) {
3360 return self
::getLocalisationCache()->getItem( $code, 'messages' );
3364 * Get a message for a given language
3366 * @param $key string
3367 * @param $code string
3371 static function getMessageFor( $key, $code ) {
3372 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
3379 function fixVariableInNamespace( $talk ) {
3380 if ( strpos( $talk, '$1' ) === false ) {
3384 global $wgMetaNamespace;
3385 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
3387 # Allow grammar transformations
3388 # Allowing full message-style parsing would make simple requests
3389 # such as action=raw much more expensive than they need to be.
3390 # This will hopefully cover most cases.
3391 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
3392 array( &$this, 'replaceGrammarInNamespace' ), $talk );
3393 return str_replace( ' ', '_', $talk );
3400 function replaceGrammarInNamespace( $m ) {
3401 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
3405 * @throws MWException
3408 static function getCaseMaps() {
3409 static $wikiUpperChars, $wikiLowerChars;
3410 if ( isset( $wikiUpperChars ) ) {
3411 return array( $wikiUpperChars, $wikiLowerChars );
3414 wfProfileIn( __METHOD__
);
3415 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
3416 if ( $arr === false ) {
3417 throw new MWException(
3418 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
3420 $wikiUpperChars = $arr['wikiUpperChars'];
3421 $wikiLowerChars = $arr['wikiLowerChars'];
3422 wfProfileOut( __METHOD__
);
3423 return array( $wikiUpperChars, $wikiLowerChars );
3427 * Decode an expiry (block, protection, etc) which has come from the DB
3429 * @param $expiry String: Database expiry String
3430 * @param $format Bool|Int true to process using language functions, or TS_ constant
3431 * to return the expiry in a given timestamp
3434 public function formatExpiry( $expiry, $format = true ) {
3435 static $infinity, $infinityMsg;
3436 if( $infinity === null ){
3437 $infinityMsg = wfMessage( 'infiniteblock' );
3438 $infinity = wfGetDB( DB_SLAVE
)->getInfinity();
3441 if ( $expiry == '' ||
$expiry == $infinity ) {
3442 return $format === true
3446 return $format === true
3447 ?
$this->timeanddate( $expiry )
3448 : wfTimestamp( $format, $expiry );
3454 * @param $seconds int|float
3455 * @param $format String Optional, one of ("avoidseconds","avoidminutes"):
3456 * "avoidseconds" - don't mention seconds if $seconds >= 1 hour
3457 * "avoidminutes" - don't mention seconds/minutes if $seconds > 48 hours
3460 function formatTimePeriod( $seconds, $format = false ) {
3461 if ( round( $seconds * 10 ) < 100 ) {
3462 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
3463 $s .= $this->getMessageFromDB( 'seconds-abbrev' );
3464 } elseif ( round( $seconds ) < 60 ) {
3465 $s = $this->formatNum( round( $seconds ) );
3466 $s .= $this->getMessageFromDB( 'seconds-abbrev' );
3467 } elseif ( round( $seconds ) < 3600 ) {
3468 $minutes = floor( $seconds / 60 );
3469 $secondsPart = round( fmod( $seconds, 60 ) );
3470 if ( $secondsPart == 60 ) {
3474 $s = $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
3476 $s .= $this->formatNum( $secondsPart ) . $this->getMessageFromDB( 'seconds-abbrev' );
3477 } elseif ( round( $seconds ) <= 2*86400 ) {
3478 $hours = floor( $seconds / 3600 );
3479 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
3480 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
3481 if ( $secondsPart == 60 ) {
3485 if ( $minutes == 60 ) {
3489 $s = $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
3491 $s .= $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
3492 if ( !in_array( $format, array( 'avoidseconds', 'avoidminutes' ) ) ) {
3493 $s .= ' ' . $this->formatNum( $secondsPart ) .
3494 $this->getMessageFromDB( 'seconds-abbrev' );
3497 $days = floor( $seconds / 86400 );
3498 if ( $format === 'avoidminutes' ) {
3499 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
3500 if ( $hours == 24 ) {
3504 $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
3506 $s .= $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
3507 } elseif ( $format === 'avoidseconds' ) {
3508 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
3509 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
3510 if ( $minutes == 60 ) {
3514 if ( $hours == 24 ) {
3518 $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
3520 $s .= $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
3522 $s .= $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
3524 $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
3526 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
3536 function formatBitrate( $bps ) {
3537 $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
3539 return $this->formatNum( $bps ) . $units[0];
3541 $unitIndex = floor( log10( $bps ) / 3 );
3542 $mantissa = $bps / pow( 1000, $unitIndex );
3543 if ( $mantissa < 10 ) {
3544 $mantissa = round( $mantissa, 1 );
3546 $mantissa = round( $mantissa );
3548 return $this->formatNum( $mantissa ) . $units[$unitIndex];
3552 * Format a size in bytes for output, using an appropriate
3553 * unit (B, KB, MB or GB) according to the magnitude in question
3555 * @param $size int Size to format
3556 * @return string Plain text (not HTML)
3558 function formatSize( $size ) {
3559 // For small sizes no decimal places necessary
3561 if ( $size > 1024 ) {
3562 $size = $size / 1024;
3563 if ( $size > 1024 ) {
3564 $size = $size / 1024;
3565 // For MB and bigger two decimal places are smarter
3567 if ( $size > 1024 ) {
3568 $size = $size / 1024;
3569 $msg = 'size-gigabytes';
3571 $msg = 'size-megabytes';
3574 $msg = 'size-kilobytes';
3577 $msg = 'size-bytes';
3579 $size = round( $size, $round );
3580 $text = $this->getMessageFromDB( $msg );
3581 return str_replace( '$1', $this->formatNum( $size ), $text );
3585 * Get the conversion rule title, if any.
3589 function getConvRuleTitle() {
3590 return $this->mConverter
->getConvRuleTitle();