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( dirname( __FILE__
) . '/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 getVariants() { return array( $this->mLang
->getCode() ); }
58 function getPreferredVariant() { return $this->mLang
->getCode(); }
59 function getDefaultVariant() { return $this->mLang
->getCode(); }
60 function getURLVariant() { return ''; }
61 function getConvRuleTitle() { return false; }
62 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
63 function getExtraHashOptions() { return ''; }
64 function getParsedTitle() { return ''; }
65 function markNoConversion( $text, $noParse = false ) { return $text; }
66 function convertCategoryKey( $key ) { return $key; }
67 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
68 function armourMath( $text ) { return $text; }
72 * Internationalisation code
78 * @var LanguageConverter
82 var $mVariants, $mCode, $mLoaded = false;
83 var $mMagicExtensions = array(), $mMagicHookDone = false;
84 private $mHtmlCode = null;
86 var $dateFormatStrings = array();
87 var $mExtendedSpecialPageAliases;
89 protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
92 * ReplacementArray object caches
94 var $transformData = array();
97 * @var LocalisationCache
99 static public $dataCache;
101 static public $mLangObjCache = array();
103 static public $mWeekdayMsgs = array(
104 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
108 static public $mWeekdayAbbrevMsgs = array(
109 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
112 static public $mMonthMsgs = array(
113 'january', 'february', 'march', 'april', 'may_long', 'june',
114 'july', 'august', 'september', 'october', 'november',
117 static public $mMonthGenMsgs = array(
118 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
119 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
122 static public $mMonthAbbrevMsgs = array(
123 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
124 'sep', 'oct', 'nov', 'dec'
127 static public $mIranianCalendarMonthMsgs = array(
128 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
129 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
130 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
131 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
134 static public $mHebrewCalendarMonthMsgs = array(
135 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
136 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
137 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
138 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
139 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
142 static public $mHebrewCalendarMonthGenMsgs = array(
143 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
144 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
145 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
146 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
147 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
150 static public $mHijriCalendarMonthMsgs = array(
151 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
152 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
153 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
154 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
161 static public $durationIntervals = array(
162 'millennia' => 31557600000,
163 'centuries' => 3155760000,
164 'decades' => 315576000,
165 'years' => 31557600, // 86400 * 365.25
174 * Get a cached language object for a given language code
175 * @param $code String
178 static function factory( $code ) {
179 if ( !isset( self
::$mLangObjCache[$code] ) ) {
180 if ( count( self
::$mLangObjCache ) > 10 ) {
181 // Don't keep a billion objects around, that's stupid.
182 self
::$mLangObjCache = array();
184 self
::$mLangObjCache[$code] = self
::newFromCode( $code );
186 return self
::$mLangObjCache[$code];
190 * Create a language object for a given language code
191 * @param $code String
192 * @throws MWException
195 protected static function newFromCode( $code ) {
196 // Protect against path traversal below
197 if ( !Language
::isValidCode( $code )
198 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
200 throw new MWException( "Invalid language code \"$code\"" );
203 if ( !Language
::isValidBuiltInCode( $code ) ) {
204 // It's not possible to customise this code with class files, so
205 // just return a Language object. This is to support uselang= hacks.
206 $lang = new Language
;
207 $lang->setCode( $code );
211 // Check if there is a language class for the code
212 $class = self
::classFromCode( $code );
213 self
::preloadLanguageClass( $class );
214 if ( MWInit
::classExists( $class ) ) {
219 // Keep trying the fallback list until we find an existing class
220 $fallbacks = Language
::getFallbacksFor( $code );
221 foreach ( $fallbacks as $fallbackCode ) {
222 if ( !Language
::isValidBuiltInCode( $fallbackCode ) ) {
223 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
226 $class = self
::classFromCode( $fallbackCode );
227 self
::preloadLanguageClass( $class );
228 if ( MWInit
::classExists( $class ) ) {
229 $lang = Language
::newFromCode( $fallbackCode );
230 $lang->setCode( $code );
235 throw new MWException( "Invalid fallback sequence for language '$code'" );
239 * Returns true if a language code string is of a valid form, whether or
240 * not it exists. This includes codes which are used solely for
241 * customisation via the MediaWiki namespace.
243 * @param $code string
247 public static function isValidCode( $code ) {
249 strcspn( $code, ":/\\\000" ) === strlen( $code )
250 && !preg_match( Title
::getTitleInvalidRegex(), $code );
254 * Returns true if a language code is of a valid form for the purposes of
255 * internal customisation of MediaWiki, via Messages*.php.
257 * @param $code string
262 public static function isValidBuiltInCode( $code ) {
264 if( !is_string($code) ) {
265 $type = gettype( $code );
266 if( $type === 'object' ) {
267 $addmsg = " of class " . get_class( $code );
271 throw new MWException( __METHOD__
. " must be passed a string, $type given$addmsg" );
274 return preg_match( '/^[a-z0-9-]+$/i', $code );
279 * @return String Name of the language class
281 public static function classFromCode( $code ) {
282 if ( $code == 'en' ) {
285 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
290 * Includes language class files
292 * @param $class string Name of the language class
294 public static function preloadLanguageClass( $class ) {
297 if ( $class === 'Language' ) {
301 if ( !defined( 'MW_COMPILED' ) ) {
302 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
303 include_once( "$IP/languages/classes/$class.php" );
309 * Get the LocalisationCache instance
311 * @return LocalisationCache
313 public static function getLocalisationCache() {
314 if ( is_null( self
::$dataCache ) ) {
315 global $wgLocalisationCacheConf;
316 $class = $wgLocalisationCacheConf['class'];
317 self
::$dataCache = new $class( $wgLocalisationCacheConf );
319 return self
::$dataCache;
322 function __construct() {
323 $this->mConverter
= new FakeConverter( $this );
324 // Set the code to the name of the descendant
325 if ( get_class( $this ) == 'Language' ) {
328 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
330 self
::getLocalisationCache();
334 * Reduce memory usage
336 function __destruct() {
337 foreach ( $this as $name => $value ) {
338 unset( $this->$name );
343 * Hook which will be called if this is the content language.
344 * Descendants can use this to register hook functions or modify globals
346 function initContLang() { }
349 * Same as getFallbacksFor for current language.
351 * @deprecated in 1.19
353 function getFallbackLanguageCode() {
354 wfDeprecated( __METHOD__
);
355 return self
::getFallbackFor( $this->mCode
);
362 function getFallbackLanguages() {
363 return self
::getFallbacksFor( $this->mCode
);
367 * Exports $wgBookstoreListEn
370 function getBookstoreList() {
371 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
377 public function getNamespaces() {
378 if ( is_null( $this->namespaceNames
) ) {
379 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
381 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
382 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
384 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
386 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
387 if ( $wgMetaNamespaceTalk ) {
388 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
390 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
391 $this->namespaceNames
[NS_PROJECT_TALK
] =
392 $this->fixVariableInNamespace( $talk );
395 # Sometimes a language will be localised but not actually exist on this wiki.
396 foreach ( $this->namespaceNames
as $key => $text ) {
397 if ( !isset( $validNamespaces[$key] ) ) {
398 unset( $this->namespaceNames
[$key] );
402 # The above mixing may leave namespaces out of canonical order.
403 # Re-order by namespace ID number...
404 ksort( $this->namespaceNames
);
406 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames
) );
408 return $this->namespaceNames
;
412 * Arbitrarily set all of the namespace names at once. Mainly used for testing
413 * @param $namespaces Array of namespaces (id => name)
415 public function setNamespaces( array $namespaces ) {
416 $this->namespaceNames
= $namespaces;
417 $this->mNamespaceIds
= null;
421 * Resets all of the namespace caches. Mainly used for testing
423 public function resetNamespaces( ) {
424 $this->namespaceNames
= null;
425 $this->mNamespaceIds
= null;
426 $this->namespaceAliases
= null;
430 * A convenience function that returns the same thing as
431 * getNamespaces() except with the array values changed to ' '
432 * where it found '_', useful for producing output to be displayed
433 * e.g. in <select> forms.
437 function getFormattedNamespaces() {
438 $ns = $this->getNamespaces();
439 foreach ( $ns as $k => $v ) {
440 $ns[$k] = strtr( $v, '_', ' ' );
446 * Get a namespace value by key
448 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
449 * echo $mw_ns; // prints 'MediaWiki'
452 * @param $index Int: the array key of the namespace to return
453 * @return mixed, string if the namespace value exists, otherwise false
455 function getNsText( $index ) {
456 $ns = $this->getNamespaces();
457 return isset( $ns[$index] ) ?
$ns[$index] : false;
461 * A convenience function that returns the same thing as
462 * getNsText() except with '_' changed to ' ', useful for
465 * @param $index string
469 function getFormattedNsText( $index ) {
470 $ns = $this->getNsText( $index );
471 return strtr( $ns, '_', ' ' );
475 * Returns gender-dependent namespace alias if available.
476 * @param $index Int: namespace index
477 * @param $gender String: gender key (male, female... )
481 function getGenderNsText( $index, $gender ) {
482 global $wgExtraGenderNamespaces;
484 $ns = $wgExtraGenderNamespaces + self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
485 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
489 * Whether this language makes distinguishes genders for example in
494 function needsGenderDistinction() {
495 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
496 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
497 // $wgExtraGenderNamespaces overrides everything
499 } elseif ( isset( $wgExtraNamespaces[NS_USER
] ) && isset( $wgExtraNamespaces[NS_USER_TALK
] ) ) {
500 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
501 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
504 // Check what is in i18n files
505 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
506 return count( $aliases ) > 0;
511 * Get a namespace key by value, case insensitive.
512 * Only matches namespace names for the current language, not the
513 * canonical ones defined in Namespace.php.
515 * @param $text String
516 * @return mixed An integer if $text is a valid value otherwise false
518 function getLocalNsIndex( $text ) {
519 $lctext = $this->lc( $text );
520 $ids = $this->getNamespaceIds();
521 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
527 function getNamespaceAliases() {
528 if ( is_null( $this->namespaceAliases
) ) {
529 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
533 foreach ( $aliases as $name => $index ) {
534 if ( $index === NS_PROJECT_TALK
) {
535 unset( $aliases[$name] );
536 $name = $this->fixVariableInNamespace( $name );
537 $aliases[$name] = $index;
542 global $wgExtraGenderNamespaces;
543 $genders = $wgExtraGenderNamespaces +
(array)self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
544 foreach ( $genders as $index => $forms ) {
545 foreach ( $forms as $alias ) {
546 $aliases[$alias] = $index;
550 $this->namespaceAliases
= $aliases;
552 return $this->namespaceAliases
;
558 function getNamespaceIds() {
559 if ( is_null( $this->mNamespaceIds
) ) {
560 global $wgNamespaceAliases;
561 # Put namespace names and aliases into a hashtable.
562 # If this is too slow, then we should arrange it so that it is done
563 # before caching. The catch is that at pre-cache time, the above
564 # class-specific fixup hasn't been done.
565 $this->mNamespaceIds
= array();
566 foreach ( $this->getNamespaces() as $index => $name ) {
567 $this->mNamespaceIds
[$this->lc( $name )] = $index;
569 foreach ( $this->getNamespaceAliases() as $name => $index ) {
570 $this->mNamespaceIds
[$this->lc( $name )] = $index;
572 if ( $wgNamespaceAliases ) {
573 foreach ( $wgNamespaceAliases as $name => $index ) {
574 $this->mNamespaceIds
[$this->lc( $name )] = $index;
578 return $this->mNamespaceIds
;
582 * Get a namespace key by value, case insensitive. Canonical namespace
583 * names override custom ones defined for the current language.
585 * @param $text String
586 * @return mixed An integer if $text is a valid value otherwise false
588 function getNsIndex( $text ) {
589 $lctext = $this->lc( $text );
590 $ns = MWNamespace
::getCanonicalIndex( $lctext );
591 if ( $ns !== null ) {
594 $ids = $this->getNamespaceIds();
595 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
599 * short names for language variants used for language conversion links.
601 * @param $code String
602 * @param $usemsg bool Use the "variantname-xyz" message if it exists
605 function getVariantname( $code, $usemsg = true ) {
606 $msg = "variantname-$code";
607 if ( $usemsg && wfMessage( $msg )->exists() ) {
608 return $this->getMessageFromDB( $msg );
610 $name = self
::fetchLanguageName( $code );
612 return $name; # if it's defined as a language name, show that
614 # otherwise, output the language code
620 * @param $name string
623 function specialPage( $name ) {
624 $aliases = $this->getSpecialPageAliases();
625 if ( isset( $aliases[$name][0] ) ) {
626 $name = $aliases[$name][0];
628 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
634 function getQuickbarSettings() {
636 $this->getMessage( 'qbsettings-none' ),
637 $this->getMessage( 'qbsettings-fixedleft' ),
638 $this->getMessage( 'qbsettings-fixedright' ),
639 $this->getMessage( 'qbsettings-floatingleft' ),
640 $this->getMessage( 'qbsettings-floatingright' ),
641 $this->getMessage( 'qbsettings-directionality' )
648 function getDatePreferences() {
649 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
655 function getDateFormats() {
656 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
660 * @return array|string
662 function getDefaultDateFormat() {
663 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
664 if ( $df === 'dmy or mdy' ) {
665 global $wgAmericanDates;
666 return $wgAmericanDates ?
'mdy' : 'dmy';
675 function getDatePreferenceMigrationMap() {
676 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
683 function getImageFile( $image ) {
684 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
690 function getExtraUserToggles() {
691 return (array)self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
698 function getUserToggle( $tog ) {
699 return $this->getMessageFromDB( "tog-$tog" );
703 * Get native language names, indexed by code.
704 * Only those defined in MediaWiki, no other data like CLDR.
705 * If $customisedOnly is true, only returns codes with a messages file
707 * @param $customisedOnly bool
710 * @deprecated in 1.20, use fetchLanguageNames()
712 public static function getLanguageNames( $customisedOnly = false ) {
713 return self
::fetchLanguageNames( null, $customisedOnly ?
'mwfile' : 'mw' );
717 * Get translated language names. This is done on best effort and
718 * by default this is exactly the same as Language::getLanguageNames.
719 * The CLDR extension provides translated names.
720 * @param $code String Language code.
721 * @return Array language code => language name
723 * @deprecated in 1.20, use fetchLanguageNames()
725 public static function getTranslatedLanguageNames( $code ) {
726 return self
::fetchLanguageNames( $code, 'all' );
730 * Get an array of language names, indexed by code.
731 * @param $inLanguage null|string: Code of language in which to return the names
732 * Use null for autonyms (native names)
733 * @param $include string:
734 * 'all' all available languages
735 * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
736 * 'mwfile' only if the language is in 'mw' *and* has a message file
737 * @return array: language code => language name
740 public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
741 global $wgExtraLanguageNames;
742 static $coreLanguageNames;
744 if ( $coreLanguageNames === null ) {
745 include( MWInit
::compiledPath( 'languages/Names.php' ) );
751 # TODO: also include when $inLanguage is null, when this code is more efficient
752 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
755 $mwNames = $wgExtraLanguageNames +
$coreLanguageNames;
756 foreach ( $mwNames as $mwCode => $mwName ) {
757 # - Prefer own MediaWiki native name when not using the hook
758 # - For other names just add if not added through the hook
759 if ( $mwCode === $inLanguage ||
!isset( $names[$mwCode] ) ) {
760 $names[$mwCode] = $mwName;
764 if ( $include === 'all' ) {
769 $coreCodes = array_keys( $mwNames );
770 foreach( $coreCodes as $coreCode ) {
771 $returnMw[$coreCode] = $names[$coreCode];
774 if( $include === 'mwfile' ) {
775 $namesMwFile = array();
776 # We do this using a foreach over the codes instead of a directory
777 # loop so that messages files in extensions will work correctly.
778 foreach ( $returnMw as $code => $value ) {
779 if ( is_readable( self
::getMessagesFileName( $code ) ) ) {
780 $namesMwFile[$code] = $names[$code];
785 # 'mw' option; default if it's not one of the other two options (all/mwfile)
790 * @param $code string: The code of the language for which to get the name
791 * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
792 * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
793 * @return string: Language name or empty
796 public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
797 $array = self
::fetchLanguageNames( $inLanguage, $include );
798 return !array_key_exists( $code, $array ) ?
'' : $array[$code];
802 * Get a message from the MediaWiki namespace.
804 * @param $msg String: message name
807 function getMessageFromDB( $msg ) {
808 return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
812 * Get the native language name of $code.
813 * Only if defined in MediaWiki, no other data like CLDR.
814 * @param $code string
816 * @deprecated in 1.20, use fetchLanguageName()
818 function getLanguageName( $code ) {
819 return self
::fetchLanguageName( $code );
826 function getMonthName( $key ) {
827 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
833 function getMonthNamesArray() {
834 $monthNames = array( '' );
835 for ( $i = 1; $i < 13; $i++
) {
836 $monthNames[] = $this->getMonthName( $i );
845 function getMonthNameGen( $key ) {
846 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
853 function getMonthAbbreviation( $key ) {
854 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
860 function getMonthAbbreviationsArray() {
861 $monthNames = array( '' );
862 for ( $i = 1; $i < 13; $i++
) {
863 $monthNames[] = $this->getMonthAbbreviation( $i );
872 function getWeekdayName( $key ) {
873 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
880 function getWeekdayAbbreviation( $key ) {
881 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
888 function getIranianCalendarMonthName( $key ) {
889 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
896 function getHebrewCalendarMonthName( $key ) {
897 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
904 function getHebrewCalendarMonthNameGen( $key ) {
905 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
912 function getHijriCalendarMonthName( $key ) {
913 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
917 * This is a workalike of PHP's date() function, but with better
918 * internationalisation, a reduced set of format characters, and a better
921 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
922 * PHP manual for definitions. There are a number of extensions, which
925 * xn Do not translate digits of the next numeric format character
926 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
927 * xr Use roman numerals for the next numeric format character
928 * xh Use hebrew numerals for the next numeric format character
930 * xg Genitive month name
932 * xij j (day number) in Iranian calendar
933 * xiF F (month name) in Iranian calendar
934 * xin n (month number) in Iranian calendar
935 * xiy y (two digit year) in Iranian calendar
936 * xiY Y (full year) in Iranian calendar
938 * xjj j (day number) in Hebrew calendar
939 * xjF F (month name) in Hebrew calendar
940 * xjt t (days in month) in Hebrew calendar
941 * xjx xg (genitive month name) in Hebrew calendar
942 * xjn n (month number) in Hebrew calendar
943 * xjY Y (full year) in Hebrew calendar
945 * xmj j (day number) in Hijri calendar
946 * xmF F (month name) in Hijri calendar
947 * xmn n (month number) in Hijri calendar
948 * xmY Y (full year) in Hijri calendar
950 * xkY Y (full year) in Thai solar calendar. Months and days are
951 * identical to the Gregorian calendar
952 * xoY Y (full year) in Minguo calendar or Juche year.
953 * Months and days are identical to the
955 * xtY Y (full year) in Japanese nengo. Months and days are
956 * identical to the Gregorian calendar
958 * Characters enclosed in double quotes will be considered literal (with
959 * the quotes themselves removed). Unmatched quotes will be considered
960 * literal quotes. Example:
962 * "The month is" F => The month is January
965 * Backslash escaping is also supported.
967 * Input timestamp is assumed to be pre-normalized to the desired local
970 * @param $format String
971 * @param $ts String: 14-character timestamp
974 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
978 function sprintfDate( $format, $ts ) {
991 for ( $p = 0; $p < strlen( $format ); $p++
) {
994 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
995 $code .= $format[++
$p];
998 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
999 $code .= $format[++
$p];
1010 $rawToggle = !$rawToggle;
1019 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
1022 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
1023 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
1026 $num = substr( $ts, 6, 2 );
1029 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
1030 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) +
1 );
1033 $num = intval( substr( $ts, 6, 2 ) );
1037 $iranian = self
::tsToIranian( $ts );
1043 $hijri = self
::tsToHijri( $ts );
1049 $hebrew = self
::tsToHebrew( $ts );
1055 $unix = wfTimestamp( TS_UNIX
, $ts );
1057 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) +
1 );
1061 $unix = wfTimestamp( TS_UNIX
, $ts );
1063 $w = gmdate( 'w', $unix );
1068 $unix = wfTimestamp( TS_UNIX
, $ts );
1070 $num = gmdate( 'w', $unix );
1074 $unix = wfTimestamp( TS_UNIX
, $ts );
1076 $num = gmdate( 'z', $unix );
1080 $unix = wfTimestamp( TS_UNIX
, $ts );
1082 $num = gmdate( 'W', $unix );
1085 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1089 $iranian = self
::tsToIranian( $ts );
1091 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1095 $hijri = self
::tsToHijri( $ts );
1097 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1101 $hebrew = self
::tsToHebrew( $ts );
1103 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1106 $num = substr( $ts, 4, 2 );
1109 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1112 $num = intval( substr( $ts, 4, 2 ) );
1116 $iranian = self
::tsToIranian( $ts );
1122 $hijri = self
::tsToHijri ( $ts );
1128 $hebrew = self
::tsToHebrew( $ts );
1134 $unix = wfTimestamp( TS_UNIX
, $ts );
1136 $num = gmdate( 't', $unix );
1140 $hebrew = self
::tsToHebrew( $ts );
1146 $unix = wfTimestamp( TS_UNIX
, $ts );
1148 $num = gmdate( 'L', $unix );
1152 $unix = wfTimestamp( TS_UNIX
, $ts );
1154 $num = gmdate( 'o', $unix );
1157 $num = substr( $ts, 0, 4 );
1161 $iranian = self
::tsToIranian( $ts );
1167 $hijri = self
::tsToHijri( $ts );
1173 $hebrew = self
::tsToHebrew( $ts );
1179 $thai = self
::tsToYear( $ts, 'thai' );
1185 $minguo = self
::tsToYear( $ts, 'minguo' );
1191 $tenno = self
::tsToYear( $ts, 'tenno' );
1196 $num = substr( $ts, 2, 2 );
1200 $iranian = self
::tsToIranian( $ts );
1202 $num = substr( $iranian[0], -2 );
1205 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
1208 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
1211 $h = substr( $ts, 8, 2 );
1212 $num = $h %
12 ?
$h %
12 : 12;
1215 $num = intval( substr( $ts, 8, 2 ) );
1218 $h = substr( $ts, 8, 2 );
1219 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
1222 $num = substr( $ts, 8, 2 );
1225 $num = substr( $ts, 10, 2 );
1228 $num = substr( $ts, 12, 2 );
1232 $unix = wfTimestamp( TS_UNIX
, $ts );
1234 $s .= gmdate( 'c', $unix );
1238 $unix = wfTimestamp( TS_UNIX
, $ts );
1240 $s .= gmdate( 'r', $unix );
1244 $unix = wfTimestamp( TS_UNIX
, $ts );
1249 # Backslash escaping
1250 if ( $p < strlen( $format ) - 1 ) {
1251 $s .= $format[++
$p];
1258 if ( $p < strlen( $format ) - 1 ) {
1259 $endQuote = strpos( $format, '"', $p +
1 );
1260 if ( $endQuote === false ) {
1261 # No terminating quote, assume literal "
1264 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1268 # Quote at end of string, assume literal "
1275 if ( $num !== false ) {
1276 if ( $rawToggle ||
$raw ) {
1279 } elseif ( $roman ) {
1280 $s .= self
::romanNumeral( $num );
1282 } elseif ( $hebrewNum ) {
1283 $s .= self
::hebrewNumeral( $num );
1286 $s .= $this->formatNum( $num, true );
1293 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1294 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1297 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1298 * Gregorian dates to Iranian dates. Originally written in C, it
1299 * is released under the terms of GNU Lesser General Public
1300 * License. Conversion to PHP was performed by Niklas Laxström.
1302 * Link: http://www.farsiweb.info/jalali/jalali.c
1308 private static function tsToIranian( $ts ) {
1309 $gy = substr( $ts, 0, 4 ) -1600;
1310 $gm = substr( $ts, 4, 2 ) -1;
1311 $gd = substr( $ts, 6, 2 ) -1;
1313 # Days passed from the beginning (including leap years)
1315 +
floor( ( $gy +
3 ) / 4 )
1316 - floor( ( $gy +
99 ) / 100 )
1317 +
floor( ( $gy +
399 ) / 400 );
1319 // Add days of the past months of this year
1320 for ( $i = 0; $i < $gm; $i++
) {
1321 $gDayNo +
= self
::$GREG_DAYS[$i];
1325 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1329 // Days passed in current month
1330 $gDayNo +
= (int)$gd;
1332 $jDayNo = $gDayNo - 79;
1334 $jNp = floor( $jDayNo / 12053 );
1337 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1340 if ( $jDayNo >= 366 ) {
1341 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1342 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1345 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1346 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1352 return array( $jy, $jm, $jd );
1356 * Converting Gregorian dates to Hijri dates.
1358 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1360 * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1366 private static function tsToHijri( $ts ) {
1367 $year = substr( $ts, 0, 4 );
1368 $month = substr( $ts, 4, 2 );
1369 $day = substr( $ts, 6, 2 );
1377 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1378 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1381 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1382 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1383 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1386 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1387 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1390 $zl = $zjd -1948440 +
10632;
1391 $zn = (int)( ( $zl - 1 ) / 10631 );
1392 $zl = $zl - 10631 * $zn +
354;
1393 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1394 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1395 $zm = (int)( ( 24 * $zl ) / 709 );
1396 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1397 $zy = 30 * $zn +
$zj - 30;
1399 return array( $zy, $zm, $zd );
1403 * Converting Gregorian dates to Hebrew dates.
1405 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1406 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1407 * to translate the relevant functions into PHP and release them under
1410 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1411 * and Adar II is 14. In a non-leap year, Adar is 6.
1417 private static function tsToHebrew( $ts ) {
1419 $year = substr( $ts, 0, 4 );
1420 $month = substr( $ts, 4, 2 );
1421 $day = substr( $ts, 6, 2 );
1423 # Calculate Hebrew year
1424 $hebrewYear = $year +
3760;
1426 # Month number when September = 1, August = 12
1428 if ( $month > 12 ) {
1435 # Calculate day of year from 1 September
1437 for ( $i = 1; $i < $month; $i++
) {
1441 # Check if the year is leap
1442 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1445 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1452 # Calculate the start of the Hebrew year
1453 $start = self
::hebrewYearStart( $hebrewYear );
1455 # Calculate next year's start
1456 if ( $dayOfYear <= $start ) {
1457 # Day is before the start of the year - it is the previous year
1459 $nextStart = $start;
1463 # Add days since previous year's 1 September
1465 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1469 # Start of the new (previous) year
1470 $start = self
::hebrewYearStart( $hebrewYear );
1473 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1476 # Calculate Hebrew day of year
1477 $hebrewDayOfYear = $dayOfYear - $start;
1479 # Difference between year's days
1480 $diff = $nextStart - $start;
1481 # Add 12 (or 13 for leap years) days to ignore the difference between
1482 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1483 # difference is only about the year type
1484 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1490 # Check the year pattern, and is leap year
1491 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1492 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1493 # and non-leap years
1494 $yearPattern = $diff %
30;
1495 # Check if leap year
1496 $isLeap = $diff >= 30;
1498 # Calculate day in the month from number of day in the Hebrew year
1499 # Don't check Adar - if the day is not in Adar, we will stop before;
1500 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1501 $hebrewDay = $hebrewDayOfYear;
1504 while ( $hebrewMonth <= 12 ) {
1505 # Calculate days in this month
1506 if ( $isLeap && $hebrewMonth == 6 ) {
1507 # Adar in a leap year
1509 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1511 if ( $hebrewDay <= $days ) {
1515 # Subtract the days of Adar I
1516 $hebrewDay -= $days;
1519 if ( $hebrewDay <= $days ) {
1525 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1526 # Cheshvan in a complete year (otherwise as the rule below)
1528 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1529 # Kislev in an incomplete year (otherwise as the rule below)
1532 # Odd months have 30 days, even have 29
1533 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1535 if ( $hebrewDay <= $days ) {
1536 # In the current month
1539 # Subtract the days of the current month
1540 $hebrewDay -= $days;
1541 # Try in the next month
1546 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1550 * This calculates the Hebrew year start, as days since 1 September.
1551 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1552 * Used for Hebrew date.
1558 private static function hebrewYearStart( $year ) {
1559 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1560 $b = intval( ( $year - 1 ) %
4 );
1561 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1565 $Mar = intval( $m );
1571 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1572 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1574 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1576 } elseif ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1580 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1585 * Algorithm to convert Gregorian dates to Thai solar dates,
1586 * Minguo dates or Minguo dates.
1588 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1589 * http://en.wikipedia.org/wiki/Minguo_calendar
1590 * http://en.wikipedia.org/wiki/Japanese_era_name
1592 * @param $ts String: 14-character timestamp
1593 * @param $cName String: calender name
1594 * @return Array: converted year, month, day
1596 private static function tsToYear( $ts, $cName ) {
1597 $gy = substr( $ts, 0, 4 );
1598 $gm = substr( $ts, 4, 2 );
1599 $gd = substr( $ts, 6, 2 );
1601 if ( !strcmp( $cName, 'thai' ) ) {
1603 # Add 543 years to the Gregorian calendar
1604 # Months and days are identical
1605 $gy_offset = $gy +
543;
1606 } elseif ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1608 # Deduct 1911 years from the Gregorian calendar
1609 # Months and days are identical
1610 $gy_offset = $gy - 1911;
1611 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1612 # Nengō dates up to Meiji period
1613 # Deduct years from the Gregorian calendar
1614 # depending on the nengo periods
1615 # Months and days are identical
1616 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1618 $gy_gannen = $gy - 1868 +
1;
1619 $gy_offset = $gy_gannen;
1620 if ( $gy_gannen == 1 ) {
1623 $gy_offset = '明治' . $gy_offset;
1625 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1626 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1627 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1628 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1629 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1633 $gy_gannen = $gy - 1912 +
1;
1634 $gy_offset = $gy_gannen;
1635 if ( $gy_gannen == 1 ) {
1638 $gy_offset = '大正' . $gy_offset;
1640 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1641 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1642 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1646 $gy_gannen = $gy - 1926 +
1;
1647 $gy_offset = $gy_gannen;
1648 if ( $gy_gannen == 1 ) {
1651 $gy_offset = '昭和' . $gy_offset;
1654 $gy_gannen = $gy - 1989 +
1;
1655 $gy_offset = $gy_gannen;
1656 if ( $gy_gannen == 1 ) {
1659 $gy_offset = '平成' . $gy_offset;
1665 return array( $gy_offset, $gm, $gd );
1669 * Roman number formatting up to 3000
1675 static function romanNumeral( $num ) {
1676 static $table = array(
1677 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1678 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1679 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1680 array( '', 'M', 'MM', 'MMM' )
1683 $num = intval( $num );
1684 if ( $num > 3000 ||
$num <= 0 ) {
1689 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1690 if ( $num >= $pow10 ) {
1691 $s .= $table[$i][(int)floor( $num / $pow10 )];
1693 $num = $num %
$pow10;
1699 * Hebrew Gematria number formatting up to 9999
1705 static function hebrewNumeral( $num ) {
1706 static $table = array(
1707 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1708 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1709 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1710 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1713 $num = intval( $num );
1714 if ( $num > 9999 ||
$num <= 0 ) {
1719 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1720 if ( $num >= $pow10 ) {
1721 if ( $num == 15 ||
$num == 16 ) {
1722 $s .= $table[0][9] . $table[0][$num - 9];
1725 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1726 if ( $pow10 == 1000 ) {
1731 $num = $num %
$pow10;
1733 if ( strlen( $s ) == 2 ) {
1736 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1737 $str .= substr( $s, strlen( $s ) - 2, 2 );
1739 $start = substr( $str, 0, strlen( $str ) - 2 );
1740 $end = substr( $str, strlen( $str ) - 2 );
1743 $str = $start . 'ך';
1746 $str = $start . 'ם';
1749 $str = $start . 'ן';
1752 $str = $start . 'ף';
1755 $str = $start . 'ץ';
1762 * Used by date() and time() to adjust the time output.
1764 * @param $ts Int the time in date('YmdHis') format
1765 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1766 * get user timecorrection setting)
1769 function userAdjust( $ts, $tz = false ) {
1770 global $wgUser, $wgLocalTZoffset;
1772 if ( $tz === false ) {
1773 $tz = $wgUser->getOption( 'timecorrection' );
1776 $data = explode( '|', $tz, 3 );
1778 if ( $data[0] == 'ZoneInfo' ) {
1779 wfSuppressWarnings();
1780 $userTZ = timezone_open( $data[2] );
1781 wfRestoreWarnings();
1782 if ( $userTZ !== false ) {
1783 $date = date_create( $ts, timezone_open( 'UTC' ) );
1784 date_timezone_set( $date, $userTZ );
1785 $date = date_format( $date, 'YmdHis' );
1788 # Unrecognized timezone, default to 'Offset' with the stored offset.
1789 $data[0] = 'Offset';
1793 if ( $data[0] == 'System' ||
$tz == '' ) {
1794 # Global offset in minutes.
1795 if ( isset( $wgLocalTZoffset ) ) {
1796 $minDiff = $wgLocalTZoffset;
1798 } elseif ( $data[0] == 'Offset' ) {
1799 $minDiff = intval( $data[1] );
1801 $data = explode( ':', $tz );
1802 if ( count( $data ) == 2 ) {
1803 $data[0] = intval( $data[0] );
1804 $data[1] = intval( $data[1] );
1805 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1806 if ( $data[0] < 0 ) {
1807 $minDiff = -$minDiff;
1810 $minDiff = intval( $data[0] ) * 60;
1814 # No difference ? Return time unchanged
1815 if ( 0 == $minDiff ) {
1819 wfSuppressWarnings(); // E_STRICT system time bitching
1820 # Generate an adjusted date; take advantage of the fact that mktime
1821 # will normalize out-of-range values so we don't have to split $minDiff
1822 # into hours and minutes.
1824 (int)substr( $ts, 8, 2 ) ), # Hours
1825 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
1826 (int)substr( $ts, 12, 2 ), # Seconds
1827 (int)substr( $ts, 4, 2 ), # Month
1828 (int)substr( $ts, 6, 2 ), # Day
1829 (int)substr( $ts, 0, 4 ) ); # Year
1831 $date = date( 'YmdHis', $t );
1832 wfRestoreWarnings();
1838 * This is meant to be used by time(), date(), and timeanddate() to get
1839 * the date preference they're supposed to use, it should be used in
1843 * function timeanddate([...], $format = true) {
1844 * $datePreference = $this->dateFormat($format);
1849 * @param $usePrefs Mixed: if true, the user's preference is used
1850 * if false, the site/language default is used
1851 * if int/string, assumed to be a format.
1854 function dateFormat( $usePrefs = true ) {
1857 if ( is_bool( $usePrefs ) ) {
1859 $datePreference = $wgUser->getDatePreference();
1861 $datePreference = (string)User
::getDefaultOption( 'date' );
1864 $datePreference = (string)$usePrefs;
1868 if ( $datePreference == '' ) {
1872 return $datePreference;
1876 * Get a format string for a given type and preference
1877 * @param $type string May be date, time or both
1878 * @param $pref string The format name as it appears in Messages*.php
1882 function getDateFormatString( $type, $pref ) {
1883 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
1884 if ( $pref == 'default' ) {
1885 $pref = $this->getDefaultDateFormat();
1886 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1888 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1889 if ( is_null( $df ) ) {
1890 $pref = $this->getDefaultDateFormat();
1891 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1894 $this->dateFormatStrings
[$type][$pref] = $df;
1896 return $this->dateFormatStrings
[$type][$pref];
1900 * @param $ts Mixed: the time format which needs to be turned into a
1901 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1902 * @param $adj Bool: whether to adjust the time output according to the
1903 * user configured offset ($timecorrection)
1904 * @param $format Mixed: true to use user's date format preference
1905 * @param $timecorrection String|bool the time offset as returned by
1906 * validateTimeZone() in Special:Preferences
1909 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1910 $ts = wfTimestamp( TS_MW
, $ts );
1912 $ts = $this->userAdjust( $ts, $timecorrection );
1914 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1915 return $this->sprintfDate( $df, $ts );
1919 * @param $ts Mixed: the time format which needs to be turned into a
1920 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1921 * @param $adj Bool: whether to adjust the time output according to the
1922 * user configured offset ($timecorrection)
1923 * @param $format Mixed: true to use user's date format preference
1924 * @param $timecorrection String|bool the time offset as returned by
1925 * validateTimeZone() in Special:Preferences
1928 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1929 $ts = wfTimestamp( TS_MW
, $ts );
1931 $ts = $this->userAdjust( $ts, $timecorrection );
1933 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1934 return $this->sprintfDate( $df, $ts );
1938 * @param $ts Mixed: the time format which needs to be turned into a
1939 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1940 * @param $adj Bool: whether to adjust the time output according to the
1941 * user configured offset ($timecorrection)
1942 * @param $format Mixed: what format to return, if it's false output the
1943 * default one (default true)
1944 * @param $timecorrection String|bool the time offset as returned by
1945 * validateTimeZone() in Special:Preferences
1948 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1949 $ts = wfTimestamp( TS_MW
, $ts );
1951 $ts = $this->userAdjust( $ts, $timecorrection );
1953 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1954 return $this->sprintfDate( $df, $ts );
1958 * Takes a number of seconds and turns it into a text using values such as hours and minutes.
1962 * @param integer $seconds The amount of seconds.
1963 * @param array $chosenIntervals The intervals to enable.
1967 public function formatDuration( $seconds, array $chosenIntervals = array() ) {
1968 $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
1970 $segments = array();
1972 foreach ( $intervals as $intervalName => $intervalValue ) {
1973 $message = new Message( 'duration-' . $intervalName, array( $intervalValue ) );
1974 $segments[] = $message->inLanguage( $this )->escaped();
1977 return $this->listToText( $segments );
1981 * Takes a number of seconds and returns an array with a set of corresponding intervals.
1982 * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
1986 * @param integer $seconds The amount of seconds.
1987 * @param array $chosenIntervals The intervals to enable.
1991 public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
1992 if ( empty( $chosenIntervals ) ) {
1993 $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
1996 $intervals = array_intersect_key( self
::$durationIntervals, array_flip( $chosenIntervals ) );
1997 $sortedNames = array_keys( $intervals );
1998 $smallestInterval = array_pop( $sortedNames );
2000 $segments = array();
2002 foreach ( $intervals as $name => $length ) {
2003 $value = floor( $seconds / $length );
2005 if ( $value > 0 ||
( $name == $smallestInterval && empty( $segments ) ) ) {
2006 $seconds -= $value * $length;
2007 $segments[$name] = $value;
2015 * Internal helper function for userDate(), userTime() and userTimeAndDate()
2017 * @param $type String: can be 'date', 'time' or 'both'
2018 * @param $ts Mixed: the time format which needs to be turned into a
2019 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2020 * @param $user User object used to get preferences for timezone and format
2021 * @param $options Array, can contain the following keys:
2022 * - 'timecorrection': time correction, can have the following values:
2023 * - true: use user's preference
2024 * - false: don't use time correction
2025 * - integer: value of time correction in minutes
2026 * - 'format': format to use, can have the following values:
2027 * - true: use user's preference
2028 * - false: use default preference
2029 * - string: format to use
2033 private function internalUserTimeAndDate( $type, $ts, User
$user, array $options ) {
2034 $ts = wfTimestamp( TS_MW
, $ts );
2035 $options +
= array( 'timecorrection' => true, 'format' => true );
2036 if ( $options['timecorrection'] !== false ) {
2037 if ( $options['timecorrection'] === true ) {
2038 $offset = $user->getOption( 'timecorrection' );
2040 $offset = $options['timecorrection'];
2042 $ts = $this->userAdjust( $ts, $offset );
2044 if ( $options['format'] === true ) {
2045 $format = $user->getDatePreference();
2047 $format = $options['format'];
2049 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
2050 return $this->sprintfDate( $df, $ts );
2054 * Get the formatted date for the given timestamp and formatted for
2057 * @param $ts Mixed: the time format which needs to be turned into a
2058 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2059 * @param $user User object used to get preferences for timezone and format
2060 * @param $options Array, can contain the following keys:
2061 * - 'timecorrection': time correction, can have the following values:
2062 * - true: use user's preference
2063 * - false: don't use time correction
2064 * - integer: value of time correction in minutes
2065 * - 'format': format to use, can have the following values:
2066 * - true: use user's preference
2067 * - false: use default preference
2068 * - string: format to use
2072 public function userDate( $ts, User
$user, array $options = array() ) {
2073 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
2077 * Get the formatted time for the given timestamp and formatted for
2080 * @param $ts Mixed: the time format which needs to be turned into a
2081 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2082 * @param $user User object used to get preferences for timezone and format
2083 * @param $options Array, can contain the following keys:
2084 * - 'timecorrection': time correction, can have the following values:
2085 * - true: use user's preference
2086 * - false: don't use time correction
2087 * - integer: value of time correction in minutes
2088 * - 'format': format to use, can have the following values:
2089 * - true: use user's preference
2090 * - false: use default preference
2091 * - string: format to use
2095 public function userTime( $ts, User
$user, array $options = array() ) {
2096 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
2100 * Get the formatted date and time for the given timestamp and formatted for
2103 * @param $ts Mixed: the time format which needs to be turned into a
2104 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2105 * @param $user User object used to get preferences for timezone and format
2106 * @param $options Array, can contain the following keys:
2107 * - 'timecorrection': time correction, can have the following values:
2108 * - true: use user's preference
2109 * - false: don't use time correction
2110 * - integer: value of time correction in minutes
2111 * - 'format': format to use, can have the following values:
2112 * - true: use user's preference
2113 * - false: use default preference
2114 * - string: format to use
2118 public function userTimeAndDate( $ts, User
$user, array $options = array() ) {
2119 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
2123 * @param $key string
2124 * @return array|null
2126 function getMessage( $key ) {
2127 return self
::$dataCache->getSubitem( $this->mCode
, 'messages', $key );
2133 function getAllMessages() {
2134 return self
::$dataCache->getItem( $this->mCode
, 'messages' );
2143 function iconv( $in, $out, $string ) {
2144 # This is a wrapper for iconv in all languages except esperanto,
2145 # which does some nasty x-conversions beforehand
2147 # Even with //IGNORE iconv can whine about illegal characters in
2148 # *input* string. We just ignore those too.
2149 # REF: http://bugs.php.net/bug.php?id=37166
2150 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
2151 wfSuppressWarnings();
2152 $text = iconv( $in, $out . '//IGNORE', $string );
2153 wfRestoreWarnings();
2157 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
2160 * @param $matches array
2161 * @return mixed|string
2163 function ucwordbreaksCallbackAscii( $matches ) {
2164 return $this->ucfirst( $matches[1] );
2168 * @param $matches array
2171 function ucwordbreaksCallbackMB( $matches ) {
2172 return mb_strtoupper( $matches[0] );
2176 * @param $matches array
2179 function ucCallback( $matches ) {
2180 list( $wikiUpperChars ) = self
::getCaseMaps();
2181 return strtr( $matches[1], $wikiUpperChars );
2185 * @param $matches array
2188 function lcCallback( $matches ) {
2189 list( , $wikiLowerChars ) = self
::getCaseMaps();
2190 return strtr( $matches[1], $wikiLowerChars );
2194 * @param $matches array
2197 function ucwordsCallbackMB( $matches ) {
2198 return mb_strtoupper( $matches[0] );
2202 * @param $matches array
2205 function ucwordsCallbackWiki( $matches ) {
2206 list( $wikiUpperChars ) = self
::getCaseMaps();
2207 return strtr( $matches[0], $wikiUpperChars );
2211 * Make a string's first character uppercase
2213 * @param $str string
2217 function ucfirst( $str ) {
2219 if ( $o < 96 ) { // if already uppercase...
2221 } elseif ( $o < 128 ) {
2222 return ucfirst( $str ); // use PHP's ucfirst()
2224 // fall back to more complex logic in case of multibyte strings
2225 return $this->uc( $str, true );
2230 * Convert a string to uppercase
2232 * @param $str string
2233 * @param $first bool
2237 function uc( $str, $first = false ) {
2238 if ( function_exists( 'mb_strtoupper' ) ) {
2240 if ( $this->isMultibyte( $str ) ) {
2241 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2243 return ucfirst( $str );
2246 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
2249 if ( $this->isMultibyte( $str ) ) {
2250 $x = $first ?
'^' : '';
2251 return preg_replace_callback(
2252 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2253 array( $this, 'ucCallback' ),
2257 return $first ?
ucfirst( $str ) : strtoupper( $str );
2263 * @param $str string
2264 * @return mixed|string
2266 function lcfirst( $str ) {
2269 return strval( $str );
2270 } elseif ( $o >= 128 ) {
2271 return $this->lc( $str, true );
2272 } elseif ( $o > 96 ) {
2275 $str[0] = strtolower( $str[0] );
2281 * @param $str string
2282 * @param $first bool
2283 * @return mixed|string
2285 function lc( $str, $first = false ) {
2286 if ( function_exists( 'mb_strtolower' ) ) {
2288 if ( $this->isMultibyte( $str ) ) {
2289 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2291 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2294 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
2297 if ( $this->isMultibyte( $str ) ) {
2298 $x = $first ?
'^' : '';
2299 return preg_replace_callback(
2300 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2301 array( $this, 'lcCallback' ),
2305 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2311 * @param $str string
2314 function isMultibyte( $str ) {
2315 return (bool)preg_match( '/[\x80-\xff]/', $str );
2319 * @param $str string
2320 * @return mixed|string
2322 function ucwords( $str ) {
2323 if ( $this->isMultibyte( $str ) ) {
2324 $str = $this->lc( $str );
2326 // regexp to find first letter in each word (i.e. after each space)
2327 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2329 // function to use to capitalize a single char
2330 if ( function_exists( 'mb_strtoupper' ) ) {
2331 return preg_replace_callback(
2333 array( $this, 'ucwordsCallbackMB' ),
2337 return preg_replace_callback(
2339 array( $this, 'ucwordsCallbackWiki' ),
2344 return ucwords( strtolower( $str ) );
2349 * capitalize words at word breaks
2351 * @param $str string
2354 function ucwordbreaks( $str ) {
2355 if ( $this->isMultibyte( $str ) ) {
2356 $str = $this->lc( $str );
2358 // since \b doesn't work for UTF-8, we explicitely define word break chars
2359 $breaks = "[ \-\(\)\}\{\.,\?!]";
2361 // find first letter after word break
2362 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2364 if ( function_exists( 'mb_strtoupper' ) ) {
2365 return preg_replace_callback(
2367 array( $this, 'ucwordbreaksCallbackMB' ),
2371 return preg_replace_callback(
2373 array( $this, 'ucwordsCallbackWiki' ),
2378 return preg_replace_callback(
2379 '/\b([\w\x80-\xff]+)\b/',
2380 array( $this, 'ucwordbreaksCallbackAscii' ),
2387 * Return a case-folded representation of $s
2389 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2390 * and $s2 are the same except for the case of their characters. It is not
2391 * necessary for the value returned to make sense when displayed.
2393 * Do *not* perform any other normalisation in this function. If a caller
2394 * uses this function when it should be using a more general normalisation
2395 * function, then fix the caller.
2401 function caseFold( $s ) {
2402 return $this->uc( $s );
2409 function checkTitleEncoding( $s ) {
2410 if ( is_array( $s ) ) {
2411 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2413 # Check for non-UTF-8 URLs
2414 $ishigh = preg_match( '/[\x80-\xff]/', $s );
2419 if ( function_exists( 'mb_check_encoding' ) ) {
2420 $isutf8 = mb_check_encoding( $s, 'UTF-8' );
2422 $isutf8 = preg_match( '/^(?>[\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2423 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
2429 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2435 function fallback8bitEncoding() {
2436 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
2440 * Most writing systems use whitespace to break up words.
2441 * Some languages such as Chinese don't conventionally do this,
2442 * which requires special handling when breaking up words for
2447 function hasWordBreaks() {
2452 * Some languages such as Chinese require word segmentation,
2453 * Specify such segmentation when overridden in derived class.
2455 * @param $string String
2458 function segmentByWord( $string ) {
2463 * Some languages have special punctuation need to be normalized.
2464 * Make such changes here.
2466 * @param $string String
2469 function normalizeForSearch( $string ) {
2470 return self
::convertDoubleWidth( $string );
2474 * convert double-width roman characters to single-width.
2475 * range: ff00-ff5f ~= 0020-007f
2477 * @param $string string
2481 protected static function convertDoubleWidth( $string ) {
2482 static $full = null;
2483 static $half = null;
2485 if ( $full === null ) {
2486 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2487 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2488 $full = str_split( $fullWidth, 3 );
2489 $half = str_split( $halfWidth );
2492 $string = str_replace( $full, $half, $string );
2497 * @param $string string
2498 * @param $pattern string
2501 protected static function insertSpace( $string, $pattern ) {
2502 $string = preg_replace( $pattern, " $1 ", $string );
2503 $string = preg_replace( '/ +/', ' ', $string );
2508 * @param $termsArray array
2511 function convertForSearchResult( $termsArray ) {
2512 # some languages, e.g. Chinese, need to do a conversion
2513 # in order for search results to be displayed correctly
2518 * Get the first character of a string.
2523 function firstChar( $s ) {
2526 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2527 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2532 if ( isset( $matches[1] ) ) {
2533 if ( strlen( $matches[1] ) != 3 ) {
2537 // Break down Hangul syllables to grab the first jamo
2538 $code = utf8ToCodepoint( $matches[1] );
2539 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
2541 } elseif ( $code < 0xb098 ) {
2542 return "\xe3\x84\xb1";
2543 } elseif ( $code < 0xb2e4 ) {
2544 return "\xe3\x84\xb4";
2545 } elseif ( $code < 0xb77c ) {
2546 return "\xe3\x84\xb7";
2547 } elseif ( $code < 0xb9c8 ) {
2548 return "\xe3\x84\xb9";
2549 } elseif ( $code < 0xbc14 ) {
2550 return "\xe3\x85\x81";
2551 } elseif ( $code < 0xc0ac ) {
2552 return "\xe3\x85\x82";
2553 } elseif ( $code < 0xc544 ) {
2554 return "\xe3\x85\x85";
2555 } elseif ( $code < 0xc790 ) {
2556 return "\xe3\x85\x87";
2557 } elseif ( $code < 0xcc28 ) {
2558 return "\xe3\x85\x88";
2559 } elseif ( $code < 0xce74 ) {
2560 return "\xe3\x85\x8a";
2561 } elseif ( $code < 0xd0c0 ) {
2562 return "\xe3\x85\x8b";
2563 } elseif ( $code < 0xd30c ) {
2564 return "\xe3\x85\x8c";
2565 } elseif ( $code < 0xd558 ) {
2566 return "\xe3\x85\x8d";
2568 return "\xe3\x85\x8e";
2575 function initEncoding() {
2576 # Some languages may have an alternate char encoding option
2577 # (Esperanto X-coding, Japanese furigana conversion, etc)
2578 # If this language is used as the primary content language,
2579 # an override to the defaults can be set here on startup.
2586 function recodeForEdit( $s ) {
2587 # For some languages we'll want to explicitly specify
2588 # which characters make it into the edit box raw
2589 # or are converted in some way or another.
2590 global $wgEditEncoding;
2591 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
2594 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2602 function recodeInput( $s ) {
2603 # Take the previous into account.
2604 global $wgEditEncoding;
2605 if ( $wgEditEncoding != '' ) {
2606 $enc = $wgEditEncoding;
2610 if ( $enc == 'UTF-8' ) {
2613 return $this->iconv( $enc, 'UTF-8', $s );
2618 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2619 * also cleans up certain backwards-compatible sequences, converting them
2620 * to the modern Unicode equivalent.
2622 * This is language-specific for performance reasons only.
2628 function normalize( $s ) {
2629 global $wgAllUnicodeFixes;
2630 $s = UtfNormal
::cleanUp( $s );
2631 if ( $wgAllUnicodeFixes ) {
2632 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2633 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2640 * Transform a string using serialized data stored in the given file (which
2641 * must be in the serialized subdirectory of $IP). The file contains pairs
2642 * mapping source characters to destination characters.
2644 * The data is cached in process memory. This will go faster if you have the
2645 * FastStringSearch extension.
2647 * @param $file string
2648 * @param $string string
2650 * @throws MWException
2653 function transformUsingPairFile( $file, $string ) {
2654 if ( !isset( $this->transformData
[$file] ) ) {
2655 $data = wfGetPrecompiledData( $file );
2656 if ( $data === false ) {
2657 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2659 $this->transformData
[$file] = new ReplacementArray( $data );
2661 return $this->transformData
[$file]->replace( $string );
2665 * For right-to-left language support
2670 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2674 * Return the correct HTML 'dir' attribute value for this language.
2678 return $this->isRTL() ?
'rtl' : 'ltr';
2682 * Return 'left' or 'right' as appropriate alignment for line-start
2683 * for this language's text direction.
2685 * Should be equivalent to CSS3 'start' text-align value....
2689 function alignStart() {
2690 return $this->isRTL() ?
'right' : 'left';
2694 * Return 'right' or 'left' as appropriate alignment for line-end
2695 * for this language's text direction.
2697 * Should be equivalent to CSS3 'end' text-align value....
2701 function alignEnd() {
2702 return $this->isRTL() ?
'left' : 'right';
2706 * A hidden direction mark (LRM or RLM), depending on the language direction.
2707 * Unlike getDirMark(), this function returns the character as an HTML entity.
2708 * This function should be used when the output is guaranteed to be HTML,
2709 * because it makes the output HTML source code more readable. When
2710 * the output is plain text or can be escaped, getDirMark() should be used.
2712 * @param $opposite Boolean Get the direction mark opposite to your language
2715 function getDirMarkEntity( $opposite = false ) {
2716 if ( $opposite ) { return $this->isRTL() ?
'‎' : '‏'; }
2717 return $this->isRTL() ?
'‏' : '‎';
2721 * A hidden direction mark (LRM or RLM), depending on the language direction.
2722 * This function produces them as invisible Unicode characters and
2723 * the output may be hard to read and debug, so it should only be used
2724 * when the output is plain text or can be escaped. When the output is
2725 * HTML, use getDirMarkEntity() instead.
2727 * @param $opposite Boolean Get the direction mark opposite to your language
2730 function getDirMark( $opposite = false ) {
2731 $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
2732 $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
2733 if ( $opposite ) { return $this->isRTL() ?
$lrm : $rlm; }
2734 return $this->isRTL() ?
$rlm : $lrm;
2740 function capitalizeAllNouns() {
2741 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2745 * An arrow, depending on the language direction.
2747 * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
2750 function getArrow( $direction = 'forwards' ) {
2751 switch ( $direction ) {
2753 return $this->isRTL() ?
'←' : '→';
2755 return $this->isRTL() ?
'→' : '←';
2768 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2772 function linkPrefixExtension() {
2773 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2779 function getMagicWords() {
2780 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2783 protected function doMagicHook() {
2784 if ( $this->mMagicHookDone
) {
2787 $this->mMagicHookDone
= true;
2788 wfProfileIn( 'LanguageGetMagic' );
2789 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2790 wfProfileOut( 'LanguageGetMagic' );
2794 * Fill a MagicWord object with data from here
2798 function getMagic( $mw ) {
2799 $this->doMagicHook();
2801 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
2802 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
2804 $magicWords = $this->getMagicWords();
2805 if ( isset( $magicWords[$mw->mId
] ) ) {
2806 $rawEntry = $magicWords[$mw->mId
];
2812 if ( !is_array( $rawEntry ) ) {
2813 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
2815 $mw->mCaseSensitive
= $rawEntry[0];
2816 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
2821 * Add magic words to the extension array
2823 * @param $newWords array
2825 function addMagicWordsByLang( $newWords ) {
2826 $fallbackChain = $this->getFallbackLanguages();
2827 $fallbackChain = array_reverse( $fallbackChain );
2828 foreach ( $fallbackChain as $code ) {
2829 if ( isset( $newWords[$code] ) ) {
2830 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
2836 * Get special page names, as an associative array
2837 * case folded alias => real name
2839 function getSpecialPageAliases() {
2840 // Cache aliases because it may be slow to load them
2841 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
2843 $this->mExtendedSpecialPageAliases
=
2844 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
2845 wfRunHooks( 'LanguageGetSpecialPageAliases',
2846 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
2849 return $this->mExtendedSpecialPageAliases
;
2853 * Italic is unsuitable for some languages
2855 * @param $text String: the text to be emphasized.
2858 function emphasize( $text ) {
2859 return "<em>$text</em>";
2863 * Normally we output all numbers in plain en_US style, that is
2864 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2865 * point twohundredthirtyfive. However this is not suitable for all
2866 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2867 * Icelandic just want to use commas instead of dots, and dots instead
2868 * of commas like "293.291,235".
2870 * An example of this function being called:
2872 * wfMsg( 'message', $wgLang->formatNum( $num ) )
2875 * See LanguageGu.php for the Gujarati implementation and
2876 * $separatorTransformTable on MessageIs.php for
2877 * the , => . and . => , implementation.
2879 * @todo check if it's viable to use localeconv() for the decimal
2881 * @param $number Mixed: the string to be formatted, should be an integer
2882 * or a floating point number.
2883 * @param $nocommafy Bool: set to true for special numbers like dates
2886 public function formatNum( $number, $nocommafy = false ) {
2887 global $wgTranslateNumerals;
2888 if ( !$nocommafy ) {
2889 $number = $this->commafy( $number );
2890 $s = $this->separatorTransformTable();
2892 $number = strtr( $number, $s );
2896 if ( $wgTranslateNumerals ) {
2897 $s = $this->digitTransformTable();
2899 $number = strtr( $number, $s );
2907 * @param $number string
2910 function parseFormattedNumber( $number ) {
2911 $s = $this->digitTransformTable();
2913 $number = strtr( $number, array_flip( $s ) );
2916 $s = $this->separatorTransformTable();
2918 $number = strtr( $number, array_flip( $s ) );
2921 $number = strtr( $number, array( ',' => '' ) );
2926 * Adds commas to a given number
2931 function commafy( $_ ) {
2932 $digitGroupingPattern = $this->digitGroupingPattern();
2933 if ( $_ === null ) {
2937 if ( !$digitGroupingPattern ||
$digitGroupingPattern === "###,###,###" ) {
2938 // default grouping is at thousands, use the same for ###,###,### pattern too.
2939 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2941 // Ref: http://cldr.unicode.org/translation/number-patterns
2943 if ( intval( $_ ) < 0 ) {
2944 // For negative numbers apply the algorithm like positive number and add sign.
2946 $_ = substr( $_, 1 );
2948 $numberpart = array();
2949 $decimalpart = array();
2950 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
2951 preg_match( "/\d+/", $_, $numberpart );
2952 preg_match( "/\.\d*/", $_, $decimalpart );
2953 $groupedNumber = ( count( $decimalpart ) > 0 ) ?
$decimalpart[0]:"";
2954 if ( $groupedNumber === $_ ) {
2955 // the string does not have any number part. Eg: .12345
2956 return $sign . $groupedNumber;
2958 $start = $end = strlen( $numberpart[0] );
2959 while ( $start > 0 ) {
2960 $match = $matches[0][$numMatches -1] ;
2961 $matchLen = strlen( $match );
2962 $start = $end - $matchLen;
2966 $groupedNumber = substr( $_ , $start, $end -$start ) . $groupedNumber ;
2968 if ( $numMatches > 1 ) {
2969 // use the last pattern for the rest of the number
2973 $groupedNumber = "," . $groupedNumber;
2976 return $sign . $groupedNumber;
2982 function digitGroupingPattern() {
2983 return self
::$dataCache->getItem( $this->mCode
, 'digitGroupingPattern' );
2989 function digitTransformTable() {
2990 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
2996 function separatorTransformTable() {
2997 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
3001 * Take a list of strings and build a locale-friendly comma-separated
3002 * list, using the local comma-separator message.
3003 * The last two strings are chained with an "and".
3008 function listToText( array $l ) {
3010 $m = count( $l ) - 1;
3012 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
3014 for ( $i = $m; $i >= 0; $i-- ) {
3017 } elseif ( $i == $m - 1 ) {
3018 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
3020 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
3028 * Take a list of strings and build a locale-friendly comma-separated
3029 * list, using the local comma-separator message.
3030 * @param $list array of strings to put in a comma list
3033 function commaList( array $list ) {
3037 array( 'parsemag', 'escapenoentities', 'language' => $this )
3044 * Take a list of strings and build a locale-friendly semicolon-separated
3045 * list, using the local semicolon-separator message.
3046 * @param $list array of strings to put in a semicolon list
3049 function semicolonList( array $list ) {
3052 'semicolon-separator',
3053 array( 'parsemag', 'escapenoentities', 'language' => $this )
3060 * Same as commaList, but separate it with the pipe instead.
3061 * @param $list array of strings to put in a pipe list
3064 function pipeList( array $list ) {
3068 array( 'escapenoentities', 'language' => $this )
3075 * Truncate a string to a specified length in bytes, appending an optional
3076 * string (e.g. for ellipses)
3078 * The database offers limited byte lengths for some columns in the database;
3079 * multi-byte character sets mean we need to ensure that only whole characters
3080 * are included, otherwise broken characters can be passed to the user
3082 * If $length is negative, the string will be truncated from the beginning
3084 * @param $string String to truncate
3085 * @param $length Int: maximum length (including ellipses)
3086 * @param $ellipsis String to append to the truncated text
3087 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
3088 * $adjustLength was introduced in 1.18, before that behaved as if false.
3091 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
3092 # Use the localized ellipsis character
3093 if ( $ellipsis == '...' ) {
3094 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
3096 # Check if there is no need to truncate
3097 if ( $length == 0 ) {
3098 return $ellipsis; // convention
3099 } elseif ( strlen( $string ) <= abs( $length ) ) {
3100 return $string; // no need to truncate
3102 $stringOriginal = $string;
3103 # If ellipsis length is >= $length then we can't apply $adjustLength
3104 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
3105 $string = $ellipsis; // this can be slightly unexpected
3106 # Otherwise, truncate and add ellipsis...
3108 $eLength = $adjustLength ?
strlen( $ellipsis ) : 0;
3109 if ( $length > 0 ) {
3110 $length -= $eLength;
3111 $string = substr( $string, 0, $length ); // xyz...
3112 $string = $this->removeBadCharLast( $string );
3113 $string = $string . $ellipsis;
3115 $length +
= $eLength;
3116 $string = substr( $string, $length ); // ...xyz
3117 $string = $this->removeBadCharFirst( $string );
3118 $string = $ellipsis . $string;
3121 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
3122 # This check is *not* redundant if $adjustLength, due to the single case where
3123 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
3124 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
3127 return $stringOriginal;
3132 * Remove bytes that represent an incomplete Unicode character
3133 * at the end of string (e.g. bytes of the char are missing)
3135 * @param $string String
3138 protected function removeBadCharLast( $string ) {
3139 if ( $string != '' ) {
3140 $char = ord( $string[strlen( $string ) - 1] );
3142 if ( $char >= 0xc0 ) {
3143 # We got the first byte only of a multibyte char; remove it.
3144 $string = substr( $string, 0, -1 );
3145 } elseif ( $char >= 0x80 &&
3146 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
3147 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
3149 # We chopped in the middle of a character; remove it
3157 * Remove bytes that represent an incomplete Unicode character
3158 * at the start of string (e.g. bytes of the char are missing)
3160 * @param $string String
3163 protected function removeBadCharFirst( $string ) {
3164 if ( $string != '' ) {
3165 $char = ord( $string[0] );
3166 if ( $char >= 0x80 && $char < 0xc0 ) {
3167 # We chopped in the middle of a character; remove the whole thing
3168 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
3175 * Truncate a string of valid HTML to a specified length in bytes,
3176 * appending an optional string (e.g. for ellipses), and return valid HTML
3178 * This is only intended for styled/linked text, such as HTML with
3179 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
3180 * Also, this will not detect things like "display:none" CSS.
3182 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
3184 * @param string $text HTML string to truncate
3185 * @param int $length (zero/positive) Maximum length (including ellipses)
3186 * @param string $ellipsis String to append to the truncated text
3189 function truncateHtml( $text, $length, $ellipsis = '...' ) {
3190 # Use the localized ellipsis character
3191 if ( $ellipsis == '...' ) {
3192 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
3194 # Check if there is clearly no need to truncate
3195 if ( $length <= 0 ) {
3196 return $ellipsis; // no text shown, nothing to format (convention)
3197 } elseif ( strlen( $text ) <= $length ) {
3198 return $text; // string short enough even *with* HTML (short-circuit)
3201 $dispLen = 0; // innerHTML legth so far
3202 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3203 $tagType = 0; // 0-open, 1-close
3204 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3205 $entityState = 0; // 0-not entity, 1-entity
3206 $tag = $ret = ''; // accumulated tag name, accumulated result string
3207 $openTags = array(); // open tag stack
3208 $maybeState = null; // possible truncation state
3210 $textLen = strlen( $text );
3211 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3212 for ( $pos = 0; true; ++
$pos ) {
3213 # Consider truncation once the display length has reached the maximim.
3214 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3215 # Check that we're not in the middle of a bracket/entity...
3216 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3217 if ( !$testingEllipsis ) {
3218 $testingEllipsis = true;
3219 # Save where we are; we will truncate here unless there turn out to
3220 # be so few remaining characters that truncation is not necessary.
3221 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3222 $maybeState = array( $ret, $openTags ); // save state
3224 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3225 # String in fact does need truncation, the truncation point was OK.
3226 list( $ret, $openTags ) = $maybeState; // reload state
3227 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3228 $ret .= $ellipsis; // add ellipsis
3232 if ( $pos >= $textLen ) break; // extra iteration just for above checks
3234 # Read the next char...
3236 $lastCh = $pos ?
$text[$pos - 1] : '';
3237 $ret .= $ch; // add to result string
3239 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3240 $entityState = 0; // for bad HTML
3241 $bracketState = 1; // tag started (checking for backslash)
3242 } elseif ( $ch == '>' ) {
3243 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3244 $entityState = 0; // for bad HTML
3245 $bracketState = 0; // out of brackets
3246 } elseif ( $bracketState == 1 ) {
3248 $tagType = 1; // close tag (e.g. "</span>")
3250 $tagType = 0; // open tag (e.g. "<span>")
3253 $bracketState = 2; // building tag name
3254 } elseif ( $bracketState == 2 ) {
3258 // Name found (e.g. "<a href=..."), add on tag attributes...
3259 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
3261 } elseif ( $bracketState == 0 ) {
3262 if ( $entityState ) {
3265 $dispLen++
; // entity is one displayed char
3268 if ( $neLength == 0 && !$maybeState ) {
3269 // Save state without $ch. We want to *hit* the first
3270 // display char (to get tags) but not *use* it if truncating.
3271 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3274 $entityState = 1; // entity found, (e.g. " ")
3276 $dispLen++
; // this char is displayed
3277 // Add the next $max display text chars after this in one swoop...
3278 $max = ( $testingEllipsis ?
$length : $neLength ) - $dispLen;
3279 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos +
1, $max );
3280 $dispLen +
= $skipped;
3286 // Close the last tag if left unclosed by bad HTML
3287 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3288 while ( count( $openTags ) > 0 ) {
3289 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3295 * truncateHtml() helper function
3296 * like strcspn() but adds the skipped chars to $ret
3305 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3306 if ( $len === null ) {
3307 $len = -1; // -1 means "no limit" for strcspn
3308 } elseif ( $len < 0 ) {
3312 if ( $start < strlen( $text ) ) {
3313 $skipCount = strcspn( $text, $search, $start, $len );
3314 $ret .= substr( $text, $start, $skipCount );
3320 * truncateHtml() helper function
3321 * (a) push or pop $tag from $openTags as needed
3322 * (b) clear $tag value
3323 * @param &$tag string Current HTML tag name we are looking at
3324 * @param $tagType int (0-open tag, 1-close tag)
3325 * @param $lastCh string Character before the '>' that ended this tag
3326 * @param &$openTags array Open tag stack (not accounting for $tag)
3328 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3329 $tag = ltrim( $tag );
3331 if ( $tagType == 0 && $lastCh != '/' ) {
3332 $openTags[] = $tag; // tag opened (didn't close itself)
3333 } elseif ( $tagType == 1 ) {
3334 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3335 array_pop( $openTags ); // tag closed
3343 * Grammatical transformations, needed for inflected languages
3344 * Invoked by putting {{grammar:case|word}} in a message
3346 * @param $word string
3347 * @param $case string
3350 function convertGrammar( $word, $case ) {
3351 global $wgGrammarForms;
3352 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3353 return $wgGrammarForms[$this->getCode()][$case][$word];
3358 * Get the grammar forms for the content language
3359 * @return array of grammar forms
3362 function getGrammarForms() {
3363 global $wgGrammarForms;
3364 if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
3365 return $wgGrammarForms[$this->getCode()];
3370 * Provides an alternative text depending on specified gender.
3371 * Usage {{gender:username|masculine|feminine|neutral}}.
3372 * username is optional, in which case the gender of current user is used,
3373 * but only in (some) interface messages; otherwise default gender is used.
3375 * If no forms are given, an empty string is returned. If only one form is
3376 * given, it will be returned unconditionally. These details are implied by
3377 * the caller and cannot be overridden in subclasses.
3379 * If more than one form is given, the default is to use the neutral one
3380 * if it is specified, and to use the masculine one otherwise. These
3381 * details can be overridden in subclasses.
3383 * @param $gender string
3384 * @param $forms array
3388 function gender( $gender, $forms ) {
3389 if ( !count( $forms ) ) {
3392 $forms = $this->preConvertPlural( $forms, 2 );
3393 if ( $gender === 'male' ) {
3396 if ( $gender === 'female' ) {
3399 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
3403 * Plural form transformations, needed for some languages.
3404 * For example, there are 3 form of plural in Russian and Polish,
3405 * depending on "count mod 10". See [[w:Plural]]
3406 * For English it is pretty simple.
3408 * Invoked by putting {{plural:count|wordform1|wordform2}}
3409 * or {{plural:count|wordform1|wordform2|wordform3}}
3411 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3413 * @param $count Integer: non-localized number
3414 * @param $forms Array: different plural forms
3415 * @return string Correct form of plural for $count in this language
3417 function convertPlural( $count, $forms ) {
3418 if ( !count( $forms ) ) {
3421 $forms = $this->preConvertPlural( $forms, 2 );
3423 return ( $count == 1 ) ?
$forms[0] : $forms[1];
3427 * Checks that convertPlural was given an array and pads it to requested
3428 * amount of forms by copying the last one.
3430 * @param $count Integer: How many forms should there be at least
3431 * @param $forms Array of forms given to convertPlural
3432 * @return array Padded array of forms or an exception if not an array
3434 protected function preConvertPlural( /* Array */ $forms, $count ) {
3435 while ( count( $forms ) < $count ) {
3436 $forms[] = $forms[count( $forms ) - 1];
3442 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3443 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3444 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3445 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3448 * @param $str String: the validated block duration in English
3449 * @return string Somehow translated block duration
3450 * @see LanguageFi.php for example implementation
3452 function translateBlockExpiry( $str ) {
3453 $duration = SpecialBlock
::getSuggestedDurations( $this );
3454 foreach ( $duration as $show => $value ) {
3455 if ( strcmp( $str, $value ) == 0 ) {
3456 return htmlspecialchars( trim( $show ) );
3460 // Since usually only infinite or indefinite is only on list, so try
3461 // equivalents if still here.
3462 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3463 if ( in_array( $str, $indefs ) ) {
3464 foreach ( $indefs as $val ) {
3465 $show = array_search( $val, $duration, true );
3466 if ( $show !== false ) {
3467 return htmlspecialchars( trim( $show ) );
3471 // If all else fails, return the original string.
3476 * languages like Chinese need to be segmented in order for the diff
3479 * @param $text String
3482 public function segmentForDiff( $text ) {
3487 * and unsegment to show the result
3489 * @param $text String
3492 public function unsegmentForDiff( $text ) {
3497 * Return the LanguageConverter used in the Language
3500 * @return LanguageConverter
3502 public function getConverter() {
3503 return $this->mConverter
;
3507 * convert text to all supported variants
3509 * @param $text string
3512 public function autoConvertToAllVariants( $text ) {
3513 return $this->mConverter
->autoConvertToAllVariants( $text );
3517 * convert text to different variants of a language.
3519 * @param $text string
3522 public function convert( $text ) {
3523 return $this->mConverter
->convert( $text );
3527 * Convert a Title object to a string in the preferred variant
3529 * @param $title Title
3532 public function convertTitle( $title ) {
3533 return $this->mConverter
->convertTitle( $title );
3537 * Check if this is a language with variants
3541 public function hasVariants() {
3542 return sizeof( $this->getVariants() ) > 1;
3546 * Check if the language has the specific variant
3549 * @param $variant string
3552 public function hasVariant( $variant ) {
3553 return (bool)$this->mConverter
->validateVariant( $variant );
3557 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3559 * @param $text string
3562 public function armourMath( $text ) {
3563 return $this->mConverter
->armourMath( $text );
3567 * Perform output conversion on a string, and encode for safe HTML output.
3568 * @param $text String text to be converted
3569 * @param $isTitle Bool whether this conversion is for the article title
3571 * @todo this should get integrated somewhere sane
3573 public function convertHtml( $text, $isTitle = false ) {
3574 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3578 * @param $key string
3581 public function convertCategoryKey( $key ) {
3582 return $this->mConverter
->convertCategoryKey( $key );
3586 * Get the list of variants supported by this language
3587 * see sample implementation in LanguageZh.php
3589 * @return array an array of language codes
3591 public function getVariants() {
3592 return $this->mConverter
->getVariants();
3598 public function getPreferredVariant() {
3599 return $this->mConverter
->getPreferredVariant();
3605 public function getDefaultVariant() {
3606 return $this->mConverter
->getDefaultVariant();
3612 public function getURLVariant() {
3613 return $this->mConverter
->getURLVariant();
3617 * If a language supports multiple variants, it is
3618 * possible that non-existing link in one variant
3619 * actually exists in another variant. this function
3620 * tries to find it. See e.g. LanguageZh.php
3622 * @param $link String: the name of the link
3623 * @param $nt Mixed: the title object of the link
3624 * @param $ignoreOtherCond Boolean: to disable other conditions when
3625 * we need to transclude a template or update a category's link
3626 * @return null the input parameters may be modified upon return
3628 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3629 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
3633 * If a language supports multiple variants, converts text
3634 * into an array of all possible variants of the text:
3635 * 'variant' => text in that variant
3637 * @deprecated since 1.17 Use autoConvertToAllVariants()
3639 * @param $text string
3643 public function convertLinkToAllVariants( $text ) {
3644 return $this->mConverter
->convertLinkToAllVariants( $text );
3648 * returns language specific options used by User::getPageRenderHash()
3649 * for example, the preferred language variant
3653 function getExtraHashOptions() {
3654 return $this->mConverter
->getExtraHashOptions();
3658 * For languages that support multiple variants, the title of an
3659 * article may be displayed differently in different variants. this
3660 * function returns the apporiate title defined in the body of the article.
3664 public function getParsedTitle() {
3665 return $this->mConverter
->getParsedTitle();
3669 * Enclose a string with the "no conversion" tag. This is used by
3670 * various functions in the Parser
3672 * @param $text String: text to be tagged for no conversion
3673 * @param $noParse bool
3674 * @return string the tagged text
3676 public function markNoConversion( $text, $noParse = false ) {
3677 return $this->mConverter
->markNoConversion( $text, $noParse );
3681 * A regular expression to match legal word-trailing characters
3682 * which should be merged onto a link of the form [[foo]]bar.
3686 public function linkTrail() {
3687 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
3693 function getLangObj() {
3698 * Get the RFC 3066 code for this language object
3702 public function getCode() {
3703 return $this->mCode
;
3707 * Get the code in Bcp47 format which we can use
3708 * inside of html lang="" tags.
3712 public function getHtmlCode() {
3713 if ( is_null( $this->mHtmlCode
) ) {
3714 $this->mHtmlCode
= wfBCP47( $this->getCode() );
3716 return $this->mHtmlCode
;
3720 * @param $code string
3722 public function setCode( $code ) {
3723 $this->mCode
= $code;
3724 // Ensure we don't leave an incorrect html code lying around
3725 $this->mHtmlCode
= null;
3729 * Get the name of a file for a certain language code
3730 * @param $prefix string Prepend this to the filename
3731 * @param $code string Language code
3732 * @param $suffix string Append this to the filename
3733 * @throws MWException
3734 * @return string $prefix . $mangledCode . $suffix
3736 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
3737 // Protect against path traversal
3738 if ( !Language
::isValidCode( $code )
3739 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
3741 throw new MWException( "Invalid language code \"$code\"" );
3744 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
3748 * Get the language code from a file name. Inverse of getFileName()
3749 * @param $filename string $prefix . $languageCode . $suffix
3750 * @param $prefix string Prefix before the language code
3751 * @param $suffix string Suffix after the language code
3752 * @return string Language code, or false if $prefix or $suffix isn't found
3754 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
3756 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
3757 preg_quote( $suffix, '/' ) . '/', $filename, $m );
3758 if ( !count( $m ) ) {
3761 return str_replace( '_', '-', strtolower( $m[1] ) );
3765 * @param $code string
3768 public static function getMessagesFileName( $code ) {
3770 $file = self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
3771 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
3776 * @param $code string
3779 public static function getClassFileName( $code ) {
3781 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
3785 * Get the first fallback for a given language.
3787 * @param $code string
3789 * @return bool|string
3791 public static function getFallbackFor( $code ) {
3792 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3795 $fallbacks = self
::getFallbacksFor( $code );
3796 $first = array_shift( $fallbacks );
3802 * Get the ordered list of fallback languages.
3805 * @param $code string Language code
3808 public static function getFallbacksFor( $code ) {
3809 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3812 $v = self
::getLocalisationCache()->getItem( $code, 'fallback' );
3813 $v = array_map( 'trim', explode( ',', $v ) );
3814 if ( $v[count( $v ) - 1] !== 'en' ) {
3822 * Get all messages for a given language
3823 * WARNING: this may take a long time. If you just need all message *keys*
3824 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
3826 * @param $code string
3830 public static function getMessagesFor( $code ) {
3831 return self
::getLocalisationCache()->getItem( $code, 'messages' );
3835 * Get a message for a given language
3837 * @param $key string
3838 * @param $code string
3842 public static function getMessageFor( $key, $code ) {
3843 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
3847 * Get all message keys for a given language. This is a faster alternative to
3848 * array_keys( Language::getMessagesFor( $code ) )
3851 * @param $code string Language code
3852 * @return array of message keys (strings)
3854 public static function getMessageKeysFor( $code ) {
3855 return self
::getLocalisationCache()->getSubItemList( $code, 'messages' );
3862 function fixVariableInNamespace( $talk ) {
3863 if ( strpos( $talk, '$1' ) === false ) {
3867 global $wgMetaNamespace;
3868 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
3870 # Allow grammar transformations
3871 # Allowing full message-style parsing would make simple requests
3872 # such as action=raw much more expensive than they need to be.
3873 # This will hopefully cover most cases.
3874 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
3875 array( &$this, 'replaceGrammarInNamespace' ), $talk );
3876 return str_replace( ' ', '_', $talk );
3883 function replaceGrammarInNamespace( $m ) {
3884 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
3888 * @throws MWException
3891 static function getCaseMaps() {
3892 static $wikiUpperChars, $wikiLowerChars;
3893 if ( isset( $wikiUpperChars ) ) {
3894 return array( $wikiUpperChars, $wikiLowerChars );
3897 wfProfileIn( __METHOD__
);
3898 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
3899 if ( $arr === false ) {
3900 throw new MWException(
3901 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
3903 $wikiUpperChars = $arr['wikiUpperChars'];
3904 $wikiLowerChars = $arr['wikiLowerChars'];
3905 wfProfileOut( __METHOD__
);
3906 return array( $wikiUpperChars, $wikiLowerChars );
3910 * Decode an expiry (block, protection, etc) which has come from the DB
3912 * @FIXME: why are we returnings DBMS-dependent strings???
3914 * @param $expiry String: Database expiry String
3915 * @param $format Bool|Int true to process using language functions, or TS_ constant
3916 * to return the expiry in a given timestamp
3919 public function formatExpiry( $expiry, $format = true ) {
3920 static $infinity, $infinityMsg;
3921 if ( $infinity === null ) {
3922 $infinityMsg = wfMessage( 'infiniteblock' );
3923 $infinity = wfGetDB( DB_SLAVE
)->getInfinity();
3926 if ( $expiry == '' ||
$expiry == $infinity ) {
3927 return $format === true
3931 return $format === true
3932 ?
$this->timeanddate( $expiry, /* User preference timezone */ true )
3933 : wfTimestamp( $format, $expiry );
3939 * @param $seconds int|float
3940 * @param $format Array Optional
3941 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
3942 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
3943 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
3944 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
3947 function formatTimePeriod( $seconds, $format = array() ) {
3948 if ( !is_array( $format ) ) {
3949 $format = array( 'avoid' => $format ); // For backwards compatibility
3951 if ( !isset( $format['avoid'] ) ) {
3952 $format['avoid'] = false;
3954 if ( !isset( $format['noabbrevs' ] ) ) {
3955 $format['noabbrevs'] = false;
3957 $secondsMsg = wfMessage(
3958 $format['noabbrevs'] ?
'seconds' : 'seconds-abbrev' )->inLanguage( $this );
3959 $minutesMsg = wfMessage(
3960 $format['noabbrevs'] ?
'minutes' : 'minutes-abbrev' )->inLanguage( $this );
3961 $hoursMsg = wfMessage(
3962 $format['noabbrevs'] ?
'hours' : 'hours-abbrev' )->inLanguage( $this );
3963 $daysMsg = wfMessage(
3964 $format['noabbrevs'] ?
'days' : 'days-abbrev' )->inLanguage( $this );
3966 if ( round( $seconds * 10 ) < 100 ) {
3967 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
3968 $s = $secondsMsg->params( $s )->text();
3969 } elseif ( round( $seconds ) < 60 ) {
3970 $s = $this->formatNum( round( $seconds ) );
3971 $s = $secondsMsg->params( $s )->text();
3972 } elseif ( round( $seconds ) < 3600 ) {
3973 $minutes = floor( $seconds / 60 );
3974 $secondsPart = round( fmod( $seconds, 60 ) );
3975 if ( $secondsPart == 60 ) {
3979 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3981 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
3982 } elseif ( round( $seconds ) <= 2 * 86400 ) {
3983 $hours = floor( $seconds / 3600 );
3984 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
3985 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
3986 if ( $secondsPart == 60 ) {
3990 if ( $minutes == 60 ) {
3994 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
3996 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
3997 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
3998 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4001 $days = floor( $seconds / 86400 );
4002 if ( $format['avoid'] === 'avoidminutes' ) {
4003 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
4004 if ( $hours == 24 ) {
4008 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4010 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4011 } elseif ( $format['avoid'] === 'avoidseconds' ) {
4012 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
4013 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
4014 if ( $minutes == 60 ) {
4018 if ( $hours == 24 ) {
4022 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4024 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4026 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4028 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4030 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
4037 * Format a bitrate for output, using an appropriate
4038 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
4040 * This use base 1000. For base 1024 use formatSize(), for another base
4041 * see formatComputingNumbers()
4046 function formatBitrate( $bps ) {
4047 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
4051 * @param $size int Size of the unit
4052 * @param $boundary int Size boundary (1000, or 1024 in most cases)
4053 * @param $messageKey string Message key to be uesd
4056 function formatComputingNumbers( $size, $boundary, $messageKey ) {
4058 return str_replace( '$1', $this->formatNum( $size ),
4059 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
4062 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
4065 $maxIndex = count( $sizes ) - 1;
4066 while ( $size >= $boundary && $index < $maxIndex ) {
4071 // For small sizes no decimal places necessary
4074 // For MB and bigger two decimal places are smarter
4077 $msg = str_replace( '$1', $sizes[$index], $messageKey );
4079 $size = round( $size, $round );
4080 $text = $this->getMessageFromDB( $msg );
4081 return str_replace( '$1', $this->formatNum( $size ), $text );
4085 * Format a size in bytes for output, using an appropriate
4086 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
4088 * This method use base 1024. For base 1000 use formatBitrate(), for
4089 * another base see formatComputingNumbers()
4091 * @param $size int Size to format
4092 * @return string Plain text (not HTML)
4094 function formatSize( $size ) {
4095 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
4099 * Make a list item, used by various special pages
4101 * @param $page String Page link
4102 * @param $details String Text between brackets
4103 * @param $oppositedm Boolean Add the direction mark opposite to your
4104 * language, to display text properly
4107 function specialList( $page, $details, $oppositedm = true ) {
4108 $dirmark = ( $oppositedm ?
$this->getDirMark( true ) : '' ) .
4109 $this->getDirMark();
4110 $details = $details ?
$dirmark . $this->getMessageFromDB( 'word-separator' ) .
4111 wfMsgExt( 'parentheses', array( 'escape', 'replaceafter', 'language' => $this ), $details ) : '';
4112 return $page . $details;
4116 * Generate (prev x| next x) (20|50|100...) type links for paging
4118 * @param $title Title object to link
4119 * @param $offset Integer offset parameter
4120 * @param $limit Integer limit parameter
4121 * @param $query String optional URL query parameter string
4122 * @param $atend Bool optional param for specified if this is the last page
4125 public function viewPrevNext( Title
$title, $offset, $limit, array $query = array(), $atend = false ) {
4126 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
4128 # Make 'previous' link
4129 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4130 if ( $offset > 0 ) {
4131 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
4132 $query, $prev, 'prevn-title', 'mw-prevlink' );
4134 $plink = htmlspecialchars( $prev );
4138 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4140 $nlink = htmlspecialchars( $next );
4142 $nlink = $this->numLink( $title, $offset +
$limit, $limit,
4143 $query, $next, 'prevn-title', 'mw-nextlink' );
4146 # Make links to set number of items per page
4147 $numLinks = array();
4148 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
4149 $numLinks[] = $this->numLink( $title, $offset, $num,
4150 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
4153 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
4154 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
4158 * Helper function for viewPrevNext() that generates links
4160 * @param $title Title object to link
4161 * @param $offset Integer offset parameter
4162 * @param $limit Integer limit parameter
4163 * @param $query Array extra query parameters
4164 * @param $link String text to use for the link; will be escaped
4165 * @param $tooltipMsg String name of the message to use as tooltip
4166 * @param $class String value of the "class" attribute of the link
4167 * @return String HTML fragment
4169 private function numLink( Title
$title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
4170 $query = array( 'limit' => $limit, 'offset' => $offset ) +
$query;
4171 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4172 return Html
::element( 'a', array( 'href' => $title->getLocalURL( $query ),
4173 'title' => $tooltip, 'class' => $class ), $link );
4177 * Get the conversion rule title, if any.
4181 public function getConvRuleTitle() {
4182 return $this->mConverter
->getConvRuleTitle();