3 * Internationalisation code.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * @defgroup Language Language
28 if ( !defined( 'MEDIAWIKI' ) ) {
29 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
34 global $wgLanguageNames;
35 require_once( __DIR__
. '/Names.php' );
37 if ( function_exists( 'mb_strtoupper' ) ) {
38 mb_internal_encoding( 'UTF-8' );
42 * a fake language converter
52 function __construct( $langobj ) { $this->mLang
= $langobj; }
53 function autoConvertToAllVariants( $text ) { return array( $this->mLang
->getCode() => $text ); }
54 function convert( $t ) { return $t; }
55 function convertTo( $text, $variant ) { return $text; }
56 function convertTitle( $t ) { return $t->getPrefixedText(); }
57 function convertNamespace( $ns ) { return $this->mLang
->getFormattedNsText( $ns ); }
58 function getVariants() { return array( $this->mLang
->getCode() ); }
59 function getPreferredVariant() { return $this->mLang
->getCode(); }
60 function getDefaultVariant() { return $this->mLang
->getCode(); }
61 function getURLVariant() { return ''; }
62 function getConvRuleTitle() { return false; }
63 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
64 function getExtraHashOptions() { return ''; }
65 function getParsedTitle() { return ''; }
66 function markNoConversion( $text, $noParse = false ) { return $text; }
67 function convertCategoryKey( $key ) { return $key; }
68 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
69 function armourMath( $text ) { return $text; }
73 * Internationalisation code
79 * @var LanguageConverter
83 public $mVariants, $mCode, $mLoaded = false;
84 public $mMagicExtensions = array(), $mMagicHookDone = false;
85 private $mHtmlCode = null;
87 public $dateFormatStrings = array();
88 public $mExtendedSpecialPageAliases;
90 protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
93 * ReplacementArray object caches
95 public $transformData = array();
98 * @var LocalisationCache
100 static public $dataCache;
102 static public $mLangObjCache = array();
104 static public $mWeekdayMsgs = array(
105 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
109 static public $mWeekdayAbbrevMsgs = array(
110 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
113 static public $mMonthMsgs = array(
114 'january', 'february', 'march', 'april', 'may_long', 'june',
115 'july', 'august', 'september', 'october', 'november',
118 static public $mMonthGenMsgs = array(
119 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
120 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
123 static public $mMonthAbbrevMsgs = array(
124 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
125 'sep', 'oct', 'nov', 'dec'
128 static public $mIranianCalendarMonthMsgs = array(
129 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
130 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
131 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
132 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
135 static public $mHebrewCalendarMonthMsgs = array(
136 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
137 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
138 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
139 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
140 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
143 static public $mHebrewCalendarMonthGenMsgs = array(
144 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
145 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
146 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
147 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
148 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
151 static public $mHijriCalendarMonthMsgs = array(
152 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
153 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
154 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
155 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
162 static public $durationIntervals = array(
163 'millennia' => 31557600000,
164 'centuries' => 3155760000,
165 'decades' => 315576000,
166 'years' => 31557600, // 86400 * 365.25
175 * Get a cached language object for a given language code
176 * @param $code String
179 static function factory( $code ) {
180 if ( !isset( self
::$mLangObjCache[$code] ) ) {
181 if ( count( self
::$mLangObjCache ) > 10 ) {
182 // Don't keep a billion objects around, that's stupid.
183 self
::$mLangObjCache = array();
185 self
::$mLangObjCache[$code] = self
::newFromCode( $code );
187 return self
::$mLangObjCache[$code];
191 * Create a language object for a given language code
192 * @param $code String
193 * @throws MWException
196 protected static function newFromCode( $code ) {
197 // Protect against path traversal below
198 if ( !Language
::isValidCode( $code )
199 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
201 throw new MWException( "Invalid language code \"$code\"" );
204 if ( !Language
::isValidBuiltInCode( $code ) ) {
205 // It's not possible to customise this code with class files, so
206 // just return a Language object. This is to support uselang= hacks.
207 $lang = new Language
;
208 $lang->setCode( $code );
212 // Check if there is a language class for the code
213 $class = self
::classFromCode( $code );
214 self
::preloadLanguageClass( $class );
215 if ( MWInit
::classExists( $class ) ) {
220 // Keep trying the fallback list until we find an existing class
221 $fallbacks = Language
::getFallbacksFor( $code );
222 foreach ( $fallbacks as $fallbackCode ) {
223 if ( !Language
::isValidBuiltInCode( $fallbackCode ) ) {
224 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
227 $class = self
::classFromCode( $fallbackCode );
228 self
::preloadLanguageClass( $class );
229 if ( MWInit
::classExists( $class ) ) {
230 $lang = Language
::newFromCode( $fallbackCode );
231 $lang->setCode( $code );
236 throw new MWException( "Invalid fallback sequence for language '$code'" );
240 * Returns true if a language code string is of a valid form, whether or
241 * not it exists. This includes codes which are used solely for
242 * customisation via the MediaWiki namespace.
244 * @param $code string
248 public static function isValidCode( $code ) {
250 // People think language codes are html safe, so enforce it.
251 // Ideally we should only allow a-zA-Z0-9-
252 // but, .+ and other chars are often used for {{int:}} hacks
253 // see bugs 37564, 37587, 36938
254 strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code )
255 && !preg_match( Title
::getTitleInvalidRegex(), $code );
259 * Returns true if a language code is of a valid form for the purposes of
260 * internal customisation of MediaWiki, via Messages*.php.
262 * @param $code string
264 * @throws MWException
268 public static function isValidBuiltInCode( $code ) {
270 if ( !is_string( $code ) ) {
271 $type = gettype( $code );
272 if ( $type === 'object' ) {
273 $addmsg = " of class " . get_class( $code );
277 throw new MWException( __METHOD__
. " must be passed a string, $type given$addmsg" );
280 return preg_match( '/^[a-z0-9-]+$/i', $code );
285 * @return String Name of the language class
287 public static function classFromCode( $code ) {
288 if ( $code == 'en' ) {
291 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
296 * Includes language class files
298 * @param $class string Name of the language class
300 public static function preloadLanguageClass( $class ) {
303 if ( $class === 'Language' ) {
307 if ( !defined( 'MW_COMPILED' ) ) {
308 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
309 include_once( "$IP/languages/classes/$class.php" );
315 * Get the LocalisationCache instance
317 * @return LocalisationCache
319 public static function getLocalisationCache() {
320 if ( is_null( self
::$dataCache ) ) {
321 global $wgLocalisationCacheConf;
322 $class = $wgLocalisationCacheConf['class'];
323 self
::$dataCache = new $class( $wgLocalisationCacheConf );
325 return self
::$dataCache;
328 function __construct() {
329 $this->mConverter
= new FakeConverter( $this );
330 // Set the code to the name of the descendant
331 if ( get_class( $this ) == 'Language' ) {
334 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
336 self
::getLocalisationCache();
340 * Reduce memory usage
342 function __destruct() {
343 foreach ( $this as $name => $value ) {
344 unset( $this->$name );
349 * Hook which will be called if this is the content language.
350 * Descendants can use this to register hook functions or modify globals
352 function initContLang() { }
355 * Same as getFallbacksFor for current language.
357 * @deprecated in 1.19
359 function getFallbackLanguageCode() {
360 wfDeprecated( __METHOD__
, '1.19' );
361 return self
::getFallbackFor( $this->mCode
);
368 function getFallbackLanguages() {
369 return self
::getFallbacksFor( $this->mCode
);
373 * Exports $wgBookstoreListEn
376 function getBookstoreList() {
377 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
383 public function getNamespaces() {
384 if ( is_null( $this->namespaceNames
) ) {
385 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
387 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
388 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
390 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
392 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
393 if ( $wgMetaNamespaceTalk ) {
394 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
396 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
397 $this->namespaceNames
[NS_PROJECT_TALK
] =
398 $this->fixVariableInNamespace( $talk );
401 # Sometimes a language will be localised but not actually exist on this wiki.
402 foreach ( $this->namespaceNames
as $key => $text ) {
403 if ( !isset( $validNamespaces[$key] ) ) {
404 unset( $this->namespaceNames
[$key] );
408 # The above mixing may leave namespaces out of canonical order.
409 # Re-order by namespace ID number...
410 ksort( $this->namespaceNames
);
412 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames
) );
414 return $this->namespaceNames
;
418 * Arbitrarily set all of the namespace names at once. Mainly used for testing
419 * @param $namespaces Array of namespaces (id => name)
421 public function setNamespaces( array $namespaces ) {
422 $this->namespaceNames
= $namespaces;
423 $this->mNamespaceIds
= null;
427 * Resets all of the namespace caches. Mainly used for testing
429 public function resetNamespaces( ) {
430 $this->namespaceNames
= null;
431 $this->mNamespaceIds
= null;
432 $this->namespaceAliases
= null;
436 * A convenience function that returns the same thing as
437 * getNamespaces() except with the array values changed to ' '
438 * where it found '_', useful for producing output to be displayed
439 * e.g. in <select> forms.
443 function getFormattedNamespaces() {
444 $ns = $this->getNamespaces();
445 foreach ( $ns as $k => $v ) {
446 $ns[$k] = strtr( $v, '_', ' ' );
452 * Get a namespace value by key
454 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
455 * echo $mw_ns; // prints 'MediaWiki'
458 * @param $index Int: the array key of the namespace to return
459 * @return mixed, string if the namespace value exists, otherwise false
461 function getNsText( $index ) {
462 $ns = $this->getNamespaces();
463 return isset( $ns[$index] ) ?
$ns[$index] : false;
467 * A convenience function that returns the same thing as
468 * getNsText() except with '_' changed to ' ', useful for
471 * @param $index string
475 function getFormattedNsText( $index ) {
476 $ns = $this->getNsText( $index );
477 return strtr( $ns, '_', ' ' );
481 * Returns gender-dependent namespace alias if available.
482 * @param $index Int: namespace index
483 * @param $gender String: gender key (male, female... )
487 function getGenderNsText( $index, $gender ) {
488 global $wgExtraGenderNamespaces;
490 $ns = $wgExtraGenderNamespaces + self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
491 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
495 * Whether this language makes distinguishes genders for example in
500 function needsGenderDistinction() {
501 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
502 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
503 // $wgExtraGenderNamespaces overrides everything
505 } elseif ( isset( $wgExtraNamespaces[NS_USER
] ) && isset( $wgExtraNamespaces[NS_USER_TALK
] ) ) {
506 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
507 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
510 // Check what is in i18n files
511 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
512 return count( $aliases ) > 0;
517 * Get a namespace key by value, case insensitive.
518 * Only matches namespace names for the current language, not the
519 * canonical ones defined in Namespace.php.
521 * @param $text String
522 * @return mixed An integer if $text is a valid value otherwise false
524 function getLocalNsIndex( $text ) {
525 $lctext = $this->lc( $text );
526 $ids = $this->getNamespaceIds();
527 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
533 function getNamespaceAliases() {
534 if ( is_null( $this->namespaceAliases
) ) {
535 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
539 foreach ( $aliases as $name => $index ) {
540 if ( $index === NS_PROJECT_TALK
) {
541 unset( $aliases[$name] );
542 $name = $this->fixVariableInNamespace( $name );
543 $aliases[$name] = $index;
548 global $wgExtraGenderNamespaces;
549 $genders = $wgExtraGenderNamespaces +
(array)self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
550 foreach ( $genders as $index => $forms ) {
551 foreach ( $forms as $alias ) {
552 $aliases[$alias] = $index;
556 $this->namespaceAliases
= $aliases;
558 return $this->namespaceAliases
;
564 function getNamespaceIds() {
565 if ( is_null( $this->mNamespaceIds
) ) {
566 global $wgNamespaceAliases;
567 # Put namespace names and aliases into a hashtable.
568 # If this is too slow, then we should arrange it so that it is done
569 # before caching. The catch is that at pre-cache time, the above
570 # class-specific fixup hasn't been done.
571 $this->mNamespaceIds
= array();
572 foreach ( $this->getNamespaces() as $index => $name ) {
573 $this->mNamespaceIds
[$this->lc( $name )] = $index;
575 foreach ( $this->getNamespaceAliases() as $name => $index ) {
576 $this->mNamespaceIds
[$this->lc( $name )] = $index;
578 if ( $wgNamespaceAliases ) {
579 foreach ( $wgNamespaceAliases as $name => $index ) {
580 $this->mNamespaceIds
[$this->lc( $name )] = $index;
584 return $this->mNamespaceIds
;
588 * Get a namespace key by value, case insensitive. Canonical namespace
589 * names override custom ones defined for the current language.
591 * @param $text String
592 * @return mixed An integer if $text is a valid value otherwise false
594 function getNsIndex( $text ) {
595 $lctext = $this->lc( $text );
596 $ns = MWNamespace
::getCanonicalIndex( $lctext );
597 if ( $ns !== null ) {
600 $ids = $this->getNamespaceIds();
601 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
605 * short names for language variants used for language conversion links.
607 * @param $code String
608 * @param $usemsg bool Use the "variantname-xyz" message if it exists
611 function getVariantname( $code, $usemsg = true ) {
612 $msg = "variantname-$code";
613 if ( $usemsg && wfMessage( $msg )->exists() ) {
614 return $this->getMessageFromDB( $msg );
616 $name = self
::fetchLanguageName( $code );
618 return $name; # if it's defined as a language name, show that
620 # otherwise, output the language code
626 * @param $name string
629 function specialPage( $name ) {
630 $aliases = $this->getSpecialPageAliases();
631 if ( isset( $aliases[$name][0] ) ) {
632 $name = $aliases[$name][0];
634 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
640 function getQuickbarSettings() {
642 $this->getMessage( 'qbsettings-none' ),
643 $this->getMessage( 'qbsettings-fixedleft' ),
644 $this->getMessage( 'qbsettings-fixedright' ),
645 $this->getMessage( 'qbsettings-floatingleft' ),
646 $this->getMessage( 'qbsettings-floatingright' ),
647 $this->getMessage( 'qbsettings-directionality' )
654 function getDatePreferences() {
655 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
661 function getDateFormats() {
662 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
666 * @return array|string
668 function getDefaultDateFormat() {
669 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
670 if ( $df === 'dmy or mdy' ) {
671 global $wgAmericanDates;
672 return $wgAmericanDates ?
'mdy' : 'dmy';
681 function getDatePreferenceMigrationMap() {
682 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
689 function getImageFile( $image ) {
690 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
696 function getExtraUserToggles() {
697 return (array)self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
704 function getUserToggle( $tog ) {
705 return $this->getMessageFromDB( "tog-$tog" );
709 * Get native language names, indexed by code.
710 * Only those defined in MediaWiki, no other data like CLDR.
711 * If $customisedOnly is true, only returns codes with a messages file
713 * @param $customisedOnly bool
716 * @deprecated in 1.20, use fetchLanguageNames()
718 public static function getLanguageNames( $customisedOnly = false ) {
719 return self
::fetchLanguageNames( null, $customisedOnly ?
'mwfile' : 'mw' );
723 * Get translated language names. This is done on best effort and
724 * by default this is exactly the same as Language::getLanguageNames.
725 * The CLDR extension provides translated names.
726 * @param $code String Language code.
727 * @return Array language code => language name
729 * @deprecated in 1.20, use fetchLanguageNames()
731 public static function getTranslatedLanguageNames( $code ) {
732 return self
::fetchLanguageNames( $code, 'all' );
736 * Get an array of language names, indexed by code.
737 * @param $inLanguage null|string: Code of language in which to return the names
738 * Use null for autonyms (native names)
739 * @param $include string:
740 * 'all' all available languages
741 * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
742 * 'mwfile' only if the language is in 'mw' *and* has a message file
743 * @return array: language code => language name
746 public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
747 global $wgExtraLanguageNames;
748 static $coreLanguageNames;
750 if ( $coreLanguageNames === null ) {
751 include( MWInit
::compiledPath( 'languages/Names.php' ) );
757 # TODO: also include when $inLanguage is null, when this code is more efficient
758 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
761 $mwNames = $wgExtraLanguageNames +
$coreLanguageNames;
762 foreach ( $mwNames as $mwCode => $mwName ) {
763 # - Prefer own MediaWiki native name when not using the hook
764 # - For other names just add if not added through the hook
765 if ( $mwCode === $inLanguage ||
!isset( $names[$mwCode] ) ) {
766 $names[$mwCode] = $mwName;
770 if ( $include === 'all' ) {
775 $coreCodes = array_keys( $mwNames );
776 foreach ( $coreCodes as $coreCode ) {
777 $returnMw[$coreCode] = $names[$coreCode];
780 if ( $include === 'mwfile' ) {
781 $namesMwFile = array();
782 # We do this using a foreach over the codes instead of a directory
783 # loop so that messages files in extensions will work correctly.
784 foreach ( $returnMw as $code => $value ) {
785 if ( is_readable( self
::getMessagesFileName( $code ) ) ) {
786 $namesMwFile[$code] = $names[$code];
791 # 'mw' option; default if it's not one of the other two options (all/mwfile)
796 * @param $code string: The code of the language for which to get the name
797 * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
798 * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
799 * @return string: Language name or empty
802 public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
803 $array = self
::fetchLanguageNames( $inLanguage, $include );
804 return !array_key_exists( $code, $array ) ?
'' : $array[$code];
808 * Get a message from the MediaWiki namespace.
810 * @param $msg String: message name
813 function getMessageFromDB( $msg ) {
814 return wfMessage( $msg )->inLanguage( $this )->text();
818 * Get the native language name of $code.
819 * Only if defined in MediaWiki, no other data like CLDR.
820 * @param $code string
822 * @deprecated in 1.20, use fetchLanguageName()
824 function getLanguageName( $code ) {
825 return self
::fetchLanguageName( $code );
832 function getMonthName( $key ) {
833 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
839 function getMonthNamesArray() {
840 $monthNames = array( '' );
841 for ( $i = 1; $i < 13; $i++
) {
842 $monthNames[] = $this->getMonthName( $i );
851 function getMonthNameGen( $key ) {
852 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
859 function getMonthAbbreviation( $key ) {
860 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
866 function getMonthAbbreviationsArray() {
867 $monthNames = array( '' );
868 for ( $i = 1; $i < 13; $i++
) {
869 $monthNames[] = $this->getMonthAbbreviation( $i );
878 function getWeekdayName( $key ) {
879 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
886 function getWeekdayAbbreviation( $key ) {
887 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
894 function getIranianCalendarMonthName( $key ) {
895 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
902 function getHebrewCalendarMonthName( $key ) {
903 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
910 function getHebrewCalendarMonthNameGen( $key ) {
911 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
918 function getHijriCalendarMonthName( $key ) {
919 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
923 * This is a workalike of PHP's date() function, but with better
924 * internationalisation, a reduced set of format characters, and a better
927 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
928 * PHP manual for definitions. There are a number of extensions, which
931 * xn Do not translate digits of the next numeric format character
932 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
933 * xr Use roman numerals for the next numeric format character
934 * xh Use hebrew numerals for the next numeric format character
936 * xg Genitive month name
938 * xij j (day number) in Iranian calendar
939 * xiF F (month name) in Iranian calendar
940 * xin n (month number) in Iranian calendar
941 * xiy y (two digit year) in Iranian calendar
942 * xiY Y (full year) in Iranian calendar
944 * xjj j (day number) in Hebrew calendar
945 * xjF F (month name) in Hebrew calendar
946 * xjt t (days in month) in Hebrew calendar
947 * xjx xg (genitive month name) in Hebrew calendar
948 * xjn n (month number) in Hebrew calendar
949 * xjY Y (full year) in Hebrew calendar
951 * xmj j (day number) in Hijri calendar
952 * xmF F (month name) in Hijri calendar
953 * xmn n (month number) in Hijri calendar
954 * xmY Y (full year) in Hijri calendar
956 * xkY Y (full year) in Thai solar calendar. Months and days are
957 * identical to the Gregorian calendar
958 * xoY Y (full year) in Minguo calendar or Juche year.
959 * Months and days are identical to the
961 * xtY Y (full year) in Japanese nengo. Months and days are
962 * identical to the Gregorian calendar
964 * Characters enclosed in double quotes will be considered literal (with
965 * the quotes themselves removed). Unmatched quotes will be considered
966 * literal quotes. Example:
968 * "The month is" F => The month is January
971 * Backslash escaping is also supported.
973 * Input timestamp is assumed to be pre-normalized to the desired local
976 * @param $format String
977 * @param $ts String: 14-character timestamp
980 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
984 function sprintfDate( $format, $ts ) {
997 for ( $p = 0; $p < strlen( $format ); $p++
) {
1000 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
1001 $code .= $format[++
$p];
1004 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
1005 $code .= $format[++
$p];
1016 $rawToggle = !$rawToggle;
1025 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
1028 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
1029 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
1032 $num = substr( $ts, 6, 2 );
1035 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
1036 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) +
1 );
1039 $num = intval( substr( $ts, 6, 2 ) );
1043 $iranian = self
::tsToIranian( $ts );
1049 $hijri = self
::tsToHijri( $ts );
1055 $hebrew = self
::tsToHebrew( $ts );
1061 $unix = wfTimestamp( TS_UNIX
, $ts );
1063 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) +
1 );
1067 $unix = wfTimestamp( TS_UNIX
, $ts );
1069 $w = gmdate( 'w', $unix );
1074 $unix = wfTimestamp( TS_UNIX
, $ts );
1076 $num = gmdate( 'w', $unix );
1080 $unix = wfTimestamp( TS_UNIX
, $ts );
1082 $num = gmdate( 'z', $unix );
1086 $unix = wfTimestamp( TS_UNIX
, $ts );
1088 $num = gmdate( 'W', $unix );
1091 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1095 $iranian = self
::tsToIranian( $ts );
1097 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1101 $hijri = self
::tsToHijri( $ts );
1103 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1107 $hebrew = self
::tsToHebrew( $ts );
1109 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1112 $num = substr( $ts, 4, 2 );
1115 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1118 $num = intval( substr( $ts, 4, 2 ) );
1122 $iranian = self
::tsToIranian( $ts );
1128 $hijri = self
::tsToHijri ( $ts );
1134 $hebrew = self
::tsToHebrew( $ts );
1140 $unix = wfTimestamp( TS_UNIX
, $ts );
1142 $num = gmdate( 't', $unix );
1146 $hebrew = self
::tsToHebrew( $ts );
1152 $unix = wfTimestamp( TS_UNIX
, $ts );
1154 $num = gmdate( 'L', $unix );
1158 $unix = wfTimestamp( TS_UNIX
, $ts );
1160 $num = gmdate( 'o', $unix );
1163 $num = substr( $ts, 0, 4 );
1167 $iranian = self
::tsToIranian( $ts );
1173 $hijri = self
::tsToHijri( $ts );
1179 $hebrew = self
::tsToHebrew( $ts );
1185 $thai = self
::tsToYear( $ts, 'thai' );
1191 $minguo = self
::tsToYear( $ts, 'minguo' );
1197 $tenno = self
::tsToYear( $ts, 'tenno' );
1202 $num = substr( $ts, 2, 2 );
1206 $iranian = self
::tsToIranian( $ts );
1208 $num = substr( $iranian[0], -2 );
1211 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
1214 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
1217 $h = substr( $ts, 8, 2 );
1218 $num = $h %
12 ?
$h %
12 : 12;
1221 $num = intval( substr( $ts, 8, 2 ) );
1224 $h = substr( $ts, 8, 2 );
1225 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
1228 $num = substr( $ts, 8, 2 );
1231 $num = substr( $ts, 10, 2 );
1234 $num = substr( $ts, 12, 2 );
1238 $unix = wfTimestamp( TS_UNIX
, $ts );
1240 $s .= gmdate( 'c', $unix );
1244 $unix = wfTimestamp( TS_UNIX
, $ts );
1246 $s .= gmdate( 'r', $unix );
1250 $unix = wfTimestamp( TS_UNIX
, $ts );
1255 # Backslash escaping
1256 if ( $p < strlen( $format ) - 1 ) {
1257 $s .= $format[++
$p];
1264 if ( $p < strlen( $format ) - 1 ) {
1265 $endQuote = strpos( $format, '"', $p +
1 );
1266 if ( $endQuote === false ) {
1267 # No terminating quote, assume literal "
1270 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1274 # Quote at end of string, assume literal "
1281 if ( $num !== false ) {
1282 if ( $rawToggle ||
$raw ) {
1285 } elseif ( $roman ) {
1286 $s .= Language
::romanNumeral( $num );
1288 } elseif ( $hebrewNum ) {
1289 $s .= self
::hebrewNumeral( $num );
1292 $s .= $this->formatNum( $num, true );
1299 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1300 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1303 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1304 * Gregorian dates to Iranian dates. Originally written in C, it
1305 * is released under the terms of GNU Lesser General Public
1306 * License. Conversion to PHP was performed by Niklas Laxström.
1308 * Link: http://www.farsiweb.info/jalali/jalali.c
1314 private static function tsToIranian( $ts ) {
1315 $gy = substr( $ts, 0, 4 ) -1600;
1316 $gm = substr( $ts, 4, 2 ) -1;
1317 $gd = substr( $ts, 6, 2 ) -1;
1319 # Days passed from the beginning (including leap years)
1321 +
floor( ( $gy +
3 ) / 4 )
1322 - floor( ( $gy +
99 ) / 100 )
1323 +
floor( ( $gy +
399 ) / 400 );
1325 // Add days of the past months of this year
1326 for ( $i = 0; $i < $gm; $i++
) {
1327 $gDayNo +
= self
::$GREG_DAYS[$i];
1331 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1335 // Days passed in current month
1336 $gDayNo +
= (int)$gd;
1338 $jDayNo = $gDayNo - 79;
1340 $jNp = floor( $jDayNo / 12053 );
1343 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1346 if ( $jDayNo >= 366 ) {
1347 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1348 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1351 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1352 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1358 return array( $jy, $jm, $jd );
1362 * Converting Gregorian dates to Hijri dates.
1364 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1366 * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1372 private static function tsToHijri( $ts ) {
1373 $year = substr( $ts, 0, 4 );
1374 $month = substr( $ts, 4, 2 );
1375 $day = substr( $ts, 6, 2 );
1383 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1384 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1387 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1388 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1389 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1392 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1393 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1396 $zl = $zjd -1948440 +
10632;
1397 $zn = (int)( ( $zl - 1 ) / 10631 );
1398 $zl = $zl - 10631 * $zn +
354;
1399 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1400 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1401 $zm = (int)( ( 24 * $zl ) / 709 );
1402 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1403 $zy = 30 * $zn +
$zj - 30;
1405 return array( $zy, $zm, $zd );
1409 * Converting Gregorian dates to Hebrew dates.
1411 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1412 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1413 * to translate the relevant functions into PHP and release them under
1416 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1417 * and Adar II is 14. In a non-leap year, Adar is 6.
1423 private static function tsToHebrew( $ts ) {
1425 $year = substr( $ts, 0, 4 );
1426 $month = substr( $ts, 4, 2 );
1427 $day = substr( $ts, 6, 2 );
1429 # Calculate Hebrew year
1430 $hebrewYear = $year +
3760;
1432 # Month number when September = 1, August = 12
1434 if ( $month > 12 ) {
1441 # Calculate day of year from 1 September
1443 for ( $i = 1; $i < $month; $i++
) {
1447 # Check if the year is leap
1448 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1451 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1458 # Calculate the start of the Hebrew year
1459 $start = self
::hebrewYearStart( $hebrewYear );
1461 # Calculate next year's start
1462 if ( $dayOfYear <= $start ) {
1463 # Day is before the start of the year - it is the previous year
1465 $nextStart = $start;
1469 # Add days since previous year's 1 September
1471 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1475 # Start of the new (previous) year
1476 $start = self
::hebrewYearStart( $hebrewYear );
1479 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1482 # Calculate Hebrew day of year
1483 $hebrewDayOfYear = $dayOfYear - $start;
1485 # Difference between year's days
1486 $diff = $nextStart - $start;
1487 # Add 12 (or 13 for leap years) days to ignore the difference between
1488 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1489 # difference is only about the year type
1490 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1496 # Check the year pattern, and is leap year
1497 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1498 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1499 # and non-leap years
1500 $yearPattern = $diff %
30;
1501 # Check if leap year
1502 $isLeap = $diff >= 30;
1504 # Calculate day in the month from number of day in the Hebrew year
1505 # Don't check Adar - if the day is not in Adar, we will stop before;
1506 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1507 $hebrewDay = $hebrewDayOfYear;
1510 while ( $hebrewMonth <= 12 ) {
1511 # Calculate days in this month
1512 if ( $isLeap && $hebrewMonth == 6 ) {
1513 # Adar in a leap year
1515 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1517 if ( $hebrewDay <= $days ) {
1521 # Subtract the days of Adar I
1522 $hebrewDay -= $days;
1525 if ( $hebrewDay <= $days ) {
1531 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1532 # Cheshvan in a complete year (otherwise as the rule below)
1534 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1535 # Kislev in an incomplete year (otherwise as the rule below)
1538 # Odd months have 30 days, even have 29
1539 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1541 if ( $hebrewDay <= $days ) {
1542 # In the current month
1545 # Subtract the days of the current month
1546 $hebrewDay -= $days;
1547 # Try in the next month
1552 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1556 * This calculates the Hebrew year start, as days since 1 September.
1557 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1558 * Used for Hebrew date.
1564 private static function hebrewYearStart( $year ) {
1565 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1566 $b = intval( ( $year - 1 ) %
4 );
1567 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1571 $Mar = intval( $m );
1577 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1578 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1580 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1582 } elseif ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1586 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1591 * Algorithm to convert Gregorian dates to Thai solar dates,
1592 * Minguo dates or Minguo dates.
1594 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1595 * http://en.wikipedia.org/wiki/Minguo_calendar
1596 * http://en.wikipedia.org/wiki/Japanese_era_name
1598 * @param $ts String: 14-character timestamp
1599 * @param $cName String: calender name
1600 * @return Array: converted year, month, day
1602 private static function tsToYear( $ts, $cName ) {
1603 $gy = substr( $ts, 0, 4 );
1604 $gm = substr( $ts, 4, 2 );
1605 $gd = substr( $ts, 6, 2 );
1607 if ( !strcmp( $cName, 'thai' ) ) {
1609 # Add 543 years to the Gregorian calendar
1610 # Months and days are identical
1611 $gy_offset = $gy +
543;
1612 } elseif ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1614 # Deduct 1911 years from the Gregorian calendar
1615 # Months and days are identical
1616 $gy_offset = $gy - 1911;
1617 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1618 # Nengō dates up to Meiji period
1619 # Deduct years from the Gregorian calendar
1620 # depending on the nengo periods
1621 # Months and days are identical
1622 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1624 $gy_gannen = $gy - 1868 +
1;
1625 $gy_offset = $gy_gannen;
1626 if ( $gy_gannen == 1 ) {
1629 $gy_offset = '明治' . $gy_offset;
1631 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1632 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1633 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1634 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1635 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1639 $gy_gannen = $gy - 1912 +
1;
1640 $gy_offset = $gy_gannen;
1641 if ( $gy_gannen == 1 ) {
1644 $gy_offset = '大正' . $gy_offset;
1646 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1647 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1648 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1652 $gy_gannen = $gy - 1926 +
1;
1653 $gy_offset = $gy_gannen;
1654 if ( $gy_gannen == 1 ) {
1657 $gy_offset = '昭和' . $gy_offset;
1660 $gy_gannen = $gy - 1989 +
1;
1661 $gy_offset = $gy_gannen;
1662 if ( $gy_gannen == 1 ) {
1665 $gy_offset = '平成' . $gy_offset;
1671 return array( $gy_offset, $gm, $gd );
1675 * Roman number formatting up to 10000
1681 static function romanNumeral( $num ) {
1682 static $table = array(
1683 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1684 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1685 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1686 array( '', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM', 'MMMMMM', 'MMMMMMM', 'MMMMMMMM', 'MMMMMMMMM', 'MMMMMMMMMM' )
1689 $num = intval( $num );
1690 if ( $num > 10000 ||
$num <= 0 ) {
1695 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1696 if ( $num >= $pow10 ) {
1697 $s .= $table[$i][(int)floor( $num / $pow10 )];
1699 $num = $num %
$pow10;
1705 * Hebrew Gematria number formatting up to 9999
1711 static function hebrewNumeral( $num ) {
1712 static $table = array(
1713 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1714 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1715 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1716 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1719 $num = intval( $num );
1720 if ( $num > 9999 ||
$num <= 0 ) {
1725 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1726 if ( $num >= $pow10 ) {
1727 if ( $num == 15 ||
$num == 16 ) {
1728 $s .= $table[0][9] . $table[0][$num - 9];
1731 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1732 if ( $pow10 == 1000 ) {
1737 $num = $num %
$pow10;
1739 if ( strlen( $s ) == 2 ) {
1742 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1743 $str .= substr( $s, strlen( $s ) - 2, 2 );
1745 $start = substr( $str, 0, strlen( $str ) - 2 );
1746 $end = substr( $str, strlen( $str ) - 2 );
1749 $str = $start . 'ך';
1752 $str = $start . 'ם';
1755 $str = $start . 'ן';
1758 $str = $start . 'ף';
1761 $str = $start . 'ץ';
1768 * Used by date() and time() to adjust the time output.
1770 * @param $ts Int the time in date('YmdHis') format
1771 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1772 * get user timecorrection setting)
1775 function userAdjust( $ts, $tz = false ) {
1776 global $wgUser, $wgLocalTZoffset;
1778 if ( $tz === false ) {
1779 $tz = $wgUser->getOption( 'timecorrection' );
1782 $data = explode( '|', $tz, 3 );
1784 if ( $data[0] == 'ZoneInfo' ) {
1785 wfSuppressWarnings();
1786 $userTZ = timezone_open( $data[2] );
1787 wfRestoreWarnings();
1788 if ( $userTZ !== false ) {
1789 $date = date_create( $ts, timezone_open( 'UTC' ) );
1790 date_timezone_set( $date, $userTZ );
1791 $date = date_format( $date, 'YmdHis' );
1794 # Unrecognized timezone, default to 'Offset' with the stored offset.
1795 $data[0] = 'Offset';
1799 if ( $data[0] == 'System' ||
$tz == '' ) {
1800 # Global offset in minutes.
1801 if ( isset( $wgLocalTZoffset ) ) {
1802 $minDiff = $wgLocalTZoffset;
1804 } elseif ( $data[0] == 'Offset' ) {
1805 $minDiff = intval( $data[1] );
1807 $data = explode( ':', $tz );
1808 if ( count( $data ) == 2 ) {
1809 $data[0] = intval( $data[0] );
1810 $data[1] = intval( $data[1] );
1811 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1812 if ( $data[0] < 0 ) {
1813 $minDiff = -$minDiff;
1816 $minDiff = intval( $data[0] ) * 60;
1820 # No difference ? Return time unchanged
1821 if ( 0 == $minDiff ) {
1825 wfSuppressWarnings(); // E_STRICT system time bitching
1826 # Generate an adjusted date; take advantage of the fact that mktime
1827 # will normalize out-of-range values so we don't have to split $minDiff
1828 # into hours and minutes.
1830 (int)substr( $ts, 8, 2 ) ), # Hours
1831 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
1832 (int)substr( $ts, 12, 2 ), # Seconds
1833 (int)substr( $ts, 4, 2 ), # Month
1834 (int)substr( $ts, 6, 2 ), # Day
1835 (int)substr( $ts, 0, 4 ) ); # Year
1837 $date = date( 'YmdHis', $t );
1838 wfRestoreWarnings();
1844 * This is meant to be used by time(), date(), and timeanddate() to get
1845 * the date preference they're supposed to use, it should be used in
1849 * function timeanddate([...], $format = true) {
1850 * $datePreference = $this->dateFormat($format);
1855 * @param $usePrefs Mixed: if true, the user's preference is used
1856 * if false, the site/language default is used
1857 * if int/string, assumed to be a format.
1860 function dateFormat( $usePrefs = true ) {
1863 if ( is_bool( $usePrefs ) ) {
1865 $datePreference = $wgUser->getDatePreference();
1867 $datePreference = (string)User
::getDefaultOption( 'date' );
1870 $datePreference = (string)$usePrefs;
1874 if ( $datePreference == '' ) {
1878 return $datePreference;
1882 * Get a format string for a given type and preference
1883 * @param $type string May be date, time or both
1884 * @param $pref string The format name as it appears in Messages*.php
1888 function getDateFormatString( $type, $pref ) {
1889 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
1890 if ( $pref == 'default' ) {
1891 $pref = $this->getDefaultDateFormat();
1892 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1894 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1895 if ( is_null( $df ) ) {
1896 $pref = $this->getDefaultDateFormat();
1897 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1900 $this->dateFormatStrings
[$type][$pref] = $df;
1902 return $this->dateFormatStrings
[$type][$pref];
1906 * @param $ts Mixed: the time format which needs to be turned into a
1907 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1908 * @param $adj Bool: whether to adjust the time output according to the
1909 * user configured offset ($timecorrection)
1910 * @param $format Mixed: true to use user's date format preference
1911 * @param $timecorrection String|bool the time offset as returned by
1912 * validateTimeZone() in Special:Preferences
1915 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1916 $ts = wfTimestamp( TS_MW
, $ts );
1918 $ts = $this->userAdjust( $ts, $timecorrection );
1920 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1921 return $this->sprintfDate( $df, $ts );
1925 * @param $ts Mixed: the time format which needs to be turned into a
1926 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1927 * @param $adj Bool: whether to adjust the time output according to the
1928 * user configured offset ($timecorrection)
1929 * @param $format Mixed: true to use user's date format preference
1930 * @param $timecorrection String|bool the time offset as returned by
1931 * validateTimeZone() in Special:Preferences
1934 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1935 $ts = wfTimestamp( TS_MW
, $ts );
1937 $ts = $this->userAdjust( $ts, $timecorrection );
1939 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1940 return $this->sprintfDate( $df, $ts );
1944 * @param $ts Mixed: the time format which needs to be turned into a
1945 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1946 * @param $adj Bool: whether to adjust the time output according to the
1947 * user configured offset ($timecorrection)
1948 * @param $format Mixed: what format to return, if it's false output the
1949 * default one (default true)
1950 * @param $timecorrection String|bool the time offset as returned by
1951 * validateTimeZone() in Special:Preferences
1954 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1955 $ts = wfTimestamp( TS_MW
, $ts );
1957 $ts = $this->userAdjust( $ts, $timecorrection );
1959 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1960 return $this->sprintfDate( $df, $ts );
1964 * Takes a number of seconds and turns it into a text using values such as hours and minutes.
1968 * @param integer $seconds The amount of seconds.
1969 * @param array $chosenIntervals The intervals to enable.
1973 public function formatDuration( $seconds, array $chosenIntervals = array() ) {
1974 $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
1976 $segments = array();
1978 foreach ( $intervals as $intervalName => $intervalValue ) {
1979 $message = new Message( 'duration-' . $intervalName, array( $intervalValue ) );
1980 $segments[] = $message->inLanguage( $this )->escaped();
1983 return $this->listToText( $segments );
1987 * Takes a number of seconds and returns an array with a set of corresponding intervals.
1988 * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
1992 * @param integer $seconds The amount of seconds.
1993 * @param array $chosenIntervals The intervals to enable.
1997 public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
1998 if ( empty( $chosenIntervals ) ) {
1999 $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
2002 $intervals = array_intersect_key( self
::$durationIntervals, array_flip( $chosenIntervals ) );
2003 $sortedNames = array_keys( $intervals );
2004 $smallestInterval = array_pop( $sortedNames );
2006 $segments = array();
2008 foreach ( $intervals as $name => $length ) {
2009 $value = floor( $seconds / $length );
2011 if ( $value > 0 ||
( $name == $smallestInterval && empty( $segments ) ) ) {
2012 $seconds -= $value * $length;
2013 $segments[$name] = $value;
2021 * Internal helper function for userDate(), userTime() and userTimeAndDate()
2023 * @param $type String: can be 'date', 'time' or 'both'
2024 * @param $ts Mixed: the time format which needs to be turned into a
2025 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2026 * @param $user User object used to get preferences for timezone and format
2027 * @param $options Array, can contain the following keys:
2028 * - 'timecorrection': time correction, can have the following values:
2029 * - true: use user's preference
2030 * - false: don't use time correction
2031 * - integer: value of time correction in minutes
2032 * - 'format': format to use, can have the following values:
2033 * - true: use user's preference
2034 * - false: use default preference
2035 * - string: format to use
2039 private function internalUserTimeAndDate( $type, $ts, User
$user, array $options ) {
2040 $ts = wfTimestamp( TS_MW
, $ts );
2041 $options +
= array( 'timecorrection' => true, 'format' => true );
2042 if ( $options['timecorrection'] !== false ) {
2043 if ( $options['timecorrection'] === true ) {
2044 $offset = $user->getOption( 'timecorrection' );
2046 $offset = $options['timecorrection'];
2048 $ts = $this->userAdjust( $ts, $offset );
2050 if ( $options['format'] === true ) {
2051 $format = $user->getDatePreference();
2053 $format = $options['format'];
2055 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
2056 return $this->sprintfDate( $df, $ts );
2060 * Get the formatted date for the given timestamp and formatted for
2063 * @param $ts Mixed: the time format which needs to be turned into a
2064 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2065 * @param $user User object used to get preferences for timezone and format
2066 * @param $options Array, can contain the following keys:
2067 * - 'timecorrection': time correction, can have the following values:
2068 * - true: use user's preference
2069 * - false: don't use time correction
2070 * - integer: value of time correction in minutes
2071 * - 'format': format to use, can have the following values:
2072 * - true: use user's preference
2073 * - false: use default preference
2074 * - string: format to use
2078 public function userDate( $ts, User
$user, array $options = array() ) {
2079 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
2083 * Get the formatted time for the given timestamp and formatted for
2086 * @param $ts Mixed: the time format which needs to be turned into a
2087 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2088 * @param $user User object used to get preferences for timezone and format
2089 * @param $options Array, can contain the following keys:
2090 * - 'timecorrection': time correction, can have the following values:
2091 * - true: use user's preference
2092 * - false: don't use time correction
2093 * - integer: value of time correction in minutes
2094 * - 'format': format to use, can have the following values:
2095 * - true: use user's preference
2096 * - false: use default preference
2097 * - string: format to use
2101 public function userTime( $ts, User
$user, array $options = array() ) {
2102 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
2106 * Get the formatted date and time for the given timestamp and formatted for
2109 * @param $ts Mixed: the time format which needs to be turned into a
2110 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2111 * @param $user User object used to get preferences for timezone and format
2112 * @param $options Array, can contain the following keys:
2113 * - 'timecorrection': time correction, can have the following values:
2114 * - true: use user's preference
2115 * - false: don't use time correction
2116 * - integer: value of time correction in minutes
2117 * - 'format': format to use, can have the following values:
2118 * - true: use user's preference
2119 * - false: use default preference
2120 * - string: format to use
2124 public function userTimeAndDate( $ts, User
$user, array $options = array() ) {
2125 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
2129 * @param $key string
2130 * @return array|null
2132 function getMessage( $key ) {
2133 return self
::$dataCache->getSubitem( $this->mCode
, 'messages', $key );
2139 function getAllMessages() {
2140 return self
::$dataCache->getItem( $this->mCode
, 'messages' );
2149 function iconv( $in, $out, $string ) {
2150 # This is a wrapper for iconv in all languages except esperanto,
2151 # which does some nasty x-conversions beforehand
2153 # Even with //IGNORE iconv can whine about illegal characters in
2154 # *input* string. We just ignore those too.
2155 # REF: http://bugs.php.net/bug.php?id=37166
2156 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
2157 wfSuppressWarnings();
2158 $text = iconv( $in, $out . '//IGNORE', $string );
2159 wfRestoreWarnings();
2163 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
2166 * @param $matches array
2167 * @return mixed|string
2169 function ucwordbreaksCallbackAscii( $matches ) {
2170 return $this->ucfirst( $matches[1] );
2174 * @param $matches array
2177 function ucwordbreaksCallbackMB( $matches ) {
2178 return mb_strtoupper( $matches[0] );
2182 * @param $matches array
2185 function ucCallback( $matches ) {
2186 list( $wikiUpperChars ) = self
::getCaseMaps();
2187 return strtr( $matches[1], $wikiUpperChars );
2191 * @param $matches array
2194 function lcCallback( $matches ) {
2195 list( , $wikiLowerChars ) = self
::getCaseMaps();
2196 return strtr( $matches[1], $wikiLowerChars );
2200 * @param $matches array
2203 function ucwordsCallbackMB( $matches ) {
2204 return mb_strtoupper( $matches[0] );
2208 * @param $matches array
2211 function ucwordsCallbackWiki( $matches ) {
2212 list( $wikiUpperChars ) = self
::getCaseMaps();
2213 return strtr( $matches[0], $wikiUpperChars );
2217 * Make a string's first character uppercase
2219 * @param $str string
2223 function ucfirst( $str ) {
2225 if ( $o < 96 ) { // if already uppercase...
2227 } elseif ( $o < 128 ) {
2228 return ucfirst( $str ); // use PHP's ucfirst()
2230 // fall back to more complex logic in case of multibyte strings
2231 return $this->uc( $str, true );
2236 * Convert a string to uppercase
2238 * @param $str string
2239 * @param $first bool
2243 function uc( $str, $first = false ) {
2244 if ( function_exists( 'mb_strtoupper' ) ) {
2246 if ( $this->isMultibyte( $str ) ) {
2247 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2249 return ucfirst( $str );
2252 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
2255 if ( $this->isMultibyte( $str ) ) {
2256 $x = $first ?
'^' : '';
2257 return preg_replace_callback(
2258 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2259 array( $this, 'ucCallback' ),
2263 return $first ?
ucfirst( $str ) : strtoupper( $str );
2269 * @param $str string
2270 * @return mixed|string
2272 function lcfirst( $str ) {
2275 return strval( $str );
2276 } elseif ( $o >= 128 ) {
2277 return $this->lc( $str, true );
2278 } elseif ( $o > 96 ) {
2281 $str[0] = strtolower( $str[0] );
2287 * @param $str string
2288 * @param $first bool
2289 * @return mixed|string
2291 function lc( $str, $first = false ) {
2292 if ( function_exists( 'mb_strtolower' ) ) {
2294 if ( $this->isMultibyte( $str ) ) {
2295 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2297 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2300 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
2303 if ( $this->isMultibyte( $str ) ) {
2304 $x = $first ?
'^' : '';
2305 return preg_replace_callback(
2306 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2307 array( $this, 'lcCallback' ),
2311 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2317 * @param $str string
2320 function isMultibyte( $str ) {
2321 return (bool)preg_match( '/[\x80-\xff]/', $str );
2325 * @param $str string
2326 * @return mixed|string
2328 function ucwords( $str ) {
2329 if ( $this->isMultibyte( $str ) ) {
2330 $str = $this->lc( $str );
2332 // regexp to find first letter in each word (i.e. after each space)
2333 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2335 // function to use to capitalize a single char
2336 if ( function_exists( 'mb_strtoupper' ) ) {
2337 return preg_replace_callback(
2339 array( $this, 'ucwordsCallbackMB' ),
2343 return preg_replace_callback(
2345 array( $this, 'ucwordsCallbackWiki' ),
2350 return ucwords( strtolower( $str ) );
2355 * capitalize words at word breaks
2357 * @param $str string
2360 function ucwordbreaks( $str ) {
2361 if ( $this->isMultibyte( $str ) ) {
2362 $str = $this->lc( $str );
2364 // since \b doesn't work for UTF-8, we explicitely define word break chars
2365 $breaks = "[ \-\(\)\}\{\.,\?!]";
2367 // find first letter after word break
2368 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2370 if ( function_exists( 'mb_strtoupper' ) ) {
2371 return preg_replace_callback(
2373 array( $this, 'ucwordbreaksCallbackMB' ),
2377 return preg_replace_callback(
2379 array( $this, 'ucwordsCallbackWiki' ),
2384 return preg_replace_callback(
2385 '/\b([\w\x80-\xff]+)\b/',
2386 array( $this, 'ucwordbreaksCallbackAscii' ),
2393 * Return a case-folded representation of $s
2395 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2396 * and $s2 are the same except for the case of their characters. It is not
2397 * necessary for the value returned to make sense when displayed.
2399 * Do *not* perform any other normalisation in this function. If a caller
2400 * uses this function when it should be using a more general normalisation
2401 * function, then fix the caller.
2407 function caseFold( $s ) {
2408 return $this->uc( $s );
2415 function checkTitleEncoding( $s ) {
2416 if ( is_array( $s ) ) {
2417 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2419 # Check for non-UTF-8 URLs
2420 $ishigh = preg_match( '/[\x80-\xff]/', $s );
2425 if ( function_exists( 'mb_check_encoding' ) ) {
2426 $isutf8 = mb_check_encoding( $s, 'UTF-8' );
2428 $isutf8 = preg_match( '/^(?>[\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2429 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
2435 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2441 function fallback8bitEncoding() {
2442 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
2446 * Most writing systems use whitespace to break up words.
2447 * Some languages such as Chinese don't conventionally do this,
2448 * which requires special handling when breaking up words for
2453 function hasWordBreaks() {
2458 * Some languages such as Chinese require word segmentation,
2459 * Specify such segmentation when overridden in derived class.
2461 * @param $string String
2464 function segmentByWord( $string ) {
2469 * Some languages have special punctuation need to be normalized.
2470 * Make such changes here.
2472 * @param $string String
2475 function normalizeForSearch( $string ) {
2476 return self
::convertDoubleWidth( $string );
2480 * convert double-width roman characters to single-width.
2481 * range: ff00-ff5f ~= 0020-007f
2483 * @param $string string
2487 protected static function convertDoubleWidth( $string ) {
2488 static $full = null;
2489 static $half = null;
2491 if ( $full === null ) {
2492 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2493 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2494 $full = str_split( $fullWidth, 3 );
2495 $half = str_split( $halfWidth );
2498 $string = str_replace( $full, $half, $string );
2503 * @param $string string
2504 * @param $pattern string
2507 protected static function insertSpace( $string, $pattern ) {
2508 $string = preg_replace( $pattern, " $1 ", $string );
2509 $string = preg_replace( '/ +/', ' ', $string );
2514 * @param $termsArray array
2517 function convertForSearchResult( $termsArray ) {
2518 # some languages, e.g. Chinese, need to do a conversion
2519 # in order for search results to be displayed correctly
2524 * Get the first character of a string.
2529 function firstChar( $s ) {
2532 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2533 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2538 if ( isset( $matches[1] ) ) {
2539 if ( strlen( $matches[1] ) != 3 ) {
2543 // Break down Hangul syllables to grab the first jamo
2544 $code = utf8ToCodepoint( $matches[1] );
2545 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
2547 } elseif ( $code < 0xb098 ) {
2548 return "\xe3\x84\xb1";
2549 } elseif ( $code < 0xb2e4 ) {
2550 return "\xe3\x84\xb4";
2551 } elseif ( $code < 0xb77c ) {
2552 return "\xe3\x84\xb7";
2553 } elseif ( $code < 0xb9c8 ) {
2554 return "\xe3\x84\xb9";
2555 } elseif ( $code < 0xbc14 ) {
2556 return "\xe3\x85\x81";
2557 } elseif ( $code < 0xc0ac ) {
2558 return "\xe3\x85\x82";
2559 } elseif ( $code < 0xc544 ) {
2560 return "\xe3\x85\x85";
2561 } elseif ( $code < 0xc790 ) {
2562 return "\xe3\x85\x87";
2563 } elseif ( $code < 0xcc28 ) {
2564 return "\xe3\x85\x88";
2565 } elseif ( $code < 0xce74 ) {
2566 return "\xe3\x85\x8a";
2567 } elseif ( $code < 0xd0c0 ) {
2568 return "\xe3\x85\x8b";
2569 } elseif ( $code < 0xd30c ) {
2570 return "\xe3\x85\x8c";
2571 } elseif ( $code < 0xd558 ) {
2572 return "\xe3\x85\x8d";
2574 return "\xe3\x85\x8e";
2581 function initEncoding() {
2582 # Some languages may have an alternate char encoding option
2583 # (Esperanto X-coding, Japanese furigana conversion, etc)
2584 # If this language is used as the primary content language,
2585 # an override to the defaults can be set here on startup.
2592 function recodeForEdit( $s ) {
2593 # For some languages we'll want to explicitly specify
2594 # which characters make it into the edit box raw
2595 # or are converted in some way or another.
2596 global $wgEditEncoding;
2597 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
2600 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2608 function recodeInput( $s ) {
2609 # Take the previous into account.
2610 global $wgEditEncoding;
2611 if ( $wgEditEncoding != '' ) {
2612 $enc = $wgEditEncoding;
2616 if ( $enc == 'UTF-8' ) {
2619 return $this->iconv( $enc, 'UTF-8', $s );
2624 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2625 * also cleans up certain backwards-compatible sequences, converting them
2626 * to the modern Unicode equivalent.
2628 * This is language-specific for performance reasons only.
2634 function normalize( $s ) {
2635 global $wgAllUnicodeFixes;
2636 $s = UtfNormal
::cleanUp( $s );
2637 if ( $wgAllUnicodeFixes ) {
2638 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2639 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2646 * Transform a string using serialized data stored in the given file (which
2647 * must be in the serialized subdirectory of $IP). The file contains pairs
2648 * mapping source characters to destination characters.
2650 * The data is cached in process memory. This will go faster if you have the
2651 * FastStringSearch extension.
2653 * @param $file string
2654 * @param $string string
2656 * @throws MWException
2659 function transformUsingPairFile( $file, $string ) {
2660 if ( !isset( $this->transformData
[$file] ) ) {
2661 $data = wfGetPrecompiledData( $file );
2662 if ( $data === false ) {
2663 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2665 $this->transformData
[$file] = new ReplacementArray( $data );
2667 return $this->transformData
[$file]->replace( $string );
2671 * For right-to-left language support
2676 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2680 * Return the correct HTML 'dir' attribute value for this language.
2684 return $this->isRTL() ?
'rtl' : 'ltr';
2688 * Return 'left' or 'right' as appropriate alignment for line-start
2689 * for this language's text direction.
2691 * Should be equivalent to CSS3 'start' text-align value....
2695 function alignStart() {
2696 return $this->isRTL() ?
'right' : 'left';
2700 * Return 'right' or 'left' as appropriate alignment for line-end
2701 * for this language's text direction.
2703 * Should be equivalent to CSS3 'end' text-align value....
2707 function alignEnd() {
2708 return $this->isRTL() ?
'left' : 'right';
2712 * A hidden direction mark (LRM or RLM), depending on the language direction.
2713 * Unlike getDirMark(), this function returns the character as an HTML entity.
2714 * This function should be used when the output is guaranteed to be HTML,
2715 * because it makes the output HTML source code more readable. When
2716 * the output is plain text or can be escaped, getDirMark() should be used.
2718 * @param $opposite Boolean Get the direction mark opposite to your language
2722 function getDirMarkEntity( $opposite = false ) {
2723 if ( $opposite ) { return $this->isRTL() ?
'‎' : '‏'; }
2724 return $this->isRTL() ?
'‏' : '‎';
2728 * A hidden direction mark (LRM or RLM), depending on the language direction.
2729 * This function produces them as invisible Unicode characters and
2730 * the output may be hard to read and debug, so it should only be used
2731 * when the output is plain text or can be escaped. When the output is
2732 * HTML, use getDirMarkEntity() instead.
2734 * @param $opposite Boolean Get the direction mark opposite to your language
2737 function getDirMark( $opposite = false ) {
2738 $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
2739 $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
2740 if ( $opposite ) { return $this->isRTL() ?
$lrm : $rlm; }
2741 return $this->isRTL() ?
$rlm : $lrm;
2747 function capitalizeAllNouns() {
2748 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2752 * An arrow, depending on the language direction.
2754 * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
2757 function getArrow( $direction = 'forwards' ) {
2758 switch ( $direction ) {
2760 return $this->isRTL() ?
'←' : '→';
2762 return $this->isRTL() ?
'→' : '←';
2775 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2779 function linkPrefixExtension() {
2780 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2786 function getMagicWords() {
2787 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2790 protected function doMagicHook() {
2791 if ( $this->mMagicHookDone
) {
2794 $this->mMagicHookDone
= true;
2795 wfProfileIn( 'LanguageGetMagic' );
2796 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2797 wfProfileOut( 'LanguageGetMagic' );
2801 * Fill a MagicWord object with data from here
2805 function getMagic( $mw ) {
2806 $this->doMagicHook();
2808 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
2809 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
2811 $magicWords = $this->getMagicWords();
2812 if ( isset( $magicWords[$mw->mId
] ) ) {
2813 $rawEntry = $magicWords[$mw->mId
];
2819 if ( !is_array( $rawEntry ) ) {
2820 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
2822 $mw->mCaseSensitive
= $rawEntry[0];
2823 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
2828 * Add magic words to the extension array
2830 * @param $newWords array
2832 function addMagicWordsByLang( $newWords ) {
2833 $fallbackChain = $this->getFallbackLanguages();
2834 $fallbackChain = array_reverse( $fallbackChain );
2835 foreach ( $fallbackChain as $code ) {
2836 if ( isset( $newWords[$code] ) ) {
2837 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
2843 * Get special page names, as an associative array
2844 * case folded alias => real name
2846 function getSpecialPageAliases() {
2847 // Cache aliases because it may be slow to load them
2848 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
2850 $this->mExtendedSpecialPageAliases
=
2851 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
2852 wfRunHooks( 'LanguageGetSpecialPageAliases',
2853 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
2856 return $this->mExtendedSpecialPageAliases
;
2860 * Italic is unsuitable for some languages
2862 * @param $text String: the text to be emphasized.
2865 function emphasize( $text ) {
2866 return "<em>$text</em>";
2870 * Normally we output all numbers in plain en_US style, that is
2871 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2872 * point twohundredthirtyfive. However this is not suitable for all
2873 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2874 * Icelandic just want to use commas instead of dots, and dots instead
2875 * of commas like "293.291,235".
2877 * An example of this function being called:
2879 * wfMessage( 'message' )->numParams( $num )->text()
2882 * See LanguageGu.php for the Gujarati implementation and
2883 * $separatorTransformTable on MessageIs.php for
2884 * the , => . and . => , implementation.
2886 * @todo check if it's viable to use localeconv() for the decimal
2888 * @param $number Mixed: the string to be formatted, should be an integer
2889 * or a floating point number.
2890 * @param $nocommafy Bool: set to true for special numbers like dates
2893 public function formatNum( $number, $nocommafy = false ) {
2894 global $wgTranslateNumerals;
2895 if ( !$nocommafy ) {
2896 $number = $this->commafy( $number );
2897 $s = $this->separatorTransformTable();
2899 $number = strtr( $number, $s );
2903 if ( $wgTranslateNumerals ) {
2904 $s = $this->digitTransformTable();
2906 $number = strtr( $number, $s );
2914 * @param $number string
2917 function parseFormattedNumber( $number ) {
2918 $s = $this->digitTransformTable();
2920 $number = strtr( $number, array_flip( $s ) );
2923 $s = $this->separatorTransformTable();
2925 $number = strtr( $number, array_flip( $s ) );
2928 $number = strtr( $number, array( ',' => '' ) );
2933 * Adds commas to a given number
2938 function commafy( $_ ) {
2939 $digitGroupingPattern = $this->digitGroupingPattern();
2940 if ( $_ === null ) {
2944 if ( !$digitGroupingPattern ||
$digitGroupingPattern === "###,###,###" ) {
2945 // default grouping is at thousands, use the same for ###,###,### pattern too.
2946 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2948 // Ref: http://cldr.unicode.org/translation/number-patterns
2950 if ( intval( $_ ) < 0 ) {
2951 // For negative numbers apply the algorithm like positive number and add sign.
2953 $_ = substr( $_, 1 );
2955 $numberpart = array();
2956 $decimalpart = array();
2957 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
2958 preg_match( "/\d+/", $_, $numberpart );
2959 preg_match( "/\.\d*/", $_, $decimalpart );
2960 $groupedNumber = ( count( $decimalpart ) > 0 ) ?
$decimalpart[0]:"";
2961 if ( $groupedNumber === $_ ) {
2962 // the string does not have any number part. Eg: .12345
2963 return $sign . $groupedNumber;
2965 $start = $end = strlen( $numberpart[0] );
2966 while ( $start > 0 ) {
2967 $match = $matches[0][$numMatches -1] ;
2968 $matchLen = strlen( $match );
2969 $start = $end - $matchLen;
2973 $groupedNumber = substr( $_ , $start, $end -$start ) . $groupedNumber ;
2975 if ( $numMatches > 1 ) {
2976 // use the last pattern for the rest of the number
2980 $groupedNumber = "," . $groupedNumber;
2983 return $sign . $groupedNumber;
2989 function digitGroupingPattern() {
2990 return self
::$dataCache->getItem( $this->mCode
, 'digitGroupingPattern' );
2996 function digitTransformTable() {
2997 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
3003 function separatorTransformTable() {
3004 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
3008 * Take a list of strings and build a locale-friendly comma-separated
3009 * list, using the local comma-separator message.
3010 * The last two strings are chained with an "and".
3011 * NOTE: This function will only work with standard numeric array keys (0, 1, 2…)
3016 function listToText( array $l ) {
3018 $m = count( $l ) - 1;
3022 } elseif ( $m === 1 ) {
3023 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
3025 for ( $i = $m; $i >= 0; $i-- ) {
3028 } elseif ( $i == $m - 1 ) {
3029 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
3031 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
3039 * Take a list of strings and build a locale-friendly comma-separated
3040 * list, using the local comma-separator message.
3041 * @param $list array of strings to put in a comma list
3044 function commaList( array $list ) {
3046 wfMessage( 'comma-separator' )->inLanguage( $this )->escaped(),
3052 * Take a list of strings and build a locale-friendly semicolon-separated
3053 * list, using the local semicolon-separator message.
3054 * @param $list array of strings to put in a semicolon list
3057 function semicolonList( array $list ) {
3059 wfMessage( 'semicolon-separator' )->inLanguage( $this )->escaped(),
3065 * Same as commaList, but separate it with the pipe instead.
3066 * @param $list array of strings to put in a pipe list
3069 function pipeList( array $list ) {
3071 wfMessage( 'pipe-separator' )->inLanguage( $this )->escaped(),
3077 * Truncate a string to a specified length in bytes, appending an optional
3078 * string (e.g. for ellipses)
3080 * The database offers limited byte lengths for some columns in the database;
3081 * multi-byte character sets mean we need to ensure that only whole characters
3082 * are included, otherwise broken characters can be passed to the user
3084 * If $length is negative, the string will be truncated from the beginning
3086 * @param $string String to truncate
3087 * @param $length Int: maximum length (including ellipses)
3088 * @param $ellipsis String to append to the truncated text
3089 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
3090 * $adjustLength was introduced in 1.18, before that behaved as if false.
3093 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
3094 # Use the localized ellipsis character
3095 if ( $ellipsis == '...' ) {
3096 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3098 # Check if there is no need to truncate
3099 if ( $length == 0 ) {
3100 return $ellipsis; // convention
3101 } elseif ( strlen( $string ) <= abs( $length ) ) {
3102 return $string; // no need to truncate
3104 $stringOriginal = $string;
3105 # If ellipsis length is >= $length then we can't apply $adjustLength
3106 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
3107 $string = $ellipsis; // this can be slightly unexpected
3108 # Otherwise, truncate and add ellipsis...
3110 $eLength = $adjustLength ?
strlen( $ellipsis ) : 0;
3111 if ( $length > 0 ) {
3112 $length -= $eLength;
3113 $string = substr( $string, 0, $length ); // xyz...
3114 $string = $this->removeBadCharLast( $string );
3115 $string = $string . $ellipsis;
3117 $length +
= $eLength;
3118 $string = substr( $string, $length ); // ...xyz
3119 $string = $this->removeBadCharFirst( $string );
3120 $string = $ellipsis . $string;
3123 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
3124 # This check is *not* redundant if $adjustLength, due to the single case where
3125 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
3126 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
3129 return $stringOriginal;
3134 * Remove bytes that represent an incomplete Unicode character
3135 * at the end of string (e.g. bytes of the char are missing)
3137 * @param $string String
3140 protected function removeBadCharLast( $string ) {
3141 if ( $string != '' ) {
3142 $char = ord( $string[strlen( $string ) - 1] );
3144 if ( $char >= 0xc0 ) {
3145 # We got the first byte only of a multibyte char; remove it.
3146 $string = substr( $string, 0, -1 );
3147 } elseif ( $char >= 0x80 &&
3148 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
3149 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
3151 # We chopped in the middle of a character; remove it
3159 * Remove bytes that represent an incomplete Unicode character
3160 * at the start of string (e.g. bytes of the char are missing)
3162 * @param $string String
3165 protected function removeBadCharFirst( $string ) {
3166 if ( $string != '' ) {
3167 $char = ord( $string[0] );
3168 if ( $char >= 0x80 && $char < 0xc0 ) {
3169 # We chopped in the middle of a character; remove the whole thing
3170 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
3177 * Truncate a string of valid HTML to a specified length in bytes,
3178 * appending an optional string (e.g. for ellipses), and return valid HTML
3180 * This is only intended for styled/linked text, such as HTML with
3181 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
3182 * Also, this will not detect things like "display:none" CSS.
3184 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
3186 * @param string $text HTML string to truncate
3187 * @param int $length (zero/positive) Maximum length (including ellipses)
3188 * @param string $ellipsis String to append to the truncated text
3191 function truncateHtml( $text, $length, $ellipsis = '...' ) {
3192 # Use the localized ellipsis character
3193 if ( $ellipsis == '...' ) {
3194 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3196 # Check if there is clearly no need to truncate
3197 if ( $length <= 0 ) {
3198 return $ellipsis; // no text shown, nothing to format (convention)
3199 } elseif ( strlen( $text ) <= $length ) {
3200 return $text; // string short enough even *with* HTML (short-circuit)
3203 $dispLen = 0; // innerHTML legth so far
3204 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3205 $tagType = 0; // 0-open, 1-close
3206 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3207 $entityState = 0; // 0-not entity, 1-entity
3208 $tag = $ret = ''; // accumulated tag name, accumulated result string
3209 $openTags = array(); // open tag stack
3210 $maybeState = null; // possible truncation state
3212 $textLen = strlen( $text );
3213 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3214 for ( $pos = 0; true; ++
$pos ) {
3215 # Consider truncation once the display length has reached the maximim.
3216 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3217 # Check that we're not in the middle of a bracket/entity...
3218 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3219 if ( !$testingEllipsis ) {
3220 $testingEllipsis = true;
3221 # Save where we are; we will truncate here unless there turn out to
3222 # be so few remaining characters that truncation is not necessary.
3223 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3224 $maybeState = array( $ret, $openTags ); // save state
3226 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3227 # String in fact does need truncation, the truncation point was OK.
3228 list( $ret, $openTags ) = $maybeState; // reload state
3229 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3230 $ret .= $ellipsis; // add ellipsis
3234 if ( $pos >= $textLen ) break; // extra iteration just for above checks
3236 # Read the next char...
3238 $lastCh = $pos ?
$text[$pos - 1] : '';
3239 $ret .= $ch; // add to result string
3241 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3242 $entityState = 0; // for bad HTML
3243 $bracketState = 1; // tag started (checking for backslash)
3244 } elseif ( $ch == '>' ) {
3245 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3246 $entityState = 0; // for bad HTML
3247 $bracketState = 0; // out of brackets
3248 } elseif ( $bracketState == 1 ) {
3250 $tagType = 1; // close tag (e.g. "</span>")
3252 $tagType = 0; // open tag (e.g. "<span>")
3255 $bracketState = 2; // building tag name
3256 } elseif ( $bracketState == 2 ) {
3260 // Name found (e.g. "<a href=..."), add on tag attributes...
3261 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
3263 } elseif ( $bracketState == 0 ) {
3264 if ( $entityState ) {
3267 $dispLen++
; // entity is one displayed char
3270 if ( $neLength == 0 && !$maybeState ) {
3271 // Save state without $ch. We want to *hit* the first
3272 // display char (to get tags) but not *use* it if truncating.
3273 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3276 $entityState = 1; // entity found, (e.g. " ")
3278 $dispLen++
; // this char is displayed
3279 // Add the next $max display text chars after this in one swoop...
3280 $max = ( $testingEllipsis ?
$length : $neLength ) - $dispLen;
3281 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos +
1, $max );
3282 $dispLen +
= $skipped;
3288 // Close the last tag if left unclosed by bad HTML
3289 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3290 while ( count( $openTags ) > 0 ) {
3291 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3297 * truncateHtml() helper function
3298 * like strcspn() but adds the skipped chars to $ret
3307 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3308 if ( $len === null ) {
3309 $len = -1; // -1 means "no limit" for strcspn
3310 } elseif ( $len < 0 ) {
3314 if ( $start < strlen( $text ) ) {
3315 $skipCount = strcspn( $text, $search, $start, $len );
3316 $ret .= substr( $text, $start, $skipCount );
3322 * truncateHtml() helper function
3323 * (a) push or pop $tag from $openTags as needed
3324 * (b) clear $tag value
3325 * @param &$tag string Current HTML tag name we are looking at
3326 * @param $tagType int (0-open tag, 1-close tag)
3327 * @param $lastCh string Character before the '>' that ended this tag
3328 * @param &$openTags array Open tag stack (not accounting for $tag)
3330 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3331 $tag = ltrim( $tag );
3333 if ( $tagType == 0 && $lastCh != '/' ) {
3334 $openTags[] = $tag; // tag opened (didn't close itself)
3335 } elseif ( $tagType == 1 ) {
3336 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3337 array_pop( $openTags ); // tag closed
3345 * Grammatical transformations, needed for inflected languages
3346 * Invoked by putting {{grammar:case|word}} in a message
3348 * @param $word string
3349 * @param $case string
3352 function convertGrammar( $word, $case ) {
3353 global $wgGrammarForms;
3354 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3355 return $wgGrammarForms[$this->getCode()][$case][$word];
3360 * Get the grammar forms for the content language
3361 * @return array of grammar forms
3364 function getGrammarForms() {
3365 global $wgGrammarForms;
3366 if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
3367 return $wgGrammarForms[$this->getCode()];
3372 * Provides an alternative text depending on specified gender.
3373 * Usage {{gender:username|masculine|feminine|neutral}}.
3374 * username is optional, in which case the gender of current user is used,
3375 * but only in (some) interface messages; otherwise default gender is used.
3377 * If no forms are given, an empty string is returned. If only one form is
3378 * given, it will be returned unconditionally. These details are implied by
3379 * the caller and cannot be overridden in subclasses.
3381 * If more than one form is given, the default is to use the neutral one
3382 * if it is specified, and to use the masculine one otherwise. These
3383 * details can be overridden in subclasses.
3385 * @param $gender string
3386 * @param $forms array
3390 function gender( $gender, $forms ) {
3391 if ( !count( $forms ) ) {
3394 $forms = $this->preConvertPlural( $forms, 2 );
3395 if ( $gender === 'male' ) {
3398 if ( $gender === 'female' ) {
3401 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
3405 * Plural form transformations, needed for some languages.
3406 * For example, there are 3 form of plural in Russian and Polish,
3407 * depending on "count mod 10". See [[w:Plural]]
3408 * For English it is pretty simple.
3410 * Invoked by putting {{plural:count|wordform1|wordform2}}
3411 * or {{plural:count|wordform1|wordform2|wordform3}}
3413 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3415 * @param $count Integer: non-localized number
3416 * @param $forms Array: different plural forms
3417 * @return string Correct form of plural for $count in this language
3419 function convertPlural( $count, $forms ) {
3420 if ( !count( $forms ) ) {
3424 // Handle explicit 0= and 1= forms
3425 foreach ( $forms as $index => $form ) {
3426 if ( isset( $form[1] ) && $form[1] === '=' ) {
3427 if ( $form[0] === (string) $count ) {
3428 return substr( $form, 2 );
3430 unset( $forms[$index] );
3433 $forms = array_values( $forms );
3435 $pluralForm = $this->getPluralForm( $count );
3436 $pluralForm = min( $pluralForm, count( $forms ) - 1 );
3437 return $forms[$pluralForm];
3441 * Checks that convertPlural was given an array and pads it to requested
3442 * amount of forms by copying the last one.
3444 * @param $count Integer: How many forms should there be at least
3445 * @param $forms Array of forms given to convertPlural
3446 * @return array Padded array of forms or an exception if not an array
3448 protected function preConvertPlural( /* Array */ $forms, $count ) {
3449 while ( count( $forms ) < $count ) {
3450 $forms[] = $forms[count( $forms ) - 1];
3456 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3457 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3458 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3459 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3462 * @param $str String: the validated block duration in English
3463 * @return string Somehow translated block duration
3464 * @see LanguageFi.php for example implementation
3466 function translateBlockExpiry( $str ) {
3467 $duration = SpecialBlock
::getSuggestedDurations( $this );
3468 foreach ( $duration as $show => $value ) {
3469 if ( strcmp( $str, $value ) == 0 ) {
3470 return htmlspecialchars( trim( $show ) );
3474 // Since usually only infinite or indefinite is only on list, so try
3475 // equivalents if still here.
3476 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3477 if ( in_array( $str, $indefs ) ) {
3478 foreach ( $indefs as $val ) {
3479 $show = array_search( $val, $duration, true );
3480 if ( $show !== false ) {
3481 return htmlspecialchars( trim( $show ) );
3486 // If all else fails, return a standard duration or timestamp description.
3487 $time = strtotime( $str, 0 );
3488 if ( $time === false ) { // Unknown format. Return it as-is in case.
3490 } elseif ( $time !== strtotime( $str, 1 ) ) { // It's a relative timestamp.
3491 // $time is relative to 0 so it's a duration length.
3492 return $this->formatDuration( $time );
3493 } else { // It's an absolute timestamp.
3494 if ( $time === 0 ) {
3495 // wfTimestamp() handles 0 as current time instead of epoch.
3496 return $this->timeanddate( '19700101000000' );
3498 return $this->timeanddate( $time );
3504 * languages like Chinese need to be segmented in order for the diff
3507 * @param $text String
3510 public function segmentForDiff( $text ) {
3515 * and unsegment to show the result
3517 * @param $text String
3520 public function unsegmentForDiff( $text ) {
3525 * Return the LanguageConverter used in the Language
3528 * @return LanguageConverter
3530 public function getConverter() {
3531 return $this->mConverter
;
3535 * convert text to all supported variants
3537 * @param $text string
3540 public function autoConvertToAllVariants( $text ) {
3541 return $this->mConverter
->autoConvertToAllVariants( $text );
3545 * convert text to different variants of a language.
3547 * @param $text string
3550 public function convert( $text ) {
3551 return $this->mConverter
->convert( $text );
3555 * Convert a Title object to a string in the preferred variant
3557 * @param $title Title
3560 public function convertTitle( $title ) {
3561 return $this->mConverter
->convertTitle( $title );
3565 * Convert a namespace index to a string in the preferred variant
3570 public function convertNamespace( $ns ) {
3571 return $this->mConverter
->convertNamespace( $ns );
3575 * Check if this is a language with variants
3579 public function hasVariants() {
3580 return sizeof( $this->getVariants() ) > 1;
3584 * Check if the language has the specific variant
3587 * @param $variant string
3590 public function hasVariant( $variant ) {
3591 return (bool)$this->mConverter
->validateVariant( $variant );
3595 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3597 * @param $text string
3600 public function armourMath( $text ) {
3601 return $this->mConverter
->armourMath( $text );
3605 * Perform output conversion on a string, and encode for safe HTML output.
3606 * @param $text String text to be converted
3607 * @param $isTitle Bool whether this conversion is for the article title
3609 * @todo this should get integrated somewhere sane
3611 public function convertHtml( $text, $isTitle = false ) {
3612 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3616 * @param $key string
3619 public function convertCategoryKey( $key ) {
3620 return $this->mConverter
->convertCategoryKey( $key );
3624 * Get the list of variants supported by this language
3625 * see sample implementation in LanguageZh.php
3627 * @return array an array of language codes
3629 public function getVariants() {
3630 return $this->mConverter
->getVariants();
3636 public function getPreferredVariant() {
3637 return $this->mConverter
->getPreferredVariant();
3643 public function getDefaultVariant() {
3644 return $this->mConverter
->getDefaultVariant();
3650 public function getURLVariant() {
3651 return $this->mConverter
->getURLVariant();
3655 * If a language supports multiple variants, it is
3656 * possible that non-existing link in one variant
3657 * actually exists in another variant. this function
3658 * tries to find it. See e.g. LanguageZh.php
3660 * @param $link String: the name of the link
3661 * @param $nt Mixed: the title object of the link
3662 * @param $ignoreOtherCond Boolean: to disable other conditions when
3663 * we need to transclude a template or update a category's link
3664 * @return null the input parameters may be modified upon return
3666 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3667 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
3671 * If a language supports multiple variants, converts text
3672 * into an array of all possible variants of the text:
3673 * 'variant' => text in that variant
3675 * @deprecated since 1.17 Use autoConvertToAllVariants()
3677 * @param $text string
3681 public function convertLinkToAllVariants( $text ) {
3682 return $this->mConverter
->convertLinkToAllVariants( $text );
3686 * returns language specific options used by User::getPageRenderHash()
3687 * for example, the preferred language variant
3691 function getExtraHashOptions() {
3692 return $this->mConverter
->getExtraHashOptions();
3696 * For languages that support multiple variants, the title of an
3697 * article may be displayed differently in different variants. this
3698 * function returns the apporiate title defined in the body of the article.
3702 public function getParsedTitle() {
3703 return $this->mConverter
->getParsedTitle();
3707 * Enclose a string with the "no conversion" tag. This is used by
3708 * various functions in the Parser
3710 * @param $text String: text to be tagged for no conversion
3711 * @param $noParse bool
3712 * @return string the tagged text
3714 public function markNoConversion( $text, $noParse = false ) {
3715 return $this->mConverter
->markNoConversion( $text, $noParse );
3719 * A regular expression to match legal word-trailing characters
3720 * which should be merged onto a link of the form [[foo]]bar.
3724 public function linkTrail() {
3725 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
3731 function getLangObj() {
3736 * Get the RFC 3066 code for this language object
3738 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
3739 * htmlspecialchars() or similar
3743 public function getCode() {
3744 return $this->mCode
;
3748 * Get the code in Bcp47 format which we can use
3749 * inside of html lang="" tags.
3751 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
3752 * htmlspecialchars() or similar.
3757 public function getHtmlCode() {
3758 if ( is_null( $this->mHtmlCode
) ) {
3759 $this->mHtmlCode
= wfBCP47( $this->getCode() );
3761 return $this->mHtmlCode
;
3765 * @param $code string
3767 public function setCode( $code ) {
3768 $this->mCode
= $code;
3769 // Ensure we don't leave an incorrect html code lying around
3770 $this->mHtmlCode
= null;
3774 * Get the name of a file for a certain language code
3775 * @param $prefix string Prepend this to the filename
3776 * @param $code string Language code
3777 * @param $suffix string Append this to the filename
3778 * @throws MWException
3779 * @return string $prefix . $mangledCode . $suffix
3781 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
3782 // Protect against path traversal
3783 if ( !Language
::isValidCode( $code )
3784 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
3786 throw new MWException( "Invalid language code \"$code\"" );
3789 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
3793 * Get the language code from a file name. Inverse of getFileName()
3794 * @param $filename string $prefix . $languageCode . $suffix
3795 * @param $prefix string Prefix before the language code
3796 * @param $suffix string Suffix after the language code
3797 * @return string Language code, or false if $prefix or $suffix isn't found
3799 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
3801 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
3802 preg_quote( $suffix, '/' ) . '/', $filename, $m );
3803 if ( !count( $m ) ) {
3806 return str_replace( '_', '-', strtolower( $m[1] ) );
3810 * @param $code string
3813 public static function getMessagesFileName( $code ) {
3815 $file = self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
3816 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
3821 * @param $code string
3824 public static function getClassFileName( $code ) {
3826 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
3830 * Get the first fallback for a given language.
3832 * @param $code string
3834 * @return bool|string
3836 public static function getFallbackFor( $code ) {
3837 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3840 $fallbacks = self
::getFallbacksFor( $code );
3841 $first = array_shift( $fallbacks );
3847 * Get the ordered list of fallback languages.
3850 * @param $code string Language code
3853 public static function getFallbacksFor( $code ) {
3854 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3857 $v = self
::getLocalisationCache()->getItem( $code, 'fallback' );
3858 $v = array_map( 'trim', explode( ',', $v ) );
3859 if ( $v[count( $v ) - 1] !== 'en' ) {
3867 * Get all messages for a given language
3868 * WARNING: this may take a long time. If you just need all message *keys*
3869 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
3871 * @param $code string
3875 public static function getMessagesFor( $code ) {
3876 return self
::getLocalisationCache()->getItem( $code, 'messages' );
3880 * Get a message for a given language
3882 * @param $key string
3883 * @param $code string
3887 public static function getMessageFor( $key, $code ) {
3888 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
3892 * Get all message keys for a given language. This is a faster alternative to
3893 * array_keys( Language::getMessagesFor( $code ) )
3896 * @param $code string Language code
3897 * @return array of message keys (strings)
3899 public static function getMessageKeysFor( $code ) {
3900 return self
::getLocalisationCache()->getSubItemList( $code, 'messages' );
3907 function fixVariableInNamespace( $talk ) {
3908 if ( strpos( $talk, '$1' ) === false ) {
3912 global $wgMetaNamespace;
3913 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
3915 # Allow grammar transformations
3916 # Allowing full message-style parsing would make simple requests
3917 # such as action=raw much more expensive than they need to be.
3918 # This will hopefully cover most cases.
3919 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
3920 array( &$this, 'replaceGrammarInNamespace' ), $talk );
3921 return str_replace( ' ', '_', $talk );
3928 function replaceGrammarInNamespace( $m ) {
3929 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
3933 * @throws MWException
3936 static function getCaseMaps() {
3937 static $wikiUpperChars, $wikiLowerChars;
3938 if ( isset( $wikiUpperChars ) ) {
3939 return array( $wikiUpperChars, $wikiLowerChars );
3942 wfProfileIn( __METHOD__
);
3943 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
3944 if ( $arr === false ) {
3945 throw new MWException(
3946 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
3948 $wikiUpperChars = $arr['wikiUpperChars'];
3949 $wikiLowerChars = $arr['wikiLowerChars'];
3950 wfProfileOut( __METHOD__
);
3951 return array( $wikiUpperChars, $wikiLowerChars );
3955 * Decode an expiry (block, protection, etc) which has come from the DB
3957 * @todo FIXME: why are we returnings DBMS-dependent strings???
3959 * @param $expiry String: Database expiry String
3960 * @param $format Bool|Int true to process using language functions, or TS_ constant
3961 * to return the expiry in a given timestamp
3965 public function formatExpiry( $expiry, $format = true ) {
3966 static $infinity, $infinityMsg;
3967 if ( $infinity === null ) {
3968 $infinityMsg = wfMessage( 'infiniteblock' );
3969 $infinity = wfGetDB( DB_SLAVE
)->getInfinity();
3972 if ( $expiry == '' ||
$expiry == $infinity ) {
3973 return $format === true
3977 return $format === true
3978 ?
$this->timeanddate( $expiry, /* User preference timezone */ true )
3979 : wfTimestamp( $format, $expiry );
3985 * @param $seconds int|float
3986 * @param $format Array Optional
3987 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
3988 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
3989 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
3990 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
3993 function formatTimePeriod( $seconds, $format = array() ) {
3994 if ( !is_array( $format ) ) {
3995 $format = array( 'avoid' => $format ); // For backwards compatibility
3997 if ( !isset( $format['avoid'] ) ) {
3998 $format['avoid'] = false;
4000 if ( !isset( $format['noabbrevs' ] ) ) {
4001 $format['noabbrevs'] = false;
4003 $secondsMsg = wfMessage(
4004 $format['noabbrevs'] ?
'seconds' : 'seconds-abbrev' )->inLanguage( $this );
4005 $minutesMsg = wfMessage(
4006 $format['noabbrevs'] ?
'minutes' : 'minutes-abbrev' )->inLanguage( $this );
4007 $hoursMsg = wfMessage(
4008 $format['noabbrevs'] ?
'hours' : 'hours-abbrev' )->inLanguage( $this );
4009 $daysMsg = wfMessage(
4010 $format['noabbrevs'] ?
'days' : 'days-abbrev' )->inLanguage( $this );
4012 if ( round( $seconds * 10 ) < 100 ) {
4013 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
4014 $s = $secondsMsg->params( $s )->text();
4015 } elseif ( round( $seconds ) < 60 ) {
4016 $s = $this->formatNum( round( $seconds ) );
4017 $s = $secondsMsg->params( $s )->text();
4018 } elseif ( round( $seconds ) < 3600 ) {
4019 $minutes = floor( $seconds / 60 );
4020 $secondsPart = round( fmod( $seconds, 60 ) );
4021 if ( $secondsPart == 60 ) {
4025 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4027 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4028 } elseif ( round( $seconds ) <= 2 * 86400 ) {
4029 $hours = floor( $seconds / 3600 );
4030 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
4031 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
4032 if ( $secondsPart == 60 ) {
4036 if ( $minutes == 60 ) {
4040 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
4042 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4043 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
4044 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4047 $days = floor( $seconds / 86400 );
4048 if ( $format['avoid'] === 'avoidminutes' ) {
4049 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
4050 if ( $hours == 24 ) {
4054 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4056 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4057 } elseif ( $format['avoid'] === 'avoidseconds' ) {
4058 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
4059 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
4060 if ( $minutes == 60 ) {
4064 if ( $hours == 24 ) {
4068 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4070 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4072 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4074 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4076 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
4083 * Format a bitrate for output, using an appropriate
4084 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
4086 * This use base 1000. For base 1024 use formatSize(), for another base
4087 * see formatComputingNumbers()
4092 function formatBitrate( $bps ) {
4093 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
4097 * @param $size int Size of the unit
4098 * @param $boundary int Size boundary (1000, or 1024 in most cases)
4099 * @param $messageKey string Message key to be uesd
4102 function formatComputingNumbers( $size, $boundary, $messageKey ) {
4104 return str_replace( '$1', $this->formatNum( $size ),
4105 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
4108 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
4111 $maxIndex = count( $sizes ) - 1;
4112 while ( $size >= $boundary && $index < $maxIndex ) {
4117 // For small sizes no decimal places necessary
4120 // For MB and bigger two decimal places are smarter
4123 $msg = str_replace( '$1', $sizes[$index], $messageKey );
4125 $size = round( $size, $round );
4126 $text = $this->getMessageFromDB( $msg );
4127 return str_replace( '$1', $this->formatNum( $size ), $text );
4131 * Format a size in bytes for output, using an appropriate
4132 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
4134 * This method use base 1024. For base 1000 use formatBitrate(), for
4135 * another base see formatComputingNumbers()
4137 * @param $size int Size to format
4138 * @return string Plain text (not HTML)
4140 function formatSize( $size ) {
4141 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
4145 * Make a list item, used by various special pages
4147 * @param $page String Page link
4148 * @param $details String Text between brackets
4149 * @param $oppositedm Boolean Add the direction mark opposite to your
4150 * language, to display text properly
4153 function specialList( $page, $details, $oppositedm = true ) {
4154 $dirmark = ( $oppositedm ?
$this->getDirMark( true ) : '' ) .
4155 $this->getDirMark();
4156 $details = $details ?
$dirmark . $this->getMessageFromDB( 'word-separator' ) .
4157 wfMessage( 'parentheses' )->rawParams( $details )->inLanguage( $this )->escaped() : '';
4158 return $page . $details;
4162 * Generate (prev x| next x) (20|50|100...) type links for paging
4164 * @param $title Title object to link
4165 * @param $offset Integer offset parameter
4166 * @param $limit Integer limit parameter
4167 * @param $query array|String optional URL query parameter string
4168 * @param $atend Bool optional param for specified if this is the last page
4171 public function viewPrevNext( Title
$title, $offset, $limit, array $query = array(), $atend = false ) {
4172 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
4174 # Make 'previous' link
4175 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4176 if ( $offset > 0 ) {
4177 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
4178 $query, $prev, 'prevn-title', 'mw-prevlink' );
4180 $plink = htmlspecialchars( $prev );
4184 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4186 $nlink = htmlspecialchars( $next );
4188 $nlink = $this->numLink( $title, $offset +
$limit, $limit,
4189 $query, $next, 'prevn-title', 'mw-nextlink' );
4192 # Make links to set number of items per page
4193 $numLinks = array();
4194 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
4195 $numLinks[] = $this->numLink( $title, $offset, $num,
4196 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
4199 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
4200 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
4204 * Helper function for viewPrevNext() that generates links
4206 * @param $title Title object to link
4207 * @param $offset Integer offset parameter
4208 * @param $limit Integer limit parameter
4209 * @param $query Array extra query parameters
4210 * @param $link String text to use for the link; will be escaped
4211 * @param $tooltipMsg String name of the message to use as tooltip
4212 * @param $class String value of the "class" attribute of the link
4213 * @return String HTML fragment
4215 private function numLink( Title
$title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
4216 $query = array( 'limit' => $limit, 'offset' => $offset ) +
$query;
4217 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4218 return Html
::element( 'a', array( 'href' => $title->getLocalURL( $query ),
4219 'title' => $tooltip, 'class' => $class ), $link );
4223 * Get the conversion rule title, if any.
4227 public function getConvRuleTitle() {
4228 return $this->mConverter
->getConvRuleTitle();
4232 * Get the compiled plural rules for the language
4234 * @return array Associative array with plural form, and plural rule as key-value pairs
4236 public function getCompiledPluralRules() {
4237 $pluralRules = self
::$dataCache->getItem( strtolower( $this->mCode
), 'compiledPluralRules' );
4238 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4239 if ( !$pluralRules ) {
4240 foreach ( $fallbacks as $fallbackCode ) {
4241 $pluralRules = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
4242 if ( $pluralRules ) {
4247 return $pluralRules;
4251 * Get the plural rules for the language
4253 * @return array Associative array with plural form, and plural rule as key-value pairs
4255 public function getPluralRules() {
4256 $pluralRules = self
::$dataCache->getItem( strtolower( $this->mCode
), 'pluralRules' );
4257 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4258 if ( !$pluralRules ) {
4259 foreach ( $fallbacks as $fallbackCode ) {
4260 $pluralRules = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRules' );
4261 if ( $pluralRules ) {
4266 return $pluralRules;
4270 * Find the plural form matching to the given number
4271 * It return the form index.
4272 * @return int The index of the plural form
4274 private function getPluralForm( $number ) {
4275 $pluralRules = $this->getCompiledPluralRules();
4276 $form = CLDRPluralRuleEvaluator
::evaluateCompiled( $number, $pluralRules );