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 );
223 * @return String Name of the language class
225 public static function classFromCode( $code ) {
226 if ( $code == 'en' ) {
229 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
234 * Includes language class files
236 * @param $class Name of the language class
238 public static function preloadLanguageClass( $class ) {
241 if ( $class === 'Language' ) {
245 if ( !defined( 'MW_COMPILED' ) ) {
246 // Preload base classes to work around APC/PHP5 bug
247 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
248 include_once( "$IP/languages/classes/$class.deps.php" );
250 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
251 include_once( "$IP/languages/classes/$class.php" );
257 * Get the LocalisationCache instance
259 * @return LocalisationCache
261 public static function getLocalisationCache() {
262 if ( is_null( self
::$dataCache ) ) {
263 global $wgLocalisationCacheConf;
264 $class = $wgLocalisationCacheConf['class'];
265 self
::$dataCache = new $class( $wgLocalisationCacheConf );
267 return self
::$dataCache;
270 function __construct() {
271 $this->mConverter
= new FakeConverter( $this );
272 // Set the code to the name of the descendant
273 if ( get_class( $this ) == 'Language' ) {
276 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
278 self
::getLocalisationCache();
282 * Reduce memory usage
284 function __destruct() {
285 foreach ( $this as $name => $value ) {
286 unset( $this->$name );
291 * Hook which will be called if this is the content language.
292 * Descendants can use this to register hook functions or modify globals
294 function initContLang() { }
297 * Same as getFallbacksFor for current language.
299 * @deprecated in 1.19
301 function getFallbackLanguageCode() {
302 wfDeprecated( __METHOD__
);
303 return self
::getFallbackFor( $this->mCode
);
310 function getFallbackLanguages() {
311 return self
::getFallbacksFor( $this->mCode
);
315 * Exports $wgBookstoreListEn
318 function getBookstoreList() {
319 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
325 function getNamespaces() {
326 if ( is_null( $this->namespaceNames
) ) {
327 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
329 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
330 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
332 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
334 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
335 if ( $wgMetaNamespaceTalk ) {
336 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
338 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
339 $this->namespaceNames
[NS_PROJECT_TALK
] =
340 $this->fixVariableInNamespace( $talk );
343 # Sometimes a language will be localised but not actually exist on this wiki.
344 foreach( $this->namespaceNames
as $key => $text ) {
345 if ( !isset( $validNamespaces[$key] ) ) {
346 unset( $this->namespaceNames
[$key] );
350 # The above mixing may leave namespaces out of canonical order.
351 # Re-order by namespace ID number...
352 ksort( $this->namespaceNames
);
354 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames
) );
356 return $this->namespaceNames
;
360 * A convenience function that returns the same thing as
361 * getNamespaces() except with the array values changed to ' '
362 * where it found '_', useful for producing output to be displayed
363 * e.g. in <select> forms.
367 function getFormattedNamespaces() {
368 $ns = $this->getNamespaces();
369 foreach ( $ns as $k => $v ) {
370 $ns[$k] = strtr( $v, '_', ' ' );
376 * Get a namespace value by key
378 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
379 * echo $mw_ns; // prints 'MediaWiki'
382 * @param $index Int: the array key of the namespace to return
383 * @return mixed, string if the namespace value exists, otherwise false
385 function getNsText( $index ) {
386 $ns = $this->getNamespaces();
387 return isset( $ns[$index] ) ?
$ns[$index] : false;
391 * A convenience function that returns the same thing as
392 * getNsText() except with '_' changed to ' ', useful for
395 * @param $index string
399 function getFormattedNsText( $index ) {
400 $ns = $this->getNsText( $index );
401 return strtr( $ns, '_', ' ' );
405 * Returns gender-dependent namespace alias if available.
406 * @param $index Int: namespace index
407 * @param $gender String: gender key (male, female... )
411 function getGenderNsText( $index, $gender ) {
412 $ns = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
413 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
417 * Whether this language makes distinguishes genders for example in
422 function needsGenderDistinction() {
423 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
424 return count( $aliases ) > 0;
428 * Get a namespace key by value, case insensitive.
429 * Only matches namespace names for the current language, not the
430 * canonical ones defined in Namespace.php.
432 * @param $text String
433 * @return mixed An integer if $text is a valid value otherwise false
435 function getLocalNsIndex( $text ) {
436 $lctext = $this->lc( $text );
437 $ids = $this->getNamespaceIds();
438 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
444 function getNamespaceAliases() {
445 if ( is_null( $this->namespaceAliases
) ) {
446 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
450 foreach ( $aliases as $name => $index ) {
451 if ( $index === NS_PROJECT_TALK
) {
452 unset( $aliases[$name] );
453 $name = $this->fixVariableInNamespace( $name );
454 $aliases[$name] = $index;
459 $genders = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
460 foreach ( $genders as $index => $forms ) {
461 foreach ( $forms as $alias ) {
462 $aliases[$alias] = $index;
466 $this->namespaceAliases
= $aliases;
468 return $this->namespaceAliases
;
474 function getNamespaceIds() {
475 if ( is_null( $this->mNamespaceIds
) ) {
476 global $wgNamespaceAliases;
477 # Put namespace names and aliases into a hashtable.
478 # If this is too slow, then we should arrange it so that it is done
479 # before caching. The catch is that at pre-cache time, the above
480 # class-specific fixup hasn't been done.
481 $this->mNamespaceIds
= array();
482 foreach ( $this->getNamespaces() as $index => $name ) {
483 $this->mNamespaceIds
[$this->lc( $name )] = $index;
485 foreach ( $this->getNamespaceAliases() as $name => $index ) {
486 $this->mNamespaceIds
[$this->lc( $name )] = $index;
488 if ( $wgNamespaceAliases ) {
489 foreach ( $wgNamespaceAliases as $name => $index ) {
490 $this->mNamespaceIds
[$this->lc( $name )] = $index;
494 return $this->mNamespaceIds
;
499 * Get a namespace key by value, case insensitive. Canonical namespace
500 * names override custom ones defined for the current language.
502 * @param $text String
503 * @return mixed An integer if $text is a valid value otherwise false
505 function getNsIndex( $text ) {
506 $lctext = $this->lc( $text );
507 if ( ( $ns = MWNamespace
::getCanonicalIndex( $lctext ) ) !== null ) {
510 $ids = $this->getNamespaceIds();
511 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
515 * short names for language variants used for language conversion links.
517 * @param $code String
518 * @param $usemsg Use the "variantname-xyz" message if it exists
521 function getVariantname( $code, $usemsg = true ) {
522 $msg = "variantname-$code";
523 $codeArray = list( $rootCode ) = explode( '-', $code );
524 if( $usemsg && wfMessage( $msg )->exists() ) {
525 return $this->getMessageFromDB( $msg );
526 } elseif( $name = self
::getLanguageName( $code ) ) {
527 return $name; # if it's defined as a language name, show that
529 # otherwise, output the language code
535 * @param $name string
538 function specialPage( $name ) {
539 $aliases = $this->getSpecialPageAliases();
540 if ( isset( $aliases[$name][0] ) ) {
541 $name = $aliases[$name][0];
543 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
549 function getQuickbarSettings() {
551 $this->getMessage( 'qbsettings-none' ),
552 $this->getMessage( 'qbsettings-fixedleft' ),
553 $this->getMessage( 'qbsettings-fixedright' ),
554 $this->getMessage( 'qbsettings-floatingleft' ),
555 $this->getMessage( 'qbsettings-floatingright' ),
556 $this->getMessage( 'qbsettings-directionality' )
563 function getDatePreferences() {
564 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
570 function getDateFormats() {
571 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
575 * @return array|string
577 function getDefaultDateFormat() {
578 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
579 if ( $df === 'dmy or mdy' ) {
580 global $wgAmericanDates;
581 return $wgAmericanDates ?
'mdy' : 'dmy';
590 function getDatePreferenceMigrationMap() {
591 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
598 function getImageFile( $image ) {
599 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
605 function getExtraUserToggles() {
606 return self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
613 function getUserToggle( $tog ) {
614 return $this->getMessageFromDB( "tog-$tog" );
618 * Get language names, indexed by code.
619 * If $customisedOnly is true, only returns codes with a messages file
621 * @param $customisedOnly bool
625 public static function getLanguageNames( $customisedOnly = false ) {
626 global $wgExtraLanguageNames;
627 static $coreLanguageNames;
629 if ( $coreLanguageNames === null ) {
630 include( MWInit
::compiledPath( 'languages/Names.php' ) );
633 $allNames = $wgExtraLanguageNames +
$coreLanguageNames;
634 if ( !$customisedOnly ) {
640 $dir = opendir( "$IP/languages/messages" );
641 while ( false !== ( $file = readdir( $dir ) ) ) {
642 $code = self
::getCodeFromFileName( $file, 'Messages' );
643 if ( $code && isset( $allNames[$code] ) ) {
644 $names[$code] = $allNames[$code];
652 * Get translated language names. This is done on best effort and
653 * by default this is exactly the same as Language::getLanguageNames.
654 * The CLDR extension provides translated names.
655 * @param $code String Language code.
656 * @return Array language code => language name
659 public static function getTranslatedLanguageNames( $code ) {
661 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
663 foreach ( self
::getLanguageNames() as $code => $name ) {
664 if ( !isset( $names[$code] ) ) $names[$code] = $name;
671 * Get a message from the MediaWiki namespace.
673 * @param $msg String: message name
676 function getMessageFromDB( $msg ) {
677 return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
681 * @param $code string
684 function getLanguageName( $code ) {
685 $names = self
::getLanguageNames();
686 if ( !array_key_exists( $code, $names ) ) {
689 return $names[$code];
696 function getMonthName( $key ) {
697 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
703 function getMonthNamesArray() {
704 $monthNames = array( '' );
705 for ( $i=1; $i < 13; $i++
) {
706 $monthNames[] = $this->getMonthName( $i );
715 function getMonthNameGen( $key ) {
716 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
723 function getMonthAbbreviation( $key ) {
724 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
730 function getMonthAbbreviationsArray() {
731 $monthNames = array( '' );
732 for ( $i=1; $i < 13; $i++
) {
733 $monthNames[] = $this->getMonthAbbreviation( $i );
742 function getWeekdayName( $key ) {
743 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
750 function getWeekdayAbbreviation( $key ) {
751 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
758 function getIranianCalendarMonthName( $key ) {
759 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
766 function getHebrewCalendarMonthName( $key ) {
767 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
774 function getHebrewCalendarMonthNameGen( $key ) {
775 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
782 function getHijriCalendarMonthName( $key ) {
783 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
787 * Used by date() and time() to adjust the time output.
789 * @param $ts Int the time in date('YmdHis') format
790 * @param $tz Mixed: adjust the time by this amount (default false, mean we
791 * get user timecorrection setting)
794 function userAdjust( $ts, $tz = false ) {
795 global $wgUser, $wgLocalTZoffset;
797 if ( $tz === false ) {
798 $tz = $wgUser->getOption( 'timecorrection' );
801 $data = explode( '|', $tz, 3 );
803 if ( $data[0] == 'ZoneInfo' ) {
804 wfSuppressWarnings();
805 $userTZ = timezone_open( $data[2] );
807 if ( $userTZ !== false ) {
808 $date = date_create( $ts, timezone_open( 'UTC' ) );
809 date_timezone_set( $date, $userTZ );
810 $date = date_format( $date, 'YmdHis' );
813 # Unrecognized timezone, default to 'Offset' with the stored offset.
818 if ( $data[0] == 'System' ||
$tz == '' ) {
819 # Global offset in minutes.
820 if ( isset( $wgLocalTZoffset ) ) {
821 $minDiff = $wgLocalTZoffset;
823 } elseif ( $data[0] == 'Offset' ) {
824 $minDiff = intval( $data[1] );
826 $data = explode( ':', $tz );
827 if ( count( $data ) == 2 ) {
828 $data[0] = intval( $data[0] );
829 $data[1] = intval( $data[1] );
830 $minDiff = abs( $data[0] ) * 60 +
$data[1];
831 if ( $data[0] < 0 ) {
832 $minDiff = -$minDiff;
835 $minDiff = intval( $data[0] ) * 60;
839 # No difference ? Return time unchanged
840 if ( 0 == $minDiff ) {
844 wfSuppressWarnings(); // E_STRICT system time bitching
845 # Generate an adjusted date; take advantage of the fact that mktime
846 # will normalize out-of-range values so we don't have to split $minDiff
847 # into hours and minutes.
849 (int)substr( $ts, 8, 2 ) ), # Hours
850 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
851 (int)substr( $ts, 12, 2 ), # Seconds
852 (int)substr( $ts, 4, 2 ), # Month
853 (int)substr( $ts, 6, 2 ), # Day
854 (int)substr( $ts, 0, 4 ) ); # Year
856 $date = date( 'YmdHis', $t );
863 * This is a workalike of PHP's date() function, but with better
864 * internationalisation, a reduced set of format characters, and a better
867 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
868 * PHP manual for definitions. There are a number of extensions, which
871 * xn Do not translate digits of the next numeric format character
872 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
873 * xr Use roman numerals for the next numeric format character
874 * xh Use hebrew numerals for the next numeric format character
876 * xg Genitive month name
878 * xij j (day number) in Iranian calendar
879 * xiF F (month name) in Iranian calendar
880 * xin n (month number) in Iranian calendar
881 * xiY Y (full year) in Iranian calendar
883 * xjj j (day number) in Hebrew calendar
884 * xjF F (month name) in Hebrew calendar
885 * xjt t (days in month) in Hebrew calendar
886 * xjx xg (genitive month name) in Hebrew calendar
887 * xjn n (month number) in Hebrew calendar
888 * xjY Y (full year) in Hebrew calendar
890 * xmj j (day number) in Hijri calendar
891 * xmF F (month name) in Hijri calendar
892 * xmn n (month number) in Hijri calendar
893 * xmY Y (full year) in Hijri calendar
895 * xkY Y (full year) in Thai solar calendar. Months and days are
896 * identical to the Gregorian calendar
897 * xoY Y (full year) in Minguo calendar or Juche year.
898 * Months and days are identical to the
900 * xtY Y (full year) in Japanese nengo. Months and days are
901 * identical to the Gregorian calendar
903 * Characters enclosed in double quotes will be considered literal (with
904 * the quotes themselves removed). Unmatched quotes will be considered
905 * literal quotes. Example:
907 * "The month is" F => The month is January
910 * Backslash escaping is also supported.
912 * Input timestamp is assumed to be pre-normalized to the desired local
915 * @param $format String
916 * @param $ts String: 14-character timestamp
919 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
923 function sprintfDate( $format, $ts ) {
936 for ( $p = 0; $p < strlen( $format ); $p++
) {
939 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
940 $code .= $format[++
$p];
943 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
944 $code .= $format[++
$p];
955 $rawToggle = !$rawToggle;
964 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
967 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
968 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
971 $num = substr( $ts, 6, 2 );
974 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
975 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) +
1 );
978 $num = intval( substr( $ts, 6, 2 ) );
982 $iranian = self
::tsToIranian( $ts );
988 $hijri = self
::tsToHijri( $ts );
994 $hebrew = self
::tsToHebrew( $ts );
1000 $unix = wfTimestamp( TS_UNIX
, $ts );
1002 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) +
1 );
1006 $unix = wfTimestamp( TS_UNIX
, $ts );
1008 $w = gmdate( 'w', $unix );
1013 $unix = wfTimestamp( TS_UNIX
, $ts );
1015 $num = gmdate( 'w', $unix );
1019 $unix = wfTimestamp( TS_UNIX
, $ts );
1021 $num = gmdate( 'z', $unix );
1025 $unix = wfTimestamp( TS_UNIX
, $ts );
1027 $num = gmdate( 'W', $unix );
1030 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1034 $iranian = self
::tsToIranian( $ts );
1036 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1040 $hijri = self
::tsToHijri( $ts );
1042 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1046 $hebrew = self
::tsToHebrew( $ts );
1048 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1051 $num = substr( $ts, 4, 2 );
1054 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1057 $num = intval( substr( $ts, 4, 2 ) );
1061 $iranian = self
::tsToIranian( $ts );
1067 $hijri = self
::tsToHijri ( $ts );
1073 $hebrew = self
::tsToHebrew( $ts );
1079 $unix = wfTimestamp( TS_UNIX
, $ts );
1081 $num = gmdate( 't', $unix );
1085 $hebrew = self
::tsToHebrew( $ts );
1091 $unix = wfTimestamp( TS_UNIX
, $ts );
1093 $num = gmdate( 'L', $unix );
1097 $unix = wfTimestamp( TS_UNIX
, $ts );
1099 $num = date( 'o', $unix );
1102 $num = substr( $ts, 0, 4 );
1106 $iranian = self
::tsToIranian( $ts );
1112 $hijri = self
::tsToHijri( $ts );
1118 $hebrew = self
::tsToHebrew( $ts );
1124 $thai = self
::tsToYear( $ts, 'thai' );
1130 $minguo = self
::tsToYear( $ts, 'minguo' );
1136 $tenno = self
::tsToYear( $ts, 'tenno' );
1141 $num = substr( $ts, 2, 2 );
1144 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
1147 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
1150 $h = substr( $ts, 8, 2 );
1151 $num = $h %
12 ?
$h %
12 : 12;
1154 $num = intval( substr( $ts, 8, 2 ) );
1157 $h = substr( $ts, 8, 2 );
1158 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
1161 $num = substr( $ts, 8, 2 );
1164 $num = substr( $ts, 10, 2 );
1167 $num = substr( $ts, 12, 2 );
1171 $unix = wfTimestamp( TS_UNIX
, $ts );
1173 $s .= gmdate( 'c', $unix );
1177 $unix = wfTimestamp( TS_UNIX
, $ts );
1179 $s .= gmdate( 'r', $unix );
1183 $unix = wfTimestamp( TS_UNIX
, $ts );
1188 # Backslash escaping
1189 if ( $p < strlen( $format ) - 1 ) {
1190 $s .= $format[++
$p];
1197 if ( $p < strlen( $format ) - 1 ) {
1198 $endQuote = strpos( $format, '"', $p +
1 );
1199 if ( $endQuote === false ) {
1200 # No terminating quote, assume literal "
1203 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1207 # Quote at end of string, assume literal "
1214 if ( $num !== false ) {
1215 if ( $rawToggle ||
$raw ) {
1218 } elseif ( $roman ) {
1219 $s .= self
::romanNumeral( $num );
1221 } elseif ( $hebrewNum ) {
1222 $s .= self
::hebrewNumeral( $num );
1225 $s .= $this->formatNum( $num, true );
1232 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1233 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1236 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1237 * Gregorian dates to Iranian dates. Originally written in C, it
1238 * is released under the terms of GNU Lesser General Public
1239 * License. Conversion to PHP was performed by Niklas Laxström.
1241 * Link: http://www.farsiweb.info/jalali/jalali.c
1247 private static function tsToIranian( $ts ) {
1248 $gy = substr( $ts, 0, 4 ) -1600;
1249 $gm = substr( $ts, 4, 2 ) -1;
1250 $gd = substr( $ts, 6, 2 ) -1;
1252 # Days passed from the beginning (including leap years)
1254 +
floor( ( $gy +
3 ) / 4 )
1255 - floor( ( $gy +
99 ) / 100 )
1256 +
floor( ( $gy +
399 ) / 400 );
1259 // Add days of the past months of this year
1260 for ( $i = 0; $i < $gm; $i++
) {
1261 $gDayNo +
= self
::$GREG_DAYS[$i];
1265 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1269 // Days passed in current month
1272 $jDayNo = $gDayNo - 79;
1274 $jNp = floor( $jDayNo / 12053 );
1277 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1280 if ( $jDayNo >= 366 ) {
1281 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1282 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1285 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1286 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1292 return array( $jy, $jm, $jd );
1296 * Converting Gregorian dates to Hijri dates.
1298 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1300 * @link http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1306 private static function tsToHijri( $ts ) {
1307 $year = substr( $ts, 0, 4 );
1308 $month = substr( $ts, 4, 2 );
1309 $day = substr( $ts, 6, 2 );
1317 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1318 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1321 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1322 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1323 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1326 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1327 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1330 $zl = $zjd -1948440 +
10632;
1331 $zn = (int)( ( $zl - 1 ) / 10631 );
1332 $zl = $zl - 10631 * $zn +
354;
1333 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1334 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1335 $zm = (int)( ( 24 * $zl ) / 709 );
1336 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1337 $zy = 30 * $zn +
$zj - 30;
1339 return array( $zy, $zm, $zd );
1343 * Converting Gregorian dates to Hebrew dates.
1345 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1346 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1347 * to translate the relevant functions into PHP and release them under
1350 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1351 * and Adar II is 14. In a non-leap year, Adar is 6.
1357 private static function tsToHebrew( $ts ) {
1359 $year = substr( $ts, 0, 4 );
1360 $month = substr( $ts, 4, 2 );
1361 $day = substr( $ts, 6, 2 );
1363 # Calculate Hebrew year
1364 $hebrewYear = $year +
3760;
1366 # Month number when September = 1, August = 12
1368 if ( $month > 12 ) {
1375 # Calculate day of year from 1 September
1377 for ( $i = 1; $i < $month; $i++
) {
1381 # Check if the year is leap
1382 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1385 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1392 # Calculate the start of the Hebrew year
1393 $start = self
::hebrewYearStart( $hebrewYear );
1395 # Calculate next year's start
1396 if ( $dayOfYear <= $start ) {
1397 # Day is before the start of the year - it is the previous year
1399 $nextStart = $start;
1403 # Add days since previous year's 1 September
1405 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1409 # Start of the new (previous) year
1410 $start = self
::hebrewYearStart( $hebrewYear );
1413 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1416 # Calculate Hebrew day of year
1417 $hebrewDayOfYear = $dayOfYear - $start;
1419 # Difference between year's days
1420 $diff = $nextStart - $start;
1421 # Add 12 (or 13 for leap years) days to ignore the difference between
1422 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1423 # difference is only about the year type
1424 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1430 # Check the year pattern, and is leap year
1431 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1432 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1433 # and non-leap years
1434 $yearPattern = $diff %
30;
1435 # Check if leap year
1436 $isLeap = $diff >= 30;
1438 # Calculate day in the month from number of day in the Hebrew year
1439 # Don't check Adar - if the day is not in Adar, we will stop before;
1440 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1441 $hebrewDay = $hebrewDayOfYear;
1444 while ( $hebrewMonth <= 12 ) {
1445 # Calculate days in this month
1446 if ( $isLeap && $hebrewMonth == 6 ) {
1447 # Adar in a leap year
1449 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1451 if ( $hebrewDay <= $days ) {
1455 # Subtract the days of Adar I
1456 $hebrewDay -= $days;
1459 if ( $hebrewDay <= $days ) {
1465 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1466 # Cheshvan in a complete year (otherwise as the rule below)
1468 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1469 # Kislev in an incomplete year (otherwise as the rule below)
1472 # Odd months have 30 days, even have 29
1473 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1475 if ( $hebrewDay <= $days ) {
1476 # In the current month
1479 # Subtract the days of the current month
1480 $hebrewDay -= $days;
1481 # Try in the next month
1486 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1490 * This calculates the Hebrew year start, as days since 1 September.
1491 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1492 * Used for Hebrew date.
1498 private static function hebrewYearStart( $year ) {
1499 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1500 $b = intval( ( $year - 1 ) %
4 );
1501 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1505 $Mar = intval( $m );
1511 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1512 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1514 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1516 } elseif ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1520 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1525 * Algorithm to convert Gregorian dates to Thai solar dates,
1526 * Minguo dates or Minguo dates.
1528 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1529 * http://en.wikipedia.org/wiki/Minguo_calendar
1530 * http://en.wikipedia.org/wiki/Japanese_era_name
1532 * @param $ts String: 14-character timestamp
1533 * @param $cName String: calender name
1534 * @return Array: converted year, month, day
1536 private static function tsToYear( $ts, $cName ) {
1537 $gy = substr( $ts, 0, 4 );
1538 $gm = substr( $ts, 4, 2 );
1539 $gd = substr( $ts, 6, 2 );
1541 if ( !strcmp( $cName, 'thai' ) ) {
1543 # Add 543 years to the Gregorian calendar
1544 # Months and days are identical
1545 $gy_offset = $gy +
543;
1546 } elseif ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1548 # Deduct 1911 years from the Gregorian calendar
1549 # Months and days are identical
1550 $gy_offset = $gy - 1911;
1551 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1552 # Nengō dates up to Meiji period
1553 # Deduct years from the Gregorian calendar
1554 # depending on the nengo periods
1555 # Months and days are identical
1556 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1558 $gy_gannen = $gy - 1868 +
1;
1559 $gy_offset = $gy_gannen;
1560 if ( $gy_gannen == 1 ) {
1563 $gy_offset = '明治' . $gy_offset;
1565 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1566 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1567 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1568 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1569 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1573 $gy_gannen = $gy - 1912 +
1;
1574 $gy_offset = $gy_gannen;
1575 if ( $gy_gannen == 1 ) {
1578 $gy_offset = '大正' . $gy_offset;
1580 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1581 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1582 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1586 $gy_gannen = $gy - 1926 +
1;
1587 $gy_offset = $gy_gannen;
1588 if ( $gy_gannen == 1 ) {
1591 $gy_offset = '昭和' . $gy_offset;
1594 $gy_gannen = $gy - 1989 +
1;
1595 $gy_offset = $gy_gannen;
1596 if ( $gy_gannen == 1 ) {
1599 $gy_offset = '平成' . $gy_offset;
1605 return array( $gy_offset, $gm, $gd );
1609 * Roman number formatting up to 3000
1615 static function romanNumeral( $num ) {
1616 static $table = array(
1617 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1618 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1619 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1620 array( '', 'M', 'MM', 'MMM' )
1623 $num = intval( $num );
1624 if ( $num > 3000 ||
$num <= 0 ) {
1629 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1630 if ( $num >= $pow10 ) {
1631 $s .= $table[$i][floor( $num / $pow10 )];
1633 $num = $num %
$pow10;
1639 * Hebrew Gematria number formatting up to 9999
1645 static function hebrewNumeral( $num ) {
1646 static $table = array(
1647 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1648 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1649 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1650 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1653 $num = intval( $num );
1654 if ( $num > 9999 ||
$num <= 0 ) {
1659 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1660 if ( $num >= $pow10 ) {
1661 if ( $num == 15 ||
$num == 16 ) {
1662 $s .= $table[0][9] . $table[0][$num - 9];
1665 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1666 if ( $pow10 == 1000 ) {
1671 $num = $num %
$pow10;
1673 if ( strlen( $s ) == 2 ) {
1676 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1677 $str .= substr( $s, strlen( $s ) - 2, 2 );
1679 $start = substr( $str, 0, strlen( $str ) - 2 );
1680 $end = substr( $str, strlen( $str ) - 2 );
1683 $str = $start . 'ך';
1686 $str = $start . 'ם';
1689 $str = $start . 'ן';
1692 $str = $start . 'ף';
1695 $str = $start . 'ץ';
1702 * This is meant to be used by time(), date(), and timeanddate() to get
1703 * the date preference they're supposed to use, it should be used in
1707 * function timeanddate([...], $format = true) {
1708 * $datePreference = $this->dateFormat($format);
1713 * @param $usePrefs Mixed: if true, the user's preference is used
1714 * if false, the site/language default is used
1715 * if int/string, assumed to be a format.
1718 function dateFormat( $usePrefs = true ) {
1721 if ( is_bool( $usePrefs ) ) {
1723 $datePreference = $wgUser->getDatePreference();
1725 $datePreference = (string)User
::getDefaultOption( 'date' );
1728 $datePreference = (string)$usePrefs;
1732 if ( $datePreference == '' ) {
1736 return $datePreference;
1740 * Get a format string for a given type and preference
1741 * @param $type string May be date, time or both
1742 * @param $pref string The format name as it appears in Messages*.php
1746 function getDateFormatString( $type, $pref ) {
1747 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
1748 if ( $pref == 'default' ) {
1749 $pref = $this->getDefaultDateFormat();
1750 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1752 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1753 if ( is_null( $df ) ) {
1754 $pref = $this->getDefaultDateFormat();
1755 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1758 $this->dateFormatStrings
[$type][$pref] = $df;
1760 return $this->dateFormatStrings
[$type][$pref];
1764 * @param $ts Mixed: the time format which needs to be turned into a
1765 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1766 * @param $adj Bool: whether to adjust the time output according to the
1767 * user configured offset ($timecorrection)
1768 * @param $format Mixed: true to use user's date format preference
1769 * @param $timecorrection String|bool the time offset as returned by
1770 * validateTimeZone() in Special:Preferences
1773 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1774 $ts = wfTimestamp( TS_MW
, $ts );
1776 $ts = $this->userAdjust( $ts, $timecorrection );
1778 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1779 return $this->sprintfDate( $df, $ts );
1783 * @param $ts Mixed: the time format which needs to be turned into a
1784 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1785 * @param $adj Bool: whether to adjust the time output according to the
1786 * user configured offset ($timecorrection)
1787 * @param $format Mixed: true to use user's date format preference
1788 * @param $timecorrection String|bool the time offset as returned by
1789 * validateTimeZone() in Special:Preferences
1792 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1793 $ts = wfTimestamp( TS_MW
, $ts );
1795 $ts = $this->userAdjust( $ts, $timecorrection );
1797 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1798 return $this->sprintfDate( $df, $ts );
1802 * @param $ts Mixed: the time format which needs to be turned into a
1803 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1804 * @param $adj Bool: whether to adjust the time output according to the
1805 * user configured offset ($timecorrection)
1806 * @param $format Mixed: what format to return, if it's false output the
1807 * default one (default true)
1808 * @param $timecorrection String|bool the time offset as returned by
1809 * validateTimeZone() in Special:Preferences
1812 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1813 $ts = wfTimestamp( TS_MW
, $ts );
1815 $ts = $this->userAdjust( $ts, $timecorrection );
1817 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1818 return $this->sprintfDate( $df, $ts );
1822 * @param $key string
1823 * @return array|null
1825 function getMessage( $key ) {
1826 return self
::$dataCache->getSubitem( $this->mCode
, 'messages', $key );
1832 function getAllMessages() {
1833 return self
::$dataCache->getItem( $this->mCode
, 'messages' );
1842 function iconv( $in, $out, $string ) {
1843 # This is a wrapper for iconv in all languages except esperanto,
1844 # which does some nasty x-conversions beforehand
1846 # Even with //IGNORE iconv can whine about illegal characters in
1847 # *input* string. We just ignore those too.
1848 # REF: http://bugs.php.net/bug.php?id=37166
1849 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
1850 wfSuppressWarnings();
1851 $text = iconv( $in, $out . '//IGNORE', $string );
1852 wfRestoreWarnings();
1856 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
1859 * @param $matches array
1860 * @return mixed|string
1862 function ucwordbreaksCallbackAscii( $matches ) {
1863 return $this->ucfirst( $matches[1] );
1867 * @param $matches array
1870 function ucwordbreaksCallbackMB( $matches ) {
1871 return mb_strtoupper( $matches[0] );
1875 * @param $matches array
1878 function ucCallback( $matches ) {
1879 list( $wikiUpperChars ) = self
::getCaseMaps();
1880 return strtr( $matches[1], $wikiUpperChars );
1884 * @param $matches array
1887 function lcCallback( $matches ) {
1888 list( , $wikiLowerChars ) = self
::getCaseMaps();
1889 return strtr( $matches[1], $wikiLowerChars );
1893 * @param $matches array
1896 function ucwordsCallbackMB( $matches ) {
1897 return mb_strtoupper( $matches[0] );
1901 * @param $matches array
1904 function ucwordsCallbackWiki( $matches ) {
1905 list( $wikiUpperChars ) = self
::getCaseMaps();
1906 return strtr( $matches[0], $wikiUpperChars );
1910 * Make a string's first character uppercase
1912 * @param $str string
1916 function ucfirst( $str ) {
1918 if ( $o < 96 ) { // if already uppercase...
1920 } elseif ( $o < 128 ) {
1921 return ucfirst( $str ); // use PHP's ucfirst()
1923 // fall back to more complex logic in case of multibyte strings
1924 return $this->uc( $str, true );
1929 * Convert a string to uppercase
1931 * @param $str string
1932 * @param $first bool
1936 function uc( $str, $first = false ) {
1937 if ( function_exists( 'mb_strtoupper' ) ) {
1939 if ( $this->isMultibyte( $str ) ) {
1940 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1942 return ucfirst( $str );
1945 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
1948 if ( $this->isMultibyte( $str ) ) {
1949 $x = $first ?
'^' : '';
1950 return preg_replace_callback(
1951 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1952 array( $this, 'ucCallback' ),
1956 return $first ?
ucfirst( $str ) : strtoupper( $str );
1962 * @param $str string
1963 * @return mixed|string
1965 function lcfirst( $str ) {
1968 return strval( $str );
1969 } elseif ( $o >= 128 ) {
1970 return $this->lc( $str, true );
1971 } elseif ( $o > 96 ) {
1974 $str[0] = strtolower( $str[0] );
1980 * @param $str string
1981 * @param $first bool
1982 * @return mixed|string
1984 function lc( $str, $first = false ) {
1985 if ( function_exists( 'mb_strtolower' ) ) {
1987 if ( $this->isMultibyte( $str ) ) {
1988 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1990 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
1993 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
1996 if ( $this->isMultibyte( $str ) ) {
1997 $x = $first ?
'^' : '';
1998 return preg_replace_callback(
1999 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2000 array( $this, 'lcCallback' ),
2004 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2010 * @param $str string
2013 function isMultibyte( $str ) {
2014 return (bool)preg_match( '/[\x80-\xff]/', $str );
2018 * @param $str string
2019 * @return mixed|string
2021 function ucwords( $str ) {
2022 if ( $this->isMultibyte( $str ) ) {
2023 $str = $this->lc( $str );
2025 // regexp to find first letter in each word (i.e. after each space)
2026 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2028 // function to use to capitalize a single char
2029 if ( function_exists( 'mb_strtoupper' ) ) {
2030 return preg_replace_callback(
2032 array( $this, 'ucwordsCallbackMB' ),
2036 return preg_replace_callback(
2038 array( $this, 'ucwordsCallbackWiki' ),
2043 return ucwords( strtolower( $str ) );
2048 * capitalize words at word breaks
2050 * @param $str string
2053 function ucwordbreaks( $str ) {
2054 if ( $this->isMultibyte( $str ) ) {
2055 $str = $this->lc( $str );
2057 // since \b doesn't work for UTF-8, we explicitely define word break chars
2058 $breaks = "[ \-\(\)\}\{\.,\?!]";
2060 // find first letter after word break
2061 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2063 if ( function_exists( 'mb_strtoupper' ) ) {
2064 return preg_replace_callback(
2066 array( $this, 'ucwordbreaksCallbackMB' ),
2070 return preg_replace_callback(
2072 array( $this, 'ucwordsCallbackWiki' ),
2077 return preg_replace_callback(
2078 '/\b([\w\x80-\xff]+)\b/',
2079 array( $this, 'ucwordbreaksCallbackAscii' ),
2086 * Return a case-folded representation of $s
2088 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2089 * and $s2 are the same except for the case of their characters. It is not
2090 * necessary for the value returned to make sense when displayed.
2092 * Do *not* perform any other normalisation in this function. If a caller
2093 * uses this function when it should be using a more general normalisation
2094 * function, then fix the caller.
2100 function caseFold( $s ) {
2101 return $this->uc( $s );
2108 function checkTitleEncoding( $s ) {
2109 if ( is_array( $s ) ) {
2110 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2112 # Check for non-UTF-8 URLs
2113 $ishigh = preg_match( '/[\x80-\xff]/', $s );
2118 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2119 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
2124 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2130 function fallback8bitEncoding() {
2131 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
2135 * Most writing systems use whitespace to break up words.
2136 * Some languages such as Chinese don't conventionally do this,
2137 * which requires special handling when breaking up words for
2142 function hasWordBreaks() {
2147 * Some languages such as Chinese require word segmentation,
2148 * Specify such segmentation when overridden in derived class.
2150 * @param $string String
2153 function segmentByWord( $string ) {
2158 * Some languages have special punctuation need to be normalized.
2159 * Make such changes here.
2161 * @param $string String
2164 function normalizeForSearch( $string ) {
2165 return self
::convertDoubleWidth( $string );
2169 * convert double-width roman characters to single-width.
2170 * range: ff00-ff5f ~= 0020-007f
2172 * @param $string string
2176 protected static function convertDoubleWidth( $string ) {
2177 static $full = null;
2178 static $half = null;
2180 if ( $full === null ) {
2181 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2182 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2183 $full = str_split( $fullWidth, 3 );
2184 $half = str_split( $halfWidth );
2187 $string = str_replace( $full, $half, $string );
2192 * @param $string string
2193 * @param $pattern string
2196 protected static function insertSpace( $string, $pattern ) {
2197 $string = preg_replace( $pattern, " $1 ", $string );
2198 $string = preg_replace( '/ +/', ' ', $string );
2203 * @param $termsArray array
2206 function convertForSearchResult( $termsArray ) {
2207 # some languages, e.g. Chinese, need to do a conversion
2208 # in order for search results to be displayed correctly
2213 * Get the first character of a string.
2218 function firstChar( $s ) {
2221 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2222 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2227 if ( isset( $matches[1] ) ) {
2228 if ( strlen( $matches[1] ) != 3 ) {
2232 // Break down Hangul syllables to grab the first jamo
2233 $code = utf8ToCodepoint( $matches[1] );
2234 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
2236 } elseif ( $code < 0xb098 ) {
2237 return "\xe3\x84\xb1";
2238 } elseif ( $code < 0xb2e4 ) {
2239 return "\xe3\x84\xb4";
2240 } elseif ( $code < 0xb77c ) {
2241 return "\xe3\x84\xb7";
2242 } elseif ( $code < 0xb9c8 ) {
2243 return "\xe3\x84\xb9";
2244 } elseif ( $code < 0xbc14 ) {
2245 return "\xe3\x85\x81";
2246 } elseif ( $code < 0xc0ac ) {
2247 return "\xe3\x85\x82";
2248 } elseif ( $code < 0xc544 ) {
2249 return "\xe3\x85\x85";
2250 } elseif ( $code < 0xc790 ) {
2251 return "\xe3\x85\x87";
2252 } elseif ( $code < 0xcc28 ) {
2253 return "\xe3\x85\x88";
2254 } elseif ( $code < 0xce74 ) {
2255 return "\xe3\x85\x8a";
2256 } elseif ( $code < 0xd0c0 ) {
2257 return "\xe3\x85\x8b";
2258 } elseif ( $code < 0xd30c ) {
2259 return "\xe3\x85\x8c";
2260 } elseif ( $code < 0xd558 ) {
2261 return "\xe3\x85\x8d";
2263 return "\xe3\x85\x8e";
2270 function initEncoding() {
2271 # Some languages may have an alternate char encoding option
2272 # (Esperanto X-coding, Japanese furigana conversion, etc)
2273 # If this language is used as the primary content language,
2274 # an override to the defaults can be set here on startup.
2281 function recodeForEdit( $s ) {
2282 # For some languages we'll want to explicitly specify
2283 # which characters make it into the edit box raw
2284 # or are converted in some way or another.
2285 global $wgEditEncoding;
2286 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
2289 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2297 function recodeInput( $s ) {
2298 # Take the previous into account.
2299 global $wgEditEncoding;
2300 if ( $wgEditEncoding != '' ) {
2301 $enc = $wgEditEncoding;
2305 if ( $enc == 'UTF-8' ) {
2308 return $this->iconv( $enc, 'UTF-8', $s );
2313 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2314 * also cleans up certain backwards-compatible sequences, converting them
2315 * to the modern Unicode equivalent.
2317 * This is language-specific for performance reasons only.
2323 function normalize( $s ) {
2324 global $wgAllUnicodeFixes;
2325 $s = UtfNormal
::cleanUp( $s );
2326 if ( $wgAllUnicodeFixes ) {
2327 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2328 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2335 * Transform a string using serialized data stored in the given file (which
2336 * must be in the serialized subdirectory of $IP). The file contains pairs
2337 * mapping source characters to destination characters.
2339 * The data is cached in process memory. This will go faster if you have the
2340 * FastStringSearch extension.
2342 * @param $file string
2343 * @param $string string
2347 function transformUsingPairFile( $file, $string ) {
2348 if ( !isset( $this->transformData
[$file] ) ) {
2349 $data = wfGetPrecompiledData( $file );
2350 if ( $data === false ) {
2351 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2353 $this->transformData
[$file] = new ReplacementArray( $data );
2355 return $this->transformData
[$file]->replace( $string );
2359 * For right-to-left language support
2364 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2368 * Return the correct HTML 'dir' attribute value for this language.
2372 return $this->isRTL() ?
'rtl' : 'ltr';
2376 * Return 'left' or 'right' as appropriate alignment for line-start
2377 * for this language's text direction.
2379 * Should be equivalent to CSS3 'start' text-align value....
2383 function alignStart() {
2384 return $this->isRTL() ?
'right' : 'left';
2388 * Return 'right' or 'left' as appropriate alignment for line-end
2389 * for this language's text direction.
2391 * Should be equivalent to CSS3 'end' text-align value....
2395 function alignEnd() {
2396 return $this->isRTL() ?
'left' : 'right';
2400 * A hidden direction mark (LRM or RLM), depending on the language direction
2402 * @param $opposite Boolean Get the direction mark opposite to your language
2405 function getDirMark( $opposite = false ) {
2406 $rtl = "\xE2\x80\x8F";
2407 $ltr = "\xE2\x80\x8E";
2408 if( $opposite ) { return $this->isRTL() ?
$ltr : $rtl; }
2409 return $this->isRTL() ?
$rtl : $ltr;
2415 function capitalizeAllNouns() {
2416 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2420 * An arrow, depending on the language direction
2424 function getArrow() {
2425 return $this->isRTL() ?
'←' : '→';
2429 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2433 function linkPrefixExtension() {
2434 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2440 function getMagicWords() {
2441 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2444 protected function doMagicHook() {
2445 if ( $this->mMagicHookDone
) {
2448 $this->mMagicHookDone
= true;
2449 wfProfileIn( 'LanguageGetMagic' );
2450 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2451 wfProfileOut( 'LanguageGetMagic' );
2455 * Fill a MagicWord object with data from here
2459 function getMagic( $mw ) {
2460 $this->doMagicHook();
2462 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
2463 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
2465 $magicWords = $this->getMagicWords();
2466 if ( isset( $magicWords[$mw->mId
] ) ) {
2467 $rawEntry = $magicWords[$mw->mId
];
2473 if ( !is_array( $rawEntry ) ) {
2474 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
2476 $mw->mCaseSensitive
= $rawEntry[0];
2477 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
2482 * Add magic words to the extension array
2484 * @param $newWords array
2486 function addMagicWordsByLang( $newWords ) {
2487 $fallbackChain = $this->getFallbackLanguages();
2488 $fallbackChain = array_reverse( $fallbackChain );
2489 foreach ( $fallbackChain as $code ) {
2490 if ( isset( $newWords[$code] ) ) {
2491 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
2497 * Get special page names, as an associative array
2498 * case folded alias => real name
2500 function getSpecialPageAliases() {
2501 // Cache aliases because it may be slow to load them
2502 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
2504 $this->mExtendedSpecialPageAliases
=
2505 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
2506 wfRunHooks( 'LanguageGetSpecialPageAliases',
2507 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
2510 return $this->mExtendedSpecialPageAliases
;
2514 * Italic is unsuitable for some languages
2516 * @param $text String: the text to be emphasized.
2519 function emphasize( $text ) {
2520 return "<em>$text</em>";
2524 * Normally we output all numbers in plain en_US style, that is
2525 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2526 * point twohundredthirtyfive. However this is not suitable for all
2527 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2528 * Icelandic just want to use commas instead of dots, and dots instead
2529 * of commas like "293.291,235".
2531 * An example of this function being called:
2533 * wfMsg( 'message', $wgLang->formatNum( $num ) )
2536 * See LanguageGu.php for the Gujarati implementation and
2537 * $separatorTransformTable on MessageIs.php for
2538 * the , => . and . => , implementation.
2540 * @todo check if it's viable to use localeconv() for the decimal
2542 * @param $number Mixed: the string to be formatted, should be an integer
2543 * or a floating point number.
2544 * @param $nocommafy Bool: set to true for special numbers like dates
2547 function formatNum( $number, $nocommafy = false ) {
2548 global $wgTranslateNumerals;
2549 if ( !$nocommafy ) {
2550 $number = $this->commafy( $number );
2551 $s = $this->separatorTransformTable();
2553 $number = strtr( $number, $s );
2557 if ( $wgTranslateNumerals ) {
2558 $s = $this->digitTransformTable();
2560 $number = strtr( $number, $s );
2568 * @param $number string
2571 function parseFormattedNumber( $number ) {
2572 $s = $this->digitTransformTable();
2574 $number = strtr( $number, array_flip( $s ) );
2577 $s = $this->separatorTransformTable();
2579 $number = strtr( $number, array_flip( $s ) );
2582 $number = strtr( $number, array( ',' => '' ) );
2587 * Adds commas to a given number
2592 function commafy( $_ ) {
2593 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2599 function digitTransformTable() {
2600 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
2606 function separatorTransformTable() {
2607 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
2611 * Take a list of strings and build a locale-friendly comma-separated
2612 * list, using the local comma-separator message.
2613 * The last two strings are chained with an "and".
2618 function listToText( $l ) {
2620 $m = count( $l ) - 1;
2622 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
2624 for ( $i = $m; $i >= 0; $i-- ) {
2627 } elseif ( $i == $m - 1 ) {
2628 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
2630 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
2638 * Take a list of strings and build a locale-friendly comma-separated
2639 * list, using the local comma-separator message.
2640 * @param $list array of strings to put in a comma list
2643 function commaList( $list ) {
2648 array( 'parsemag', 'escapenoentities', 'language' => $this )
2654 * Take a list of strings and build a locale-friendly semicolon-separated
2655 * list, using the local semicolon-separator message.
2656 * @param $list array of strings to put in a semicolon list
2659 function semicolonList( $list ) {
2663 'semicolon-separator',
2664 array( 'parsemag', 'escapenoentities', 'language' => $this )
2670 * Same as commaList, but separate it with the pipe instead.
2671 * @param $list array of strings to put in a pipe list
2674 function pipeList( $list ) {
2679 array( 'escapenoentities', 'language' => $this )
2685 * Truncate a string to a specified length in bytes, appending an optional
2686 * string (e.g. for ellipses)
2688 * The database offers limited byte lengths for some columns in the database;
2689 * multi-byte character sets mean we need to ensure that only whole characters
2690 * are included, otherwise broken characters can be passed to the user
2692 * If $length is negative, the string will be truncated from the beginning
2694 * @param $string String to truncate
2695 * @param $length Int: maximum length (including ellipses)
2696 * @param $ellipsis String to append to the truncated text
2697 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
2698 * $adjustLength was introduced in 1.18, before that behaved as if false.
2701 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
2702 # Use the localized ellipsis character
2703 if ( $ellipsis == '...' ) {
2704 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
2706 # Check if there is no need to truncate
2707 if ( $length == 0 ) {
2708 return $ellipsis; // convention
2709 } elseif ( strlen( $string ) <= abs( $length ) ) {
2710 return $string; // no need to truncate
2712 $stringOriginal = $string;
2713 # If ellipsis length is >= $length then we can't apply $adjustLength
2714 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
2715 $string = $ellipsis; // this can be slightly unexpected
2716 # Otherwise, truncate and add ellipsis...
2718 $eLength = $adjustLength ?
strlen( $ellipsis ) : 0;
2719 if ( $length > 0 ) {
2720 $length -= $eLength;
2721 $string = substr( $string, 0, $length ); // xyz...
2722 $string = $this->removeBadCharLast( $string );
2723 $string = $string . $ellipsis;
2725 $length +
= $eLength;
2726 $string = substr( $string, $length ); // ...xyz
2727 $string = $this->removeBadCharFirst( $string );
2728 $string = $ellipsis . $string;
2731 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
2732 # This check is *not* redundant if $adjustLength, due to the single case where
2733 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
2734 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
2737 return $stringOriginal;
2742 * Remove bytes that represent an incomplete Unicode character
2743 * at the end of string (e.g. bytes of the char are missing)
2745 * @param $string String
2748 protected function removeBadCharLast( $string ) {
2749 if ( $string != '' ) {
2750 $char = ord( $string[strlen( $string ) - 1] );
2752 if ( $char >= 0xc0 ) {
2753 # We got the first byte only of a multibyte char; remove it.
2754 $string = substr( $string, 0, -1 );
2755 } elseif ( $char >= 0x80 &&
2756 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
2757 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
2759 # We chopped in the middle of a character; remove it
2767 * Remove bytes that represent an incomplete Unicode character
2768 * at the start of string (e.g. bytes of the char are missing)
2770 * @param $string String
2773 protected function removeBadCharFirst( $string ) {
2774 if ( $string != '' ) {
2775 $char = ord( $string[0] );
2776 if ( $char >= 0x80 && $char < 0xc0 ) {
2777 # We chopped in the middle of a character; remove the whole thing
2778 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
2785 * Truncate a string of valid HTML to a specified length in bytes,
2786 * appending an optional string (e.g. for ellipses), and return valid HTML
2788 * This is only intended for styled/linked text, such as HTML with
2789 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
2790 * Also, this will not detect things like "display:none" CSS.
2792 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
2794 * @param string $text HTML string to truncate
2795 * @param int $length (zero/positive) Maximum length (including ellipses)
2796 * @param string $ellipsis String to append to the truncated text
2799 function truncateHtml( $text, $length, $ellipsis = '...' ) {
2800 # Use the localized ellipsis character
2801 if ( $ellipsis == '...' ) {
2802 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
2804 # Check if there is clearly no need to truncate
2805 if ( $length <= 0 ) {
2806 return $ellipsis; // no text shown, nothing to format (convention)
2807 } elseif ( strlen( $text ) <= $length ) {
2808 return $text; // string short enough even *with* HTML (short-circuit)
2811 $dispLen = 0; // innerHTML legth so far
2812 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
2813 $tagType = 0; // 0-open, 1-close
2814 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
2815 $entityState = 0; // 0-not entity, 1-entity
2816 $tag = $ret = ''; // accumulated tag name, accumulated result string
2817 $openTags = array(); // open tag stack
2818 $maybeState = null; // possible truncation state
2820 $textLen = strlen( $text );
2821 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
2822 for ( $pos = 0; true; ++
$pos ) {
2823 # Consider truncation once the display length has reached the maximim.
2824 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
2825 # Check that we're not in the middle of a bracket/entity...
2826 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
2827 if ( !$testingEllipsis ) {
2828 $testingEllipsis = true;
2829 # Save where we are; we will truncate here unless there turn out to
2830 # be so few remaining characters that truncation is not necessary.
2831 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
2832 $maybeState = array( $ret, $openTags ); // save state
2834 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
2835 # String in fact does need truncation, the truncation point was OK.
2836 list( $ret, $openTags ) = $maybeState; // reload state
2837 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
2838 $ret .= $ellipsis; // add ellipsis
2842 if ( $pos >= $textLen ) break; // extra iteration just for above checks
2844 # Read the next char...
2846 $lastCh = $pos ?
$text[$pos - 1] : '';
2847 $ret .= $ch; // add to result string
2849 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
2850 $entityState = 0; // for bad HTML
2851 $bracketState = 1; // tag started (checking for backslash)
2852 } elseif ( $ch == '>' ) {
2853 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
2854 $entityState = 0; // for bad HTML
2855 $bracketState = 0; // out of brackets
2856 } elseif ( $bracketState == 1 ) {
2858 $tagType = 1; // close tag (e.g. "</span>")
2860 $tagType = 0; // open tag (e.g. "<span>")
2863 $bracketState = 2; // building tag name
2864 } elseif ( $bracketState == 2 ) {
2868 // Name found (e.g. "<a href=..."), add on tag attributes...
2869 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
2871 } elseif ( $bracketState == 0 ) {
2872 if ( $entityState ) {
2875 $dispLen++
; // entity is one displayed char
2878 if ( $neLength == 0 && !$maybeState ) {
2879 // Save state without $ch. We want to *hit* the first
2880 // display char (to get tags) but not *use* it if truncating.
2881 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
2884 $entityState = 1; // entity found, (e.g. " ")
2886 $dispLen++
; // this char is displayed
2887 // Add the next $max display text chars after this in one swoop...
2888 $max = ( $testingEllipsis ?
$length : $neLength ) - $dispLen;
2889 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos +
1, $max );
2890 $dispLen +
= $skipped;
2896 // Close the last tag if left unclosed by bad HTML
2897 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
2898 while ( count( $openTags ) > 0 ) {
2899 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
2905 * truncateHtml() helper function
2906 * like strcspn() but adds the skipped chars to $ret
2915 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
2916 if ( $len === null ) {
2917 $len = -1; // -1 means "no limit" for strcspn
2918 } elseif ( $len < 0 ) {
2922 if ( $start < strlen( $text ) ) {
2923 $skipCount = strcspn( $text, $search, $start, $len );
2924 $ret .= substr( $text, $start, $skipCount );
2930 * truncateHtml() helper function
2931 * (a) push or pop $tag from $openTags as needed
2932 * (b) clear $tag value
2933 * @param String &$tag Current HTML tag name we are looking at
2934 * @param int $tagType (0-open tag, 1-close tag)
2935 * @param char $lastCh Character before the '>' that ended this tag
2936 * @param array &$openTags Open tag stack (not accounting for $tag)
2938 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
2939 $tag = ltrim( $tag );
2941 if ( $tagType == 0 && $lastCh != '/' ) {
2942 $openTags[] = $tag; // tag opened (didn't close itself)
2943 } elseif ( $tagType == 1 ) {
2944 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
2945 array_pop( $openTags ); // tag closed
2953 * Grammatical transformations, needed for inflected languages
2954 * Invoked by putting {{grammar:case|word}} in a message
2956 * @param $word string
2957 * @param $case string
2960 function convertGrammar( $word, $case ) {
2961 global $wgGrammarForms;
2962 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
2963 return $wgGrammarForms[$this->getCode()][$case][$word];
2969 * Provides an alternative text depending on specified gender.
2970 * Usage {{gender:username|masculine|feminine|neutral}}.
2971 * username is optional, in which case the gender of current user is used,
2972 * but only in (some) interface messages; otherwise default gender is used.
2973 * If second or third parameter are not specified, masculine is used.
2974 * These details may be overriden per language.
2976 * @param $gender string
2977 * @param $forms array
2981 function gender( $gender, $forms ) {
2982 if ( !count( $forms ) ) {
2985 $forms = $this->preConvertPlural( $forms, 2 );
2986 if ( $gender === 'male' ) {
2989 if ( $gender === 'female' ) {
2992 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
2996 * Plural form transformations, needed for some languages.
2997 * For example, there are 3 form of plural in Russian and Polish,
2998 * depending on "count mod 10". See [[w:Plural]]
2999 * For English it is pretty simple.
3001 * Invoked by putting {{plural:count|wordform1|wordform2}}
3002 * or {{plural:count|wordform1|wordform2|wordform3}}
3004 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3006 * @param $count Integer: non-localized number
3007 * @param $forms Array: different plural forms
3008 * @return string Correct form of plural for $count in this language
3010 function convertPlural( $count, $forms ) {
3011 if ( !count( $forms ) ) {
3014 $forms = $this->preConvertPlural( $forms, 2 );
3016 return ( $count == 1 ) ?
$forms[0] : $forms[1];
3020 * Checks that convertPlural was given an array and pads it to requested
3021 * amount of forms by copying the last one.
3023 * @param $count Integer: How many forms should there be at least
3024 * @param $forms Array of forms given to convertPlural
3025 * @return array Padded array of forms or an exception if not an array
3027 protected function preConvertPlural( /* Array */ $forms, $count ) {
3028 while ( count( $forms ) < $count ) {
3029 $forms[] = $forms[count( $forms ) - 1];
3035 * This translates the duration ("1 week", "4 days", etc)
3036 * as well as the expiry time (which is an absolute timestamp).
3037 * @param $str String: the validated block duration in English
3038 * @return Somehow translated block duration
3039 * @see LanguageFi.php for example implementation
3041 function translateBlockExpiry( $str ) {
3042 $duration = SpecialBlock
::getSuggestedDurations( $this );
3043 foreach( $duration as $show => $value ){
3044 if ( strcmp( $str, $value ) == 0 ) {
3045 return htmlspecialchars( trim( $show ) );
3049 // Since usually only infinite or indefinite is only on list, so try
3050 // equivalents if still here.
3051 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3052 if ( in_array( $str, $indefs ) ) {
3053 foreach( $indefs as $val ) {
3054 $show = array_search( $val, $duration, true );
3055 if ( $show !== false ) {
3056 return htmlspecialchars( trim( $show ) );
3060 // If no duration is given, but a timestamp, display that
3061 return ( strtotime( $str ) ?
$this->timeanddate( strtotime( $str ) ) : $str );
3065 * languages like Chinese need to be segmented in order for the diff
3068 * @param $text String
3071 function segmentForDiff( $text ) {
3076 * and unsegment to show the result
3078 * @param $text String
3081 function unsegmentForDiff( $text ) {
3086 * convert text to all supported variants
3088 * @param $text string
3091 function autoConvertToAllVariants( $text ) {
3092 return $this->mConverter
->autoConvertToAllVariants( $text );
3096 * convert text to different variants of a language.
3098 * @param $text string
3101 function convert( $text ) {
3102 return $this->mConverter
->convert( $text );
3107 * Convert a Title object to a string in the preferred variant
3109 * @param $title Title
3112 function convertTitle( $title ) {
3113 return $this->mConverter
->convertTitle( $title );
3117 * Check if this is a language with variants
3121 function hasVariants() {
3122 return sizeof( $this->getVariants() ) > 1;
3126 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3128 * @param $text string
3131 function armourMath( $text ) {
3132 return $this->mConverter
->armourMath( $text );
3136 * Perform output conversion on a string, and encode for safe HTML output.
3137 * @param $text String text to be converted
3138 * @param $isTitle Bool whether this conversion is for the article title
3140 * @todo this should get integrated somewhere sane
3142 function convertHtml( $text, $isTitle = false ) {
3143 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3147 * @param $key string
3150 function convertCategoryKey( $key ) {
3151 return $this->mConverter
->convertCategoryKey( $key );
3155 * Get the list of variants supported by this language
3156 * see sample implementation in LanguageZh.php
3158 * @return array an array of language codes
3160 function getVariants() {
3161 return $this->mConverter
->getVariants();
3167 function getPreferredVariant() {
3168 return $this->mConverter
->getPreferredVariant();
3174 function getDefaultVariant() {
3175 return $this->mConverter
->getDefaultVariant();
3181 function getURLVariant() {
3182 return $this->mConverter
->getURLVariant();
3186 * If a language supports multiple variants, it is
3187 * possible that non-existing link in one variant
3188 * actually exists in another variant. this function
3189 * tries to find it. See e.g. LanguageZh.php
3191 * @param $link String: the name of the link
3192 * @param $nt Mixed: the title object of the link
3193 * @param $ignoreOtherCond Boolean: to disable other conditions when
3194 * we need to transclude a template or update a category's link
3195 * @return null the input parameters may be modified upon return
3197 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3198 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
3202 * If a language supports multiple variants, converts text
3203 * into an array of all possible variants of the text:
3204 * 'variant' => text in that variant
3206 * @deprecated since 1.17 Use autoConvertToAllVariants()
3208 * @param $text string
3212 function convertLinkToAllVariants( $text ) {
3213 return $this->mConverter
->convertLinkToAllVariants( $text );
3217 * returns language specific options used by User::getPageRenderHash()
3218 * for example, the preferred language variant
3222 function getExtraHashOptions() {
3223 return $this->mConverter
->getExtraHashOptions();
3227 * For languages that support multiple variants, the title of an
3228 * article may be displayed differently in different variants. this
3229 * function returns the apporiate title defined in the body of the article.
3233 function getParsedTitle() {
3234 return $this->mConverter
->getParsedTitle();
3238 * Enclose a string with the "no conversion" tag. This is used by
3239 * various functions in the Parser
3241 * @param $text String: text to be tagged for no conversion
3242 * @param $noParse bool
3243 * @return string the tagged text
3245 function markNoConversion( $text, $noParse = false ) {
3246 return $this->mConverter
->markNoConversion( $text, $noParse );
3250 * A regular expression to match legal word-trailing characters
3251 * which should be merged onto a link of the form [[foo]]bar.
3255 function linkTrail() {
3256 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
3262 function getLangObj() {
3267 * Get the RFC 3066 code for this language object
3271 function getCode() {
3272 return $this->mCode
;
3276 * @param $code string
3278 function setCode( $code ) {
3279 $this->mCode
= $code;
3283 * Get the name of a file for a certain language code
3284 * @param $prefix string Prepend this to the filename
3285 * @param $code string Language code
3286 * @param $suffix string Append this to the filename
3287 * @return string $prefix . $mangledCode . $suffix
3289 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
3290 // Protect against path traversal
3291 if ( !Language
::isValidCode( $code )
3292 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
3294 throw new MWException( "Invalid language code \"$code\"" );
3297 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
3301 * Get the language code from a file name. Inverse of getFileName()
3302 * @param $filename string $prefix . $languageCode . $suffix
3303 * @param $prefix string Prefix before the language code
3304 * @param $suffix string Suffix after the language code
3305 * @return string Language code, or false if $prefix or $suffix isn't found
3307 static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
3309 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
3310 preg_quote( $suffix, '/' ) . '/', $filename, $m );
3311 if ( !count( $m ) ) {
3314 return str_replace( '_', '-', strtolower( $m[1] ) );
3318 * @param $code string
3321 static function getMessagesFileName( $code ) {
3323 return self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
3327 * @param $code string
3330 static function getClassFileName( $code ) {
3332 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
3336 * Get the first fallback for a given language.
3338 * @param $code string
3340 * @return false|string
3342 static function getFallbackFor( $code ) {
3343 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3346 $fallbacks = self
::getFallbacksFor( $code );
3347 $first = array_shift( $fallbacks );
3353 * Get the ordered list of fallback languages.
3356 * @param $code string Language code
3359 static function getFallbacksFor( $code ) {
3360 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3363 $v = self
::getLocalisationCache()->getItem( $code, 'fallback' );
3364 $v = array_map( 'trim', explode( ',', $v ) );
3365 if ( $v[count( $v ) - 1] !== 'en' ) {
3373 * Get all messages for a given language
3374 * WARNING: this may take a long time. If you just need all message *keys*
3375 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
3377 * @param $code string
3381 static function getMessagesFor( $code ) {
3382 return self
::getLocalisationCache()->getItem( $code, 'messages' );
3386 * Get a message for a given language
3388 * @param $key string
3389 * @param $code string
3393 static function getMessageFor( $key, $code ) {
3394 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
3398 * Get all message keys for a given language. This is a faster alternative to
3399 * array_keys( Language::getMessagesFor( $code ) )
3400 * @param $code string Language code
3401 * @return array of message keys (strings)
3403 static function getMessageKeysFor( $code ) {
3404 return self
::getLocalisationCache()->getSubItemList( $code, 'messages' );
3411 function fixVariableInNamespace( $talk ) {
3412 if ( strpos( $talk, '$1' ) === false ) {
3416 global $wgMetaNamespace;
3417 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
3419 # Allow grammar transformations
3420 # Allowing full message-style parsing would make simple requests
3421 # such as action=raw much more expensive than they need to be.
3422 # This will hopefully cover most cases.
3423 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
3424 array( &$this, 'replaceGrammarInNamespace' ), $talk );
3425 return str_replace( ' ', '_', $talk );
3432 function replaceGrammarInNamespace( $m ) {
3433 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
3437 * @throws MWException
3440 static function getCaseMaps() {
3441 static $wikiUpperChars, $wikiLowerChars;
3442 if ( isset( $wikiUpperChars ) ) {
3443 return array( $wikiUpperChars, $wikiLowerChars );
3446 wfProfileIn( __METHOD__
);
3447 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
3448 if ( $arr === false ) {
3449 throw new MWException(
3450 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
3452 $wikiUpperChars = $arr['wikiUpperChars'];
3453 $wikiLowerChars = $arr['wikiLowerChars'];
3454 wfProfileOut( __METHOD__
);
3455 return array( $wikiUpperChars, $wikiLowerChars );
3459 * Decode an expiry (block, protection, etc) which has come from the DB
3461 * @param $expiry String: Database expiry String
3462 * @param $format Bool|Int true to process using language functions, or TS_ constant
3463 * to return the expiry in a given timestamp
3466 public function formatExpiry( $expiry, $format = true ) {
3467 static $infinity, $infinityMsg;
3468 if( $infinity === null ){
3469 $infinityMsg = wfMessage( 'infiniteblock' );
3470 $infinity = wfGetDB( DB_SLAVE
)->getInfinity();
3473 if ( $expiry == '' ||
$expiry == $infinity ) {
3474 return $format === true
3478 return $format === true
3479 ?
$this->timeanddate( $expiry )
3480 : wfTimestamp( $format, $expiry );
3486 * @param $seconds int|float
3487 * @param $format String Optional, one of ("avoidseconds","avoidminutes"):
3488 * "avoidseconds" - don't mention seconds if $seconds >= 1 hour
3489 * "avoidminutes" - don't mention seconds/minutes if $seconds > 48 hours
3492 function formatTimePeriod( $seconds, $format = false ) {
3493 if ( round( $seconds * 10 ) < 100 ) {
3494 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
3495 $s .= $this->getMessageFromDB( 'seconds-abbrev' );
3496 } elseif ( round( $seconds ) < 60 ) {
3497 $s = $this->formatNum( round( $seconds ) );
3498 $s .= $this->getMessageFromDB( 'seconds-abbrev' );
3499 } elseif ( round( $seconds ) < 3600 ) {
3500 $minutes = floor( $seconds / 60 );
3501 $secondsPart = round( fmod( $seconds, 60 ) );
3502 if ( $secondsPart == 60 ) {
3506 $s = $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
3508 $s .= $this->formatNum( $secondsPart ) . $this->getMessageFromDB( 'seconds-abbrev' );
3509 } elseif ( round( $seconds ) <= 2*86400 ) {
3510 $hours = floor( $seconds / 3600 );
3511 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
3512 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
3513 if ( $secondsPart == 60 ) {
3517 if ( $minutes == 60 ) {
3521 $s = $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
3523 $s .= $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
3524 if ( !in_array( $format, array( 'avoidseconds', 'avoidminutes' ) ) ) {
3525 $s .= ' ' . $this->formatNum( $secondsPart ) .
3526 $this->getMessageFromDB( 'seconds-abbrev' );
3529 $days = floor( $seconds / 86400 );
3530 if ( $format === 'avoidminutes' ) {
3531 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
3532 if ( $hours == 24 ) {
3536 $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
3538 $s .= $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
3539 } elseif ( $format === 'avoidseconds' ) {
3540 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
3541 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
3542 if ( $minutes == 60 ) {
3546 if ( $hours == 24 ) {
3550 $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
3552 $s .= $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
3554 $s .= $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
3556 $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
3558 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
3568 function formatBitrate( $bps ) {
3569 $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
3571 return $this->formatNum( $bps ) . $units[0];
3573 $unitIndex = floor( log10( $bps ) / 3 );
3574 $mantissa = $bps / pow( 1000, $unitIndex );
3575 if ( $mantissa < 10 ) {
3576 $mantissa = round( $mantissa, 1 );
3578 $mantissa = round( $mantissa );
3580 return $this->formatNum( $mantissa ) . $units[$unitIndex];
3584 * Format a size in bytes for output, using an appropriate
3585 * unit (B, KB, MB or GB) according to the magnitude in question
3587 * @param $size int Size to format
3588 * @return string Plain text (not HTML)
3590 function formatSize( $size ) {
3591 // For small sizes no decimal places necessary
3593 if ( $size > 1024 ) {
3594 $size = $size / 1024;
3595 if ( $size > 1024 ) {
3596 $size = $size / 1024;
3597 // For MB and bigger two decimal places are smarter
3599 if ( $size > 1024 ) {
3600 $size = $size / 1024;
3601 $msg = 'size-gigabytes';
3603 $msg = 'size-megabytes';
3606 $msg = 'size-kilobytes';
3609 $msg = 'size-bytes';
3611 $size = round( $size, $round );
3612 $text = $this->getMessageFromDB( $msg );
3613 return str_replace( '$1', $this->formatNum( $size ), $text );
3617 * Get the conversion rule title, if any.
3621 function getConvRuleTitle() {
3622 return $this->mConverter
->getConvRuleTitle();