Localisation updates for core and extension messages from translatewiki.net
[mediawiki.git] / languages / Language.php
blob7b08891de77d4130c7b06ff1ce2493ee596b522e
1 <?php
2 /**
3 * Internationalisation code
5 * @file
6 * @ingroup Language
7 */
9 /**
10 * @defgroup Language Language
13 if ( !defined( 'MEDIAWIKI' ) ) {
14 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
15 exit( 1 );
18 # Read language names
19 global $wgLanguageNames;
20 require_once( dirname( __FILE__ ) . '/Names.php' );
22 if ( function_exists( 'mb_strtoupper' ) ) {
23 mb_internal_encoding( 'UTF-8' );
26 /**
27 * a fake language converter
29 * @ingroup Language
31 class FakeConverter {
32 var $mLang;
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; }
51 /**
52 * Internationalisation code
53 * @ingroup Language
55 class Language {
57 /**
58 * @var LanguageConverter
60 var $mConverter;
62 var $mVariants, $mCode, $mLoaded = false;
63 var $mMagicExtensions = array(), $mMagicHookDone = false;
64 private $mHtmlCode = null;
66 var $mNamespaceIds, $namespaceAliases;
67 var $dateFormatStrings = array();
68 var $mExtendedSpecialPageAliases;
70 public $namespaceNames;
72 /**
73 * ReplacementArray object caches
75 var $transformData = array();
77 /**
78 * @var LocalisationCache
80 static public $dataCache;
82 static public $mLangObjCache = array();
84 static public $mWeekdayMsgs = array(
85 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
86 'friday', 'saturday'
89 static public $mWeekdayAbbrevMsgs = array(
90 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
93 static public $mMonthMsgs = array(
94 'january', 'february', 'march', 'april', 'may_long', 'june',
95 'july', 'august', 'september', 'october', 'november',
96 'december'
98 static public $mMonthGenMsgs = array(
99 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
100 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
101 'december-gen'
103 static public $mMonthAbbrevMsgs = array(
104 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
105 'sep', 'oct', 'nov', 'dec'
108 static public $mIranianCalendarMonthMsgs = array(
109 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
110 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
111 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
112 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
115 static public $mHebrewCalendarMonthMsgs = array(
116 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
117 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
118 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
119 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
120 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
123 static public $mHebrewCalendarMonthGenMsgs = array(
124 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
125 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
126 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
127 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
128 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
131 static public $mHijriCalendarMonthMsgs = array(
132 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
133 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
134 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
135 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
139 * Get a cached language object for a given language code
140 * @param $code String
141 * @return Language
143 static function factory( $code ) {
144 if ( !isset( self::$mLangObjCache[$code] ) ) {
145 if ( count( self::$mLangObjCache ) > 10 ) {
146 // Don't keep a billion objects around, that's stupid.
147 self::$mLangObjCache = array();
149 self::$mLangObjCache[$code] = self::newFromCode( $code );
151 return self::$mLangObjCache[$code];
155 * Create a language object for a given language code
156 * @param $code String
157 * @return Language
159 protected static function newFromCode( $code ) {
160 // Protect against path traversal below
161 if ( !Language::isValidCode( $code )
162 || strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
164 throw new MWException( "Invalid language code \"$code\"" );
167 if ( !Language::isValidBuiltInCode( $code ) ) {
168 // It's not possible to customise this code with class files, so
169 // just return a Language object. This is to support uselang= hacks.
170 $lang = new Language;
171 $lang->setCode( $code );
172 return $lang;
175 // Check if there is a language class for the code
176 $class = self::classFromCode( $code );
177 self::preloadLanguageClass( $class );
178 if ( MWInit::classExists( $class ) ) {
179 $lang = new $class;
180 return $lang;
183 // Keep trying the fallback list until we find an existing class
184 $fallbacks = Language::getFallbacksFor( $code );
185 foreach ( $fallbacks as $fallbackCode ) {
186 if ( !Language::isValidBuiltInCode( $fallbackCode ) ) {
187 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
190 $class = self::classFromCode( $fallbackCode );
191 self::preloadLanguageClass( $class );
192 if ( MWInit::classExists( $class ) ) {
193 $lang = Language::newFromCode( $fallbackCode );
194 $lang->setCode( $code );
195 return $lang;
199 throw new MWException( "Invalid fallback sequence for language '$code'" );
203 * Returns true if a language code string is of a valid form, whether or
204 * not it exists. This includes codes which are used solely for
205 * customisation via the MediaWiki namespace.
207 * @param $code string
209 * @return bool
211 public static function isValidCode( $code ) {
212 return
213 strcspn( $code, ":/\\\000" ) === strlen( $code )
214 && !preg_match( Title::getTitleInvalidRegex(), $code );
218 * Returns true if a language code is of a valid form for the purposes of
219 * internal customisation of MediaWiki, via Messages*.php.
221 * @param $code string
223 * @since 1.18
224 * @return bool
226 public static function isValidBuiltInCode( $code ) {
227 return preg_match( '/^[a-z0-9-]+$/i', $code );
231 * @param $code
232 * @return String Name of the language class
234 public static function classFromCode( $code ) {
235 if ( $code == 'en' ) {
236 return 'Language';
237 } else {
238 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
243 * Includes language class files
245 * @param $class string Name of the language class
247 public static function preloadLanguageClass( $class ) {
248 global $IP;
250 if ( $class === 'Language' ) {
251 return;
254 if ( !defined( 'MW_COMPILED' ) ) {
255 // Preload base classes to work around APC/PHP5 bug
256 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
257 include_once( "$IP/languages/classes/$class.deps.php" );
259 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
260 include_once( "$IP/languages/classes/$class.php" );
266 * Get the LocalisationCache instance
268 * @return LocalisationCache
270 public static function getLocalisationCache() {
271 if ( is_null( self::$dataCache ) ) {
272 global $wgLocalisationCacheConf;
273 $class = $wgLocalisationCacheConf['class'];
274 self::$dataCache = new $class( $wgLocalisationCacheConf );
276 return self::$dataCache;
279 function __construct() {
280 $this->mConverter = new FakeConverter( $this );
281 // Set the code to the name of the descendant
282 if ( get_class( $this ) == 'Language' ) {
283 $this->mCode = 'en';
284 } else {
285 $this->mCode = str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
287 self::getLocalisationCache();
291 * Reduce memory usage
293 function __destruct() {
294 foreach ( $this as $name => $value ) {
295 unset( $this->$name );
300 * Hook which will be called if this is the content language.
301 * Descendants can use this to register hook functions or modify globals
303 function initContLang() { }
306 * Same as getFallbacksFor for current language.
307 * @return array|bool
308 * @deprecated in 1.19
310 function getFallbackLanguageCode() {
311 wfDeprecated( __METHOD__ );
312 return self::getFallbackFor( $this->mCode );
316 * @return array
317 * @since 1.19
319 function getFallbackLanguages() {
320 return self::getFallbacksFor( $this->mCode );
324 * Exports $wgBookstoreListEn
325 * @return array
327 function getBookstoreList() {
328 return self::$dataCache->getItem( $this->mCode, 'bookstoreList' );
332 * @return array
334 function getNamespaces() {
335 if ( is_null( $this->namespaceNames ) ) {
336 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
338 $this->namespaceNames = self::$dataCache->getItem( $this->mCode, 'namespaceNames' );
339 $validNamespaces = MWNamespace::getCanonicalNamespaces();
341 $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames + $validNamespaces;
343 $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace;
344 if ( $wgMetaNamespaceTalk ) {
345 $this->namespaceNames[NS_PROJECT_TALK] = $wgMetaNamespaceTalk;
346 } else {
347 $talk = $this->namespaceNames[NS_PROJECT_TALK];
348 $this->namespaceNames[NS_PROJECT_TALK] =
349 $this->fixVariableInNamespace( $talk );
352 # Sometimes a language will be localised but not actually exist on this wiki.
353 foreach ( $this->namespaceNames as $key => $text ) {
354 if ( !isset( $validNamespaces[$key] ) ) {
355 unset( $this->namespaceNames[$key] );
359 # The above mixing may leave namespaces out of canonical order.
360 # Re-order by namespace ID number...
361 ksort( $this->namespaceNames );
363 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames ) );
365 return $this->namespaceNames;
369 * A convenience function that returns the same thing as
370 * getNamespaces() except with the array values changed to ' '
371 * where it found '_', useful for producing output to be displayed
372 * e.g. in <select> forms.
374 * @return array
376 function getFormattedNamespaces() {
377 $ns = $this->getNamespaces();
378 foreach ( $ns as $k => $v ) {
379 $ns[$k] = strtr( $v, '_', ' ' );
381 return $ns;
385 * Get a namespace value by key
386 * <code>
387 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
388 * echo $mw_ns; // prints 'MediaWiki'
389 * </code>
391 * @param $index Int: the array key of the namespace to return
392 * @return mixed, string if the namespace value exists, otherwise false
394 function getNsText( $index ) {
395 $ns = $this->getNamespaces();
396 return isset( $ns[$index] ) ? $ns[$index] : false;
400 * A convenience function that returns the same thing as
401 * getNsText() except with '_' changed to ' ', useful for
402 * producing output.
404 * @param $index string
406 * @return array
408 function getFormattedNsText( $index ) {
409 $ns = $this->getNsText( $index );
410 return strtr( $ns, '_', ' ' );
414 * Returns gender-dependent namespace alias if available.
415 * @param $index Int: namespace index
416 * @param $gender String: gender key (male, female... )
417 * @return String
418 * @since 1.18
420 function getGenderNsText( $index, $gender ) {
421 global $wgExtraGenderNamespaces;
423 $ns = $wgExtraGenderNamespaces + self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
424 return isset( $ns[$index][$gender] ) ? $ns[$index][$gender] : $this->getNsText( $index );
428 * Whether this language makes distinguishes genders for example in
429 * namespaces.
430 * @return bool
431 * @since 1.18
433 function needsGenderDistinction() {
434 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
435 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
436 // $wgExtraGenderNamespaces overrides everything
437 return true;
438 } elseif ( isset( $wgExtraNamespaces[NS_USER] ) && isset( $wgExtraNamespaces[NS_USER_TALK] ) ) {
439 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
440 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
441 return false;
442 } else {
443 // Check what is in i18n files
444 $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
445 return count( $aliases ) > 0;
450 * Get a namespace key by value, case insensitive.
451 * Only matches namespace names for the current language, not the
452 * canonical ones defined in Namespace.php.
454 * @param $text String
455 * @return mixed An integer if $text is a valid value otherwise false
457 function getLocalNsIndex( $text ) {
458 $lctext = $this->lc( $text );
459 $ids = $this->getNamespaceIds();
460 return isset( $ids[$lctext] ) ? $ids[$lctext] : false;
464 * @return array
466 function getNamespaceAliases() {
467 if ( is_null( $this->namespaceAliases ) ) {
468 $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceAliases' );
469 if ( !$aliases ) {
470 $aliases = array();
471 } else {
472 foreach ( $aliases as $name => $index ) {
473 if ( $index === NS_PROJECT_TALK ) {
474 unset( $aliases[$name] );
475 $name = $this->fixVariableInNamespace( $name );
476 $aliases[$name] = $index;
481 global $wgExtraGenderNamespaces;
482 $genders = $wgExtraGenderNamespaces + (array)self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' );
483 foreach ( $genders as $index => $forms ) {
484 foreach ( $forms as $alias ) {
485 $aliases[$alias] = $index;
489 $this->namespaceAliases = $aliases;
491 return $this->namespaceAliases;
495 * @return array
497 function getNamespaceIds() {
498 if ( is_null( $this->mNamespaceIds ) ) {
499 global $wgNamespaceAliases;
500 # Put namespace names and aliases into a hashtable.
501 # If this is too slow, then we should arrange it so that it is done
502 # before caching. The catch is that at pre-cache time, the above
503 # class-specific fixup hasn't been done.
504 $this->mNamespaceIds = array();
505 foreach ( $this->getNamespaces() as $index => $name ) {
506 $this->mNamespaceIds[$this->lc( $name )] = $index;
508 foreach ( $this->getNamespaceAliases() as $name => $index ) {
509 $this->mNamespaceIds[$this->lc( $name )] = $index;
511 if ( $wgNamespaceAliases ) {
512 foreach ( $wgNamespaceAliases as $name => $index ) {
513 $this->mNamespaceIds[$this->lc( $name )] = $index;
517 return $this->mNamespaceIds;
521 * Get a namespace key by value, case insensitive. Canonical namespace
522 * names override custom ones defined for the current language.
524 * @param $text String
525 * @return mixed An integer if $text is a valid value otherwise false
527 function getNsIndex( $text ) {
528 $lctext = $this->lc( $text );
529 $ns = MWNamespace::getCanonicalIndex( $lctext );
530 if ( $ns !== null ) {
531 return $ns;
533 $ids = $this->getNamespaceIds();
534 return isset( $ids[$lctext] ) ? $ids[$lctext] : false;
538 * short names for language variants used for language conversion links.
540 * @param $code String
541 * @param $usemsg bool Use the "variantname-xyz" message if it exists
542 * @return string
544 function getVariantname( $code, $usemsg = true ) {
545 $msg = "variantname-$code";
546 list( $rootCode ) = explode( '-', $code );
547 if ( $usemsg && wfMessage( $msg )->exists() ) {
548 return $this->getMessageFromDB( $msg );
550 $name = self::getLanguageName( $code );
551 if ( $name ) {
552 return $name; # if it's defined as a language name, show that
553 } else {
554 # otherwise, output the language code
555 return $code;
560 * @param $name string
561 * @return string
563 function specialPage( $name ) {
564 $aliases = $this->getSpecialPageAliases();
565 if ( isset( $aliases[$name][0] ) ) {
566 $name = $aliases[$name][0];
568 return $this->getNsText( NS_SPECIAL ) . ':' . $name;
572 * @return array
574 function getQuickbarSettings() {
575 return array(
576 $this->getMessage( 'qbsettings-none' ),
577 $this->getMessage( 'qbsettings-fixedleft' ),
578 $this->getMessage( 'qbsettings-fixedright' ),
579 $this->getMessage( 'qbsettings-floatingleft' ),
580 $this->getMessage( 'qbsettings-floatingright' ),
581 $this->getMessage( 'qbsettings-directionality' )
586 * @return array
588 function getDatePreferences() {
589 return self::$dataCache->getItem( $this->mCode, 'datePreferences' );
593 * @return array
595 function getDateFormats() {
596 return self::$dataCache->getItem( $this->mCode, 'dateFormats' );
600 * @return array|string
602 function getDefaultDateFormat() {
603 $df = self::$dataCache->getItem( $this->mCode, 'defaultDateFormat' );
604 if ( $df === 'dmy or mdy' ) {
605 global $wgAmericanDates;
606 return $wgAmericanDates ? 'mdy' : 'dmy';
607 } else {
608 return $df;
613 * @return array
615 function getDatePreferenceMigrationMap() {
616 return self::$dataCache->getItem( $this->mCode, 'datePreferenceMigrationMap' );
620 * @param $image
621 * @return array|null
623 function getImageFile( $image ) {
624 return self::$dataCache->getSubitem( $this->mCode, 'imageFiles', $image );
628 * @return array
630 function getExtraUserToggles() {
631 return (array)self::$dataCache->getItem( $this->mCode, 'extraUserToggles' );
635 * @param $tog
636 * @return string
638 function getUserToggle( $tog ) {
639 return $this->getMessageFromDB( "tog-$tog" );
643 * Get native language names, indexed by code.
644 * Only those defined in MediaWiki, no other data like CLDR.
645 * If $customisedOnly is true, only returns codes with a messages file
647 * @param $customisedOnly bool
649 * @return array
651 public static function getLanguageNames( $customisedOnly = false ) {
652 global $wgExtraLanguageNames;
653 static $coreLanguageNames;
655 if ( $coreLanguageNames === null ) {
656 include( MWInit::compiledPath( 'languages/Names.php' ) );
659 $allNames = $wgExtraLanguageNames + $coreLanguageNames;
660 if ( !$customisedOnly ) {
661 return $allNames;
664 $names = array();
665 // We do this using a foreach over the codes instead of a directory
666 // loop so that messages files in extensions will work correctly.
667 foreach ( $allNames as $code => $value ) {
668 if ( is_readable( self::getMessagesFileName( $code ) ) ) {
669 $names[$code] = $allNames[$code];
672 return $names;
676 * Get translated language names. This is done on best effort and
677 * by default this is exactly the same as Language::getLanguageNames.
678 * The CLDR extension provides translated names.
679 * @param $code String Language code.
680 * @return Array language code => language name
681 * @since 1.18.0
683 public static function getTranslatedLanguageNames( $code ) {
684 $names = array();
685 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
687 foreach ( self::getLanguageNames() as $code => $name ) {
688 if ( !isset( $names[$code] ) ) $names[$code] = $name;
691 return $names;
695 * Get a message from the MediaWiki namespace.
697 * @param $msg String: message name
698 * @return string
700 function getMessageFromDB( $msg ) {
701 return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
705 * Get the native language name of $code.
706 * Only if defined in MediaWiki, no other data like CLDR.
707 * @param $code string
708 * @return string
710 function getLanguageName( $code ) {
711 $names = self::getLanguageNames();
712 if ( !array_key_exists( $code, $names ) ) {
713 return '';
715 return $names[$code];
719 * @param $key string
720 * @return string
722 function getMonthName( $key ) {
723 return $this->getMessageFromDB( self::$mMonthMsgs[$key - 1] );
727 * @return array
729 function getMonthNamesArray() {
730 $monthNames = array( '' );
731 for ( $i = 1; $i < 13; $i++ ) {
732 $monthNames[] = $this->getMonthName( $i );
734 return $monthNames;
738 * @param $key string
739 * @return string
741 function getMonthNameGen( $key ) {
742 return $this->getMessageFromDB( self::$mMonthGenMsgs[$key - 1] );
746 * @param $key string
747 * @return string
749 function getMonthAbbreviation( $key ) {
750 return $this->getMessageFromDB( self::$mMonthAbbrevMsgs[$key - 1] );
754 * @return array
756 function getMonthAbbreviationsArray() {
757 $monthNames = array( '' );
758 for ( $i = 1; $i < 13; $i++ ) {
759 $monthNames[] = $this->getMonthAbbreviation( $i );
761 return $monthNames;
765 * @param $key string
766 * @return string
768 function getWeekdayName( $key ) {
769 return $this->getMessageFromDB( self::$mWeekdayMsgs[$key - 1] );
773 * @param $key string
774 * @return string
776 function getWeekdayAbbreviation( $key ) {
777 return $this->getMessageFromDB( self::$mWeekdayAbbrevMsgs[$key - 1] );
781 * @param $key string
782 * @return string
784 function getIranianCalendarMonthName( $key ) {
785 return $this->getMessageFromDB( self::$mIranianCalendarMonthMsgs[$key - 1] );
789 * @param $key string
790 * @return string
792 function getHebrewCalendarMonthName( $key ) {
793 return $this->getMessageFromDB( self::$mHebrewCalendarMonthMsgs[$key - 1] );
797 * @param $key string
798 * @return string
800 function getHebrewCalendarMonthNameGen( $key ) {
801 return $this->getMessageFromDB( self::$mHebrewCalendarMonthGenMsgs[$key - 1] );
805 * @param $key string
806 * @return string
808 function getHijriCalendarMonthName( $key ) {
809 return $this->getMessageFromDB( self::$mHijriCalendarMonthMsgs[$key - 1] );
813 * This is a workalike of PHP's date() function, but with better
814 * internationalisation, a reduced set of format characters, and a better
815 * escaping format.
817 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
818 * PHP manual for definitions. There are a number of extensions, which
819 * start with "x":
821 * xn Do not translate digits of the next numeric format character
822 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
823 * xr Use roman numerals for the next numeric format character
824 * xh Use hebrew numerals for the next numeric format character
825 * xx Literal x
826 * xg Genitive month name
828 * xij j (day number) in Iranian calendar
829 * xiF F (month name) in Iranian calendar
830 * xin n (month number) in Iranian calendar
831 * xiy y (two digit year) in Iranian calendar
832 * xiY Y (full year) in Iranian calendar
834 * xjj j (day number) in Hebrew calendar
835 * xjF F (month name) in Hebrew calendar
836 * xjt t (days in month) in Hebrew calendar
837 * xjx xg (genitive month name) in Hebrew calendar
838 * xjn n (month number) in Hebrew calendar
839 * xjY Y (full year) in Hebrew calendar
841 * xmj j (day number) in Hijri calendar
842 * xmF F (month name) in Hijri calendar
843 * xmn n (month number) in Hijri calendar
844 * xmY Y (full year) in Hijri calendar
846 * xkY Y (full year) in Thai solar calendar. Months and days are
847 * identical to the Gregorian calendar
848 * xoY Y (full year) in Minguo calendar or Juche year.
849 * Months and days are identical to the
850 * Gregorian calendar
851 * xtY Y (full year) in Japanese nengo. Months and days are
852 * identical to the Gregorian calendar
854 * Characters enclosed in double quotes will be considered literal (with
855 * the quotes themselves removed). Unmatched quotes will be considered
856 * literal quotes. Example:
858 * "The month is" F => The month is January
859 * i's" => 20'11"
861 * Backslash escaping is also supported.
863 * Input timestamp is assumed to be pre-normalized to the desired local
864 * time zone, if any.
866 * @param $format String
867 * @param $ts String: 14-character timestamp
868 * YYYYMMDDHHMMSS
869 * 01234567890123
870 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
872 * @return string
874 function sprintfDate( $format, $ts ) {
875 $s = '';
876 $raw = false;
877 $roman = false;
878 $hebrewNum = false;
879 $unix = false;
880 $rawToggle = false;
881 $iranian = false;
882 $hebrew = false;
883 $hijri = false;
884 $thai = false;
885 $minguo = false;
886 $tenno = false;
887 for ( $p = 0; $p < strlen( $format ); $p++ ) {
888 $num = false;
889 $code = $format[$p];
890 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
891 $code .= $format[++$p];
894 if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' || $code == 'xm' || $code == 'xo' || $code == 'xt' ) && $p < strlen( $format ) - 1 ) {
895 $code .= $format[++$p];
898 switch ( $code ) {
899 case 'xx':
900 $s .= 'x';
901 break;
902 case 'xn':
903 $raw = true;
904 break;
905 case 'xN':
906 $rawToggle = !$rawToggle;
907 break;
908 case 'xr':
909 $roman = true;
910 break;
911 case 'xh':
912 $hebrewNum = true;
913 break;
914 case 'xg':
915 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
916 break;
917 case 'xjx':
918 if ( !$hebrew ) $hebrew = self::tsToHebrew( $ts );
919 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
920 break;
921 case 'd':
922 $num = substr( $ts, 6, 2 );
923 break;
924 case 'D':
925 if ( !$unix ) $unix = wfTimestamp( TS_UNIX, $ts );
926 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) + 1 );
927 break;
928 case 'j':
929 $num = intval( substr( $ts, 6, 2 ) );
930 break;
931 case 'xij':
932 if ( !$iranian ) {
933 $iranian = self::tsToIranian( $ts );
935 $num = $iranian[2];
936 break;
937 case 'xmj':
938 if ( !$hijri ) {
939 $hijri = self::tsToHijri( $ts );
941 $num = $hijri[2];
942 break;
943 case 'xjj':
944 if ( !$hebrew ) {
945 $hebrew = self::tsToHebrew( $ts );
947 $num = $hebrew[2];
948 break;
949 case 'l':
950 if ( !$unix ) {
951 $unix = wfTimestamp( TS_UNIX, $ts );
953 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) + 1 );
954 break;
955 case 'N':
956 if ( !$unix ) {
957 $unix = wfTimestamp( TS_UNIX, $ts );
959 $w = gmdate( 'w', $unix );
960 $num = $w ? $w : 7;
961 break;
962 case 'w':
963 if ( !$unix ) {
964 $unix = wfTimestamp( TS_UNIX, $ts );
966 $num = gmdate( 'w', $unix );
967 break;
968 case 'z':
969 if ( !$unix ) {
970 $unix = wfTimestamp( TS_UNIX, $ts );
972 $num = gmdate( 'z', $unix );
973 break;
974 case 'W':
975 if ( !$unix ) {
976 $unix = wfTimestamp( TS_UNIX, $ts );
978 $num = gmdate( 'W', $unix );
979 break;
980 case 'F':
981 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
982 break;
983 case 'xiF':
984 if ( !$iranian ) {
985 $iranian = self::tsToIranian( $ts );
987 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
988 break;
989 case 'xmF':
990 if ( !$hijri ) {
991 $hijri = self::tsToHijri( $ts );
993 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
994 break;
995 case 'xjF':
996 if ( !$hebrew ) {
997 $hebrew = self::tsToHebrew( $ts );
999 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1000 break;
1001 case 'm':
1002 $num = substr( $ts, 4, 2 );
1003 break;
1004 case 'M':
1005 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1006 break;
1007 case 'n':
1008 $num = intval( substr( $ts, 4, 2 ) );
1009 break;
1010 case 'xin':
1011 if ( !$iranian ) {
1012 $iranian = self::tsToIranian( $ts );
1014 $num = $iranian[1];
1015 break;
1016 case 'xmn':
1017 if ( !$hijri ) {
1018 $hijri = self::tsToHijri ( $ts );
1020 $num = $hijri[1];
1021 break;
1022 case 'xjn':
1023 if ( !$hebrew ) {
1024 $hebrew = self::tsToHebrew( $ts );
1026 $num = $hebrew[1];
1027 break;
1028 case 't':
1029 if ( !$unix ) {
1030 $unix = wfTimestamp( TS_UNIX, $ts );
1032 $num = gmdate( 't', $unix );
1033 break;
1034 case 'xjt':
1035 if ( !$hebrew ) {
1036 $hebrew = self::tsToHebrew( $ts );
1038 $num = $hebrew[3];
1039 break;
1040 case 'L':
1041 if ( !$unix ) {
1042 $unix = wfTimestamp( TS_UNIX, $ts );
1044 $num = gmdate( 'L', $unix );
1045 break;
1046 case 'o':
1047 if ( !$unix ) {
1048 $unix = wfTimestamp( TS_UNIX, $ts );
1050 $num = gmdate( 'o', $unix );
1051 break;
1052 case 'Y':
1053 $num = substr( $ts, 0, 4 );
1054 break;
1055 case 'xiY':
1056 if ( !$iranian ) {
1057 $iranian = self::tsToIranian( $ts );
1059 $num = $iranian[0];
1060 break;
1061 case 'xmY':
1062 if ( !$hijri ) {
1063 $hijri = self::tsToHijri( $ts );
1065 $num = $hijri[0];
1066 break;
1067 case 'xjY':
1068 if ( !$hebrew ) {
1069 $hebrew = self::tsToHebrew( $ts );
1071 $num = $hebrew[0];
1072 break;
1073 case 'xkY':
1074 if ( !$thai ) {
1075 $thai = self::tsToYear( $ts, 'thai' );
1077 $num = $thai[0];
1078 break;
1079 case 'xoY':
1080 if ( !$minguo ) {
1081 $minguo = self::tsToYear( $ts, 'minguo' );
1083 $num = $minguo[0];
1084 break;
1085 case 'xtY':
1086 if ( !$tenno ) {
1087 $tenno = self::tsToYear( $ts, 'tenno' );
1089 $num = $tenno[0];
1090 break;
1091 case 'y':
1092 $num = substr( $ts, 2, 2 );
1093 break;
1094 case 'xiy':
1095 if ( !$iranian ) {
1096 $iranian = self::tsToIranian( $ts );
1098 $num = substr( $iranian[0], -2 );
1099 break;
1100 case 'a':
1101 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
1102 break;
1103 case 'A':
1104 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
1105 break;
1106 case 'g':
1107 $h = substr( $ts, 8, 2 );
1108 $num = $h % 12 ? $h % 12 : 12;
1109 break;
1110 case 'G':
1111 $num = intval( substr( $ts, 8, 2 ) );
1112 break;
1113 case 'h':
1114 $h = substr( $ts, 8, 2 );
1115 $num = sprintf( '%02d', $h % 12 ? $h % 12 : 12 );
1116 break;
1117 case 'H':
1118 $num = substr( $ts, 8, 2 );
1119 break;
1120 case 'i':
1121 $num = substr( $ts, 10, 2 );
1122 break;
1123 case 's':
1124 $num = substr( $ts, 12, 2 );
1125 break;
1126 case 'c':
1127 if ( !$unix ) {
1128 $unix = wfTimestamp( TS_UNIX, $ts );
1130 $s .= gmdate( 'c', $unix );
1131 break;
1132 case 'r':
1133 if ( !$unix ) {
1134 $unix = wfTimestamp( TS_UNIX, $ts );
1136 $s .= gmdate( 'r', $unix );
1137 break;
1138 case 'U':
1139 if ( !$unix ) {
1140 $unix = wfTimestamp( TS_UNIX, $ts );
1142 $num = $unix;
1143 break;
1144 case '\\':
1145 # Backslash escaping
1146 if ( $p < strlen( $format ) - 1 ) {
1147 $s .= $format[++$p];
1148 } else {
1149 $s .= '\\';
1151 break;
1152 case '"':
1153 # Quoted literal
1154 if ( $p < strlen( $format ) - 1 ) {
1155 $endQuote = strpos( $format, '"', $p + 1 );
1156 if ( $endQuote === false ) {
1157 # No terminating quote, assume literal "
1158 $s .= '"';
1159 } else {
1160 $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
1161 $p = $endQuote;
1163 } else {
1164 # Quote at end of string, assume literal "
1165 $s .= '"';
1167 break;
1168 default:
1169 $s .= $format[$p];
1171 if ( $num !== false ) {
1172 if ( $rawToggle || $raw ) {
1173 $s .= $num;
1174 $raw = false;
1175 } elseif ( $roman ) {
1176 $s .= self::romanNumeral( $num );
1177 $roman = false;
1178 } elseif ( $hebrewNum ) {
1179 $s .= self::hebrewNumeral( $num );
1180 $hebrewNum = false;
1181 } else {
1182 $s .= $this->formatNum( $num, true );
1186 return $s;
1189 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1190 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1193 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1194 * Gregorian dates to Iranian dates. Originally written in C, it
1195 * is released under the terms of GNU Lesser General Public
1196 * License. Conversion to PHP was performed by Niklas Laxström.
1198 * Link: http://www.farsiweb.info/jalali/jalali.c
1200 * @param $ts string
1202 * @return string
1204 private static function tsToIranian( $ts ) {
1205 $gy = substr( $ts, 0, 4 ) -1600;
1206 $gm = substr( $ts, 4, 2 ) -1;
1207 $gd = substr( $ts, 6, 2 ) -1;
1209 # Days passed from the beginning (including leap years)
1210 $gDayNo = 365 * $gy
1211 + floor( ( $gy + 3 ) / 4 )
1212 - floor( ( $gy + 99 ) / 100 )
1213 + floor( ( $gy + 399 ) / 400 );
1215 // Add days of the past months of this year
1216 for ( $i = 0; $i < $gm; $i++ ) {
1217 $gDayNo += self::$GREG_DAYS[$i];
1220 // Leap years
1221 if ( $gm > 1 && ( ( $gy % 4 === 0 && $gy % 100 !== 0 || ( $gy % 400 == 0 ) ) ) ) {
1222 $gDayNo++;
1225 // Days passed in current month
1226 $gDayNo += (int)$gd;
1228 $jDayNo = $gDayNo - 79;
1230 $jNp = floor( $jDayNo / 12053 );
1231 $jDayNo %= 12053;
1233 $jy = 979 + 33 * $jNp + 4 * floor( $jDayNo / 1461 );
1234 $jDayNo %= 1461;
1236 if ( $jDayNo >= 366 ) {
1237 $jy += floor( ( $jDayNo - 1 ) / 365 );
1238 $jDayNo = floor( ( $jDayNo - 1 ) % 365 );
1241 for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
1242 $jDayNo -= self::$IRANIAN_DAYS[$i];
1245 $jm = $i + 1;
1246 $jd = $jDayNo + 1;
1248 return array( $jy, $jm, $jd );
1252 * Converting Gregorian dates to Hijri dates.
1254 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1256 * @link http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1258 * @param $ts string
1260 * @return string
1262 private static function tsToHijri( $ts ) {
1263 $year = substr( $ts, 0, 4 );
1264 $month = substr( $ts, 4, 2 );
1265 $day = substr( $ts, 6, 2 );
1267 $zyr = $year;
1268 $zd = $day;
1269 $zm = $month;
1270 $zy = $zyr;
1272 if (
1273 ( $zy > 1582 ) || ( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1274 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1277 $zjd = (int)( ( 1461 * ( $zy + 4800 + (int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1278 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1279 (int)( ( 3 * (int)( ( ( $zy + 4900 + (int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1280 $zd - 32075;
1281 } else {
1282 $zjd = 367 * $zy - (int)( ( 7 * ( $zy + 5001 + (int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1283 (int)( ( 275 * $zm ) / 9 ) + $zd + 1729777;
1286 $zl = $zjd -1948440 + 10632;
1287 $zn = (int)( ( $zl - 1 ) / 10631 );
1288 $zl = $zl - 10631 * $zn + 354;
1289 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) + ( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1290 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) + 29;
1291 $zm = (int)( ( 24 * $zl ) / 709 );
1292 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1293 $zy = 30 * $zn + $zj - 30;
1295 return array( $zy, $zm, $zd );
1299 * Converting Gregorian dates to Hebrew dates.
1301 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1302 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1303 * to translate the relevant functions into PHP and release them under
1304 * GNU GPL.
1306 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1307 * and Adar II is 14. In a non-leap year, Adar is 6.
1309 * @param $ts string
1311 * @return string
1313 private static function tsToHebrew( $ts ) {
1314 # Parse date
1315 $year = substr( $ts, 0, 4 );
1316 $month = substr( $ts, 4, 2 );
1317 $day = substr( $ts, 6, 2 );
1319 # Calculate Hebrew year
1320 $hebrewYear = $year + 3760;
1322 # Month number when September = 1, August = 12
1323 $month += 4;
1324 if ( $month > 12 ) {
1325 # Next year
1326 $month -= 12;
1327 $year++;
1328 $hebrewYear++;
1331 # Calculate day of year from 1 September
1332 $dayOfYear = $day;
1333 for ( $i = 1; $i < $month; $i++ ) {
1334 if ( $i == 6 ) {
1335 # February
1336 $dayOfYear += 28;
1337 # Check if the year is leap
1338 if ( $year % 400 == 0 || ( $year % 4 == 0 && $year % 100 > 0 ) ) {
1339 $dayOfYear++;
1341 } elseif ( $i == 8 || $i == 10 || $i == 1 || $i == 3 ) {
1342 $dayOfYear += 30;
1343 } else {
1344 $dayOfYear += 31;
1348 # Calculate the start of the Hebrew year
1349 $start = self::hebrewYearStart( $hebrewYear );
1351 # Calculate next year's start
1352 if ( $dayOfYear <= $start ) {
1353 # Day is before the start of the year - it is the previous year
1354 # Next year's start
1355 $nextStart = $start;
1356 # Previous year
1357 $year--;
1358 $hebrewYear--;
1359 # Add days since previous year's 1 September
1360 $dayOfYear += 365;
1361 if ( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
1362 # Leap year
1363 $dayOfYear++;
1365 # Start of the new (previous) year
1366 $start = self::hebrewYearStart( $hebrewYear );
1367 } else {
1368 # Next year's start
1369 $nextStart = self::hebrewYearStart( $hebrewYear + 1 );
1372 # Calculate Hebrew day of year
1373 $hebrewDayOfYear = $dayOfYear - $start;
1375 # Difference between year's days
1376 $diff = $nextStart - $start;
1377 # Add 12 (or 13 for leap years) days to ignore the difference between
1378 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1379 # difference is only about the year type
1380 if ( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
1381 $diff += 13;
1382 } else {
1383 $diff += 12;
1386 # Check the year pattern, and is leap year
1387 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1388 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1389 # and non-leap years
1390 $yearPattern = $diff % 30;
1391 # Check if leap year
1392 $isLeap = $diff >= 30;
1394 # Calculate day in the month from number of day in the Hebrew year
1395 # Don't check Adar - if the day is not in Adar, we will stop before;
1396 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1397 $hebrewDay = $hebrewDayOfYear;
1398 $hebrewMonth = 1;
1399 $days = 0;
1400 while ( $hebrewMonth <= 12 ) {
1401 # Calculate days in this month
1402 if ( $isLeap && $hebrewMonth == 6 ) {
1403 # Adar in a leap year
1404 if ( $isLeap ) {
1405 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1406 $days = 30;
1407 if ( $hebrewDay <= $days ) {
1408 # Day in Adar I
1409 $hebrewMonth = 13;
1410 } else {
1411 # Subtract the days of Adar I
1412 $hebrewDay -= $days;
1413 # Try Adar II
1414 $days = 29;
1415 if ( $hebrewDay <= $days ) {
1416 # Day in Adar II
1417 $hebrewMonth = 14;
1421 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1422 # Cheshvan in a complete year (otherwise as the rule below)
1423 $days = 30;
1424 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1425 # Kislev in an incomplete year (otherwise as the rule below)
1426 $days = 29;
1427 } else {
1428 # Odd months have 30 days, even have 29
1429 $days = 30 - ( $hebrewMonth - 1 ) % 2;
1431 if ( $hebrewDay <= $days ) {
1432 # In the current month
1433 break;
1434 } else {
1435 # Subtract the days of the current month
1436 $hebrewDay -= $days;
1437 # Try in the next month
1438 $hebrewMonth++;
1442 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1446 * This calculates the Hebrew year start, as days since 1 September.
1447 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1448 * Used for Hebrew date.
1450 * @param $year int
1452 * @return string
1454 private static function hebrewYearStart( $year ) {
1455 $a = intval( ( 12 * ( $year - 1 ) + 17 ) % 19 );
1456 $b = intval( ( $year - 1 ) % 4 );
1457 $m = 32.044093161144 + 1.5542417966212 * $a + $b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1458 if ( $m < 0 ) {
1459 $m--;
1461 $Mar = intval( $m );
1462 if ( $m < 0 ) {
1463 $m++;
1465 $m -= $Mar;
1467 $c = intval( ( $Mar + 3 * ( $year - 1 ) + 5 * $b + 5 ) % 7 );
1468 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1469 $Mar++;
1470 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1471 $Mar += 2;
1472 } elseif ( $c == 2 || $c == 4 || $c == 6 ) {
1473 $Mar++;
1476 $Mar += intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1477 return $Mar;
1481 * Algorithm to convert Gregorian dates to Thai solar dates,
1482 * Minguo dates or Minguo dates.
1484 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1485 * http://en.wikipedia.org/wiki/Minguo_calendar
1486 * http://en.wikipedia.org/wiki/Japanese_era_name
1488 * @param $ts String: 14-character timestamp
1489 * @param $cName String: calender name
1490 * @return Array: converted year, month, day
1492 private static function tsToYear( $ts, $cName ) {
1493 $gy = substr( $ts, 0, 4 );
1494 $gm = substr( $ts, 4, 2 );
1495 $gd = substr( $ts, 6, 2 );
1497 if ( !strcmp( $cName, 'thai' ) ) {
1498 # Thai solar dates
1499 # Add 543 years to the Gregorian calendar
1500 # Months and days are identical
1501 $gy_offset = $gy + 543;
1502 } elseif ( ( !strcmp( $cName, 'minguo' ) ) || !strcmp( $cName, 'juche' ) ) {
1503 # Minguo dates
1504 # Deduct 1911 years from the Gregorian calendar
1505 # Months and days are identical
1506 $gy_offset = $gy - 1911;
1507 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1508 # Nengō dates up to Meiji period
1509 # Deduct years from the Gregorian calendar
1510 # depending on the nengo periods
1511 # Months and days are identical
1512 if ( ( $gy < 1912 ) || ( ( $gy == 1912 ) && ( $gm < 7 ) ) || ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1513 # Meiji period
1514 $gy_gannen = $gy - 1868 + 1;
1515 $gy_offset = $gy_gannen;
1516 if ( $gy_gannen == 1 ) {
1517 $gy_offset = '元';
1519 $gy_offset = '明治' . $gy_offset;
1520 } elseif (
1521 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1522 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1523 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1524 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1525 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1528 # Taishō period
1529 $gy_gannen = $gy - 1912 + 1;
1530 $gy_offset = $gy_gannen;
1531 if ( $gy_gannen == 1 ) {
1532 $gy_offset = '元';
1534 $gy_offset = '大正' . $gy_offset;
1535 } elseif (
1536 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1537 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1538 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1541 # Shōwa period
1542 $gy_gannen = $gy - 1926 + 1;
1543 $gy_offset = $gy_gannen;
1544 if ( $gy_gannen == 1 ) {
1545 $gy_offset = '元';
1547 $gy_offset = '昭和' . $gy_offset;
1548 } else {
1549 # Heisei period
1550 $gy_gannen = $gy - 1989 + 1;
1551 $gy_offset = $gy_gannen;
1552 if ( $gy_gannen == 1 ) {
1553 $gy_offset = '元';
1555 $gy_offset = '平成' . $gy_offset;
1557 } else {
1558 $gy_offset = $gy;
1561 return array( $gy_offset, $gm, $gd );
1565 * Roman number formatting up to 3000
1567 * @param $num int
1569 * @return string
1571 static function romanNumeral( $num ) {
1572 static $table = array(
1573 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1574 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1575 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1576 array( '', 'M', 'MM', 'MMM' )
1579 $num = intval( $num );
1580 if ( $num > 3000 || $num <= 0 ) {
1581 return $num;
1584 $s = '';
1585 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1586 if ( $num >= $pow10 ) {
1587 $s .= $table[$i][(int)floor( $num / $pow10 )];
1589 $num = $num % $pow10;
1591 return $s;
1595 * Hebrew Gematria number formatting up to 9999
1597 * @param $num int
1599 * @return string
1601 static function hebrewNumeral( $num ) {
1602 static $table = array(
1603 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1604 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1605 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1606 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1609 $num = intval( $num );
1610 if ( $num > 9999 || $num <= 0 ) {
1611 return $num;
1614 $s = '';
1615 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1616 if ( $num >= $pow10 ) {
1617 if ( $num == 15 || $num == 16 ) {
1618 $s .= $table[0][9] . $table[0][$num - 9];
1619 $num = 0;
1620 } else {
1621 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1622 if ( $pow10 == 1000 ) {
1623 $s .= "'";
1627 $num = $num % $pow10;
1629 if ( strlen( $s ) == 2 ) {
1630 $str = $s . "'";
1631 } else {
1632 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1633 $str .= substr( $s, strlen( $s ) - 2, 2 );
1635 $start = substr( $str, 0, strlen( $str ) - 2 );
1636 $end = substr( $str, strlen( $str ) - 2 );
1637 switch( $end ) {
1638 case 'כ':
1639 $str = $start . 'ך';
1640 break;
1641 case 'מ':
1642 $str = $start . 'ם';
1643 break;
1644 case 'נ':
1645 $str = $start . 'ן';
1646 break;
1647 case 'פ':
1648 $str = $start . 'ף';
1649 break;
1650 case 'צ':
1651 $str = $start . 'ץ';
1652 break;
1654 return $str;
1658 * Used by date() and time() to adjust the time output.
1660 * @param $ts Int the time in date('YmdHis') format
1661 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1662 * get user timecorrection setting)
1663 * @return int
1665 function userAdjust( $ts, $tz = false ) {
1666 global $wgUser, $wgLocalTZoffset;
1668 if ( $tz === false ) {
1669 $tz = $wgUser->getOption( 'timecorrection' );
1672 $data = explode( '|', $tz, 3 );
1674 if ( $data[0] == 'ZoneInfo' ) {
1675 wfSuppressWarnings();
1676 $userTZ = timezone_open( $data[2] );
1677 wfRestoreWarnings();
1678 if ( $userTZ !== false ) {
1679 $date = date_create( $ts, timezone_open( 'UTC' ) );
1680 date_timezone_set( $date, $userTZ );
1681 $date = date_format( $date, 'YmdHis' );
1682 return $date;
1684 # Unrecognized timezone, default to 'Offset' with the stored offset.
1685 $data[0] = 'Offset';
1688 $minDiff = 0;
1689 if ( $data[0] == 'System' || $tz == '' ) {
1690 #  Global offset in minutes.
1691 if ( isset( $wgLocalTZoffset ) ) {
1692 $minDiff = $wgLocalTZoffset;
1694 } elseif ( $data[0] == 'Offset' ) {
1695 $minDiff = intval( $data[1] );
1696 } else {
1697 $data = explode( ':', $tz );
1698 if ( count( $data ) == 2 ) {
1699 $data[0] = intval( $data[0] );
1700 $data[1] = intval( $data[1] );
1701 $minDiff = abs( $data[0] ) * 60 + $data[1];
1702 if ( $data[0] < 0 ) {
1703 $minDiff = -$minDiff;
1705 } else {
1706 $minDiff = intval( $data[0] ) * 60;
1710 # No difference ? Return time unchanged
1711 if ( 0 == $minDiff ) {
1712 return $ts;
1715 wfSuppressWarnings(); // E_STRICT system time bitching
1716 # Generate an adjusted date; take advantage of the fact that mktime
1717 # will normalize out-of-range values so we don't have to split $minDiff
1718 # into hours and minutes.
1719 $t = mktime( (
1720 (int)substr( $ts, 8, 2 ) ), # Hours
1721 (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
1722 (int)substr( $ts, 12, 2 ), # Seconds
1723 (int)substr( $ts, 4, 2 ), # Month
1724 (int)substr( $ts, 6, 2 ), # Day
1725 (int)substr( $ts, 0, 4 ) ); # Year
1727 $date = date( 'YmdHis', $t );
1728 wfRestoreWarnings();
1730 return $date;
1734 * This is meant to be used by time(), date(), and timeanddate() to get
1735 * the date preference they're supposed to use, it should be used in
1736 * all children.
1738 *<code>
1739 * function timeanddate([...], $format = true) {
1740 * $datePreference = $this->dateFormat($format);
1741 * [...]
1743 *</code>
1745 * @param $usePrefs Mixed: if true, the user's preference is used
1746 * if false, the site/language default is used
1747 * if int/string, assumed to be a format.
1748 * @return string
1750 function dateFormat( $usePrefs = true ) {
1751 global $wgUser;
1753 if ( is_bool( $usePrefs ) ) {
1754 if ( $usePrefs ) {
1755 $datePreference = $wgUser->getDatePreference();
1756 } else {
1757 $datePreference = (string)User::getDefaultOption( 'date' );
1759 } else {
1760 $datePreference = (string)$usePrefs;
1763 // return int
1764 if ( $datePreference == '' ) {
1765 return 'default';
1768 return $datePreference;
1772 * Get a format string for a given type and preference
1773 * @param $type string May be date, time or both
1774 * @param $pref string The format name as it appears in Messages*.php
1776 * @return string
1778 function getDateFormatString( $type, $pref ) {
1779 if ( !isset( $this->dateFormatStrings[$type][$pref] ) ) {
1780 if ( $pref == 'default' ) {
1781 $pref = $this->getDefaultDateFormat();
1782 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
1783 } else {
1784 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
1785 if ( is_null( $df ) ) {
1786 $pref = $this->getDefaultDateFormat();
1787 $df = self::$dataCache->getSubitem( $this->mCode, 'dateFormats', "$pref $type" );
1790 $this->dateFormatStrings[$type][$pref] = $df;
1792 return $this->dateFormatStrings[$type][$pref];
1796 * @param $ts Mixed: the time format which needs to be turned into a
1797 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1798 * @param $adj Bool: whether to adjust the time output according to the
1799 * user configured offset ($timecorrection)
1800 * @param $format Mixed: true to use user's date format preference
1801 * @param $timecorrection String|bool the time offset as returned by
1802 * validateTimeZone() in Special:Preferences
1803 * @return string
1805 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1806 $ts = wfTimestamp( TS_MW, $ts );
1807 if ( $adj ) {
1808 $ts = $this->userAdjust( $ts, $timecorrection );
1810 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1811 return $this->sprintfDate( $df, $ts );
1815 * @param $ts Mixed: the time format which needs to be turned into a
1816 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1817 * @param $adj Bool: whether to adjust the time output according to the
1818 * user configured offset ($timecorrection)
1819 * @param $format Mixed: true to use user's date format preference
1820 * @param $timecorrection String|bool the time offset as returned by
1821 * validateTimeZone() in Special:Preferences
1822 * @return string
1824 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1825 $ts = wfTimestamp( TS_MW, $ts );
1826 if ( $adj ) {
1827 $ts = $this->userAdjust( $ts, $timecorrection );
1829 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1830 return $this->sprintfDate( $df, $ts );
1834 * @param $ts Mixed: the time format which needs to be turned into a
1835 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1836 * @param $adj Bool: whether to adjust the time output according to the
1837 * user configured offset ($timecorrection)
1838 * @param $format Mixed: what format to return, if it's false output the
1839 * default one (default true)
1840 * @param $timecorrection String|bool the time offset as returned by
1841 * validateTimeZone() in Special:Preferences
1842 * @return string
1844 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1845 $ts = wfTimestamp( TS_MW, $ts );
1846 if ( $adj ) {
1847 $ts = $this->userAdjust( $ts, $timecorrection );
1849 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1850 return $this->sprintfDate( $df, $ts );
1854 * Internal helper function for userDate(), userTime() and userTimeAndDate()
1856 * @param $type String: can be 'date', 'time' or 'both'
1857 * @param $ts Mixed: the time format which needs to be turned into a
1858 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1859 * @param $user User object used to get preferences for timezone and format
1860 * @param $options Array, can contain the following keys:
1861 * - 'timecorrection': time correction, can have the following values:
1862 * - true: use user's preference
1863 * - false: don't use time correction
1864 * - integer: value of time correction in minutes
1865 * - 'format': format to use, can have the following values:
1866 * - true: use user's preference
1867 * - false: use default preference
1868 * - string: format to use
1869 * @since 1.19
1870 * @return String
1872 private function internalUserTimeAndDate( $type, $ts, User $user, array $options ) {
1873 $ts = wfTimestamp( TS_MW, $ts );
1874 $options += array( 'timecorrection' => true, 'format' => true );
1875 if ( $options['timecorrection'] !== false ) {
1876 if ( $options['timecorrection'] === true ) {
1877 $offset = $user->getOption( 'timecorrection' );
1878 } else {
1879 $offset = $options['timecorrection'];
1881 $ts = $this->userAdjust( $ts, $offset );
1883 if ( $options['format'] === true ) {
1884 $format = $user->getDatePreference();
1885 } else {
1886 $format = $options['format'];
1888 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
1889 return $this->sprintfDate( $df, $ts );
1893 * Get the formatted date for the given timestamp and formatted for
1894 * the given user.
1896 * @param $ts Mixed: the time format which needs to be turned into a
1897 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1898 * @param $user User object used to get preferences for timezone and format
1899 * @param $options Array, can contain the following keys:
1900 * - 'timecorrection': time correction, can have the following values:
1901 * - true: use user's preference
1902 * - false: don't use time correction
1903 * - integer: value of time correction in minutes
1904 * - 'format': format to use, can have the following values:
1905 * - true: use user's preference
1906 * - false: use default preference
1907 * - string: format to use
1908 * @since 1.19
1909 * @return String
1911 public function userDate( $ts, User $user, array $options = array() ) {
1912 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
1916 * Get the formatted time for the given timestamp and formatted for
1917 * the given user.
1919 * @param $ts Mixed: the time format which needs to be turned into a
1920 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1921 * @param $user User object used to get preferences for timezone and format
1922 * @param $options Array, can contain the following keys:
1923 * - 'timecorrection': time correction, can have the following values:
1924 * - true: use user's preference
1925 * - false: don't use time correction
1926 * - integer: value of time correction in minutes
1927 * - 'format': format to use, can have the following values:
1928 * - true: use user's preference
1929 * - false: use default preference
1930 * - string: format to use
1931 * @since 1.19
1932 * @return String
1934 public function userTime( $ts, User $user, array $options = array() ) {
1935 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
1939 * Get the formatted date and time for the given timestamp and formatted for
1940 * the given user.
1942 * @param $ts Mixed: the time format which needs to be turned into a
1943 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1944 * @param $user User object used to get preferences for timezone and format
1945 * @param $options Array, can contain the following keys:
1946 * - 'timecorrection': time correction, can have the following values:
1947 * - true: use user's preference
1948 * - false: don't use time correction
1949 * - integer: value of time correction in minutes
1950 * - 'format': format to use, can have the following values:
1951 * - true: use user's preference
1952 * - false: use default preference
1953 * - string: format to use
1954 * @since 1.19
1955 * @return String
1957 public function userTimeAndDate( $ts, User $user, array $options = array() ) {
1958 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
1962 * @param $key string
1963 * @return array|null
1965 function getMessage( $key ) {
1966 return self::$dataCache->getSubitem( $this->mCode, 'messages', $key );
1970 * @return array
1972 function getAllMessages() {
1973 return self::$dataCache->getItem( $this->mCode, 'messages' );
1977 * @param $in
1978 * @param $out
1979 * @param $string
1980 * @return string
1982 function iconv( $in, $out, $string ) {
1983 # This is a wrapper for iconv in all languages except esperanto,
1984 # which does some nasty x-conversions beforehand
1986 # Even with //IGNORE iconv can whine about illegal characters in
1987 # *input* string. We just ignore those too.
1988 # REF: http://bugs.php.net/bug.php?id=37166
1989 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
1990 wfSuppressWarnings();
1991 $text = iconv( $in, $out . '//IGNORE', $string );
1992 wfRestoreWarnings();
1993 return $text;
1996 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
1999 * @param $matches array
2000 * @return mixed|string
2002 function ucwordbreaksCallbackAscii( $matches ) {
2003 return $this->ucfirst( $matches[1] );
2007 * @param $matches array
2008 * @return string
2010 function ucwordbreaksCallbackMB( $matches ) {
2011 return mb_strtoupper( $matches[0] );
2015 * @param $matches array
2016 * @return string
2018 function ucCallback( $matches ) {
2019 list( $wikiUpperChars ) = self::getCaseMaps();
2020 return strtr( $matches[1], $wikiUpperChars );
2024 * @param $matches array
2025 * @return string
2027 function lcCallback( $matches ) {
2028 list( , $wikiLowerChars ) = self::getCaseMaps();
2029 return strtr( $matches[1], $wikiLowerChars );
2033 * @param $matches array
2034 * @return string
2036 function ucwordsCallbackMB( $matches ) {
2037 return mb_strtoupper( $matches[0] );
2041 * @param $matches array
2042 * @return string
2044 function ucwordsCallbackWiki( $matches ) {
2045 list( $wikiUpperChars ) = self::getCaseMaps();
2046 return strtr( $matches[0], $wikiUpperChars );
2050 * Make a string's first character uppercase
2052 * @param $str string
2054 * @return string
2056 function ucfirst( $str ) {
2057 $o = ord( $str );
2058 if ( $o < 96 ) { // if already uppercase...
2059 return $str;
2060 } elseif ( $o < 128 ) {
2061 return ucfirst( $str ); // use PHP's ucfirst()
2062 } else {
2063 // fall back to more complex logic in case of multibyte strings
2064 return $this->uc( $str, true );
2069 * Convert a string to uppercase
2071 * @param $str string
2072 * @param $first bool
2074 * @return string
2076 function uc( $str, $first = false ) {
2077 if ( function_exists( 'mb_strtoupper' ) ) {
2078 if ( $first ) {
2079 if ( $this->isMultibyte( $str ) ) {
2080 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2081 } else {
2082 return ucfirst( $str );
2084 } else {
2085 return $this->isMultibyte( $str ) ? mb_strtoupper( $str ) : strtoupper( $str );
2087 } else {
2088 if ( $this->isMultibyte( $str ) ) {
2089 $x = $first ? '^' : '';
2090 return preg_replace_callback(
2091 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2092 array( $this, 'ucCallback' ),
2093 $str
2095 } else {
2096 return $first ? ucfirst( $str ) : strtoupper( $str );
2102 * @param $str string
2103 * @return mixed|string
2105 function lcfirst( $str ) {
2106 $o = ord( $str );
2107 if ( !$o ) {
2108 return strval( $str );
2109 } elseif ( $o >= 128 ) {
2110 return $this->lc( $str, true );
2111 } elseif ( $o > 96 ) {
2112 return $str;
2113 } else {
2114 $str[0] = strtolower( $str[0] );
2115 return $str;
2120 * @param $str string
2121 * @param $first bool
2122 * @return mixed|string
2124 function lc( $str, $first = false ) {
2125 if ( function_exists( 'mb_strtolower' ) ) {
2126 if ( $first ) {
2127 if ( $this->isMultibyte( $str ) ) {
2128 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2129 } else {
2130 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2132 } else {
2133 return $this->isMultibyte( $str ) ? mb_strtolower( $str ) : strtolower( $str );
2135 } else {
2136 if ( $this->isMultibyte( $str ) ) {
2137 $x = $first ? '^' : '';
2138 return preg_replace_callback(
2139 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2140 array( $this, 'lcCallback' ),
2141 $str
2143 } else {
2144 return $first ? strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2150 * @param $str string
2151 * @return bool
2153 function isMultibyte( $str ) {
2154 return (bool)preg_match( '/[\x80-\xff]/', $str );
2158 * @param $str string
2159 * @return mixed|string
2161 function ucwords( $str ) {
2162 if ( $this->isMultibyte( $str ) ) {
2163 $str = $this->lc( $str );
2165 // regexp to find first letter in each word (i.e. after each space)
2166 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2168 // function to use to capitalize a single char
2169 if ( function_exists( 'mb_strtoupper' ) ) {
2170 return preg_replace_callback(
2171 $replaceRegexp,
2172 array( $this, 'ucwordsCallbackMB' ),
2173 $str
2175 } else {
2176 return preg_replace_callback(
2177 $replaceRegexp,
2178 array( $this, 'ucwordsCallbackWiki' ),
2179 $str
2182 } else {
2183 return ucwords( strtolower( $str ) );
2188 * capitalize words at word breaks
2190 * @param $str string
2191 * @return mixed
2193 function ucwordbreaks( $str ) {
2194 if ( $this->isMultibyte( $str ) ) {
2195 $str = $this->lc( $str );
2197 // since \b doesn't work for UTF-8, we explicitely define word break chars
2198 $breaks = "[ \-\(\)\}\{\.,\?!]";
2200 // find first letter after word break
2201 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2203 if ( function_exists( 'mb_strtoupper' ) ) {
2204 return preg_replace_callback(
2205 $replaceRegexp,
2206 array( $this, 'ucwordbreaksCallbackMB' ),
2207 $str
2209 } else {
2210 return preg_replace_callback(
2211 $replaceRegexp,
2212 array( $this, 'ucwordsCallbackWiki' ),
2213 $str
2216 } else {
2217 return preg_replace_callback(
2218 '/\b([\w\x80-\xff]+)\b/',
2219 array( $this, 'ucwordbreaksCallbackAscii' ),
2220 $str
2226 * Return a case-folded representation of $s
2228 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2229 * and $s2 are the same except for the case of their characters. It is not
2230 * necessary for the value returned to make sense when displayed.
2232 * Do *not* perform any other normalisation in this function. If a caller
2233 * uses this function when it should be using a more general normalisation
2234 * function, then fix the caller.
2236 * @param $s string
2238 * @return string
2240 function caseFold( $s ) {
2241 return $this->uc( $s );
2245 * @param $s string
2246 * @return string
2248 function checkTitleEncoding( $s ) {
2249 if ( is_array( $s ) ) {
2250 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2252 # Check for non-UTF-8 URLs
2253 $ishigh = preg_match( '/[\x80-\xff]/', $s );
2254 if ( !$ishigh ) {
2255 return $s;
2258 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2259 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
2260 if ( $isutf8 ) {
2261 return $s;
2264 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2268 * @return array
2270 function fallback8bitEncoding() {
2271 return self::$dataCache->getItem( $this->mCode, 'fallback8bitEncoding' );
2275 * Most writing systems use whitespace to break up words.
2276 * Some languages such as Chinese don't conventionally do this,
2277 * which requires special handling when breaking up words for
2278 * searching etc.
2280 * @return bool
2282 function hasWordBreaks() {
2283 return true;
2287 * Some languages such as Chinese require word segmentation,
2288 * Specify such segmentation when overridden in derived class.
2290 * @param $string String
2291 * @return String
2293 function segmentByWord( $string ) {
2294 return $string;
2298 * Some languages have special punctuation need to be normalized.
2299 * Make such changes here.
2301 * @param $string String
2302 * @return String
2304 function normalizeForSearch( $string ) {
2305 return self::convertDoubleWidth( $string );
2309 * convert double-width roman characters to single-width.
2310 * range: ff00-ff5f ~= 0020-007f
2312 * @param $string string
2314 * @return string
2316 protected static function convertDoubleWidth( $string ) {
2317 static $full = null;
2318 static $half = null;
2320 if ( $full === null ) {
2321 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2322 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2323 $full = str_split( $fullWidth, 3 );
2324 $half = str_split( $halfWidth );
2327 $string = str_replace( $full, $half, $string );
2328 return $string;
2332 * @param $string string
2333 * @param $pattern string
2334 * @return string
2336 protected static function insertSpace( $string, $pattern ) {
2337 $string = preg_replace( $pattern, " $1 ", $string );
2338 $string = preg_replace( '/ +/', ' ', $string );
2339 return $string;
2343 * @param $termsArray array
2344 * @return array
2346 function convertForSearchResult( $termsArray ) {
2347 # some languages, e.g. Chinese, need to do a conversion
2348 # in order for search results to be displayed correctly
2349 return $termsArray;
2353 * Get the first character of a string.
2355 * @param $s string
2356 * @return string
2358 function firstChar( $s ) {
2359 $matches = array();
2360 preg_match(
2361 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2362 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2364 $matches
2367 if ( isset( $matches[1] ) ) {
2368 if ( strlen( $matches[1] ) != 3 ) {
2369 return $matches[1];
2372 // Break down Hangul syllables to grab the first jamo
2373 $code = utf8ToCodepoint( $matches[1] );
2374 if ( $code < 0xac00 || 0xd7a4 <= $code ) {
2375 return $matches[1];
2376 } elseif ( $code < 0xb098 ) {
2377 return "\xe3\x84\xb1";
2378 } elseif ( $code < 0xb2e4 ) {
2379 return "\xe3\x84\xb4";
2380 } elseif ( $code < 0xb77c ) {
2381 return "\xe3\x84\xb7";
2382 } elseif ( $code < 0xb9c8 ) {
2383 return "\xe3\x84\xb9";
2384 } elseif ( $code < 0xbc14 ) {
2385 return "\xe3\x85\x81";
2386 } elseif ( $code < 0xc0ac ) {
2387 return "\xe3\x85\x82";
2388 } elseif ( $code < 0xc544 ) {
2389 return "\xe3\x85\x85";
2390 } elseif ( $code < 0xc790 ) {
2391 return "\xe3\x85\x87";
2392 } elseif ( $code < 0xcc28 ) {
2393 return "\xe3\x85\x88";
2394 } elseif ( $code < 0xce74 ) {
2395 return "\xe3\x85\x8a";
2396 } elseif ( $code < 0xd0c0 ) {
2397 return "\xe3\x85\x8b";
2398 } elseif ( $code < 0xd30c ) {
2399 return "\xe3\x85\x8c";
2400 } elseif ( $code < 0xd558 ) {
2401 return "\xe3\x85\x8d";
2402 } else {
2403 return "\xe3\x85\x8e";
2405 } else {
2406 return '';
2410 function initEncoding() {
2411 # Some languages may have an alternate char encoding option
2412 # (Esperanto X-coding, Japanese furigana conversion, etc)
2413 # If this language is used as the primary content language,
2414 # an override to the defaults can be set here on startup.
2418 * @param $s string
2419 * @return string
2421 function recodeForEdit( $s ) {
2422 # For some languages we'll want to explicitly specify
2423 # which characters make it into the edit box raw
2424 # or are converted in some way or another.
2425 global $wgEditEncoding;
2426 if ( $wgEditEncoding == '' || $wgEditEncoding == 'UTF-8' ) {
2427 return $s;
2428 } else {
2429 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2434 * @param $s string
2435 * @return string
2437 function recodeInput( $s ) {
2438 # Take the previous into account.
2439 global $wgEditEncoding;
2440 if ( $wgEditEncoding != '' ) {
2441 $enc = $wgEditEncoding;
2442 } else {
2443 $enc = 'UTF-8';
2445 if ( $enc == 'UTF-8' ) {
2446 return $s;
2447 } else {
2448 return $this->iconv( $enc, 'UTF-8', $s );
2453 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2454 * also cleans up certain backwards-compatible sequences, converting them
2455 * to the modern Unicode equivalent.
2457 * This is language-specific for performance reasons only.
2459 * @param $s string
2461 * @return string
2463 function normalize( $s ) {
2464 global $wgAllUnicodeFixes;
2465 $s = UtfNormal::cleanUp( $s );
2466 if ( $wgAllUnicodeFixes ) {
2467 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2468 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2471 return $s;
2475 * Transform a string using serialized data stored in the given file (which
2476 * must be in the serialized subdirectory of $IP). The file contains pairs
2477 * mapping source characters to destination characters.
2479 * The data is cached in process memory. This will go faster if you have the
2480 * FastStringSearch extension.
2482 * @param $file string
2483 * @param $string string
2485 * @return string
2487 function transformUsingPairFile( $file, $string ) {
2488 if ( !isset( $this->transformData[$file] ) ) {
2489 $data = wfGetPrecompiledData( $file );
2490 if ( $data === false ) {
2491 throw new MWException( __METHOD__ . ": The transformation file $file is missing" );
2493 $this->transformData[$file] = new ReplacementArray( $data );
2495 return $this->transformData[$file]->replace( $string );
2499 * For right-to-left language support
2501 * @return bool
2503 function isRTL() {
2504 return self::$dataCache->getItem( $this->mCode, 'rtl' );
2508 * Return the correct HTML 'dir' attribute value for this language.
2509 * @return String
2511 function getDir() {
2512 return $this->isRTL() ? 'rtl' : 'ltr';
2516 * Return 'left' or 'right' as appropriate alignment for line-start
2517 * for this language's text direction.
2519 * Should be equivalent to CSS3 'start' text-align value....
2521 * @return String
2523 function alignStart() {
2524 return $this->isRTL() ? 'right' : 'left';
2528 * Return 'right' or 'left' as appropriate alignment for line-end
2529 * for this language's text direction.
2531 * Should be equivalent to CSS3 'end' text-align value....
2533 * @return String
2535 function alignEnd() {
2536 return $this->isRTL() ? 'left' : 'right';
2540 * A hidden direction mark (LRM or RLM), depending on the language direction
2542 * @param $opposite Boolean Get the direction mark opposite to your language
2543 * @return string
2545 function getDirMark( $opposite = false ) {
2546 $rtl = "\xE2\x80\x8F";
2547 $ltr = "\xE2\x80\x8E";
2548 if ( $opposite ) { return $this->isRTL() ? $ltr : $rtl; }
2549 return $this->isRTL() ? $rtl : $ltr;
2553 * @return array
2555 function capitalizeAllNouns() {
2556 return self::$dataCache->getItem( $this->mCode, 'capitalizeAllNouns' );
2560 * An arrow, depending on the language direction
2562 * @return string
2564 function getArrow() {
2565 return $this->isRTL() ? '←' : '→';
2569 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2571 * @return bool
2573 function linkPrefixExtension() {
2574 return self::$dataCache->getItem( $this->mCode, 'linkPrefixExtension' );
2578 * @return array
2580 function getMagicWords() {
2581 return self::$dataCache->getItem( $this->mCode, 'magicWords' );
2584 protected function doMagicHook() {
2585 if ( $this->mMagicHookDone ) {
2586 return;
2588 $this->mMagicHookDone = true;
2589 wfProfileIn( 'LanguageGetMagic' );
2590 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
2591 wfProfileOut( 'LanguageGetMagic' );
2595 * Fill a MagicWord object with data from here
2597 * @param $mw
2599 function getMagic( $mw ) {
2600 $this->doMagicHook();
2602 if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
2603 $rawEntry = $this->mMagicExtensions[$mw->mId];
2604 } else {
2605 $magicWords = $this->getMagicWords();
2606 if ( isset( $magicWords[$mw->mId] ) ) {
2607 $rawEntry = $magicWords[$mw->mId];
2608 } else {
2609 $rawEntry = false;
2613 if ( !is_array( $rawEntry ) ) {
2614 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
2615 } else {
2616 $mw->mCaseSensitive = $rawEntry[0];
2617 $mw->mSynonyms = array_slice( $rawEntry, 1 );
2622 * Add magic words to the extension array
2624 * @param $newWords array
2626 function addMagicWordsByLang( $newWords ) {
2627 $fallbackChain = $this->getFallbackLanguages();
2628 $fallbackChain = array_reverse( $fallbackChain );
2629 foreach ( $fallbackChain as $code ) {
2630 if ( isset( $newWords[$code] ) ) {
2631 $this->mMagicExtensions = $newWords[$code] + $this->mMagicExtensions;
2637 * Get special page names, as an associative array
2638 * case folded alias => real name
2640 function getSpecialPageAliases() {
2641 // Cache aliases because it may be slow to load them
2642 if ( is_null( $this->mExtendedSpecialPageAliases ) ) {
2643 // Initialise array
2644 $this->mExtendedSpecialPageAliases =
2645 self::$dataCache->getItem( $this->mCode, 'specialPageAliases' );
2646 wfRunHooks( 'LanguageGetSpecialPageAliases',
2647 array( &$this->mExtendedSpecialPageAliases, $this->getCode() ) );
2650 return $this->mExtendedSpecialPageAliases;
2654 * Italic is unsuitable for some languages
2656 * @param $text String: the text to be emphasized.
2657 * @return string
2659 function emphasize( $text ) {
2660 return "<em>$text</em>";
2664 * Normally we output all numbers in plain en_US style, that is
2665 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2666 * point twohundredthirtyfive. However this is not suitable for all
2667 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2668 * Icelandic just want to use commas instead of dots, and dots instead
2669 * of commas like "293.291,235".
2671 * An example of this function being called:
2672 * <code>
2673 * wfMsg( 'message', $wgLang->formatNum( $num ) )
2674 * </code>
2676 * See LanguageGu.php for the Gujarati implementation and
2677 * $separatorTransformTable on MessageIs.php for
2678 * the , => . and . => , implementation.
2680 * @todo check if it's viable to use localeconv() for the decimal
2681 * separator thing.
2682 * @param $number Mixed: the string to be formatted, should be an integer
2683 * or a floating point number.
2684 * @param $nocommafy Bool: set to true for special numbers like dates
2685 * @return string
2687 public function formatNum( $number, $nocommafy = false ) {
2688 global $wgTranslateNumerals;
2689 if ( !$nocommafy ) {
2690 $number = $this->commafy( $number );
2691 $s = $this->separatorTransformTable();
2692 if ( $s ) {
2693 $number = strtr( $number, $s );
2697 if ( $wgTranslateNumerals ) {
2698 $s = $this->digitTransformTable();
2699 if ( $s ) {
2700 $number = strtr( $number, $s );
2704 return $number;
2708 * @param $number string
2709 * @return string
2711 function parseFormattedNumber( $number ) {
2712 $s = $this->digitTransformTable();
2713 if ( $s ) {
2714 $number = strtr( $number, array_flip( $s ) );
2717 $s = $this->separatorTransformTable();
2718 if ( $s ) {
2719 $number = strtr( $number, array_flip( $s ) );
2722 $number = strtr( $number, array( ',' => '' ) );
2723 return $number;
2727 * Adds commas to a given number
2728 * @since 1.19
2729 * @param $_ mixed
2730 * @return string
2732 function commafy( $_ ) {
2733 $digitGroupingPattern = $this->digitGroupingPattern();
2734 if ( $_ === null ) {
2735 return '';
2738 if ( !$digitGroupingPattern || $digitGroupingPattern === "###,###,###" ) {
2739 // default grouping is at thousands, use the same for ###,###,### pattern too.
2740 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2741 } else {
2742 // Ref: http://cldr.unicode.org/translation/number-patterns
2743 $sign = "";
2744 if ( intval( $_ ) < 0 ) {
2745 // For negative numbers apply the algorithm like positive number and add sign.
2746 $sign = "-";
2747 $_ = substr( $_, 1 );
2749 $numberpart = array();
2750 $decimalpart = array();
2751 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
2752 preg_match( "/\d+/", $_, $numberpart );
2753 preg_match( "/\.\d*/", $_, $decimalpart );
2754 $groupedNumber = ( count( $decimalpart ) > 0 ) ? $decimalpart[0]:"";
2755 if ( $groupedNumber === $_ ) {
2756 // the string does not have any number part. Eg: .12345
2757 return $sign . $groupedNumber;
2759 $start = $end = strlen( $numberpart[0] );
2760 while ( $start > 0 ) {
2761 $match = $matches[0][$numMatches -1] ;
2762 $matchLen = strlen( $match );
2763 $start = $end - $matchLen;
2764 if ( $start < 0 ) {
2765 $start = 0;
2767 $groupedNumber = substr( $_ , $start, $end -$start ) . $groupedNumber ;
2768 $end = $start;
2769 if ( $numMatches > 1 ) {
2770 // use the last pattern for the rest of the number
2771 $numMatches--;
2773 if ( $start > 0 ) {
2774 $groupedNumber = "," . $groupedNumber;
2777 return $sign . $groupedNumber;
2781 * @return String
2783 function digitGroupingPattern() {
2784 return self::$dataCache->getItem( $this->mCode, 'digitGroupingPattern' );
2788 * @return array
2790 function digitTransformTable() {
2791 return self::$dataCache->getItem( $this->mCode, 'digitTransformTable' );
2795 * @return array
2797 function separatorTransformTable() {
2798 return self::$dataCache->getItem( $this->mCode, 'separatorTransformTable' );
2802 * Take a list of strings and build a locale-friendly comma-separated
2803 * list, using the local comma-separator message.
2804 * The last two strings are chained with an "and".
2806 * @param $l Array
2807 * @return string
2809 function listToText( array $l ) {
2810 $s = '';
2811 $m = count( $l ) - 1;
2812 if ( $m == 1 ) {
2813 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
2814 } else {
2815 for ( $i = $m; $i >= 0; $i-- ) {
2816 if ( $i == $m ) {
2817 $s = $l[$i];
2818 } elseif ( $i == $m - 1 ) {
2819 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
2820 } else {
2821 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
2824 return $s;
2829 * Take a list of strings and build a locale-friendly comma-separated
2830 * list, using the local comma-separator message.
2831 * @param $list array of strings to put in a comma list
2832 * @return string
2834 function commaList( array $list ) {
2835 return implode(
2836 wfMsgExt(
2837 'comma-separator',
2838 array( 'parsemag', 'escapenoentities', 'language' => $this )
2840 $list
2845 * Take a list of strings and build a locale-friendly semicolon-separated
2846 * list, using the local semicolon-separator message.
2847 * @param $list array of strings to put in a semicolon list
2848 * @return string
2850 function semicolonList( array $list ) {
2851 return implode(
2852 wfMsgExt(
2853 'semicolon-separator',
2854 array( 'parsemag', 'escapenoentities', 'language' => $this )
2856 $list
2861 * Same as commaList, but separate it with the pipe instead.
2862 * @param $list array of strings to put in a pipe list
2863 * @return string
2865 function pipeList( array $list ) {
2866 return implode(
2867 wfMsgExt(
2868 'pipe-separator',
2869 array( 'escapenoentities', 'language' => $this )
2871 $list
2876 * Truncate a string to a specified length in bytes, appending an optional
2877 * string (e.g. for ellipses)
2879 * The database offers limited byte lengths for some columns in the database;
2880 * multi-byte character sets mean we need to ensure that only whole characters
2881 * are included, otherwise broken characters can be passed to the user
2883 * If $length is negative, the string will be truncated from the beginning
2885 * @param $string String to truncate
2886 * @param $length Int: maximum length (including ellipses)
2887 * @param $ellipsis String to append to the truncated text
2888 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
2889 * $adjustLength was introduced in 1.18, before that behaved as if false.
2890 * @return string
2892 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
2893 # Use the localized ellipsis character
2894 if ( $ellipsis == '...' ) {
2895 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
2897 # Check if there is no need to truncate
2898 if ( $length == 0 ) {
2899 return $ellipsis; // convention
2900 } elseif ( strlen( $string ) <= abs( $length ) ) {
2901 return $string; // no need to truncate
2903 $stringOriginal = $string;
2904 # If ellipsis length is >= $length then we can't apply $adjustLength
2905 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
2906 $string = $ellipsis; // this can be slightly unexpected
2907 # Otherwise, truncate and add ellipsis...
2908 } else {
2909 $eLength = $adjustLength ? strlen( $ellipsis ) : 0;
2910 if ( $length > 0 ) {
2911 $length -= $eLength;
2912 $string = substr( $string, 0, $length ); // xyz...
2913 $string = $this->removeBadCharLast( $string );
2914 $string = $string . $ellipsis;
2915 } else {
2916 $length += $eLength;
2917 $string = substr( $string, $length ); // ...xyz
2918 $string = $this->removeBadCharFirst( $string );
2919 $string = $ellipsis . $string;
2922 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
2923 # This check is *not* redundant if $adjustLength, due to the single case where
2924 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
2925 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
2926 return $string;
2927 } else {
2928 return $stringOriginal;
2933 * Remove bytes that represent an incomplete Unicode character
2934 * at the end of string (e.g. bytes of the char are missing)
2936 * @param $string String
2937 * @return string
2939 protected function removeBadCharLast( $string ) {
2940 if ( $string != '' ) {
2941 $char = ord( $string[strlen( $string ) - 1] );
2942 $m = array();
2943 if ( $char >= 0xc0 ) {
2944 # We got the first byte only of a multibyte char; remove it.
2945 $string = substr( $string, 0, -1 );
2946 } elseif ( $char >= 0x80 &&
2947 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
2948 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
2950 # We chopped in the middle of a character; remove it
2951 $string = $m[1];
2954 return $string;
2958 * Remove bytes that represent an incomplete Unicode character
2959 * at the start of string (e.g. bytes of the char are missing)
2961 * @param $string String
2962 * @return string
2964 protected function removeBadCharFirst( $string ) {
2965 if ( $string != '' ) {
2966 $char = ord( $string[0] );
2967 if ( $char >= 0x80 && $char < 0xc0 ) {
2968 # We chopped in the middle of a character; remove the whole thing
2969 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
2972 return $string;
2976 * Truncate a string of valid HTML to a specified length in bytes,
2977 * appending an optional string (e.g. for ellipses), and return valid HTML
2979 * This is only intended for styled/linked text, such as HTML with
2980 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
2981 * Also, this will not detect things like "display:none" CSS.
2983 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
2985 * @param string $text HTML string to truncate
2986 * @param int $length (zero/positive) Maximum length (including ellipses)
2987 * @param string $ellipsis String to append to the truncated text
2988 * @return string
2990 function truncateHtml( $text, $length, $ellipsis = '...' ) {
2991 # Use the localized ellipsis character
2992 if ( $ellipsis == '...' ) {
2993 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
2995 # Check if there is clearly no need to truncate
2996 if ( $length <= 0 ) {
2997 return $ellipsis; // no text shown, nothing to format (convention)
2998 } elseif ( strlen( $text ) <= $length ) {
2999 return $text; // string short enough even *with* HTML (short-circuit)
3002 $dispLen = 0; // innerHTML legth so far
3003 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3004 $tagType = 0; // 0-open, 1-close
3005 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3006 $entityState = 0; // 0-not entity, 1-entity
3007 $tag = $ret = ''; // accumulated tag name, accumulated result string
3008 $openTags = array(); // open tag stack
3009 $maybeState = null; // possible truncation state
3011 $textLen = strlen( $text );
3012 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3013 for ( $pos = 0; true; ++$pos ) {
3014 # Consider truncation once the display length has reached the maximim.
3015 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3016 # Check that we're not in the middle of a bracket/entity...
3017 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3018 if ( !$testingEllipsis ) {
3019 $testingEllipsis = true;
3020 # Save where we are; we will truncate here unless there turn out to
3021 # be so few remaining characters that truncation is not necessary.
3022 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3023 $maybeState = array( $ret, $openTags ); // save state
3025 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3026 # String in fact does need truncation, the truncation point was OK.
3027 list( $ret, $openTags ) = $maybeState; // reload state
3028 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3029 $ret .= $ellipsis; // add ellipsis
3030 break;
3033 if ( $pos >= $textLen ) break; // extra iteration just for above checks
3035 # Read the next char...
3036 $ch = $text[$pos];
3037 $lastCh = $pos ? $text[$pos - 1] : '';
3038 $ret .= $ch; // add to result string
3039 if ( $ch == '<' ) {
3040 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3041 $entityState = 0; // for bad HTML
3042 $bracketState = 1; // tag started (checking for backslash)
3043 } elseif ( $ch == '>' ) {
3044 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3045 $entityState = 0; // for bad HTML
3046 $bracketState = 0; // out of brackets
3047 } elseif ( $bracketState == 1 ) {
3048 if ( $ch == '/' ) {
3049 $tagType = 1; // close tag (e.g. "</span>")
3050 } else {
3051 $tagType = 0; // open tag (e.g. "<span>")
3052 $tag .= $ch;
3054 $bracketState = 2; // building tag name
3055 } elseif ( $bracketState == 2 ) {
3056 if ( $ch != ' ' ) {
3057 $tag .= $ch;
3058 } else {
3059 // Name found (e.g. "<a href=..."), add on tag attributes...
3060 $pos += $this->truncate_skip( $ret, $text, "<>", $pos + 1 );
3062 } elseif ( $bracketState == 0 ) {
3063 if ( $entityState ) {
3064 if ( $ch == ';' ) {
3065 $entityState = 0;
3066 $dispLen++; // entity is one displayed char
3068 } else {
3069 if ( $neLength == 0 && !$maybeState ) {
3070 // Save state without $ch. We want to *hit* the first
3071 // display char (to get tags) but not *use* it if truncating.
3072 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3074 if ( $ch == '&' ) {
3075 $entityState = 1; // entity found, (e.g. "&#160;")
3076 } else {
3077 $dispLen++; // this char is displayed
3078 // Add the next $max display text chars after this in one swoop...
3079 $max = ( $testingEllipsis ? $length : $neLength ) - $dispLen;
3080 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos + 1, $max );
3081 $dispLen += $skipped;
3082 $pos += $skipped;
3087 // Close the last tag if left unclosed by bad HTML
3088 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3089 while ( count( $openTags ) > 0 ) {
3090 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3092 return $ret;
3096 * truncateHtml() helper function
3097 * like strcspn() but adds the skipped chars to $ret
3099 * @param $ret
3100 * @param $text
3101 * @param $search
3102 * @param $start
3103 * @param $len
3104 * @return int
3106 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3107 if ( $len === null ) {
3108 $len = -1; // -1 means "no limit" for strcspn
3109 } elseif ( $len < 0 ) {
3110 $len = 0; // sanity
3112 $skipCount = 0;
3113 if ( $start < strlen( $text ) ) {
3114 $skipCount = strcspn( $text, $search, $start, $len );
3115 $ret .= substr( $text, $start, $skipCount );
3117 return $skipCount;
3121 * truncateHtml() helper function
3122 * (a) push or pop $tag from $openTags as needed
3123 * (b) clear $tag value
3124 * @param &$tag string Current HTML tag name we are looking at
3125 * @param $tagType int (0-open tag, 1-close tag)
3126 * @param $lastCh char|string Character before the '>' that ended this tag
3127 * @param &$openTags array Open tag stack (not accounting for $tag)
3129 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3130 $tag = ltrim( $tag );
3131 if ( $tag != '' ) {
3132 if ( $tagType == 0 && $lastCh != '/' ) {
3133 $openTags[] = $tag; // tag opened (didn't close itself)
3134 } elseif ( $tagType == 1 ) {
3135 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3136 array_pop( $openTags ); // tag closed
3139 $tag = '';
3144 * Grammatical transformations, needed for inflected languages
3145 * Invoked by putting {{grammar:case|word}} in a message
3147 * @param $word string
3148 * @param $case string
3149 * @return string
3151 function convertGrammar( $word, $case ) {
3152 global $wgGrammarForms;
3153 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3154 return $wgGrammarForms[$this->getCode()][$case][$word];
3156 return $word;
3160 * Provides an alternative text depending on specified gender.
3161 * Usage {{gender:username|masculine|feminine|neutral}}.
3162 * username is optional, in which case the gender of current user is used,
3163 * but only in (some) interface messages; otherwise default gender is used.
3164 * If second or third parameter are not specified, masculine is used.
3165 * These details may be overriden per language.
3167 * @param $gender string
3168 * @param $forms array
3170 * @return string
3172 function gender( $gender, $forms ) {
3173 if ( !count( $forms ) ) {
3174 return '';
3176 $forms = $this->preConvertPlural( $forms, 2 );
3177 if ( $gender === 'male' ) {
3178 return $forms[0];
3180 if ( $gender === 'female' ) {
3181 return $forms[1];
3183 return isset( $forms[2] ) ? $forms[2] : $forms[0];
3187 * Plural form transformations, needed for some languages.
3188 * For example, there are 3 form of plural in Russian and Polish,
3189 * depending on "count mod 10". See [[w:Plural]]
3190 * For English it is pretty simple.
3192 * Invoked by putting {{plural:count|wordform1|wordform2}}
3193 * or {{plural:count|wordform1|wordform2|wordform3}}
3195 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3197 * @param $count Integer: non-localized number
3198 * @param $forms Array: different plural forms
3199 * @return string Correct form of plural for $count in this language
3201 function convertPlural( $count, $forms ) {
3202 if ( !count( $forms ) ) {
3203 return '';
3205 $forms = $this->preConvertPlural( $forms, 2 );
3207 return ( $count == 1 ) ? $forms[0] : $forms[1];
3211 * Checks that convertPlural was given an array and pads it to requested
3212 * amount of forms by copying the last one.
3214 * @param $count Integer: How many forms should there be at least
3215 * @param $forms Array of forms given to convertPlural
3216 * @return array Padded array of forms or an exception if not an array
3218 protected function preConvertPlural( /* Array */ $forms, $count ) {
3219 while ( count( $forms ) < $count ) {
3220 $forms[] = $forms[count( $forms ) - 1];
3222 return $forms;
3226 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3227 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3228 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3229 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3230 * match up with it.
3232 * @param $str String: the validated block duration in English
3233 * @return Somehow translated block duration
3234 * @see LanguageFi.php for example implementation
3236 function translateBlockExpiry( $str ) {
3237 $duration = SpecialBlock::getSuggestedDurations( $this );
3238 foreach ( $duration as $show => $value ) {
3239 if ( strcmp( $str, $value ) == 0 ) {
3240 return htmlspecialchars( trim( $show ) );
3244 // Since usually only infinite or indefinite is only on list, so try
3245 // equivalents if still here.
3246 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3247 if ( in_array( $str, $indefs ) ) {
3248 foreach ( $indefs as $val ) {
3249 $show = array_search( $val, $duration, true );
3250 if ( $show !== false ) {
3251 return htmlspecialchars( trim( $show ) );
3255 // If all else fails, return the original string.
3256 return $str;
3260 * languages like Chinese need to be segmented in order for the diff
3261 * to be of any use
3263 * @param $text String
3264 * @return String
3266 public function segmentForDiff( $text ) {
3267 return $text;
3271 * and unsegment to show the result
3273 * @param $text String
3274 * @return String
3276 public function unsegmentForDiff( $text ) {
3277 return $text;
3281 * Return the LanguageConverter used in the Language
3282 * @return LanguageConverter
3284 public function getConverter() {
3285 return $this->mConverter;
3289 * convert text to all supported variants
3291 * @param $text string
3292 * @return array
3294 public function autoConvertToAllVariants( $text ) {
3295 return $this->mConverter->autoConvertToAllVariants( $text );
3299 * convert text to different variants of a language.
3301 * @param $text string
3302 * @return string
3304 public function convert( $text ) {
3305 return $this->mConverter->convert( $text );
3309 * Convert a Title object to a string in the preferred variant
3311 * @param $title Title
3312 * @return string
3314 public function convertTitle( $title ) {
3315 return $this->mConverter->convertTitle( $title );
3319 * Check if this is a language with variants
3321 * @return bool
3323 public function hasVariants() {
3324 return sizeof( $this->getVariants() ) > 1;
3328 * Check if the language has the specific variant
3329 * @param $variant string
3330 * @return bool
3332 public function hasVariant( $variant ) {
3333 return (bool)$this->mConverter->validateVariant( $variant );
3337 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3339 * @param $text string
3340 * @return string
3342 public function armourMath( $text ) {
3343 return $this->mConverter->armourMath( $text );
3347 * Perform output conversion on a string, and encode for safe HTML output.
3348 * @param $text String text to be converted
3349 * @param $isTitle Bool whether this conversion is for the article title
3350 * @return string
3351 * @todo this should get integrated somewhere sane
3353 public function convertHtml( $text, $isTitle = false ) {
3354 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3358 * @param $key string
3359 * @return string
3361 public function convertCategoryKey( $key ) {
3362 return $this->mConverter->convertCategoryKey( $key );
3366 * Get the list of variants supported by this language
3367 * see sample implementation in LanguageZh.php
3369 * @return array an array of language codes
3371 public function getVariants() {
3372 return $this->mConverter->getVariants();
3376 * @return string
3378 public function getPreferredVariant() {
3379 return $this->mConverter->getPreferredVariant();
3383 * @return string
3385 public function getDefaultVariant() {
3386 return $this->mConverter->getDefaultVariant();
3390 * @return string
3392 public function getURLVariant() {
3393 return $this->mConverter->getURLVariant();
3397 * If a language supports multiple variants, it is
3398 * possible that non-existing link in one variant
3399 * actually exists in another variant. this function
3400 * tries to find it. See e.g. LanguageZh.php
3402 * @param $link String: the name of the link
3403 * @param $nt Mixed: the title object of the link
3404 * @param $ignoreOtherCond Boolean: to disable other conditions when
3405 * we need to transclude a template or update a category's link
3406 * @return null the input parameters may be modified upon return
3408 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3409 $this->mConverter->findVariantLink( $link, $nt, $ignoreOtherCond );
3413 * If a language supports multiple variants, converts text
3414 * into an array of all possible variants of the text:
3415 * 'variant' => text in that variant
3417 * @deprecated since 1.17 Use autoConvertToAllVariants()
3419 * @param $text string
3421 * @return string
3423 public function convertLinkToAllVariants( $text ) {
3424 return $this->mConverter->convertLinkToAllVariants( $text );
3428 * returns language specific options used by User::getPageRenderHash()
3429 * for example, the preferred language variant
3431 * @return string
3433 function getExtraHashOptions() {
3434 return $this->mConverter->getExtraHashOptions();
3438 * For languages that support multiple variants, the title of an
3439 * article may be displayed differently in different variants. this
3440 * function returns the apporiate title defined in the body of the article.
3442 * @return string
3444 public function getParsedTitle() {
3445 return $this->mConverter->getParsedTitle();
3449 * Enclose a string with the "no conversion" tag. This is used by
3450 * various functions in the Parser
3452 * @param $text String: text to be tagged for no conversion
3453 * @param $noParse bool
3454 * @return string the tagged text
3456 public function markNoConversion( $text, $noParse = false ) {
3457 return $this->mConverter->markNoConversion( $text, $noParse );
3461 * A regular expression to match legal word-trailing characters
3462 * which should be merged onto a link of the form [[foo]]bar.
3464 * @return string
3466 public function linkTrail() {
3467 return self::$dataCache->getItem( $this->mCode, 'linkTrail' );
3471 * @return Language
3473 function getLangObj() {
3474 return $this;
3478 * Get the RFC 3066 code for this language object
3480 * @return string
3482 public function getCode() {
3483 return $this->mCode;
3487 * Get the code in Bcp47 format which we can use
3488 * inside of html lang="" tags.
3489 * @since 1.19
3490 * @return string
3492 public function getHtmlCode() {
3493 if ( is_null( $this->mHtmlCode ) ) {
3494 $this->mHtmlCode = wfBCP47( $this->getCode() );
3496 return $this->mHtmlCode;
3500 * @param $code string
3502 public function setCode( $code ) {
3503 $this->mCode = $code;
3504 // Ensure we don't leave an incorrect html code lying around
3505 $this->mHtmlCode = null;
3509 * Get the name of a file for a certain language code
3510 * @param $prefix string Prepend this to the filename
3511 * @param $code string Language code
3512 * @param $suffix string Append this to the filename
3513 * @return string $prefix . $mangledCode . $suffix
3515 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
3516 // Protect against path traversal
3517 if ( !Language::isValidCode( $code )
3518 || strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
3520 throw new MWException( "Invalid language code \"$code\"" );
3523 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
3527 * Get the language code from a file name. Inverse of getFileName()
3528 * @param $filename string $prefix . $languageCode . $suffix
3529 * @param $prefix string Prefix before the language code
3530 * @param $suffix string Suffix after the language code
3531 * @return string Language code, or false if $prefix or $suffix isn't found
3533 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
3534 $m = null;
3535 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
3536 preg_quote( $suffix, '/' ) . '/', $filename, $m );
3537 if ( !count( $m ) ) {
3538 return false;
3540 return str_replace( '_', '-', strtolower( $m[1] ) );
3544 * @param $code string
3545 * @return string
3547 public static function getMessagesFileName( $code ) {
3548 global $IP;
3549 $file = self::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
3550 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
3551 return $file;
3555 * @param $code string
3556 * @return string
3558 public static function getClassFileName( $code ) {
3559 global $IP;
3560 return self::getFileName( "$IP/languages/classes/Language", $code, '.php' );
3564 * Get the first fallback for a given language.
3566 * @param $code string
3568 * @return false|string
3570 public static function getFallbackFor( $code ) {
3571 if ( $code === 'en' || !Language::isValidBuiltInCode( $code ) ) {
3572 return false;
3573 } else {
3574 $fallbacks = self::getFallbacksFor( $code );
3575 $first = array_shift( $fallbacks );
3576 return $first;
3581 * Get the ordered list of fallback languages.
3583 * @since 1.19
3584 * @param $code string Language code
3585 * @return array
3587 public static function getFallbacksFor( $code ) {
3588 if ( $code === 'en' || !Language::isValidBuiltInCode( $code ) ) {
3589 return array();
3590 } else {
3591 $v = self::getLocalisationCache()->getItem( $code, 'fallback' );
3592 $v = array_map( 'trim', explode( ',', $v ) );
3593 if ( $v[count( $v ) - 1] !== 'en' ) {
3594 $v[] = 'en';
3596 return $v;
3601 * Get all messages for a given language
3602 * WARNING: this may take a long time. If you just need all message *keys*
3603 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
3605 * @param $code string
3607 * @return array
3609 public static function getMessagesFor( $code ) {
3610 return self::getLocalisationCache()->getItem( $code, 'messages' );
3614 * Get a message for a given language
3616 * @param $key string
3617 * @param $code string
3619 * @return string
3621 public static function getMessageFor( $key, $code ) {
3622 return self::getLocalisationCache()->getSubitem( $code, 'messages', $key );
3626 * Get all message keys for a given language. This is a faster alternative to
3627 * array_keys( Language::getMessagesFor( $code ) )
3629 * @since 1.19
3630 * @param $code string Language code
3631 * @return array of message keys (strings)
3633 public static function getMessageKeysFor( $code ) {
3634 return self::getLocalisationCache()->getSubItemList( $code, 'messages' );
3638 * @param $talk
3639 * @return mixed
3641 function fixVariableInNamespace( $talk ) {
3642 if ( strpos( $talk, '$1' ) === false ) {
3643 return $talk;
3646 global $wgMetaNamespace;
3647 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
3649 # Allow grammar transformations
3650 # Allowing full message-style parsing would make simple requests
3651 # such as action=raw much more expensive than they need to be.
3652 # This will hopefully cover most cases.
3653 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
3654 array( &$this, 'replaceGrammarInNamespace' ), $talk );
3655 return str_replace( ' ', '_', $talk );
3659 * @param $m string
3660 * @return string
3662 function replaceGrammarInNamespace( $m ) {
3663 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
3667 * @throws MWException
3668 * @return array
3670 static function getCaseMaps() {
3671 static $wikiUpperChars, $wikiLowerChars;
3672 if ( isset( $wikiUpperChars ) ) {
3673 return array( $wikiUpperChars, $wikiLowerChars );
3676 wfProfileIn( __METHOD__ );
3677 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
3678 if ( $arr === false ) {
3679 throw new MWException(
3680 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
3682 $wikiUpperChars = $arr['wikiUpperChars'];
3683 $wikiLowerChars = $arr['wikiLowerChars'];
3684 wfProfileOut( __METHOD__ );
3685 return array( $wikiUpperChars, $wikiLowerChars );
3689 * Decode an expiry (block, protection, etc) which has come from the DB
3691 * @param $expiry String: Database expiry String
3692 * @param $format Bool|Int true to process using language functions, or TS_ constant
3693 * to return the expiry in a given timestamp
3694 * @return String
3696 public function formatExpiry( $expiry, $format = true ) {
3697 static $infinity, $infinityMsg;
3698 if ( $infinity === null ) {
3699 $infinityMsg = wfMessage( 'infiniteblock' );
3700 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
3703 if ( $expiry == '' || $expiry == $infinity ) {
3704 return $format === true
3705 ? $infinityMsg
3706 : $infinity;
3707 } else {
3708 return $format === true
3709 ? $this->timeanddate( $expiry, /* User preference timezone */ true )
3710 : wfTimestamp( $format, $expiry );
3715 * @todo Document
3716 * @param $seconds int|float
3717 * @param $format Array Optional
3718 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
3719 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
3720 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
3721 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
3722 * @return string
3724 function formatTimePeriod( $seconds, $format = array() ) {
3725 if ( !is_array( $format ) ) {
3726 $format = array( 'avoid' => $format ); // For backwards compatibility
3728 if ( !isset( $format['avoid'] ) ) {
3729 $format['avoid'] = false;
3731 if ( !isset( $format['noabbrevs' ] ) ) {
3732 $format['noabbrevs'] = false;
3734 $secondsMsg = wfMessage(
3735 $format['noabbrevs'] ? 'seconds' : 'seconds-abbrev' )->inLanguage( $this );
3736 $minutesMsg = wfMessage(
3737 $format['noabbrevs'] ? 'minutes' : 'minutes-abbrev' )->inLanguage( $this );
3738 $hoursMsg = wfMessage(
3739 $format['noabbrevs'] ? 'hours' : 'hours-abbrev' )->inLanguage( $this );
3740 $daysMsg = wfMessage(
3741 $format['noabbrevs'] ? 'days' : 'days-abbrev' )->inLanguage( $this );
3743 if ( round( $seconds * 10 ) < 100 ) {
3744 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
3745 $s = $secondsMsg->params( $s )->text();
3746 } elseif ( round( $seconds ) < 60 ) {
3747 $s = $this->formatNum( round( $seconds ) );
3748 $s = $secondsMsg->params( $s )->text();
3749 } elseif ( round( $seconds ) < 3600 ) {
3750 $minutes = floor( $seconds / 60 );
3751 $secondsPart = round( fmod( $seconds, 60 ) );
3752 if ( $secondsPart == 60 ) {
3753 $secondsPart = 0;
3754 $minutes++;
3756 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3757 $s .= ' ';
3758 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
3759 } elseif ( round( $seconds ) <= 2 * 86400 ) {
3760 $hours = floor( $seconds / 3600 );
3761 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
3762 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
3763 if ( $secondsPart == 60 ) {
3764 $secondsPart = 0;
3765 $minutes++;
3767 if ( $minutes == 60 ) {
3768 $minutes = 0;
3769 $hours++;
3771 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
3772 $s .= ' ';
3773 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3774 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
3775 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
3777 } else {
3778 $days = floor( $seconds / 86400 );
3779 if ( $format['avoid'] === 'avoidminutes' ) {
3780 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
3781 if ( $hours == 24 ) {
3782 $hours = 0;
3783 $days++;
3785 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
3786 $s .= ' ';
3787 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
3788 } elseif ( $format['avoid'] === 'avoidseconds' ) {
3789 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
3790 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
3791 if ( $minutes == 60 ) {
3792 $minutes = 0;
3793 $hours++;
3795 if ( $hours == 24 ) {
3796 $hours = 0;
3797 $days++;
3799 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
3800 $s .= ' ';
3801 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
3802 $s .= ' ';
3803 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3804 } else {
3805 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
3806 $s .= ' ';
3807 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
3810 return $s;
3814 * Format a bitrate for output, using an appropriate
3815 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
3817 * This use base 1000. For base 1024 use formatSize(), for another base
3818 * see formatComputingNumbers()
3820 * @param $bps int
3821 * @return string
3823 function formatBitrate( $bps ) {
3824 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
3828 * @param $size int Size of the unit
3829 * @param $boundary int Size boundary (1000, or 1024 in most cases)
3830 * @param $messageKey string Message key to be uesd
3831 * @return string
3833 function formatComputingNumbers( $size, $boundary, $messageKey ) {
3834 if ( $size <= 0 ) {
3835 return str_replace( '$1', $this->formatNum( $size ),
3836 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
3839 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
3840 $index = 0;
3842 $maxIndex = count( $sizes ) - 1;
3843 while ( $size >= $boundary && $index < $maxIndex ) {
3844 $index++;
3845 $size /= $boundary;
3848 // For small sizes no decimal places necessary
3849 $round = 0;
3850 if ( $index > 1 ) {
3851 // For MB and bigger two decimal places are smarter
3852 $round = 2;
3854 $msg = str_replace( '$1', $sizes[$index], $messageKey );
3856 $size = round( $size, $round );
3857 $text = $this->getMessageFromDB( $msg );
3858 return str_replace( '$1', $this->formatNum( $size ), $text );
3862 * Format a size in bytes for output, using an appropriate
3863 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
3865 * This method use base 1024. For base 1000 use formatBitrate(), for
3866 * another base see formatComputingNumbers()
3868 * @param $size int Size to format
3869 * @return string Plain text (not HTML)
3871 function formatSize( $size ) {
3872 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
3876 * Make a list item, used by various special pages
3878 * @param $page String Page link
3879 * @param $details String Text between brackets
3880 * @param $oppositedm Boolean Add the direction mark opposite to your
3881 * language, to display text properly
3882 * @return String
3884 function specialList( $page, $details, $oppositedm = true ) {
3885 $dirmark = ( $oppositedm ? $this->getDirMark( true ) : '' ) .
3886 $this->getDirMark();
3887 $details = $details ? $dirmark . $this->getMessageFromDB( 'word-separator' ) .
3888 wfMsgExt( 'parentheses', array( 'escape', 'replaceafter', 'language' => $this ), $details ) : '';
3889 return $page . $details;
3893 * Generate (prev x| next x) (20|50|100...) type links for paging
3895 * @param $title Title object to link
3896 * @param $offset Integer offset parameter
3897 * @param $limit Integer limit parameter
3898 * @param $query String optional URL query parameter string
3899 * @param $atend Bool optional param for specified if this is the last page
3900 * @return String
3902 public function viewPrevNext( Title $title, $offset, $limit, array $query = array(), $atend = false ) {
3903 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
3905 # Make 'previous' link
3906 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
3907 if ( $offset > 0 ) {
3908 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
3909 $query, $prev, 'prevn-title', 'mw-prevlink' );
3910 } else {
3911 $plink = htmlspecialchars( $prev );
3914 # Make 'next' link
3915 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
3916 if ( $atend ) {
3917 $nlink = htmlspecialchars( $next );
3918 } else {
3919 $nlink = $this->numLink( $title, $offset + $limit, $limit,
3920 $query, $next, 'prevn-title', 'mw-nextlink' );
3923 # Make links to set number of items per page
3924 $numLinks = array();
3925 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
3926 $numLinks[] = $this->numLink( $title, $offset, $num,
3927 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
3930 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
3931 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
3935 * Helper function for viewPrevNext() that generates links
3937 * @param $title Title object to link
3938 * @param $offset Integer offset parameter
3939 * @param $limit Integer limit parameter
3940 * @param $query Array extra query parameters
3941 * @param $link String text to use for the link; will be escaped
3942 * @param $tooltipMsg String name of the message to use as tooltip
3943 * @param $class String value of the "class" attribute of the link
3944 * @return String HTML fragment
3946 private function numLink( Title $title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
3947 $query = array( 'limit' => $limit, 'offset' => $offset ) + $query;
3948 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
3949 return Html::element( 'a', array( 'href' => $title->getLocalURL( $query ),
3950 'title' => $tooltip, 'class' => $class ), $link );
3954 * Get the conversion rule title, if any.
3956 * @return string
3958 public function getConvRuleTitle() {
3959 return $this->mConverter->getConvRuleTitle();