3 * Internationalisation code
10 * @defgroup Language Language
13 if ( !defined( 'MEDIAWIKI' ) ) {
14 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
19 global $wgLanguageNames;
20 require_once( dirname( __FILE__
) . '/Names.php' );
22 if ( function_exists( 'mb_strtoupper' ) ) {
23 mb_internal_encoding( 'UTF-8' );
27 * a fake language converter
37 function __construct( $langobj ) { $this->mLang
= $langobj; }
38 function autoConvertToAllVariants( $text ) { return array( $this->mLang
->getCode() => $text ); }
39 function convert( $t ) { return $t; }
40 function convertTo( $text, $variant ) { return $text; }
41 function convertTitle( $t ) { return $t->getPrefixedText(); }
42 function getVariants() { return array( $this->mLang
->getCode() ); }
43 function getPreferredVariant() { return $this->mLang
->getCode(); }
44 function getDefaultVariant() { return $this->mLang
->getCode(); }
45 function getURLVariant() { return ''; }
46 function getConvRuleTitle() { return false; }
47 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
48 function getExtraHashOptions() { return ''; }
49 function getParsedTitle() { return ''; }
50 function markNoConversion( $text, $noParse = false ) { return $text; }
51 function convertCategoryKey( $key ) { return $key; }
52 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
53 function armourMath( $text ) { return $text; }
57 * Internationalisation code
63 * @var LanguageConverter
67 var $mVariants, $mCode, $mLoaded = false;
68 var $mMagicExtensions = array(), $mMagicHookDone = false;
69 private $mHtmlCode = null;
71 var $dateFormatStrings = array();
72 var $mExtendedSpecialPageAliases;
74 protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
77 * ReplacementArray object caches
79 var $transformData = array();
82 * @var LocalisationCache
84 static public $dataCache;
86 static public $mLangObjCache = array();
88 static public $mWeekdayMsgs = array(
89 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
93 static public $mWeekdayAbbrevMsgs = array(
94 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
97 static public $mMonthMsgs = array(
98 'january', 'february', 'march', 'april', 'may_long', 'june',
99 'july', 'august', 'september', 'october', 'november',
102 static public $mMonthGenMsgs = array(
103 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
104 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
107 static public $mMonthAbbrevMsgs = array(
108 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
109 'sep', 'oct', 'nov', 'dec'
112 static public $mIranianCalendarMonthMsgs = array(
113 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
114 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
115 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
116 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
119 static public $mHebrewCalendarMonthMsgs = array(
120 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
121 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
122 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
123 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
124 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
127 static public $mHebrewCalendarMonthGenMsgs = array(
128 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
129 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
130 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
131 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
132 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
135 static public $mHijriCalendarMonthMsgs = array(
136 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
137 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
138 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
139 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
146 static public $durationIntervals = array(
147 'millennia' => 31557600000,
148 'centuries' => 3155760000,
149 'decades' => 315576000,
150 'years' => 31557600, // 86400 * 365.25
159 * Get a cached language object for a given language code
160 * @param $code String
163 static function factory( $code ) {
164 if ( !isset( self
::$mLangObjCache[$code] ) ) {
165 if ( count( self
::$mLangObjCache ) > 10 ) {
166 // Don't keep a billion objects around, that's stupid.
167 self
::$mLangObjCache = array();
169 self
::$mLangObjCache[$code] = self
::newFromCode( $code );
171 return self
::$mLangObjCache[$code];
175 * Create a language object for a given language code
176 * @param $code String
177 * @throws MWException
180 protected static function newFromCode( $code ) {
181 // Protect against path traversal below
182 if ( !Language
::isValidCode( $code )
183 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
185 throw new MWException( "Invalid language code \"$code\"" );
188 if ( !Language
::isValidBuiltInCode( $code ) ) {
189 // It's not possible to customise this code with class files, so
190 // just return a Language object. This is to support uselang= hacks.
191 $lang = new Language
;
192 $lang->setCode( $code );
196 // Check if there is a language class for the code
197 $class = self
::classFromCode( $code );
198 self
::preloadLanguageClass( $class );
199 if ( MWInit
::classExists( $class ) ) {
204 // Keep trying the fallback list until we find an existing class
205 $fallbacks = Language
::getFallbacksFor( $code );
206 foreach ( $fallbacks as $fallbackCode ) {
207 if ( !Language
::isValidBuiltInCode( $fallbackCode ) ) {
208 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
211 $class = self
::classFromCode( $fallbackCode );
212 self
::preloadLanguageClass( $class );
213 if ( MWInit
::classExists( $class ) ) {
214 $lang = Language
::newFromCode( $fallbackCode );
215 $lang->setCode( $code );
220 throw new MWException( "Invalid fallback sequence for language '$code'" );
224 * Returns true if a language code string is of a valid form, whether or
225 * not it exists. This includes codes which are used solely for
226 * customisation via the MediaWiki namespace.
228 * @param $code string
232 public static function isValidCode( $code ) {
234 strcspn( $code, ":/\\\000" ) === strlen( $code )
235 && !preg_match( Title
::getTitleInvalidRegex(), $code );
239 * Returns true if a language code is of a valid form for the purposes of
240 * internal customisation of MediaWiki, via Messages*.php.
242 * @param $code string
247 public static function isValidBuiltInCode( $code ) {
249 if( !is_string($code) ) {
250 $type = gettype( $code );
251 if( $type === 'object' ) {
252 $addmsg = " of class " . get_class( $code );
256 throw new MWException( __METHOD__
. " must be passed a string, $type given$addmsg" );
259 return preg_match( '/^[a-z0-9-]+$/i', $code );
264 * @return String Name of the language class
266 public static function classFromCode( $code ) {
267 if ( $code == 'en' ) {
270 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
275 * Includes language class files
277 * @param $class string Name of the language class
279 public static function preloadLanguageClass( $class ) {
282 if ( $class === 'Language' ) {
286 if ( !defined( 'MW_COMPILED' ) ) {
287 // Preload base classes to work around APC/PHP5 bug
288 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
289 include_once( "$IP/languages/classes/$class.deps.php" );
291 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
292 include_once( "$IP/languages/classes/$class.php" );
298 * Get the LocalisationCache instance
300 * @return LocalisationCache
302 public static function getLocalisationCache() {
303 if ( is_null( self
::$dataCache ) ) {
304 global $wgLocalisationCacheConf;
305 $class = $wgLocalisationCacheConf['class'];
306 self
::$dataCache = new $class( $wgLocalisationCacheConf );
308 return self
::$dataCache;
311 function __construct() {
312 $this->mConverter
= new FakeConverter( $this );
313 // Set the code to the name of the descendant
314 if ( get_class( $this ) == 'Language' ) {
317 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
319 self
::getLocalisationCache();
323 * Reduce memory usage
325 function __destruct() {
326 foreach ( $this as $name => $value ) {
327 unset( $this->$name );
332 * Hook which will be called if this is the content language.
333 * Descendants can use this to register hook functions or modify globals
335 function initContLang() { }
338 * Same as getFallbacksFor for current language.
340 * @deprecated in 1.19
342 function getFallbackLanguageCode() {
343 wfDeprecated( __METHOD__
);
344 return self
::getFallbackFor( $this->mCode
);
351 function getFallbackLanguages() {
352 return self
::getFallbacksFor( $this->mCode
);
356 * Exports $wgBookstoreListEn
359 function getBookstoreList() {
360 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
366 public function getNamespaces() {
367 if ( is_null( $this->namespaceNames
) ) {
368 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
370 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
371 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
373 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
375 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
376 if ( $wgMetaNamespaceTalk ) {
377 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
379 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
380 $this->namespaceNames
[NS_PROJECT_TALK
] =
381 $this->fixVariableInNamespace( $talk );
384 # Sometimes a language will be localised but not actually exist on this wiki.
385 foreach ( $this->namespaceNames
as $key => $text ) {
386 if ( !isset( $validNamespaces[$key] ) ) {
387 unset( $this->namespaceNames
[$key] );
391 # The above mixing may leave namespaces out of canonical order.
392 # Re-order by namespace ID number...
393 ksort( $this->namespaceNames
);
395 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames
) );
397 return $this->namespaceNames
;
401 * Arbitrarily set all of the namespace names at once. Mainly used for testing
402 * @param $namespaces Array of namespaces (id => name)
404 public function setNamespaces( array $namespaces ) {
405 $this->namespaceNames
= $namespaces;
409 * A convenience function that returns the same thing as
410 * getNamespaces() except with the array values changed to ' '
411 * where it found '_', useful for producing output to be displayed
412 * e.g. in <select> forms.
416 function getFormattedNamespaces() {
417 $ns = $this->getNamespaces();
418 foreach ( $ns as $k => $v ) {
419 $ns[$k] = strtr( $v, '_', ' ' );
425 * Get a namespace value by key
427 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
428 * echo $mw_ns; // prints 'MediaWiki'
431 * @param $index Int: the array key of the namespace to return
432 * @return mixed, string if the namespace value exists, otherwise false
434 function getNsText( $index ) {
435 $ns = $this->getNamespaces();
436 return isset( $ns[$index] ) ?
$ns[$index] : false;
440 * A convenience function that returns the same thing as
441 * getNsText() except with '_' changed to ' ', useful for
444 * @param $index string
448 function getFormattedNsText( $index ) {
449 $ns = $this->getNsText( $index );
450 return strtr( $ns, '_', ' ' );
454 * Returns gender-dependent namespace alias if available.
455 * @param $index Int: namespace index
456 * @param $gender String: gender key (male, female... )
460 function getGenderNsText( $index, $gender ) {
461 global $wgExtraGenderNamespaces;
463 $ns = $wgExtraGenderNamespaces + self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
464 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
468 * Whether this language makes distinguishes genders for example in
473 function needsGenderDistinction() {
474 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
475 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
476 // $wgExtraGenderNamespaces overrides everything
478 } elseif ( isset( $wgExtraNamespaces[NS_USER
] ) && isset( $wgExtraNamespaces[NS_USER_TALK
] ) ) {
479 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
480 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
483 // Check what is in i18n files
484 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
485 return count( $aliases ) > 0;
490 * Get a namespace key by value, case insensitive.
491 * Only matches namespace names for the current language, not the
492 * canonical ones defined in Namespace.php.
494 * @param $text String
495 * @return mixed An integer if $text is a valid value otherwise false
497 function getLocalNsIndex( $text ) {
498 $lctext = $this->lc( $text );
499 $ids = $this->getNamespaceIds();
500 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
506 function getNamespaceAliases() {
507 if ( is_null( $this->namespaceAliases
) ) {
508 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
512 foreach ( $aliases as $name => $index ) {
513 if ( $index === NS_PROJECT_TALK
) {
514 unset( $aliases[$name] );
515 $name = $this->fixVariableInNamespace( $name );
516 $aliases[$name] = $index;
521 global $wgExtraGenderNamespaces;
522 $genders = $wgExtraGenderNamespaces +
(array)self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
523 foreach ( $genders as $index => $forms ) {
524 foreach ( $forms as $alias ) {
525 $aliases[$alias] = $index;
529 $this->namespaceAliases
= $aliases;
531 return $this->namespaceAliases
;
537 function getNamespaceIds() {
538 if ( is_null( $this->mNamespaceIds
) ) {
539 global $wgNamespaceAliases;
540 # Put namespace names and aliases into a hashtable.
541 # If this is too slow, then we should arrange it so that it is done
542 # before caching. The catch is that at pre-cache time, the above
543 # class-specific fixup hasn't been done.
544 $this->mNamespaceIds
= array();
545 foreach ( $this->getNamespaces() as $index => $name ) {
546 $this->mNamespaceIds
[$this->lc( $name )] = $index;
548 foreach ( $this->getNamespaceAliases() as $name => $index ) {
549 $this->mNamespaceIds
[$this->lc( $name )] = $index;
551 if ( $wgNamespaceAliases ) {
552 foreach ( $wgNamespaceAliases as $name => $index ) {
553 $this->mNamespaceIds
[$this->lc( $name )] = $index;
557 return $this->mNamespaceIds
;
561 * Get a namespace key by value, case insensitive. Canonical namespace
562 * names override custom ones defined for the current language.
564 * @param $text String
565 * @return mixed An integer if $text is a valid value otherwise false
567 function getNsIndex( $text ) {
568 $lctext = $this->lc( $text );
569 $ns = MWNamespace
::getCanonicalIndex( $lctext );
570 if ( $ns !== null ) {
573 $ids = $this->getNamespaceIds();
574 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
578 * short names for language variants used for language conversion links.
580 * @param $code String
581 * @param $usemsg bool Use the "variantname-xyz" message if it exists
584 function getVariantname( $code, $usemsg = true ) {
585 $msg = "variantname-$code";
586 if ( $usemsg && wfMessage( $msg )->exists() ) {
587 return $this->getMessageFromDB( $msg );
589 $name = self
::fetchLanguageName( $code );
591 return $name; # if it's defined as a language name, show that
593 # otherwise, output the language code
599 * @param $name string
602 function specialPage( $name ) {
603 $aliases = $this->getSpecialPageAliases();
604 if ( isset( $aliases[$name][0] ) ) {
605 $name = $aliases[$name][0];
607 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
613 function getQuickbarSettings() {
615 $this->getMessage( 'qbsettings-none' ),
616 $this->getMessage( 'qbsettings-fixedleft' ),
617 $this->getMessage( 'qbsettings-fixedright' ),
618 $this->getMessage( 'qbsettings-floatingleft' ),
619 $this->getMessage( 'qbsettings-floatingright' ),
620 $this->getMessage( 'qbsettings-directionality' )
627 function getDatePreferences() {
628 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
634 function getDateFormats() {
635 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
639 * @return array|string
641 function getDefaultDateFormat() {
642 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
643 if ( $df === 'dmy or mdy' ) {
644 global $wgAmericanDates;
645 return $wgAmericanDates ?
'mdy' : 'dmy';
654 function getDatePreferenceMigrationMap() {
655 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
662 function getImageFile( $image ) {
663 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
669 function getExtraUserToggles() {
670 return (array)self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
677 function getUserToggle( $tog ) {
678 return $this->getMessageFromDB( "tog-$tog" );
682 * Get native language names, indexed by code.
683 * Only those defined in MediaWiki, no other data like CLDR.
684 * If $customisedOnly is true, only returns codes with a messages file
686 * @param $customisedOnly bool
689 * @deprecated in 1.20, use fetchLanguageNames()
691 public static function getLanguageNames( $customisedOnly = false ) {
692 return self
::fetchLanguageNames( null, $customisedOnly ?
'mwfile' : 'mw' );
696 * Get translated language names. This is done on best effort and
697 * by default this is exactly the same as Language::getLanguageNames.
698 * The CLDR extension provides translated names.
699 * @param $code String Language code.
700 * @return Array language code => language name
702 * @deprecated in 1.20, use fetchLanguageNames()
704 public static function getTranslatedLanguageNames( $code ) {
705 return self
::fetchLanguageNames( $code, 'all' );
709 * Get an array of language names, indexed by code.
710 * @param $inLanguage null|string: Code of language in which to return the names
711 * Use null for autonyms (native names)
712 * @param $include string:
713 * 'all' all available languages
714 * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
715 * 'mwfile' only if the language is in 'mw' *and* has a message file
716 * @return array: language code => language name
719 public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
720 global $wgExtraLanguageNames;
721 static $coreLanguageNames;
723 if ( $coreLanguageNames === null ) {
724 include( MWInit
::compiledPath( 'languages/Names.php' ) );
730 # TODO: also include when $inLanguage is null, when this code is more efficient
731 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
734 $mwNames = $wgExtraLanguageNames +
$coreLanguageNames;
735 foreach ( $mwNames as $mwCode => $mwName ) {
736 # - Prefer own MediaWiki native name when not using the hook
737 # - For other names just add if not added through the hook
738 if ( $mwCode === $inLanguage ||
!isset( $names[$mwCode] ) ) {
739 $names[$mwCode] = $mwName;
743 if ( $include === 'all' ) {
748 $coreCodes = array_keys( $mwNames );
749 foreach( $coreCodes as $coreCode ) {
750 $returnMw[$coreCode] = $names[$coreCode];
753 if( $include === 'mwfile' ) {
754 $namesMwFile = array();
755 # We do this using a foreach over the codes instead of a directory
756 # loop so that messages files in extensions will work correctly.
757 foreach ( $returnMw as $code => $value ) {
758 if ( is_readable( self
::getMessagesFileName( $code ) ) ) {
759 $namesMwFile[$code] = $names[$code];
764 # 'mw' option; default if it's not one of the other two options (all/mwfile)
769 * @param $code string: The code of the language for which to get the name
770 * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
771 * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
772 * @return string: Language name or empty
775 public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
776 $array = self
::fetchLanguageNames( $inLanguage, $include );
777 return !array_key_exists( $code, $array ) ?
'' : $array[$code];
781 * Get a message from the MediaWiki namespace.
783 * @param $msg String: message name
786 function getMessageFromDB( $msg ) {
787 return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
791 * Get the native language name of $code.
792 * Only if defined in MediaWiki, no other data like CLDR.
793 * @param $code string
795 * @deprecated in 1.20, use fetchLanguageName()
797 function getLanguageName( $code ) {
798 return self
::fetchLanguageName( $code );
805 function getMonthName( $key ) {
806 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
812 function getMonthNamesArray() {
813 $monthNames = array( '' );
814 for ( $i = 1; $i < 13; $i++
) {
815 $monthNames[] = $this->getMonthName( $i );
824 function getMonthNameGen( $key ) {
825 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
832 function getMonthAbbreviation( $key ) {
833 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
839 function getMonthAbbreviationsArray() {
840 $monthNames = array( '' );
841 for ( $i = 1; $i < 13; $i++
) {
842 $monthNames[] = $this->getMonthAbbreviation( $i );
851 function getWeekdayName( $key ) {
852 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
859 function getWeekdayAbbreviation( $key ) {
860 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
867 function getIranianCalendarMonthName( $key ) {
868 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
875 function getHebrewCalendarMonthName( $key ) {
876 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
883 function getHebrewCalendarMonthNameGen( $key ) {
884 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
891 function getHijriCalendarMonthName( $key ) {
892 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
896 * This is a workalike of PHP's date() function, but with better
897 * internationalisation, a reduced set of format characters, and a better
900 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
901 * PHP manual for definitions. There are a number of extensions, which
904 * xn Do not translate digits of the next numeric format character
905 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
906 * xr Use roman numerals for the next numeric format character
907 * xh Use hebrew numerals for the next numeric format character
909 * xg Genitive month name
911 * xij j (day number) in Iranian calendar
912 * xiF F (month name) in Iranian calendar
913 * xin n (month number) in Iranian calendar
914 * xiy y (two digit year) in Iranian calendar
915 * xiY Y (full year) in Iranian calendar
917 * xjj j (day number) in Hebrew calendar
918 * xjF F (month name) in Hebrew calendar
919 * xjt t (days in month) in Hebrew calendar
920 * xjx xg (genitive month name) in Hebrew calendar
921 * xjn n (month number) in Hebrew calendar
922 * xjY Y (full year) in Hebrew calendar
924 * xmj j (day number) in Hijri calendar
925 * xmF F (month name) in Hijri calendar
926 * xmn n (month number) in Hijri calendar
927 * xmY Y (full year) in Hijri calendar
929 * xkY Y (full year) in Thai solar calendar. Months and days are
930 * identical to the Gregorian calendar
931 * xoY Y (full year) in Minguo calendar or Juche year.
932 * Months and days are identical to the
934 * xtY Y (full year) in Japanese nengo. Months and days are
935 * identical to the Gregorian calendar
937 * Characters enclosed in double quotes will be considered literal (with
938 * the quotes themselves removed). Unmatched quotes will be considered
939 * literal quotes. Example:
941 * "The month is" F => The month is January
944 * Backslash escaping is also supported.
946 * Input timestamp is assumed to be pre-normalized to the desired local
949 * @param $format String
950 * @param $ts String: 14-character timestamp
953 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
957 function sprintfDate( $format, $ts ) {
970 for ( $p = 0; $p < strlen( $format ); $p++
) {
973 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
974 $code .= $format[++
$p];
977 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
978 $code .= $format[++
$p];
989 $rawToggle = !$rawToggle;
998 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
1001 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
1002 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
1005 $num = substr( $ts, 6, 2 );
1008 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
1009 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) +
1 );
1012 $num = intval( substr( $ts, 6, 2 ) );
1016 $iranian = self
::tsToIranian( $ts );
1022 $hijri = self
::tsToHijri( $ts );
1028 $hebrew = self
::tsToHebrew( $ts );
1034 $unix = wfTimestamp( TS_UNIX
, $ts );
1036 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) +
1 );
1040 $unix = wfTimestamp( TS_UNIX
, $ts );
1042 $w = gmdate( 'w', $unix );
1047 $unix = wfTimestamp( TS_UNIX
, $ts );
1049 $num = gmdate( 'w', $unix );
1053 $unix = wfTimestamp( TS_UNIX
, $ts );
1055 $num = gmdate( 'z', $unix );
1059 $unix = wfTimestamp( TS_UNIX
, $ts );
1061 $num = gmdate( 'W', $unix );
1064 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1068 $iranian = self
::tsToIranian( $ts );
1070 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1074 $hijri = self
::tsToHijri( $ts );
1076 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1080 $hebrew = self
::tsToHebrew( $ts );
1082 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1085 $num = substr( $ts, 4, 2 );
1088 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1091 $num = intval( substr( $ts, 4, 2 ) );
1095 $iranian = self
::tsToIranian( $ts );
1101 $hijri = self
::tsToHijri ( $ts );
1107 $hebrew = self
::tsToHebrew( $ts );
1113 $unix = wfTimestamp( TS_UNIX
, $ts );
1115 $num = gmdate( 't', $unix );
1119 $hebrew = self
::tsToHebrew( $ts );
1125 $unix = wfTimestamp( TS_UNIX
, $ts );
1127 $num = gmdate( 'L', $unix );
1131 $unix = wfTimestamp( TS_UNIX
, $ts );
1133 $num = gmdate( 'o', $unix );
1136 $num = substr( $ts, 0, 4 );
1140 $iranian = self
::tsToIranian( $ts );
1146 $hijri = self
::tsToHijri( $ts );
1152 $hebrew = self
::tsToHebrew( $ts );
1158 $thai = self
::tsToYear( $ts, 'thai' );
1164 $minguo = self
::tsToYear( $ts, 'minguo' );
1170 $tenno = self
::tsToYear( $ts, 'tenno' );
1175 $num = substr( $ts, 2, 2 );
1179 $iranian = self
::tsToIranian( $ts );
1181 $num = substr( $iranian[0], -2 );
1184 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
1187 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
1190 $h = substr( $ts, 8, 2 );
1191 $num = $h %
12 ?
$h %
12 : 12;
1194 $num = intval( substr( $ts, 8, 2 ) );
1197 $h = substr( $ts, 8, 2 );
1198 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
1201 $num = substr( $ts, 8, 2 );
1204 $num = substr( $ts, 10, 2 );
1207 $num = substr( $ts, 12, 2 );
1211 $unix = wfTimestamp( TS_UNIX
, $ts );
1213 $s .= gmdate( 'c', $unix );
1217 $unix = wfTimestamp( TS_UNIX
, $ts );
1219 $s .= gmdate( 'r', $unix );
1223 $unix = wfTimestamp( TS_UNIX
, $ts );
1228 # Backslash escaping
1229 if ( $p < strlen( $format ) - 1 ) {
1230 $s .= $format[++
$p];
1237 if ( $p < strlen( $format ) - 1 ) {
1238 $endQuote = strpos( $format, '"', $p +
1 );
1239 if ( $endQuote === false ) {
1240 # No terminating quote, assume literal "
1243 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1247 # Quote at end of string, assume literal "
1254 if ( $num !== false ) {
1255 if ( $rawToggle ||
$raw ) {
1258 } elseif ( $roman ) {
1259 $s .= self
::romanNumeral( $num );
1261 } elseif ( $hebrewNum ) {
1262 $s .= self
::hebrewNumeral( $num );
1265 $s .= $this->formatNum( $num, true );
1272 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1273 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1276 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1277 * Gregorian dates to Iranian dates. Originally written in C, it
1278 * is released under the terms of GNU Lesser General Public
1279 * License. Conversion to PHP was performed by Niklas Laxström.
1281 * Link: http://www.farsiweb.info/jalali/jalali.c
1287 private static function tsToIranian( $ts ) {
1288 $gy = substr( $ts, 0, 4 ) -1600;
1289 $gm = substr( $ts, 4, 2 ) -1;
1290 $gd = substr( $ts, 6, 2 ) -1;
1292 # Days passed from the beginning (including leap years)
1294 +
floor( ( $gy +
3 ) / 4 )
1295 - floor( ( $gy +
99 ) / 100 )
1296 +
floor( ( $gy +
399 ) / 400 );
1298 // Add days of the past months of this year
1299 for ( $i = 0; $i < $gm; $i++
) {
1300 $gDayNo +
= self
::$GREG_DAYS[$i];
1304 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1308 // Days passed in current month
1309 $gDayNo +
= (int)$gd;
1311 $jDayNo = $gDayNo - 79;
1313 $jNp = floor( $jDayNo / 12053 );
1316 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1319 if ( $jDayNo >= 366 ) {
1320 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1321 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1324 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1325 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1331 return array( $jy, $jm, $jd );
1335 * Converting Gregorian dates to Hijri dates.
1337 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1339 * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1345 private static function tsToHijri( $ts ) {
1346 $year = substr( $ts, 0, 4 );
1347 $month = substr( $ts, 4, 2 );
1348 $day = substr( $ts, 6, 2 );
1356 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1357 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1360 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1361 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1362 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1365 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1366 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1369 $zl = $zjd -1948440 +
10632;
1370 $zn = (int)( ( $zl - 1 ) / 10631 );
1371 $zl = $zl - 10631 * $zn +
354;
1372 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1373 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1374 $zm = (int)( ( 24 * $zl ) / 709 );
1375 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1376 $zy = 30 * $zn +
$zj - 30;
1378 return array( $zy, $zm, $zd );
1382 * Converting Gregorian dates to Hebrew dates.
1384 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1385 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1386 * to translate the relevant functions into PHP and release them under
1389 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1390 * and Adar II is 14. In a non-leap year, Adar is 6.
1396 private static function tsToHebrew( $ts ) {
1398 $year = substr( $ts, 0, 4 );
1399 $month = substr( $ts, 4, 2 );
1400 $day = substr( $ts, 6, 2 );
1402 # Calculate Hebrew year
1403 $hebrewYear = $year +
3760;
1405 # Month number when September = 1, August = 12
1407 if ( $month > 12 ) {
1414 # Calculate day of year from 1 September
1416 for ( $i = 1; $i < $month; $i++
) {
1420 # Check if the year is leap
1421 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1424 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1431 # Calculate the start of the Hebrew year
1432 $start = self
::hebrewYearStart( $hebrewYear );
1434 # Calculate next year's start
1435 if ( $dayOfYear <= $start ) {
1436 # Day is before the start of the year - it is the previous year
1438 $nextStart = $start;
1442 # Add days since previous year's 1 September
1444 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1448 # Start of the new (previous) year
1449 $start = self
::hebrewYearStart( $hebrewYear );
1452 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1455 # Calculate Hebrew day of year
1456 $hebrewDayOfYear = $dayOfYear - $start;
1458 # Difference between year's days
1459 $diff = $nextStart - $start;
1460 # Add 12 (or 13 for leap years) days to ignore the difference between
1461 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1462 # difference is only about the year type
1463 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1469 # Check the year pattern, and is leap year
1470 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1471 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1472 # and non-leap years
1473 $yearPattern = $diff %
30;
1474 # Check if leap year
1475 $isLeap = $diff >= 30;
1477 # Calculate day in the month from number of day in the Hebrew year
1478 # Don't check Adar - if the day is not in Adar, we will stop before;
1479 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1480 $hebrewDay = $hebrewDayOfYear;
1483 while ( $hebrewMonth <= 12 ) {
1484 # Calculate days in this month
1485 if ( $isLeap && $hebrewMonth == 6 ) {
1486 # Adar in a leap year
1488 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1490 if ( $hebrewDay <= $days ) {
1494 # Subtract the days of Adar I
1495 $hebrewDay -= $days;
1498 if ( $hebrewDay <= $days ) {
1504 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1505 # Cheshvan in a complete year (otherwise as the rule below)
1507 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1508 # Kislev in an incomplete year (otherwise as the rule below)
1511 # Odd months have 30 days, even have 29
1512 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1514 if ( $hebrewDay <= $days ) {
1515 # In the current month
1518 # Subtract the days of the current month
1519 $hebrewDay -= $days;
1520 # Try in the next month
1525 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1529 * This calculates the Hebrew year start, as days since 1 September.
1530 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1531 * Used for Hebrew date.
1537 private static function hebrewYearStart( $year ) {
1538 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1539 $b = intval( ( $year - 1 ) %
4 );
1540 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1544 $Mar = intval( $m );
1550 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1551 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1553 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1555 } elseif ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1559 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1564 * Algorithm to convert Gregorian dates to Thai solar dates,
1565 * Minguo dates or Minguo dates.
1567 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1568 * http://en.wikipedia.org/wiki/Minguo_calendar
1569 * http://en.wikipedia.org/wiki/Japanese_era_name
1571 * @param $ts String: 14-character timestamp
1572 * @param $cName String: calender name
1573 * @return Array: converted year, month, day
1575 private static function tsToYear( $ts, $cName ) {
1576 $gy = substr( $ts, 0, 4 );
1577 $gm = substr( $ts, 4, 2 );
1578 $gd = substr( $ts, 6, 2 );
1580 if ( !strcmp( $cName, 'thai' ) ) {
1582 # Add 543 years to the Gregorian calendar
1583 # Months and days are identical
1584 $gy_offset = $gy +
543;
1585 } elseif ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1587 # Deduct 1911 years from the Gregorian calendar
1588 # Months and days are identical
1589 $gy_offset = $gy - 1911;
1590 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1591 # Nengō dates up to Meiji period
1592 # Deduct years from the Gregorian calendar
1593 # depending on the nengo periods
1594 # Months and days are identical
1595 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1597 $gy_gannen = $gy - 1868 +
1;
1598 $gy_offset = $gy_gannen;
1599 if ( $gy_gannen == 1 ) {
1602 $gy_offset = '明治' . $gy_offset;
1604 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1605 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1606 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1607 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1608 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1612 $gy_gannen = $gy - 1912 +
1;
1613 $gy_offset = $gy_gannen;
1614 if ( $gy_gannen == 1 ) {
1617 $gy_offset = '大正' . $gy_offset;
1619 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1620 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1621 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1625 $gy_gannen = $gy - 1926 +
1;
1626 $gy_offset = $gy_gannen;
1627 if ( $gy_gannen == 1 ) {
1630 $gy_offset = '昭和' . $gy_offset;
1633 $gy_gannen = $gy - 1989 +
1;
1634 $gy_offset = $gy_gannen;
1635 if ( $gy_gannen == 1 ) {
1638 $gy_offset = '平成' . $gy_offset;
1644 return array( $gy_offset, $gm, $gd );
1648 * Roman number formatting up to 3000
1654 static function romanNumeral( $num ) {
1655 static $table = array(
1656 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1657 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1658 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1659 array( '', 'M', 'MM', 'MMM' )
1662 $num = intval( $num );
1663 if ( $num > 3000 ||
$num <= 0 ) {
1668 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1669 if ( $num >= $pow10 ) {
1670 $s .= $table[$i][(int)floor( $num / $pow10 )];
1672 $num = $num %
$pow10;
1678 * Hebrew Gematria number formatting up to 9999
1684 static function hebrewNumeral( $num ) {
1685 static $table = array(
1686 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1687 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1688 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1689 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1692 $num = intval( $num );
1693 if ( $num > 9999 ||
$num <= 0 ) {
1698 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1699 if ( $num >= $pow10 ) {
1700 if ( $num == 15 ||
$num == 16 ) {
1701 $s .= $table[0][9] . $table[0][$num - 9];
1704 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1705 if ( $pow10 == 1000 ) {
1710 $num = $num %
$pow10;
1712 if ( strlen( $s ) == 2 ) {
1715 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1716 $str .= substr( $s, strlen( $s ) - 2, 2 );
1718 $start = substr( $str, 0, strlen( $str ) - 2 );
1719 $end = substr( $str, strlen( $str ) - 2 );
1722 $str = $start . 'ך';
1725 $str = $start . 'ם';
1728 $str = $start . 'ן';
1731 $str = $start . 'ף';
1734 $str = $start . 'ץ';
1741 * Used by date() and time() to adjust the time output.
1743 * @param $ts Int the time in date('YmdHis') format
1744 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1745 * get user timecorrection setting)
1748 function userAdjust( $ts, $tz = false ) {
1749 global $wgUser, $wgLocalTZoffset;
1751 if ( $tz === false ) {
1752 $tz = $wgUser->getOption( 'timecorrection' );
1755 $data = explode( '|', $tz, 3 );
1757 if ( $data[0] == 'ZoneInfo' ) {
1758 wfSuppressWarnings();
1759 $userTZ = timezone_open( $data[2] );
1760 wfRestoreWarnings();
1761 if ( $userTZ !== false ) {
1762 $date = date_create( $ts, timezone_open( 'UTC' ) );
1763 date_timezone_set( $date, $userTZ );
1764 $date = date_format( $date, 'YmdHis' );
1767 # Unrecognized timezone, default to 'Offset' with the stored offset.
1768 $data[0] = 'Offset';
1772 if ( $data[0] == 'System' ||
$tz == '' ) {
1773 # Global offset in minutes.
1774 if ( isset( $wgLocalTZoffset ) ) {
1775 $minDiff = $wgLocalTZoffset;
1777 } elseif ( $data[0] == 'Offset' ) {
1778 $minDiff = intval( $data[1] );
1780 $data = explode( ':', $tz );
1781 if ( count( $data ) == 2 ) {
1782 $data[0] = intval( $data[0] );
1783 $data[1] = intval( $data[1] );
1784 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1785 if ( $data[0] < 0 ) {
1786 $minDiff = -$minDiff;
1789 $minDiff = intval( $data[0] ) * 60;
1793 # No difference ? Return time unchanged
1794 if ( 0 == $minDiff ) {
1798 wfSuppressWarnings(); // E_STRICT system time bitching
1799 # Generate an adjusted date; take advantage of the fact that mktime
1800 # will normalize out-of-range values so we don't have to split $minDiff
1801 # into hours and minutes.
1803 (int)substr( $ts, 8, 2 ) ), # Hours
1804 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
1805 (int)substr( $ts, 12, 2 ), # Seconds
1806 (int)substr( $ts, 4, 2 ), # Month
1807 (int)substr( $ts, 6, 2 ), # Day
1808 (int)substr( $ts, 0, 4 ) ); # Year
1810 $date = date( 'YmdHis', $t );
1811 wfRestoreWarnings();
1817 * This is meant to be used by time(), date(), and timeanddate() to get
1818 * the date preference they're supposed to use, it should be used in
1822 * function timeanddate([...], $format = true) {
1823 * $datePreference = $this->dateFormat($format);
1828 * @param $usePrefs Mixed: if true, the user's preference is used
1829 * if false, the site/language default is used
1830 * if int/string, assumed to be a format.
1833 function dateFormat( $usePrefs = true ) {
1836 if ( is_bool( $usePrefs ) ) {
1838 $datePreference = $wgUser->getDatePreference();
1840 $datePreference = (string)User
::getDefaultOption( 'date' );
1843 $datePreference = (string)$usePrefs;
1847 if ( $datePreference == '' ) {
1851 return $datePreference;
1855 * Get a format string for a given type and preference
1856 * @param $type string May be date, time or both
1857 * @param $pref string The format name as it appears in Messages*.php
1861 function getDateFormatString( $type, $pref ) {
1862 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
1863 if ( $pref == 'default' ) {
1864 $pref = $this->getDefaultDateFormat();
1865 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1867 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1868 if ( is_null( $df ) ) {
1869 $pref = $this->getDefaultDateFormat();
1870 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1873 $this->dateFormatStrings
[$type][$pref] = $df;
1875 return $this->dateFormatStrings
[$type][$pref];
1879 * @param $ts Mixed: the time format which needs to be turned into a
1880 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1881 * @param $adj Bool: whether to adjust the time output according to the
1882 * user configured offset ($timecorrection)
1883 * @param $format Mixed: true to use user's date format preference
1884 * @param $timecorrection String|bool the time offset as returned by
1885 * validateTimeZone() in Special:Preferences
1888 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1889 $ts = wfTimestamp( TS_MW
, $ts );
1891 $ts = $this->userAdjust( $ts, $timecorrection );
1893 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1894 return $this->sprintfDate( $df, $ts );
1898 * @param $ts Mixed: the time format which needs to be turned into a
1899 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1900 * @param $adj Bool: whether to adjust the time output according to the
1901 * user configured offset ($timecorrection)
1902 * @param $format Mixed: true to use user's date format preference
1903 * @param $timecorrection String|bool the time offset as returned by
1904 * validateTimeZone() in Special:Preferences
1907 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1908 $ts = wfTimestamp( TS_MW
, $ts );
1910 $ts = $this->userAdjust( $ts, $timecorrection );
1912 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1913 return $this->sprintfDate( $df, $ts );
1917 * @param $ts Mixed: the time format which needs to be turned into a
1918 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1919 * @param $adj Bool: whether to adjust the time output according to the
1920 * user configured offset ($timecorrection)
1921 * @param $format Mixed: what format to return, if it's false output the
1922 * default one (default true)
1923 * @param $timecorrection String|bool the time offset as returned by
1924 * validateTimeZone() in Special:Preferences
1927 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1928 $ts = wfTimestamp( TS_MW
, $ts );
1930 $ts = $this->userAdjust( $ts, $timecorrection );
1932 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1933 return $this->sprintfDate( $df, $ts );
1937 * Takes a number of seconds and turns it into a text using values such as hours and minutes.
1941 * @param integer $seconds The amount of seconds.
1942 * @param array $chosenIntervals The intervals to enable.
1946 public function formatDuration( $seconds, array $chosenIntervals = array() ) {
1947 $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
1949 $segments = array();
1951 foreach ( $intervals as $intervalName => $intervalValue ) {
1952 $message = new Message( 'duration-' . $intervalName, array( $intervalValue ) );
1953 $segments[] = $message->inLanguage( $this )->escaped();
1956 return $this->listToText( $segments );
1960 * Takes a number of seconds and returns an array with a set of corresponding intervals.
1961 * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
1965 * @param integer $seconds The amount of seconds.
1966 * @param array $chosenIntervals The intervals to enable.
1970 public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
1971 if ( empty( $chosenIntervals ) ) {
1972 $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
1975 $intervals = array_intersect_key( self
::$durationIntervals, array_flip( $chosenIntervals ) );
1976 $sortedNames = array_keys( $intervals );
1977 $smallestInterval = array_pop( $sortedNames );
1979 $segments = array();
1981 foreach ( $intervals as $name => $length ) {
1982 $value = floor( $seconds / $length );
1984 if ( $value > 0 ||
( $name == $smallestInterval && empty( $segments ) ) ) {
1985 $seconds -= $value * $length;
1986 $segments[$name] = $value;
1994 * Internal helper function for userDate(), userTime() and userTimeAndDate()
1996 * @param $type String: can be 'date', 'time' or 'both'
1997 * @param $ts Mixed: the time format which needs to be turned into a
1998 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1999 * @param $user User object used to get preferences for timezone and format
2000 * @param $options Array, can contain the following keys:
2001 * - 'timecorrection': time correction, can have the following values:
2002 * - true: use user's preference
2003 * - false: don't use time correction
2004 * - integer: value of time correction in minutes
2005 * - 'format': format to use, can have the following values:
2006 * - true: use user's preference
2007 * - false: use default preference
2008 * - string: format to use
2012 private function internalUserTimeAndDate( $type, $ts, User
$user, array $options ) {
2013 $ts = wfTimestamp( TS_MW
, $ts );
2014 $options +
= array( 'timecorrection' => true, 'format' => true );
2015 if ( $options['timecorrection'] !== false ) {
2016 if ( $options['timecorrection'] === true ) {
2017 $offset = $user->getOption( 'timecorrection' );
2019 $offset = $options['timecorrection'];
2021 $ts = $this->userAdjust( $ts, $offset );
2023 if ( $options['format'] === true ) {
2024 $format = $user->getDatePreference();
2026 $format = $options['format'];
2028 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
2029 return $this->sprintfDate( $df, $ts );
2033 * Get the formatted date for the given timestamp and formatted for
2036 * @param $ts Mixed: the time format which needs to be turned into a
2037 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2038 * @param $user User object used to get preferences for timezone and format
2039 * @param $options Array, can contain the following keys:
2040 * - 'timecorrection': time correction, can have the following values:
2041 * - true: use user's preference
2042 * - false: don't use time correction
2043 * - integer: value of time correction in minutes
2044 * - 'format': format to use, can have the following values:
2045 * - true: use user's preference
2046 * - false: use default preference
2047 * - string: format to use
2051 public function userDate( $ts, User
$user, array $options = array() ) {
2052 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
2056 * Get the formatted time for the given timestamp and formatted for
2059 * @param $ts Mixed: the time format which needs to be turned into a
2060 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2061 * @param $user User object used to get preferences for timezone and format
2062 * @param $options Array, can contain the following keys:
2063 * - 'timecorrection': time correction, can have the following values:
2064 * - true: use user's preference
2065 * - false: don't use time correction
2066 * - integer: value of time correction in minutes
2067 * - 'format': format to use, can have the following values:
2068 * - true: use user's preference
2069 * - false: use default preference
2070 * - string: format to use
2074 public function userTime( $ts, User
$user, array $options = array() ) {
2075 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
2079 * Get the formatted date and time for the given timestamp and formatted for
2082 * @param $ts Mixed: the time format which needs to be turned into a
2083 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2084 * @param $user User object used to get preferences for timezone and format
2085 * @param $options Array, can contain the following keys:
2086 * - 'timecorrection': time correction, can have the following values:
2087 * - true: use user's preference
2088 * - false: don't use time correction
2089 * - integer: value of time correction in minutes
2090 * - 'format': format to use, can have the following values:
2091 * - true: use user's preference
2092 * - false: use default preference
2093 * - string: format to use
2097 public function userTimeAndDate( $ts, User
$user, array $options = array() ) {
2098 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
2102 * @param $key string
2103 * @return array|null
2105 function getMessage( $key ) {
2106 return self
::$dataCache->getSubitem( $this->mCode
, 'messages', $key );
2112 function getAllMessages() {
2113 return self
::$dataCache->getItem( $this->mCode
, 'messages' );
2122 function iconv( $in, $out, $string ) {
2123 # This is a wrapper for iconv in all languages except esperanto,
2124 # which does some nasty x-conversions beforehand
2126 # Even with //IGNORE iconv can whine about illegal characters in
2127 # *input* string. We just ignore those too.
2128 # REF: http://bugs.php.net/bug.php?id=37166
2129 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
2130 wfSuppressWarnings();
2131 $text = iconv( $in, $out . '//IGNORE', $string );
2132 wfRestoreWarnings();
2136 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
2139 * @param $matches array
2140 * @return mixed|string
2142 function ucwordbreaksCallbackAscii( $matches ) {
2143 return $this->ucfirst( $matches[1] );
2147 * @param $matches array
2150 function ucwordbreaksCallbackMB( $matches ) {
2151 return mb_strtoupper( $matches[0] );
2155 * @param $matches array
2158 function ucCallback( $matches ) {
2159 list( $wikiUpperChars ) = self
::getCaseMaps();
2160 return strtr( $matches[1], $wikiUpperChars );
2164 * @param $matches array
2167 function lcCallback( $matches ) {
2168 list( , $wikiLowerChars ) = self
::getCaseMaps();
2169 return strtr( $matches[1], $wikiLowerChars );
2173 * @param $matches array
2176 function ucwordsCallbackMB( $matches ) {
2177 return mb_strtoupper( $matches[0] );
2181 * @param $matches array
2184 function ucwordsCallbackWiki( $matches ) {
2185 list( $wikiUpperChars ) = self
::getCaseMaps();
2186 return strtr( $matches[0], $wikiUpperChars );
2190 * Make a string's first character uppercase
2192 * @param $str string
2196 function ucfirst( $str ) {
2198 if ( $o < 96 ) { // if already uppercase...
2200 } elseif ( $o < 128 ) {
2201 return ucfirst( $str ); // use PHP's ucfirst()
2203 // fall back to more complex logic in case of multibyte strings
2204 return $this->uc( $str, true );
2209 * Convert a string to uppercase
2211 * @param $str string
2212 * @param $first bool
2216 function uc( $str, $first = false ) {
2217 if ( function_exists( 'mb_strtoupper' ) ) {
2219 if ( $this->isMultibyte( $str ) ) {
2220 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2222 return ucfirst( $str );
2225 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
2228 if ( $this->isMultibyte( $str ) ) {
2229 $x = $first ?
'^' : '';
2230 return preg_replace_callback(
2231 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2232 array( $this, 'ucCallback' ),
2236 return $first ?
ucfirst( $str ) : strtoupper( $str );
2242 * @param $str string
2243 * @return mixed|string
2245 function lcfirst( $str ) {
2248 return strval( $str );
2249 } elseif ( $o >= 128 ) {
2250 return $this->lc( $str, true );
2251 } elseif ( $o > 96 ) {
2254 $str[0] = strtolower( $str[0] );
2260 * @param $str string
2261 * @param $first bool
2262 * @return mixed|string
2264 function lc( $str, $first = false ) {
2265 if ( function_exists( 'mb_strtolower' ) ) {
2267 if ( $this->isMultibyte( $str ) ) {
2268 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2270 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2273 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
2276 if ( $this->isMultibyte( $str ) ) {
2277 $x = $first ?
'^' : '';
2278 return preg_replace_callback(
2279 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2280 array( $this, 'lcCallback' ),
2284 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2290 * @param $str string
2293 function isMultibyte( $str ) {
2294 return (bool)preg_match( '/[\x80-\xff]/', $str );
2298 * @param $str string
2299 * @return mixed|string
2301 function ucwords( $str ) {
2302 if ( $this->isMultibyte( $str ) ) {
2303 $str = $this->lc( $str );
2305 // regexp to find first letter in each word (i.e. after each space)
2306 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2308 // function to use to capitalize a single char
2309 if ( function_exists( 'mb_strtoupper' ) ) {
2310 return preg_replace_callback(
2312 array( $this, 'ucwordsCallbackMB' ),
2316 return preg_replace_callback(
2318 array( $this, 'ucwordsCallbackWiki' ),
2323 return ucwords( strtolower( $str ) );
2328 * capitalize words at word breaks
2330 * @param $str string
2333 function ucwordbreaks( $str ) {
2334 if ( $this->isMultibyte( $str ) ) {
2335 $str = $this->lc( $str );
2337 // since \b doesn't work for UTF-8, we explicitely define word break chars
2338 $breaks = "[ \-\(\)\}\{\.,\?!]";
2340 // find first letter after word break
2341 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2343 if ( function_exists( 'mb_strtoupper' ) ) {
2344 return preg_replace_callback(
2346 array( $this, 'ucwordbreaksCallbackMB' ),
2350 return preg_replace_callback(
2352 array( $this, 'ucwordsCallbackWiki' ),
2357 return preg_replace_callback(
2358 '/\b([\w\x80-\xff]+)\b/',
2359 array( $this, 'ucwordbreaksCallbackAscii' ),
2366 * Return a case-folded representation of $s
2368 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2369 * and $s2 are the same except for the case of their characters. It is not
2370 * necessary for the value returned to make sense when displayed.
2372 * Do *not* perform any other normalisation in this function. If a caller
2373 * uses this function when it should be using a more general normalisation
2374 * function, then fix the caller.
2380 function caseFold( $s ) {
2381 return $this->uc( $s );
2388 function checkTitleEncoding( $s ) {
2389 if ( is_array( $s ) ) {
2390 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2392 # Check for non-UTF-8 URLs
2393 $ishigh = preg_match( '/[\x80-\xff]/', $s );
2398 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2399 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
2404 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2410 function fallback8bitEncoding() {
2411 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
2415 * Most writing systems use whitespace to break up words.
2416 * Some languages such as Chinese don't conventionally do this,
2417 * which requires special handling when breaking up words for
2422 function hasWordBreaks() {
2427 * Some languages such as Chinese require word segmentation,
2428 * Specify such segmentation when overridden in derived class.
2430 * @param $string String
2433 function segmentByWord( $string ) {
2438 * Some languages have special punctuation need to be normalized.
2439 * Make such changes here.
2441 * @param $string String
2444 function normalizeForSearch( $string ) {
2445 return self
::convertDoubleWidth( $string );
2449 * convert double-width roman characters to single-width.
2450 * range: ff00-ff5f ~= 0020-007f
2452 * @param $string string
2456 protected static function convertDoubleWidth( $string ) {
2457 static $full = null;
2458 static $half = null;
2460 if ( $full === null ) {
2461 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2462 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2463 $full = str_split( $fullWidth, 3 );
2464 $half = str_split( $halfWidth );
2467 $string = str_replace( $full, $half, $string );
2472 * @param $string string
2473 * @param $pattern string
2476 protected static function insertSpace( $string, $pattern ) {
2477 $string = preg_replace( $pattern, " $1 ", $string );
2478 $string = preg_replace( '/ +/', ' ', $string );
2483 * @param $termsArray array
2486 function convertForSearchResult( $termsArray ) {
2487 # some languages, e.g. Chinese, need to do a conversion
2488 # in order for search results to be displayed correctly
2493 * Get the first character of a string.
2498 function firstChar( $s ) {
2501 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2502 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2507 if ( isset( $matches[1] ) ) {
2508 if ( strlen( $matches[1] ) != 3 ) {
2512 // Break down Hangul syllables to grab the first jamo
2513 $code = utf8ToCodepoint( $matches[1] );
2514 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
2516 } elseif ( $code < 0xb098 ) {
2517 return "\xe3\x84\xb1";
2518 } elseif ( $code < 0xb2e4 ) {
2519 return "\xe3\x84\xb4";
2520 } elseif ( $code < 0xb77c ) {
2521 return "\xe3\x84\xb7";
2522 } elseif ( $code < 0xb9c8 ) {
2523 return "\xe3\x84\xb9";
2524 } elseif ( $code < 0xbc14 ) {
2525 return "\xe3\x85\x81";
2526 } elseif ( $code < 0xc0ac ) {
2527 return "\xe3\x85\x82";
2528 } elseif ( $code < 0xc544 ) {
2529 return "\xe3\x85\x85";
2530 } elseif ( $code < 0xc790 ) {
2531 return "\xe3\x85\x87";
2532 } elseif ( $code < 0xcc28 ) {
2533 return "\xe3\x85\x88";
2534 } elseif ( $code < 0xce74 ) {
2535 return "\xe3\x85\x8a";
2536 } elseif ( $code < 0xd0c0 ) {
2537 return "\xe3\x85\x8b";
2538 } elseif ( $code < 0xd30c ) {
2539 return "\xe3\x85\x8c";
2540 } elseif ( $code < 0xd558 ) {
2541 return "\xe3\x85\x8d";
2543 return "\xe3\x85\x8e";
2550 function initEncoding() {
2551 # Some languages may have an alternate char encoding option
2552 # (Esperanto X-coding, Japanese furigana conversion, etc)
2553 # If this language is used as the primary content language,
2554 # an override to the defaults can be set here on startup.
2561 function recodeForEdit( $s ) {
2562 # For some languages we'll want to explicitly specify
2563 # which characters make it into the edit box raw
2564 # or are converted in some way or another.
2565 global $wgEditEncoding;
2566 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
2569 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2577 function recodeInput( $s ) {
2578 # Take the previous into account.
2579 global $wgEditEncoding;
2580 if ( $wgEditEncoding != '' ) {
2581 $enc = $wgEditEncoding;
2585 if ( $enc == 'UTF-8' ) {
2588 return $this->iconv( $enc, 'UTF-8', $s );
2593 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2594 * also cleans up certain backwards-compatible sequences, converting them
2595 * to the modern Unicode equivalent.
2597 * This is language-specific for performance reasons only.
2603 function normalize( $s ) {
2604 global $wgAllUnicodeFixes;
2605 $s = UtfNormal
::cleanUp( $s );
2606 if ( $wgAllUnicodeFixes ) {
2607 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2608 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2615 * Transform a string using serialized data stored in the given file (which
2616 * must be in the serialized subdirectory of $IP). The file contains pairs
2617 * mapping source characters to destination characters.
2619 * The data is cached in process memory. This will go faster if you have the
2620 * FastStringSearch extension.
2622 * @param $file string
2623 * @param $string string
2625 * @throws MWException
2628 function transformUsingPairFile( $file, $string ) {
2629 if ( !isset( $this->transformData
[$file] ) ) {
2630 $data = wfGetPrecompiledData( $file );
2631 if ( $data === false ) {
2632 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2634 $this->transformData
[$file] = new ReplacementArray( $data );
2636 return $this->transformData
[$file]->replace( $string );
2640 * For right-to-left language support
2645 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2649 * Return the correct HTML 'dir' attribute value for this language.
2653 return $this->isRTL() ?
'rtl' : 'ltr';
2657 * Return 'left' or 'right' as appropriate alignment for line-start
2658 * for this language's text direction.
2660 * Should be equivalent to CSS3 'start' text-align value....
2664 function alignStart() {
2665 return $this->isRTL() ?
'right' : 'left';
2669 * Return 'right' or 'left' as appropriate alignment for line-end
2670 * for this language's text direction.
2672 * Should be equivalent to CSS3 'end' text-align value....
2676 function alignEnd() {
2677 return $this->isRTL() ?
'left' : 'right';
2681 * A hidden direction mark (LRM or RLM), depending on the language direction.
2682 * Unlike getDirMark(), this function returns the character as an HTML entity.
2683 * This function should be used when the output is guaranteed to be HTML,
2684 * because it makes the output HTML source code more readable. When
2685 * the output is plain text or can be escaped, getDirMark() should be used.
2687 * @param $opposite Boolean Get the direction mark opposite to your language
2690 function getDirMarkEntity( $opposite = false ) {
2691 if ( $opposite ) { return $this->isRTL() ?
'‎' : '‏'; }
2692 return $this->isRTL() ?
'‏' : '‎';
2696 * A hidden direction mark (LRM or RLM), depending on the language direction.
2697 * This function produces them as invisible Unicode characters and
2698 * the output may be hard to read and debug, so it should only be used
2699 * when the output is plain text or can be escaped. When the output is
2700 * HTML, use getDirMarkEntity() instead.
2702 * @param $opposite Boolean Get the direction mark opposite to your language
2705 function getDirMark( $opposite = false ) {
2706 $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
2707 $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
2708 if ( $opposite ) { return $this->isRTL() ?
$lrm : $rlm; }
2709 return $this->isRTL() ?
$rlm : $lrm;
2715 function capitalizeAllNouns() {
2716 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2720 * An arrow, depending on the language direction.
2722 * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
2725 function getArrow( $direction = 'forwards' ) {
2726 switch ( $direction ) {
2728 return $this->isRTL() ?
'←' : '→';
2730 return $this->isRTL() ?
'→' : '←';
2743 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2747 function linkPrefixExtension() {
2748 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2754 function getMagicWords() {
2755 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2758 protected function doMagicHook() {
2759 if ( $this->mMagicHookDone
) {
2762 $this->mMagicHookDone
= true;
2763 wfProfileIn( 'LanguageGetMagic' );
2764 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2765 wfProfileOut( 'LanguageGetMagic' );
2769 * Fill a MagicWord object with data from here
2773 function getMagic( $mw ) {
2774 $this->doMagicHook();
2776 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
2777 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
2779 $magicWords = $this->getMagicWords();
2780 if ( isset( $magicWords[$mw->mId
] ) ) {
2781 $rawEntry = $magicWords[$mw->mId
];
2787 if ( !is_array( $rawEntry ) ) {
2788 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
2790 $mw->mCaseSensitive
= $rawEntry[0];
2791 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
2796 * Add magic words to the extension array
2798 * @param $newWords array
2800 function addMagicWordsByLang( $newWords ) {
2801 $fallbackChain = $this->getFallbackLanguages();
2802 $fallbackChain = array_reverse( $fallbackChain );
2803 foreach ( $fallbackChain as $code ) {
2804 if ( isset( $newWords[$code] ) ) {
2805 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
2811 * Get special page names, as an associative array
2812 * case folded alias => real name
2814 function getSpecialPageAliases() {
2815 // Cache aliases because it may be slow to load them
2816 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
2818 $this->mExtendedSpecialPageAliases
=
2819 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
2820 wfRunHooks( 'LanguageGetSpecialPageAliases',
2821 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
2824 return $this->mExtendedSpecialPageAliases
;
2828 * Italic is unsuitable for some languages
2830 * @param $text String: the text to be emphasized.
2833 function emphasize( $text ) {
2834 return "<em>$text</em>";
2838 * Normally we output all numbers in plain en_US style, that is
2839 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2840 * point twohundredthirtyfive. However this is not suitable for all
2841 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2842 * Icelandic just want to use commas instead of dots, and dots instead
2843 * of commas like "293.291,235".
2845 * An example of this function being called:
2847 * wfMsg( 'message', $wgLang->formatNum( $num ) )
2850 * See LanguageGu.php for the Gujarati implementation and
2851 * $separatorTransformTable on MessageIs.php for
2852 * the , => . and . => , implementation.
2854 * @todo check if it's viable to use localeconv() for the decimal
2856 * @param $number Mixed: the string to be formatted, should be an integer
2857 * or a floating point number.
2858 * @param $nocommafy Bool: set to true for special numbers like dates
2861 public function formatNum( $number, $nocommafy = false ) {
2862 global $wgTranslateNumerals;
2863 if ( !$nocommafy ) {
2864 $number = $this->commafy( $number );
2865 $s = $this->separatorTransformTable();
2867 $number = strtr( $number, $s );
2871 if ( $wgTranslateNumerals ) {
2872 $s = $this->digitTransformTable();
2874 $number = strtr( $number, $s );
2882 * @param $number string
2885 function parseFormattedNumber( $number ) {
2886 $s = $this->digitTransformTable();
2888 $number = strtr( $number, array_flip( $s ) );
2891 $s = $this->separatorTransformTable();
2893 $number = strtr( $number, array_flip( $s ) );
2896 $number = strtr( $number, array( ',' => '' ) );
2901 * Adds commas to a given number
2906 function commafy( $_ ) {
2907 $digitGroupingPattern = $this->digitGroupingPattern();
2908 if ( $_ === null ) {
2912 if ( !$digitGroupingPattern ||
$digitGroupingPattern === "###,###,###" ) {
2913 // default grouping is at thousands, use the same for ###,###,### pattern too.
2914 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2916 // Ref: http://cldr.unicode.org/translation/number-patterns
2918 if ( intval( $_ ) < 0 ) {
2919 // For negative numbers apply the algorithm like positive number and add sign.
2921 $_ = substr( $_, 1 );
2923 $numberpart = array();
2924 $decimalpart = array();
2925 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
2926 preg_match( "/\d+/", $_, $numberpart );
2927 preg_match( "/\.\d*/", $_, $decimalpart );
2928 $groupedNumber = ( count( $decimalpart ) > 0 ) ?
$decimalpart[0]:"";
2929 if ( $groupedNumber === $_ ) {
2930 // the string does not have any number part. Eg: .12345
2931 return $sign . $groupedNumber;
2933 $start = $end = strlen( $numberpart[0] );
2934 while ( $start > 0 ) {
2935 $match = $matches[0][$numMatches -1] ;
2936 $matchLen = strlen( $match );
2937 $start = $end - $matchLen;
2941 $groupedNumber = substr( $_ , $start, $end -$start ) . $groupedNumber ;
2943 if ( $numMatches > 1 ) {
2944 // use the last pattern for the rest of the number
2948 $groupedNumber = "," . $groupedNumber;
2951 return $sign . $groupedNumber;
2957 function digitGroupingPattern() {
2958 return self
::$dataCache->getItem( $this->mCode
, 'digitGroupingPattern' );
2964 function digitTransformTable() {
2965 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
2971 function separatorTransformTable() {
2972 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
2976 * Take a list of strings and build a locale-friendly comma-separated
2977 * list, using the local comma-separator message.
2978 * The last two strings are chained with an "and".
2983 function listToText( array $l ) {
2985 $m = count( $l ) - 1;
2987 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
2989 for ( $i = $m; $i >= 0; $i-- ) {
2992 } elseif ( $i == $m - 1 ) {
2993 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
2995 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
3003 * Take a list of strings and build a locale-friendly comma-separated
3004 * list, using the local comma-separator message.
3005 * @param $list array of strings to put in a comma list
3008 function commaList( array $list ) {
3012 array( 'parsemag', 'escapenoentities', 'language' => $this )
3019 * Take a list of strings and build a locale-friendly semicolon-separated
3020 * list, using the local semicolon-separator message.
3021 * @param $list array of strings to put in a semicolon list
3024 function semicolonList( array $list ) {
3027 'semicolon-separator',
3028 array( 'parsemag', 'escapenoentities', 'language' => $this )
3035 * Same as commaList, but separate it with the pipe instead.
3036 * @param $list array of strings to put in a pipe list
3039 function pipeList( array $list ) {
3043 array( 'escapenoentities', 'language' => $this )
3050 * Truncate a string to a specified length in bytes, appending an optional
3051 * string (e.g. for ellipses)
3053 * The database offers limited byte lengths for some columns in the database;
3054 * multi-byte character sets mean we need to ensure that only whole characters
3055 * are included, otherwise broken characters can be passed to the user
3057 * If $length is negative, the string will be truncated from the beginning
3059 * @param $string String to truncate
3060 * @param $length Int: maximum length (including ellipses)
3061 * @param $ellipsis String to append to the truncated text
3062 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
3063 * $adjustLength was introduced in 1.18, before that behaved as if false.
3066 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
3067 # Use the localized ellipsis character
3068 if ( $ellipsis == '...' ) {
3069 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
3071 # Check if there is no need to truncate
3072 if ( $length == 0 ) {
3073 return $ellipsis; // convention
3074 } elseif ( strlen( $string ) <= abs( $length ) ) {
3075 return $string; // no need to truncate
3077 $stringOriginal = $string;
3078 # If ellipsis length is >= $length then we can't apply $adjustLength
3079 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
3080 $string = $ellipsis; // this can be slightly unexpected
3081 # Otherwise, truncate and add ellipsis...
3083 $eLength = $adjustLength ?
strlen( $ellipsis ) : 0;
3084 if ( $length > 0 ) {
3085 $length -= $eLength;
3086 $string = substr( $string, 0, $length ); // xyz...
3087 $string = $this->removeBadCharLast( $string );
3088 $string = $string . $ellipsis;
3090 $length +
= $eLength;
3091 $string = substr( $string, $length ); // ...xyz
3092 $string = $this->removeBadCharFirst( $string );
3093 $string = $ellipsis . $string;
3096 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
3097 # This check is *not* redundant if $adjustLength, due to the single case where
3098 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
3099 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
3102 return $stringOriginal;
3107 * Remove bytes that represent an incomplete Unicode character
3108 * at the end of string (e.g. bytes of the char are missing)
3110 * @param $string String
3113 protected function removeBadCharLast( $string ) {
3114 if ( $string != '' ) {
3115 $char = ord( $string[strlen( $string ) - 1] );
3117 if ( $char >= 0xc0 ) {
3118 # We got the first byte only of a multibyte char; remove it.
3119 $string = substr( $string, 0, -1 );
3120 } elseif ( $char >= 0x80 &&
3121 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
3122 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
3124 # We chopped in the middle of a character; remove it
3132 * Remove bytes that represent an incomplete Unicode character
3133 * at the start of string (e.g. bytes of the char are missing)
3135 * @param $string String
3138 protected function removeBadCharFirst( $string ) {
3139 if ( $string != '' ) {
3140 $char = ord( $string[0] );
3141 if ( $char >= 0x80 && $char < 0xc0 ) {
3142 # We chopped in the middle of a character; remove the whole thing
3143 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
3150 * Truncate a string of valid HTML to a specified length in bytes,
3151 * appending an optional string (e.g. for ellipses), and return valid HTML
3153 * This is only intended for styled/linked text, such as HTML with
3154 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
3155 * Also, this will not detect things like "display:none" CSS.
3157 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
3159 * @param string $text HTML string to truncate
3160 * @param int $length (zero/positive) Maximum length (including ellipses)
3161 * @param string $ellipsis String to append to the truncated text
3164 function truncateHtml( $text, $length, $ellipsis = '...' ) {
3165 # Use the localized ellipsis character
3166 if ( $ellipsis == '...' ) {
3167 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
3169 # Check if there is clearly no need to truncate
3170 if ( $length <= 0 ) {
3171 return $ellipsis; // no text shown, nothing to format (convention)
3172 } elseif ( strlen( $text ) <= $length ) {
3173 return $text; // string short enough even *with* HTML (short-circuit)
3176 $dispLen = 0; // innerHTML legth so far
3177 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3178 $tagType = 0; // 0-open, 1-close
3179 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3180 $entityState = 0; // 0-not entity, 1-entity
3181 $tag = $ret = ''; // accumulated tag name, accumulated result string
3182 $openTags = array(); // open tag stack
3183 $maybeState = null; // possible truncation state
3185 $textLen = strlen( $text );
3186 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3187 for ( $pos = 0; true; ++
$pos ) {
3188 # Consider truncation once the display length has reached the maximim.
3189 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3190 # Check that we're not in the middle of a bracket/entity...
3191 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3192 if ( !$testingEllipsis ) {
3193 $testingEllipsis = true;
3194 # Save where we are; we will truncate here unless there turn out to
3195 # be so few remaining characters that truncation is not necessary.
3196 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3197 $maybeState = array( $ret, $openTags ); // save state
3199 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3200 # String in fact does need truncation, the truncation point was OK.
3201 list( $ret, $openTags ) = $maybeState; // reload state
3202 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3203 $ret .= $ellipsis; // add ellipsis
3207 if ( $pos >= $textLen ) break; // extra iteration just for above checks
3209 # Read the next char...
3211 $lastCh = $pos ?
$text[$pos - 1] : '';
3212 $ret .= $ch; // add to result string
3214 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3215 $entityState = 0; // for bad HTML
3216 $bracketState = 1; // tag started (checking for backslash)
3217 } elseif ( $ch == '>' ) {
3218 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3219 $entityState = 0; // for bad HTML
3220 $bracketState = 0; // out of brackets
3221 } elseif ( $bracketState == 1 ) {
3223 $tagType = 1; // close tag (e.g. "</span>")
3225 $tagType = 0; // open tag (e.g. "<span>")
3228 $bracketState = 2; // building tag name
3229 } elseif ( $bracketState == 2 ) {
3233 // Name found (e.g. "<a href=..."), add on tag attributes...
3234 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
3236 } elseif ( $bracketState == 0 ) {
3237 if ( $entityState ) {
3240 $dispLen++
; // entity is one displayed char
3243 if ( $neLength == 0 && !$maybeState ) {
3244 // Save state without $ch. We want to *hit* the first
3245 // display char (to get tags) but not *use* it if truncating.
3246 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3249 $entityState = 1; // entity found, (e.g. " ")
3251 $dispLen++
; // this char is displayed
3252 // Add the next $max display text chars after this in one swoop...
3253 $max = ( $testingEllipsis ?
$length : $neLength ) - $dispLen;
3254 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos +
1, $max );
3255 $dispLen +
= $skipped;
3261 // Close the last tag if left unclosed by bad HTML
3262 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3263 while ( count( $openTags ) > 0 ) {
3264 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3270 * truncateHtml() helper function
3271 * like strcspn() but adds the skipped chars to $ret
3280 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3281 if ( $len === null ) {
3282 $len = -1; // -1 means "no limit" for strcspn
3283 } elseif ( $len < 0 ) {
3287 if ( $start < strlen( $text ) ) {
3288 $skipCount = strcspn( $text, $search, $start, $len );
3289 $ret .= substr( $text, $start, $skipCount );
3295 * truncateHtml() helper function
3296 * (a) push or pop $tag from $openTags as needed
3297 * (b) clear $tag value
3298 * @param &$tag string Current HTML tag name we are looking at
3299 * @param $tagType int (0-open tag, 1-close tag)
3300 * @param $lastCh string Character before the '>' that ended this tag
3301 * @param &$openTags array Open tag stack (not accounting for $tag)
3303 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3304 $tag = ltrim( $tag );
3306 if ( $tagType == 0 && $lastCh != '/' ) {
3307 $openTags[] = $tag; // tag opened (didn't close itself)
3308 } elseif ( $tagType == 1 ) {
3309 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3310 array_pop( $openTags ); // tag closed
3318 * Grammatical transformations, needed for inflected languages
3319 * Invoked by putting {{grammar:case|word}} in a message
3321 * @param $word string
3322 * @param $case string
3325 function convertGrammar( $word, $case ) {
3326 global $wgGrammarForms;
3327 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3328 return $wgGrammarForms[$this->getCode()][$case][$word];
3333 * Get the grammar forms for the content language
3334 * @return array of grammar forms
3337 function getGrammarForms() {
3338 global $wgGrammarForms;
3339 if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
3340 return $wgGrammarForms[$this->getCode()];
3345 * Provides an alternative text depending on specified gender.
3346 * Usage {{gender:username|masculine|feminine|neutral}}.
3347 * username is optional, in which case the gender of current user is used,
3348 * but only in (some) interface messages; otherwise default gender is used.
3350 * If no forms are given, an empty string is returned. If only one form is
3351 * given, it will be returned unconditionally. These details are implied by
3352 * the caller and cannot be overridden in subclasses.
3354 * If more than one form is given, the default is to use the neutral one
3355 * if it is specified, and to use the masculine one otherwise. These
3356 * details can be overridden in subclasses.
3358 * @param $gender string
3359 * @param $forms array
3363 function gender( $gender, $forms ) {
3364 if ( !count( $forms ) ) {
3367 $forms = $this->preConvertPlural( $forms, 2 );
3368 if ( $gender === 'male' ) {
3371 if ( $gender === 'female' ) {
3374 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
3378 * Plural form transformations, needed for some languages.
3379 * For example, there are 3 form of plural in Russian and Polish,
3380 * depending on "count mod 10". See [[w:Plural]]
3381 * For English it is pretty simple.
3383 * Invoked by putting {{plural:count|wordform1|wordform2}}
3384 * or {{plural:count|wordform1|wordform2|wordform3}}
3386 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3388 * @param $count Integer: non-localized number
3389 * @param $forms Array: different plural forms
3390 * @return string Correct form of plural for $count in this language
3392 function convertPlural( $count, $forms ) {
3393 if ( !count( $forms ) ) {
3396 $forms = $this->preConvertPlural( $forms, 2 );
3398 return ( $count == 1 ) ?
$forms[0] : $forms[1];
3402 * Checks that convertPlural was given an array and pads it to requested
3403 * amount of forms by copying the last one.
3405 * @param $count Integer: How many forms should there be at least
3406 * @param $forms Array of forms given to convertPlural
3407 * @return array Padded array of forms or an exception if not an array
3409 protected function preConvertPlural( /* Array */ $forms, $count ) {
3410 while ( count( $forms ) < $count ) {
3411 $forms[] = $forms[count( $forms ) - 1];
3417 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3418 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3419 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3420 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3423 * @param $str String: the validated block duration in English
3424 * @return string Somehow translated block duration
3425 * @see LanguageFi.php for example implementation
3427 function translateBlockExpiry( $str ) {
3428 $duration = SpecialBlock
::getSuggestedDurations( $this );
3429 foreach ( $duration as $show => $value ) {
3430 if ( strcmp( $str, $value ) == 0 ) {
3431 return htmlspecialchars( trim( $show ) );
3435 // Since usually only infinite or indefinite is only on list, so try
3436 // equivalents if still here.
3437 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3438 if ( in_array( $str, $indefs ) ) {
3439 foreach ( $indefs as $val ) {
3440 $show = array_search( $val, $duration, true );
3441 if ( $show !== false ) {
3442 return htmlspecialchars( trim( $show ) );
3446 // If all else fails, return the original string.
3451 * languages like Chinese need to be segmented in order for the diff
3454 * @param $text String
3457 public function segmentForDiff( $text ) {
3462 * and unsegment to show the result
3464 * @param $text String
3467 public function unsegmentForDiff( $text ) {
3472 * Return the LanguageConverter used in the Language
3475 * @return LanguageConverter
3477 public function getConverter() {
3478 return $this->mConverter
;
3482 * convert text to all supported variants
3484 * @param $text string
3487 public function autoConvertToAllVariants( $text ) {
3488 return $this->mConverter
->autoConvertToAllVariants( $text );
3492 * convert text to different variants of a language.
3494 * @param $text string
3497 public function convert( $text ) {
3498 return $this->mConverter
->convert( $text );
3502 * Convert a Title object to a string in the preferred variant
3504 * @param $title Title
3507 public function convertTitle( $title ) {
3508 return $this->mConverter
->convertTitle( $title );
3512 * Check if this is a language with variants
3516 public function hasVariants() {
3517 return sizeof( $this->getVariants() ) > 1;
3521 * Check if the language has the specific variant
3524 * @param $variant string
3527 public function hasVariant( $variant ) {
3528 return (bool)$this->mConverter
->validateVariant( $variant );
3532 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3534 * @param $text string
3537 public function armourMath( $text ) {
3538 return $this->mConverter
->armourMath( $text );
3542 * Perform output conversion on a string, and encode for safe HTML output.
3543 * @param $text String text to be converted
3544 * @param $isTitle Bool whether this conversion is for the article title
3546 * @todo this should get integrated somewhere sane
3548 public function convertHtml( $text, $isTitle = false ) {
3549 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3553 * @param $key string
3556 public function convertCategoryKey( $key ) {
3557 return $this->mConverter
->convertCategoryKey( $key );
3561 * Get the list of variants supported by this language
3562 * see sample implementation in LanguageZh.php
3564 * @return array an array of language codes
3566 public function getVariants() {
3567 return $this->mConverter
->getVariants();
3573 public function getPreferredVariant() {
3574 return $this->mConverter
->getPreferredVariant();
3580 public function getDefaultVariant() {
3581 return $this->mConverter
->getDefaultVariant();
3587 public function getURLVariant() {
3588 return $this->mConverter
->getURLVariant();
3592 * If a language supports multiple variants, it is
3593 * possible that non-existing link in one variant
3594 * actually exists in another variant. this function
3595 * tries to find it. See e.g. LanguageZh.php
3597 * @param $link String: the name of the link
3598 * @param $nt Mixed: the title object of the link
3599 * @param $ignoreOtherCond Boolean: to disable other conditions when
3600 * we need to transclude a template or update a category's link
3601 * @return null the input parameters may be modified upon return
3603 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3604 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
3608 * If a language supports multiple variants, converts text
3609 * into an array of all possible variants of the text:
3610 * 'variant' => text in that variant
3612 * @deprecated since 1.17 Use autoConvertToAllVariants()
3614 * @param $text string
3618 public function convertLinkToAllVariants( $text ) {
3619 return $this->mConverter
->convertLinkToAllVariants( $text );
3623 * returns language specific options used by User::getPageRenderHash()
3624 * for example, the preferred language variant
3628 function getExtraHashOptions() {
3629 return $this->mConverter
->getExtraHashOptions();
3633 * For languages that support multiple variants, the title of an
3634 * article may be displayed differently in different variants. this
3635 * function returns the apporiate title defined in the body of the article.
3639 public function getParsedTitle() {
3640 return $this->mConverter
->getParsedTitle();
3644 * Enclose a string with the "no conversion" tag. This is used by
3645 * various functions in the Parser
3647 * @param $text String: text to be tagged for no conversion
3648 * @param $noParse bool
3649 * @return string the tagged text
3651 public function markNoConversion( $text, $noParse = false ) {
3652 return $this->mConverter
->markNoConversion( $text, $noParse );
3656 * A regular expression to match legal word-trailing characters
3657 * which should be merged onto a link of the form [[foo]]bar.
3661 public function linkTrail() {
3662 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
3668 function getLangObj() {
3673 * Get the RFC 3066 code for this language object
3677 public function getCode() {
3678 return $this->mCode
;
3682 * Get the code in Bcp47 format which we can use
3683 * inside of html lang="" tags.
3687 public function getHtmlCode() {
3688 if ( is_null( $this->mHtmlCode
) ) {
3689 $this->mHtmlCode
= wfBCP47( $this->getCode() );
3691 return $this->mHtmlCode
;
3695 * @param $code string
3697 public function setCode( $code ) {
3698 $this->mCode
= $code;
3699 // Ensure we don't leave an incorrect html code lying around
3700 $this->mHtmlCode
= null;
3704 * Get the name of a file for a certain language code
3705 * @param $prefix string Prepend this to the filename
3706 * @param $code string Language code
3707 * @param $suffix string Append this to the filename
3708 * @throws MWException
3709 * @return string $prefix . $mangledCode . $suffix
3711 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
3712 // Protect against path traversal
3713 if ( !Language
::isValidCode( $code )
3714 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
3716 throw new MWException( "Invalid language code \"$code\"" );
3719 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
3723 * Get the language code from a file name. Inverse of getFileName()
3724 * @param $filename string $prefix . $languageCode . $suffix
3725 * @param $prefix string Prefix before the language code
3726 * @param $suffix string Suffix after the language code
3727 * @return string Language code, or false if $prefix or $suffix isn't found
3729 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
3731 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
3732 preg_quote( $suffix, '/' ) . '/', $filename, $m );
3733 if ( !count( $m ) ) {
3736 return str_replace( '_', '-', strtolower( $m[1] ) );
3740 * @param $code string
3743 public static function getMessagesFileName( $code ) {
3745 $file = self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
3746 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
3751 * @param $code string
3754 public static function getClassFileName( $code ) {
3756 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
3760 * Get the first fallback for a given language.
3762 * @param $code string
3764 * @return bool|string
3766 public static function getFallbackFor( $code ) {
3767 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3770 $fallbacks = self
::getFallbacksFor( $code );
3771 $first = array_shift( $fallbacks );
3777 * Get the ordered list of fallback languages.
3780 * @param $code string Language code
3783 public static function getFallbacksFor( $code ) {
3784 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3787 $v = self
::getLocalisationCache()->getItem( $code, 'fallback' );
3788 $v = array_map( 'trim', explode( ',', $v ) );
3789 if ( $v[count( $v ) - 1] !== 'en' ) {
3797 * Get all messages for a given language
3798 * WARNING: this may take a long time. If you just need all message *keys*
3799 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
3801 * @param $code string
3805 public static function getMessagesFor( $code ) {
3806 return self
::getLocalisationCache()->getItem( $code, 'messages' );
3810 * Get a message for a given language
3812 * @param $key string
3813 * @param $code string
3817 public static function getMessageFor( $key, $code ) {
3818 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
3822 * Get all message keys for a given language. This is a faster alternative to
3823 * array_keys( Language::getMessagesFor( $code ) )
3826 * @param $code string Language code
3827 * @return array of message keys (strings)
3829 public static function getMessageKeysFor( $code ) {
3830 return self
::getLocalisationCache()->getSubItemList( $code, 'messages' );
3837 function fixVariableInNamespace( $talk ) {
3838 if ( strpos( $talk, '$1' ) === false ) {
3842 global $wgMetaNamespace;
3843 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
3845 # Allow grammar transformations
3846 # Allowing full message-style parsing would make simple requests
3847 # such as action=raw much more expensive than they need to be.
3848 # This will hopefully cover most cases.
3849 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
3850 array( &$this, 'replaceGrammarInNamespace' ), $talk );
3851 return str_replace( ' ', '_', $talk );
3858 function replaceGrammarInNamespace( $m ) {
3859 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
3863 * @throws MWException
3866 static function getCaseMaps() {
3867 static $wikiUpperChars, $wikiLowerChars;
3868 if ( isset( $wikiUpperChars ) ) {
3869 return array( $wikiUpperChars, $wikiLowerChars );
3872 wfProfileIn( __METHOD__
);
3873 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
3874 if ( $arr === false ) {
3875 throw new MWException(
3876 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
3878 $wikiUpperChars = $arr['wikiUpperChars'];
3879 $wikiLowerChars = $arr['wikiLowerChars'];
3880 wfProfileOut( __METHOD__
);
3881 return array( $wikiUpperChars, $wikiLowerChars );
3885 * Decode an expiry (block, protection, etc) which has come from the DB
3887 * @FIXME: why are we returnings DBMS-dependent strings???
3889 * @param $expiry String: Database expiry String
3890 * @param $format Bool|Int true to process using language functions, or TS_ constant
3891 * to return the expiry in a given timestamp
3894 public function formatExpiry( $expiry, $format = true ) {
3895 static $infinity, $infinityMsg;
3896 if ( $infinity === null ) {
3897 $infinityMsg = wfMessage( 'infiniteblock' );
3898 $infinity = wfGetDB( DB_SLAVE
)->getInfinity();
3901 if ( $expiry == '' ||
$expiry == $infinity ) {
3902 return $format === true
3906 return $format === true
3907 ?
$this->timeanddate( $expiry, /* User preference timezone */ true )
3908 : wfTimestamp( $format, $expiry );
3914 * @param $seconds int|float
3915 * @param $format Array Optional
3916 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
3917 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
3918 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
3919 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
3922 function formatTimePeriod( $seconds, $format = array() ) {
3923 if ( !is_array( $format ) ) {
3924 $format = array( 'avoid' => $format ); // For backwards compatibility
3926 if ( !isset( $format['avoid'] ) ) {
3927 $format['avoid'] = false;
3929 if ( !isset( $format['noabbrevs' ] ) ) {
3930 $format['noabbrevs'] = false;
3932 $secondsMsg = wfMessage(
3933 $format['noabbrevs'] ?
'seconds' : 'seconds-abbrev' )->inLanguage( $this );
3934 $minutesMsg = wfMessage(
3935 $format['noabbrevs'] ?
'minutes' : 'minutes-abbrev' )->inLanguage( $this );
3936 $hoursMsg = wfMessage(
3937 $format['noabbrevs'] ?
'hours' : 'hours-abbrev' )->inLanguage( $this );
3938 $daysMsg = wfMessage(
3939 $format['noabbrevs'] ?
'days' : 'days-abbrev' )->inLanguage( $this );
3941 if ( round( $seconds * 10 ) < 100 ) {
3942 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
3943 $s = $secondsMsg->params( $s )->text();
3944 } elseif ( round( $seconds ) < 60 ) {
3945 $s = $this->formatNum( round( $seconds ) );
3946 $s = $secondsMsg->params( $s )->text();
3947 } elseif ( round( $seconds ) < 3600 ) {
3948 $minutes = floor( $seconds / 60 );
3949 $secondsPart = round( fmod( $seconds, 60 ) );
3950 if ( $secondsPart == 60 ) {
3954 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3956 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
3957 } elseif ( round( $seconds ) <= 2 * 86400 ) {
3958 $hours = floor( $seconds / 3600 );
3959 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
3960 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
3961 if ( $secondsPart == 60 ) {
3965 if ( $minutes == 60 ) {
3969 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
3971 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3972 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
3973 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
3976 $days = floor( $seconds / 86400 );
3977 if ( $format['avoid'] === 'avoidminutes' ) {
3978 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
3979 if ( $hours == 24 ) {
3983 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
3985 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
3986 } elseif ( $format['avoid'] === 'avoidseconds' ) {
3987 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
3988 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
3989 if ( $minutes == 60 ) {
3993 if ( $hours == 24 ) {
3997 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
3999 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4001 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4003 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4005 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
4012 * Format a bitrate for output, using an appropriate
4013 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
4015 * This use base 1000. For base 1024 use formatSize(), for another base
4016 * see formatComputingNumbers()
4021 function formatBitrate( $bps ) {
4022 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
4026 * @param $size int Size of the unit
4027 * @param $boundary int Size boundary (1000, or 1024 in most cases)
4028 * @param $messageKey string Message key to be uesd
4031 function formatComputingNumbers( $size, $boundary, $messageKey ) {
4033 return str_replace( '$1', $this->formatNum( $size ),
4034 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
4037 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
4040 $maxIndex = count( $sizes ) - 1;
4041 while ( $size >= $boundary && $index < $maxIndex ) {
4046 // For small sizes no decimal places necessary
4049 // For MB and bigger two decimal places are smarter
4052 $msg = str_replace( '$1', $sizes[$index], $messageKey );
4054 $size = round( $size, $round );
4055 $text = $this->getMessageFromDB( $msg );
4056 return str_replace( '$1', $this->formatNum( $size ), $text );
4060 * Format a size in bytes for output, using an appropriate
4061 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
4063 * This method use base 1024. For base 1000 use formatBitrate(), for
4064 * another base see formatComputingNumbers()
4066 * @param $size int Size to format
4067 * @return string Plain text (not HTML)
4069 function formatSize( $size ) {
4070 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
4074 * Make a list item, used by various special pages
4076 * @param $page String Page link
4077 * @param $details String Text between brackets
4078 * @param $oppositedm Boolean Add the direction mark opposite to your
4079 * language, to display text properly
4082 function specialList( $page, $details, $oppositedm = true ) {
4083 $dirmark = ( $oppositedm ?
$this->getDirMark( true ) : '' ) .
4084 $this->getDirMark();
4085 $details = $details ?
$dirmark . $this->getMessageFromDB( 'word-separator' ) .
4086 wfMsgExt( 'parentheses', array( 'escape', 'replaceafter', 'language' => $this ), $details ) : '';
4087 return $page . $details;
4091 * Generate (prev x| next x) (20|50|100...) type links for paging
4093 * @param $title Title object to link
4094 * @param $offset Integer offset parameter
4095 * @param $limit Integer limit parameter
4096 * @param $query String optional URL query parameter string
4097 * @param $atend Bool optional param for specified if this is the last page
4100 public function viewPrevNext( Title
$title, $offset, $limit, array $query = array(), $atend = false ) {
4101 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
4103 # Make 'previous' link
4104 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4105 if ( $offset > 0 ) {
4106 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
4107 $query, $prev, 'prevn-title', 'mw-prevlink' );
4109 $plink = htmlspecialchars( $prev );
4113 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4115 $nlink = htmlspecialchars( $next );
4117 $nlink = $this->numLink( $title, $offset +
$limit, $limit,
4118 $query, $next, 'prevn-title', 'mw-nextlink' );
4121 # Make links to set number of items per page
4122 $numLinks = array();
4123 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
4124 $numLinks[] = $this->numLink( $title, $offset, $num,
4125 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
4128 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
4129 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
4133 * Helper function for viewPrevNext() that generates links
4135 * @param $title Title object to link
4136 * @param $offset Integer offset parameter
4137 * @param $limit Integer limit parameter
4138 * @param $query Array extra query parameters
4139 * @param $link String text to use for the link; will be escaped
4140 * @param $tooltipMsg String name of the message to use as tooltip
4141 * @param $class String value of the "class" attribute of the link
4142 * @return String HTML fragment
4144 private function numLink( Title
$title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
4145 $query = array( 'limit' => $limit, 'offset' => $offset ) +
$query;
4146 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4147 return Html
::element( 'a', array( 'href' => $title->getLocalURL( $query ),
4148 'title' => $tooltip, 'class' => $class ), $link );
4152 * Get the conversion rule title, if any.
4156 public function getConvRuleTitle() {
4157 return $this->mConverter
->getConvRuleTitle();