3 * Internationalisation code.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * @defgroup Language Language
28 if ( !defined( 'MEDIAWIKI' ) ) {
29 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
34 global $wgLanguageNames;
35 require_once( __DIR__
. '/Names.php' );
37 if ( function_exists( 'mb_strtoupper' ) ) {
38 mb_internal_encoding( 'UTF-8' );
42 * a fake language converter
52 function __construct( $langobj ) { $this->mLang
= $langobj; }
53 function autoConvertToAllVariants( $text ) { return array( $this->mLang
->getCode() => $text ); }
54 function convert( $t ) { return $t; }
55 function convertTo( $text, $variant ) { return $text; }
56 function convertTitle( $t ) { return $t->getPrefixedText(); }
57 function convertNamespace( $ns ) { return $this->mLang
->getFormattedNsText( $ns ); }
58 function getVariants() { return array( $this->mLang
->getCode() ); }
59 function getPreferredVariant() { return $this->mLang
->getCode(); }
60 function getDefaultVariant() { return $this->mLang
->getCode(); }
61 function getURLVariant() { return ''; }
62 function getConvRuleTitle() { return false; }
63 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
64 function getExtraHashOptions() { return ''; }
65 function getParsedTitle() { return ''; }
66 function markNoConversion( $text, $noParse = false ) { return $text; }
67 function convertCategoryKey( $key ) { return $key; }
68 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
69 function armourMath( $text ) { return $text; }
73 * Internationalisation code
79 * @var LanguageConverter
83 public $mVariants, $mCode, $mLoaded = false;
84 public $mMagicExtensions = array(), $mMagicHookDone = false;
85 private $mHtmlCode = null;
87 public $dateFormatStrings = array();
88 public $mExtendedSpecialPageAliases;
90 protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
93 * ReplacementArray object caches
95 public $transformData = array();
98 * @var LocalisationCache
100 static public $dataCache;
102 static public $mLangObjCache = array();
104 static public $mWeekdayMsgs = array(
105 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
109 static public $mWeekdayAbbrevMsgs = array(
110 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
113 static public $mMonthMsgs = array(
114 'january', 'february', 'march', 'april', 'may_long', 'june',
115 'july', 'august', 'september', 'october', 'november',
118 static public $mMonthGenMsgs = array(
119 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
120 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
123 static public $mMonthAbbrevMsgs = array(
124 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
125 'sep', 'oct', 'nov', 'dec'
128 static public $mIranianCalendarMonthMsgs = array(
129 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
130 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
131 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
132 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
135 static public $mHebrewCalendarMonthMsgs = array(
136 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
137 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
138 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
139 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
140 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
143 static public $mHebrewCalendarMonthGenMsgs = array(
144 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
145 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
146 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
147 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
148 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
151 static public $mHijriCalendarMonthMsgs = array(
152 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
153 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
154 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
155 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
162 static public $durationIntervals = array(
163 'millennia' => 31557600000,
164 'centuries' => 3155760000,
165 'decades' => 315576000,
166 'years' => 31557600, // 86400 * 365.25
175 * Get a cached or new language object for a given language code
176 * @param $code String
179 static function factory( $code ) {
180 global $wgLangObjCacheSize;
182 // get the language object to process
183 $langObj = isset( self
::$mLangObjCache[$code] )
184 ? self
::$mLangObjCache[$code]
185 : self
::newFromCode( $code );
187 // merge the language object in to get it up front in the cache
188 self
::$mLangObjCache = array_merge( array( $code => $langObj ), self
::$mLangObjCache );
189 // get rid of the oldest ones in case we have an overflow
190 self
::$mLangObjCache = array_slice( self
::$mLangObjCache, 0, $wgLangObjCacheSize, true );
196 * Create a language object for a given language code
197 * @param $code String
198 * @throws MWException
201 protected static function newFromCode( $code ) {
202 // Protect against path traversal below
203 if ( !Language
::isValidCode( $code )
204 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
206 throw new MWException( "Invalid language code \"$code\"" );
209 if ( !Language
::isValidBuiltInCode( $code ) ) {
210 // It's not possible to customise this code with class files, so
211 // just return a Language object. This is to support uselang= hacks.
212 $lang = new Language
;
213 $lang->setCode( $code );
217 // Check if there is a language class for the code
218 $class = self
::classFromCode( $code );
219 self
::preloadLanguageClass( $class );
220 if ( MWInit
::classExists( $class ) ) {
225 // Keep trying the fallback list until we find an existing class
226 $fallbacks = Language
::getFallbacksFor( $code );
227 foreach ( $fallbacks as $fallbackCode ) {
228 if ( !Language
::isValidBuiltInCode( $fallbackCode ) ) {
229 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
232 $class = self
::classFromCode( $fallbackCode );
233 self
::preloadLanguageClass( $class );
234 if ( MWInit
::classExists( $class ) ) {
235 $lang = Language
::newFromCode( $fallbackCode );
236 $lang->setCode( $code );
241 throw new MWException( "Invalid fallback sequence for language '$code'" );
245 * Returns true if a language code string is of a valid form, whether or
246 * not it exists. This includes codes which are used solely for
247 * customisation via the MediaWiki namespace.
249 * @param $code string
253 public static function isValidCode( $code ) {
255 // People think language codes are html safe, so enforce it.
256 // Ideally we should only allow a-zA-Z0-9-
257 // but, .+ and other chars are often used for {{int:}} hacks
258 // see bugs 37564, 37587, 36938
259 strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code )
260 && !preg_match( Title
::getTitleInvalidRegex(), $code );
264 * Returns true if a language code is of a valid form for the purposes of
265 * internal customisation of MediaWiki, via Messages*.php.
267 * @param $code string
269 * @throws MWException
273 public static function isValidBuiltInCode( $code ) {
275 if ( !is_string( $code ) ) {
276 $type = gettype( $code );
277 if ( $type === 'object' ) {
278 $addmsg = " of class " . get_class( $code );
282 throw new MWException( __METHOD__
. " must be passed a string, $type given$addmsg" );
285 return (bool)preg_match( '/^[a-z0-9-]+$/i', $code );
290 * @return String Name of the language class
292 public static function classFromCode( $code ) {
293 if ( $code == 'en' ) {
296 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
301 * Includes language class files
303 * @param $class string Name of the language class
305 public static function preloadLanguageClass( $class ) {
308 if ( $class === 'Language' ) {
312 if ( !defined( 'MW_COMPILED' ) ) {
313 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
314 include_once( "$IP/languages/classes/$class.php" );
320 * Get the LocalisationCache instance
322 * @return LocalisationCache
324 public static function getLocalisationCache() {
325 if ( is_null( self
::$dataCache ) ) {
326 global $wgLocalisationCacheConf;
327 $class = $wgLocalisationCacheConf['class'];
328 self
::$dataCache = new $class( $wgLocalisationCacheConf );
330 return self
::$dataCache;
333 function __construct() {
334 $this->mConverter
= new FakeConverter( $this );
335 // Set the code to the name of the descendant
336 if ( get_class( $this ) == 'Language' ) {
339 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
341 self
::getLocalisationCache();
345 * Reduce memory usage
347 function __destruct() {
348 foreach ( $this as $name => $value ) {
349 unset( $this->$name );
354 * Hook which will be called if this is the content language.
355 * Descendants can use this to register hook functions or modify globals
357 function initContLang() { }
360 * Same as getFallbacksFor for current language.
362 * @deprecated in 1.19
364 function getFallbackLanguageCode() {
365 wfDeprecated( __METHOD__
, '1.19' );
366 return self
::getFallbackFor( $this->mCode
);
373 function getFallbackLanguages() {
374 return self
::getFallbacksFor( $this->mCode
);
378 * Exports $wgBookstoreListEn
381 function getBookstoreList() {
382 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
388 public function getNamespaces() {
389 if ( is_null( $this->namespaceNames
) ) {
390 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
392 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
393 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
395 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
397 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
398 if ( $wgMetaNamespaceTalk ) {
399 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
401 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
402 $this->namespaceNames
[NS_PROJECT_TALK
] =
403 $this->fixVariableInNamespace( $talk );
406 # Sometimes a language will be localised but not actually exist on this wiki.
407 foreach ( $this->namespaceNames
as $key => $text ) {
408 if ( !isset( $validNamespaces[$key] ) ) {
409 unset( $this->namespaceNames
[$key] );
413 # The above mixing may leave namespaces out of canonical order.
414 # Re-order by namespace ID number...
415 ksort( $this->namespaceNames
);
417 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames
) );
419 return $this->namespaceNames
;
423 * Arbitrarily set all of the namespace names at once. Mainly used for testing
424 * @param $namespaces Array of namespaces (id => name)
426 public function setNamespaces( array $namespaces ) {
427 $this->namespaceNames
= $namespaces;
428 $this->mNamespaceIds
= null;
432 * Resets all of the namespace caches. Mainly used for testing
434 public function resetNamespaces( ) {
435 $this->namespaceNames
= null;
436 $this->mNamespaceIds
= null;
437 $this->namespaceAliases
= null;
441 * A convenience function that returns the same thing as
442 * getNamespaces() except with the array values changed to ' '
443 * where it found '_', useful for producing output to be displayed
444 * e.g. in <select> forms.
448 function getFormattedNamespaces() {
449 $ns = $this->getNamespaces();
450 foreach ( $ns as $k => $v ) {
451 $ns[$k] = strtr( $v, '_', ' ' );
457 * Get a namespace value by key
459 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
460 * echo $mw_ns; // prints 'MediaWiki'
463 * @param $index Int: the array key of the namespace to return
464 * @return mixed, string if the namespace value exists, otherwise false
466 function getNsText( $index ) {
467 $ns = $this->getNamespaces();
468 return isset( $ns[$index] ) ?
$ns[$index] : false;
472 * A convenience function that returns the same thing as
473 * getNsText() except with '_' changed to ' ', useful for
476 * @param $index string
480 function getFormattedNsText( $index ) {
481 $ns = $this->getNsText( $index );
482 return strtr( $ns, '_', ' ' );
486 * Returns gender-dependent namespace alias if available.
487 * @param $index Int: namespace index
488 * @param $gender String: gender key (male, female... )
492 function getGenderNsText( $index, $gender ) {
493 global $wgExtraGenderNamespaces;
495 $ns = $wgExtraGenderNamespaces + self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
496 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
500 * Whether this language makes distinguishes genders for example in
505 function needsGenderDistinction() {
506 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
507 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
508 // $wgExtraGenderNamespaces overrides everything
510 } elseif ( isset( $wgExtraNamespaces[NS_USER
] ) && isset( $wgExtraNamespaces[NS_USER_TALK
] ) ) {
511 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
512 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
515 // Check what is in i18n files
516 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
517 return count( $aliases ) > 0;
522 * Get a namespace key by value, case insensitive.
523 * Only matches namespace names for the current language, not the
524 * canonical ones defined in Namespace.php.
526 * @param $text String
527 * @return mixed An integer if $text is a valid value otherwise false
529 function getLocalNsIndex( $text ) {
530 $lctext = $this->lc( $text );
531 $ids = $this->getNamespaceIds();
532 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
538 function getNamespaceAliases() {
539 if ( is_null( $this->namespaceAliases
) ) {
540 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
544 foreach ( $aliases as $name => $index ) {
545 if ( $index === NS_PROJECT_TALK
) {
546 unset( $aliases[$name] );
547 $name = $this->fixVariableInNamespace( $name );
548 $aliases[$name] = $index;
553 global $wgExtraGenderNamespaces;
554 $genders = $wgExtraGenderNamespaces +
(array)self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
555 foreach ( $genders as $index => $forms ) {
556 foreach ( $forms as $alias ) {
557 $aliases[$alias] = $index;
561 $this->namespaceAliases
= $aliases;
563 return $this->namespaceAliases
;
569 function getNamespaceIds() {
570 if ( is_null( $this->mNamespaceIds
) ) {
571 global $wgNamespaceAliases;
572 # Put namespace names and aliases into a hashtable.
573 # If this is too slow, then we should arrange it so that it is done
574 # before caching. The catch is that at pre-cache time, the above
575 # class-specific fixup hasn't been done.
576 $this->mNamespaceIds
= array();
577 foreach ( $this->getNamespaces() as $index => $name ) {
578 $this->mNamespaceIds
[$this->lc( $name )] = $index;
580 foreach ( $this->getNamespaceAliases() as $name => $index ) {
581 $this->mNamespaceIds
[$this->lc( $name )] = $index;
583 if ( $wgNamespaceAliases ) {
584 foreach ( $wgNamespaceAliases as $name => $index ) {
585 $this->mNamespaceIds
[$this->lc( $name )] = $index;
589 return $this->mNamespaceIds
;
593 * Get a namespace key by value, case insensitive. Canonical namespace
594 * names override custom ones defined for the current language.
596 * @param $text String
597 * @return mixed An integer if $text is a valid value otherwise false
599 function getNsIndex( $text ) {
600 $lctext = $this->lc( $text );
601 $ns = MWNamespace
::getCanonicalIndex( $lctext );
602 if ( $ns !== null ) {
605 $ids = $this->getNamespaceIds();
606 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
610 * short names for language variants used for language conversion links.
612 * @param $code String
613 * @param $usemsg bool Use the "variantname-xyz" message if it exists
616 function getVariantname( $code, $usemsg = true ) {
617 $msg = "variantname-$code";
618 if ( $usemsg && wfMessage( $msg )->exists() ) {
619 return $this->getMessageFromDB( $msg );
621 $name = self
::fetchLanguageName( $code );
623 return $name; # if it's defined as a language name, show that
625 # otherwise, output the language code
631 * @param $name string
634 function specialPage( $name ) {
635 $aliases = $this->getSpecialPageAliases();
636 if ( isset( $aliases[$name][0] ) ) {
637 $name = $aliases[$name][0];
639 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
645 function getQuickbarSettings() {
647 $this->getMessage( 'qbsettings-none' ),
648 $this->getMessage( 'qbsettings-fixedleft' ),
649 $this->getMessage( 'qbsettings-fixedright' ),
650 $this->getMessage( 'qbsettings-floatingleft' ),
651 $this->getMessage( 'qbsettings-floatingright' ),
652 $this->getMessage( 'qbsettings-directionality' )
659 function getDatePreferences() {
660 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
666 function getDateFormats() {
667 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
671 * @return array|string
673 function getDefaultDateFormat() {
674 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
675 if ( $df === 'dmy or mdy' ) {
676 global $wgAmericanDates;
677 return $wgAmericanDates ?
'mdy' : 'dmy';
686 function getDatePreferenceMigrationMap() {
687 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
694 function getImageFile( $image ) {
695 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
701 function getExtraUserToggles() {
702 return (array)self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
709 function getUserToggle( $tog ) {
710 return $this->getMessageFromDB( "tog-$tog" );
714 * Get native language names, indexed by code.
715 * Only those defined in MediaWiki, no other data like CLDR.
716 * If $customisedOnly is true, only returns codes with a messages file
718 * @param $customisedOnly bool
721 * @deprecated in 1.20, use fetchLanguageNames()
723 public static function getLanguageNames( $customisedOnly = false ) {
724 return self
::fetchLanguageNames( null, $customisedOnly ?
'mwfile' : 'mw' );
728 * Get translated language names. This is done on best effort and
729 * by default this is exactly the same as Language::getLanguageNames.
730 * The CLDR extension provides translated names.
731 * @param $code String Language code.
732 * @return Array language code => language name
734 * @deprecated in 1.20, use fetchLanguageNames()
736 public static function getTranslatedLanguageNames( $code ) {
737 return self
::fetchLanguageNames( $code, 'all' );
741 * Get an array of language names, indexed by code.
742 * @param $inLanguage null|string: Code of language in which to return the names
743 * Use null for autonyms (native names)
744 * @param $include string:
745 * 'all' all available languages
746 * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
747 * 'mwfile' only if the language is in 'mw' *and* has a message file
748 * @return array: language code => language name
751 public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
752 global $wgExtraLanguageNames;
753 static $coreLanguageNames;
755 if ( $coreLanguageNames === null ) {
756 include( MWInit
::compiledPath( 'languages/Names.php' ) );
762 # TODO: also include when $inLanguage is null, when this code is more efficient
763 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
766 $mwNames = $wgExtraLanguageNames +
$coreLanguageNames;
767 foreach ( $mwNames as $mwCode => $mwName ) {
768 # - Prefer own MediaWiki native name when not using the hook
769 # - For other names just add if not added through the hook
770 if ( $mwCode === $inLanguage ||
!isset( $names[$mwCode] ) ) {
771 $names[$mwCode] = $mwName;
775 if ( $include === 'all' ) {
780 $coreCodes = array_keys( $mwNames );
781 foreach ( $coreCodes as $coreCode ) {
782 $returnMw[$coreCode] = $names[$coreCode];
785 if ( $include === 'mwfile' ) {
786 $namesMwFile = array();
787 # We do this using a foreach over the codes instead of a directory
788 # loop so that messages files in extensions will work correctly.
789 foreach ( $returnMw as $code => $value ) {
790 if ( is_readable( self
::getMessagesFileName( $code ) ) ) {
791 $namesMwFile[$code] = $names[$code];
796 # 'mw' option; default if it's not one of the other two options (all/mwfile)
801 * @param $code string: The code of the language for which to get the name
802 * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
803 * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
804 * @return string: Language name or empty
807 public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
808 $array = self
::fetchLanguageNames( $inLanguage, $include );
809 return !array_key_exists( $code, $array ) ?
'' : $array[$code];
813 * Get a message from the MediaWiki namespace.
815 * @param $msg String: message name
818 function getMessageFromDB( $msg ) {
819 return wfMessage( $msg )->inLanguage( $this )->text();
823 * Get the native language name of $code.
824 * Only if defined in MediaWiki, no other data like CLDR.
825 * @param $code string
827 * @deprecated in 1.20, use fetchLanguageName()
829 function getLanguageName( $code ) {
830 return self
::fetchLanguageName( $code );
837 function getMonthName( $key ) {
838 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
844 function getMonthNamesArray() {
845 $monthNames = array( '' );
846 for ( $i = 1; $i < 13; $i++
) {
847 $monthNames[] = $this->getMonthName( $i );
856 function getMonthNameGen( $key ) {
857 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
864 function getMonthAbbreviation( $key ) {
865 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
871 function getMonthAbbreviationsArray() {
872 $monthNames = array( '' );
873 for ( $i = 1; $i < 13; $i++
) {
874 $monthNames[] = $this->getMonthAbbreviation( $i );
883 function getWeekdayName( $key ) {
884 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
891 function getWeekdayAbbreviation( $key ) {
892 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
899 function getIranianCalendarMonthName( $key ) {
900 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
907 function getHebrewCalendarMonthName( $key ) {
908 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
915 function getHebrewCalendarMonthNameGen( $key ) {
916 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
923 function getHijriCalendarMonthName( $key ) {
924 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
928 * This is a workalike of PHP's date() function, but with better
929 * internationalisation, a reduced set of format characters, and a better
932 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
933 * PHP manual for definitions. There are a number of extensions, which
936 * xn Do not translate digits of the next numeric format character
937 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
938 * xr Use roman numerals for the next numeric format character
939 * xh Use hebrew numerals for the next numeric format character
941 * xg Genitive month name
943 * xij j (day number) in Iranian calendar
944 * xiF F (month name) in Iranian calendar
945 * xin n (month number) in Iranian calendar
946 * xiy y (two digit year) in Iranian calendar
947 * xiY Y (full year) in Iranian calendar
949 * xjj j (day number) in Hebrew calendar
950 * xjF F (month name) in Hebrew calendar
951 * xjt t (days in month) in Hebrew calendar
952 * xjx xg (genitive month name) in Hebrew calendar
953 * xjn n (month number) in Hebrew calendar
954 * xjY Y (full year) in Hebrew calendar
956 * xmj j (day number) in Hijri calendar
957 * xmF F (month name) in Hijri calendar
958 * xmn n (month number) in Hijri calendar
959 * xmY Y (full year) in Hijri calendar
961 * xkY Y (full year) in Thai solar calendar. Months and days are
962 * identical to the Gregorian calendar
963 * xoY Y (full year) in Minguo calendar or Juche year.
964 * Months and days are identical to the
966 * xtY Y (full year) in Japanese nengo. Months and days are
967 * identical to the Gregorian calendar
969 * Characters enclosed in double quotes will be considered literal (with
970 * the quotes themselves removed). Unmatched quotes will be considered
971 * literal quotes. Example:
973 * "The month is" F => The month is January
976 * Backslash escaping is also supported.
978 * Input timestamp is assumed to be pre-normalized to the desired local
981 * @param $format String
982 * @param $ts String: 14-character timestamp
985 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
989 function sprintfDate( $format, $ts ) {
1002 for ( $p = 0; $p < strlen( $format ); $p++
) {
1004 $code = $format[$p];
1005 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
1006 $code .= $format[++
$p];
1009 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
1010 $code .= $format[++
$p];
1021 $rawToggle = !$rawToggle;
1030 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
1033 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
1034 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
1037 $num = substr( $ts, 6, 2 );
1040 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
1041 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) +
1 );
1044 $num = intval( substr( $ts, 6, 2 ) );
1048 $iranian = self
::tsToIranian( $ts );
1054 $hijri = self
::tsToHijri( $ts );
1060 $hebrew = self
::tsToHebrew( $ts );
1066 $unix = wfTimestamp( TS_UNIX
, $ts );
1068 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) +
1 );
1072 $unix = wfTimestamp( TS_UNIX
, $ts );
1074 $w = gmdate( 'w', $unix );
1079 $unix = wfTimestamp( TS_UNIX
, $ts );
1081 $num = gmdate( 'w', $unix );
1085 $unix = wfTimestamp( TS_UNIX
, $ts );
1087 $num = gmdate( 'z', $unix );
1091 $unix = wfTimestamp( TS_UNIX
, $ts );
1093 $num = gmdate( 'W', $unix );
1096 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1100 $iranian = self
::tsToIranian( $ts );
1102 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1106 $hijri = self
::tsToHijri( $ts );
1108 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1112 $hebrew = self
::tsToHebrew( $ts );
1114 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1117 $num = substr( $ts, 4, 2 );
1120 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1123 $num = intval( substr( $ts, 4, 2 ) );
1127 $iranian = self
::tsToIranian( $ts );
1133 $hijri = self
::tsToHijri ( $ts );
1139 $hebrew = self
::tsToHebrew( $ts );
1145 $unix = wfTimestamp( TS_UNIX
, $ts );
1147 $num = gmdate( 't', $unix );
1151 $hebrew = self
::tsToHebrew( $ts );
1157 $unix = wfTimestamp( TS_UNIX
, $ts );
1159 $num = gmdate( 'L', $unix );
1163 $unix = wfTimestamp( TS_UNIX
, $ts );
1165 $num = gmdate( 'o', $unix );
1168 $num = substr( $ts, 0, 4 );
1172 $iranian = self
::tsToIranian( $ts );
1178 $hijri = self
::tsToHijri( $ts );
1184 $hebrew = self
::tsToHebrew( $ts );
1190 $thai = self
::tsToYear( $ts, 'thai' );
1196 $minguo = self
::tsToYear( $ts, 'minguo' );
1202 $tenno = self
::tsToYear( $ts, 'tenno' );
1207 $num = substr( $ts, 2, 2 );
1211 $iranian = self
::tsToIranian( $ts );
1213 $num = substr( $iranian[0], -2 );
1216 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
1219 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
1222 $h = substr( $ts, 8, 2 );
1223 $num = $h %
12 ?
$h %
12 : 12;
1226 $num = intval( substr( $ts, 8, 2 ) );
1229 $h = substr( $ts, 8, 2 );
1230 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
1233 $num = substr( $ts, 8, 2 );
1236 $num = substr( $ts, 10, 2 );
1239 $num = substr( $ts, 12, 2 );
1243 $unix = wfTimestamp( TS_UNIX
, $ts );
1245 $s .= gmdate( 'c', $unix );
1249 $unix = wfTimestamp( TS_UNIX
, $ts );
1251 $s .= gmdate( 'r', $unix );
1255 $unix = wfTimestamp( TS_UNIX
, $ts );
1260 # Backslash escaping
1261 if ( $p < strlen( $format ) - 1 ) {
1262 $s .= $format[++
$p];
1269 if ( $p < strlen( $format ) - 1 ) {
1270 $endQuote = strpos( $format, '"', $p +
1 );
1271 if ( $endQuote === false ) {
1272 # No terminating quote, assume literal "
1275 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1279 # Quote at end of string, assume literal "
1286 if ( $num !== false ) {
1287 if ( $rawToggle ||
$raw ) {
1290 } elseif ( $roman ) {
1291 $s .= Language
::romanNumeral( $num );
1293 } elseif ( $hebrewNum ) {
1294 $s .= self
::hebrewNumeral( $num );
1297 $s .= $this->formatNum( $num, true );
1304 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1305 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1308 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1309 * Gregorian dates to Iranian dates. Originally written in C, it
1310 * is released under the terms of GNU Lesser General Public
1311 * License. Conversion to PHP was performed by Niklas Laxström.
1313 * Link: http://www.farsiweb.info/jalali/jalali.c
1319 private static function tsToIranian( $ts ) {
1320 $gy = substr( $ts, 0, 4 ) -1600;
1321 $gm = substr( $ts, 4, 2 ) -1;
1322 $gd = substr( $ts, 6, 2 ) -1;
1324 # Days passed from the beginning (including leap years)
1326 +
floor( ( $gy +
3 ) / 4 )
1327 - floor( ( $gy +
99 ) / 100 )
1328 +
floor( ( $gy +
399 ) / 400 );
1330 // Add days of the past months of this year
1331 for ( $i = 0; $i < $gm; $i++
) {
1332 $gDayNo +
= self
::$GREG_DAYS[$i];
1336 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1340 // Days passed in current month
1341 $gDayNo +
= (int)$gd;
1343 $jDayNo = $gDayNo - 79;
1345 $jNp = floor( $jDayNo / 12053 );
1348 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1351 if ( $jDayNo >= 366 ) {
1352 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1353 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1356 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1357 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1363 return array( $jy, $jm, $jd );
1367 * Converting Gregorian dates to Hijri dates.
1369 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1371 * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1377 private static function tsToHijri( $ts ) {
1378 $year = substr( $ts, 0, 4 );
1379 $month = substr( $ts, 4, 2 );
1380 $day = substr( $ts, 6, 2 );
1388 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1389 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1392 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1393 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1394 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1397 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1398 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1401 $zl = $zjd -1948440 +
10632;
1402 $zn = (int)( ( $zl - 1 ) / 10631 );
1403 $zl = $zl - 10631 * $zn +
354;
1404 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1405 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1406 $zm = (int)( ( 24 * $zl ) / 709 );
1407 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1408 $zy = 30 * $zn +
$zj - 30;
1410 return array( $zy, $zm, $zd );
1414 * Converting Gregorian dates to Hebrew dates.
1416 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1417 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1418 * to translate the relevant functions into PHP and release them under
1421 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1422 * and Adar II is 14. In a non-leap year, Adar is 6.
1428 private static function tsToHebrew( $ts ) {
1430 $year = substr( $ts, 0, 4 );
1431 $month = substr( $ts, 4, 2 );
1432 $day = substr( $ts, 6, 2 );
1434 # Calculate Hebrew year
1435 $hebrewYear = $year +
3760;
1437 # Month number when September = 1, August = 12
1439 if ( $month > 12 ) {
1446 # Calculate day of year from 1 September
1448 for ( $i = 1; $i < $month; $i++
) {
1452 # Check if the year is leap
1453 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1456 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1463 # Calculate the start of the Hebrew year
1464 $start = self
::hebrewYearStart( $hebrewYear );
1466 # Calculate next year's start
1467 if ( $dayOfYear <= $start ) {
1468 # Day is before the start of the year - it is the previous year
1470 $nextStart = $start;
1474 # Add days since previous year's 1 September
1476 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1480 # Start of the new (previous) year
1481 $start = self
::hebrewYearStart( $hebrewYear );
1484 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1487 # Calculate Hebrew day of year
1488 $hebrewDayOfYear = $dayOfYear - $start;
1490 # Difference between year's days
1491 $diff = $nextStart - $start;
1492 # Add 12 (or 13 for leap years) days to ignore the difference between
1493 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1494 # difference is only about the year type
1495 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1501 # Check the year pattern, and is leap year
1502 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1503 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1504 # and non-leap years
1505 $yearPattern = $diff %
30;
1506 # Check if leap year
1507 $isLeap = $diff >= 30;
1509 # Calculate day in the month from number of day in the Hebrew year
1510 # Don't check Adar - if the day is not in Adar, we will stop before;
1511 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1512 $hebrewDay = $hebrewDayOfYear;
1515 while ( $hebrewMonth <= 12 ) {
1516 # Calculate days in this month
1517 if ( $isLeap && $hebrewMonth == 6 ) {
1518 # Adar in a leap year
1520 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1522 if ( $hebrewDay <= $days ) {
1526 # Subtract the days of Adar I
1527 $hebrewDay -= $days;
1530 if ( $hebrewDay <= $days ) {
1536 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1537 # Cheshvan in a complete year (otherwise as the rule below)
1539 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1540 # Kislev in an incomplete year (otherwise as the rule below)
1543 # Odd months have 30 days, even have 29
1544 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1546 if ( $hebrewDay <= $days ) {
1547 # In the current month
1550 # Subtract the days of the current month
1551 $hebrewDay -= $days;
1552 # Try in the next month
1557 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1561 * This calculates the Hebrew year start, as days since 1 September.
1562 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1563 * Used for Hebrew date.
1569 private static function hebrewYearStart( $year ) {
1570 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1571 $b = intval( ( $year - 1 ) %
4 );
1572 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1576 $Mar = intval( $m );
1582 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1583 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1585 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1587 } elseif ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1591 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1596 * Algorithm to convert Gregorian dates to Thai solar dates,
1597 * Minguo dates or Minguo dates.
1599 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1600 * http://en.wikipedia.org/wiki/Minguo_calendar
1601 * http://en.wikipedia.org/wiki/Japanese_era_name
1603 * @param $ts String: 14-character timestamp
1604 * @param $cName String: calender name
1605 * @return Array: converted year, month, day
1607 private static function tsToYear( $ts, $cName ) {
1608 $gy = substr( $ts, 0, 4 );
1609 $gm = substr( $ts, 4, 2 );
1610 $gd = substr( $ts, 6, 2 );
1612 if ( !strcmp( $cName, 'thai' ) ) {
1614 # Add 543 years to the Gregorian calendar
1615 # Months and days are identical
1616 $gy_offset = $gy +
543;
1617 } elseif ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1619 # Deduct 1911 years from the Gregorian calendar
1620 # Months and days are identical
1621 $gy_offset = $gy - 1911;
1622 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1623 # Nengō dates up to Meiji period
1624 # Deduct years from the Gregorian calendar
1625 # depending on the nengo periods
1626 # Months and days are identical
1627 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1629 $gy_gannen = $gy - 1868 +
1;
1630 $gy_offset = $gy_gannen;
1631 if ( $gy_gannen == 1 ) {
1634 $gy_offset = '明治' . $gy_offset;
1636 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1637 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1638 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1639 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1640 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1644 $gy_gannen = $gy - 1912 +
1;
1645 $gy_offset = $gy_gannen;
1646 if ( $gy_gannen == 1 ) {
1649 $gy_offset = '大正' . $gy_offset;
1651 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1652 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1653 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1657 $gy_gannen = $gy - 1926 +
1;
1658 $gy_offset = $gy_gannen;
1659 if ( $gy_gannen == 1 ) {
1662 $gy_offset = '昭和' . $gy_offset;
1665 $gy_gannen = $gy - 1989 +
1;
1666 $gy_offset = $gy_gannen;
1667 if ( $gy_gannen == 1 ) {
1670 $gy_offset = '平成' . $gy_offset;
1676 return array( $gy_offset, $gm, $gd );
1680 * Roman number formatting up to 10000
1686 static function romanNumeral( $num ) {
1687 static $table = array(
1688 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1689 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1690 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1691 array( '', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM', 'MMMMMM', 'MMMMMMM', 'MMMMMMMM', 'MMMMMMMMM', 'MMMMMMMMMM' )
1694 $num = intval( $num );
1695 if ( $num > 10000 ||
$num <= 0 ) {
1700 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1701 if ( $num >= $pow10 ) {
1702 $s .= $table[$i][(int)floor( $num / $pow10 )];
1704 $num = $num %
$pow10;
1710 * Hebrew Gematria number formatting up to 9999
1716 static function hebrewNumeral( $num ) {
1717 static $table = array(
1718 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1719 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1720 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1721 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1724 $num = intval( $num );
1725 if ( $num > 9999 ||
$num <= 0 ) {
1730 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1731 if ( $num >= $pow10 ) {
1732 if ( $num == 15 ||
$num == 16 ) {
1733 $s .= $table[0][9] . $table[0][$num - 9];
1736 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1737 if ( $pow10 == 1000 ) {
1742 $num = $num %
$pow10;
1744 if ( strlen( $s ) == 2 ) {
1747 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1748 $str .= substr( $s, strlen( $s ) - 2, 2 );
1750 $start = substr( $str, 0, strlen( $str ) - 2 );
1751 $end = substr( $str, strlen( $str ) - 2 );
1754 $str = $start . 'ך';
1757 $str = $start . 'ם';
1760 $str = $start . 'ן';
1763 $str = $start . 'ף';
1766 $str = $start . 'ץ';
1773 * Used by date() and time() to adjust the time output.
1775 * @param $ts Int the time in date('YmdHis') format
1776 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1777 * get user timecorrection setting)
1780 function userAdjust( $ts, $tz = false ) {
1781 global $wgUser, $wgLocalTZoffset;
1783 if ( $tz === false ) {
1784 $tz = $wgUser->getOption( 'timecorrection' );
1787 $data = explode( '|', $tz, 3 );
1789 if ( $data[0] == 'ZoneInfo' ) {
1790 wfSuppressWarnings();
1791 $userTZ = timezone_open( $data[2] );
1792 wfRestoreWarnings();
1793 if ( $userTZ !== false ) {
1794 $date = date_create( $ts, timezone_open( 'UTC' ) );
1795 date_timezone_set( $date, $userTZ );
1796 $date = date_format( $date, 'YmdHis' );
1799 # Unrecognized timezone, default to 'Offset' with the stored offset.
1800 $data[0] = 'Offset';
1804 if ( $data[0] == 'System' ||
$tz == '' ) {
1805 # Global offset in minutes.
1806 if ( isset( $wgLocalTZoffset ) ) {
1807 $minDiff = $wgLocalTZoffset;
1809 } elseif ( $data[0] == 'Offset' ) {
1810 $minDiff = intval( $data[1] );
1812 $data = explode( ':', $tz );
1813 if ( count( $data ) == 2 ) {
1814 $data[0] = intval( $data[0] );
1815 $data[1] = intval( $data[1] );
1816 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1817 if ( $data[0] < 0 ) {
1818 $minDiff = -$minDiff;
1821 $minDiff = intval( $data[0] ) * 60;
1825 # No difference ? Return time unchanged
1826 if ( 0 == $minDiff ) {
1830 wfSuppressWarnings(); // E_STRICT system time bitching
1831 # Generate an adjusted date; take advantage of the fact that mktime
1832 # will normalize out-of-range values so we don't have to split $minDiff
1833 # into hours and minutes.
1835 (int)substr( $ts, 8, 2 ) ), # Hours
1836 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
1837 (int)substr( $ts, 12, 2 ), # Seconds
1838 (int)substr( $ts, 4, 2 ), # Month
1839 (int)substr( $ts, 6, 2 ), # Day
1840 (int)substr( $ts, 0, 4 ) ); # Year
1842 $date = date( 'YmdHis', $t );
1843 wfRestoreWarnings();
1849 * This is meant to be used by time(), date(), and timeanddate() to get
1850 * the date preference they're supposed to use, it should be used in
1854 * function timeanddate([...], $format = true) {
1855 * $datePreference = $this->dateFormat($format);
1860 * @param $usePrefs Mixed: if true, the user's preference is used
1861 * if false, the site/language default is used
1862 * if int/string, assumed to be a format.
1865 function dateFormat( $usePrefs = true ) {
1868 if ( is_bool( $usePrefs ) ) {
1870 $datePreference = $wgUser->getDatePreference();
1872 $datePreference = (string)User
::getDefaultOption( 'date' );
1875 $datePreference = (string)$usePrefs;
1879 if ( $datePreference == '' ) {
1883 return $datePreference;
1887 * Get a format string for a given type and preference
1888 * @param $type string May be date, time or both
1889 * @param $pref string The format name as it appears in Messages*.php
1893 function getDateFormatString( $type, $pref ) {
1894 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
1895 if ( $pref == 'default' ) {
1896 $pref = $this->getDefaultDateFormat();
1897 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1899 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1900 if ( is_null( $df ) ) {
1901 $pref = $this->getDefaultDateFormat();
1902 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1905 $this->dateFormatStrings
[$type][$pref] = $df;
1907 return $this->dateFormatStrings
[$type][$pref];
1911 * @param $ts Mixed: the time format which needs to be turned into a
1912 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1913 * @param $adj Bool: whether to adjust the time output according to the
1914 * user configured offset ($timecorrection)
1915 * @param $format Mixed: true to use user's date format preference
1916 * @param $timecorrection String|bool the time offset as returned by
1917 * validateTimeZone() in Special:Preferences
1920 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1921 $ts = wfTimestamp( TS_MW
, $ts );
1923 $ts = $this->userAdjust( $ts, $timecorrection );
1925 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1926 return $this->sprintfDate( $df, $ts );
1930 * @param $ts Mixed: the time format which needs to be turned into a
1931 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1932 * @param $adj Bool: whether to adjust the time output according to the
1933 * user configured offset ($timecorrection)
1934 * @param $format Mixed: true to use user's date format preference
1935 * @param $timecorrection String|bool the time offset as returned by
1936 * validateTimeZone() in Special:Preferences
1939 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1940 $ts = wfTimestamp( TS_MW
, $ts );
1942 $ts = $this->userAdjust( $ts, $timecorrection );
1944 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1945 return $this->sprintfDate( $df, $ts );
1949 * @param $ts Mixed: the time format which needs to be turned into a
1950 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1951 * @param $adj Bool: whether to adjust the time output according to the
1952 * user configured offset ($timecorrection)
1953 * @param $format Mixed: what format to return, if it's false output the
1954 * default one (default true)
1955 * @param $timecorrection String|bool the time offset as returned by
1956 * validateTimeZone() in Special:Preferences
1959 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1960 $ts = wfTimestamp( TS_MW
, $ts );
1962 $ts = $this->userAdjust( $ts, $timecorrection );
1964 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1965 return $this->sprintfDate( $df, $ts );
1969 * Takes a number of seconds and turns it into a text using values such as hours and minutes.
1973 * @param integer $seconds The amount of seconds.
1974 * @param array $chosenIntervals The intervals to enable.
1978 public function formatDuration( $seconds, array $chosenIntervals = array() ) {
1979 $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
1981 $segments = array();
1983 foreach ( $intervals as $intervalName => $intervalValue ) {
1984 $message = new Message( 'duration-' . $intervalName, array( $intervalValue ) );
1985 $segments[] = $message->inLanguage( $this )->escaped();
1988 return $this->listToText( $segments );
1992 * Takes a number of seconds and returns an array with a set of corresponding intervals.
1993 * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
1997 * @param integer $seconds The amount of seconds.
1998 * @param array $chosenIntervals The intervals to enable.
2002 public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
2003 if ( empty( $chosenIntervals ) ) {
2004 $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
2007 $intervals = array_intersect_key( self
::$durationIntervals, array_flip( $chosenIntervals ) );
2008 $sortedNames = array_keys( $intervals );
2009 $smallestInterval = array_pop( $sortedNames );
2011 $segments = array();
2013 foreach ( $intervals as $name => $length ) {
2014 $value = floor( $seconds / $length );
2016 if ( $value > 0 ||
( $name == $smallestInterval && empty( $segments ) ) ) {
2017 $seconds -= $value * $length;
2018 $segments[$name] = $value;
2026 * Internal helper function for userDate(), userTime() and userTimeAndDate()
2028 * @param $type String: can be 'date', 'time' or 'both'
2029 * @param $ts Mixed: the time format which needs to be turned into a
2030 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2031 * @param $user User object used to get preferences for timezone and format
2032 * @param $options Array, can contain the following keys:
2033 * - 'timecorrection': time correction, can have the following values:
2034 * - true: use user's preference
2035 * - false: don't use time correction
2036 * - integer: value of time correction in minutes
2037 * - 'format': format to use, can have the following values:
2038 * - true: use user's preference
2039 * - false: use default preference
2040 * - string: format to use
2044 private function internalUserTimeAndDate( $type, $ts, User
$user, array $options ) {
2045 $ts = wfTimestamp( TS_MW
, $ts );
2046 $options +
= array( 'timecorrection' => true, 'format' => true );
2047 if ( $options['timecorrection'] !== false ) {
2048 if ( $options['timecorrection'] === true ) {
2049 $offset = $user->getOption( 'timecorrection' );
2051 $offset = $options['timecorrection'];
2053 $ts = $this->userAdjust( $ts, $offset );
2055 if ( $options['format'] === true ) {
2056 $format = $user->getDatePreference();
2058 $format = $options['format'];
2060 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
2061 return $this->sprintfDate( $df, $ts );
2065 * Get the formatted date for the given timestamp and formatted for
2068 * @param $ts Mixed: the time format which needs to be turned into a
2069 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2070 * @param $user User object used to get preferences for timezone and format
2071 * @param $options Array, can contain the following keys:
2072 * - 'timecorrection': time correction, can have the following values:
2073 * - true: use user's preference
2074 * - false: don't use time correction
2075 * - integer: value of time correction in minutes
2076 * - 'format': format to use, can have the following values:
2077 * - true: use user's preference
2078 * - false: use default preference
2079 * - string: format to use
2083 public function userDate( $ts, User
$user, array $options = array() ) {
2084 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
2088 * Get the formatted time for the given timestamp and formatted for
2091 * @param $ts Mixed: the time format which needs to be turned into a
2092 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2093 * @param $user User object used to get preferences for timezone and format
2094 * @param $options Array, can contain the following keys:
2095 * - 'timecorrection': time correction, can have the following values:
2096 * - true: use user's preference
2097 * - false: don't use time correction
2098 * - integer: value of time correction in minutes
2099 * - 'format': format to use, can have the following values:
2100 * - true: use user's preference
2101 * - false: use default preference
2102 * - string: format to use
2106 public function userTime( $ts, User
$user, array $options = array() ) {
2107 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
2111 * Get the formatted date and time for the given timestamp and formatted for
2114 * @param $ts Mixed: the time format which needs to be turned into a
2115 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2116 * @param $user User object used to get preferences for timezone and format
2117 * @param $options Array, can contain the following keys:
2118 * - 'timecorrection': time correction, can have the following values:
2119 * - true: use user's preference
2120 * - false: don't use time correction
2121 * - integer: value of time correction in minutes
2122 * - 'format': format to use, can have the following values:
2123 * - true: use user's preference
2124 * - false: use default preference
2125 * - string: format to use
2129 public function userTimeAndDate( $ts, User
$user, array $options = array() ) {
2130 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
2134 * @param $key string
2135 * @return array|null
2137 function getMessage( $key ) {
2138 return self
::$dataCache->getSubitem( $this->mCode
, 'messages', $key );
2144 function getAllMessages() {
2145 return self
::$dataCache->getItem( $this->mCode
, 'messages' );
2154 function iconv( $in, $out, $string ) {
2155 # This is a wrapper for iconv in all languages except esperanto,
2156 # which does some nasty x-conversions beforehand
2158 # Even with //IGNORE iconv can whine about illegal characters in
2159 # *input* string. We just ignore those too.
2160 # REF: http://bugs.php.net/bug.php?id=37166
2161 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
2162 wfSuppressWarnings();
2163 $text = iconv( $in, $out . '//IGNORE', $string );
2164 wfRestoreWarnings();
2168 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
2171 * @param $matches array
2172 * @return mixed|string
2174 function ucwordbreaksCallbackAscii( $matches ) {
2175 return $this->ucfirst( $matches[1] );
2179 * @param $matches array
2182 function ucwordbreaksCallbackMB( $matches ) {
2183 return mb_strtoupper( $matches[0] );
2187 * @param $matches array
2190 function ucCallback( $matches ) {
2191 list( $wikiUpperChars ) = self
::getCaseMaps();
2192 return strtr( $matches[1], $wikiUpperChars );
2196 * @param $matches array
2199 function lcCallback( $matches ) {
2200 list( , $wikiLowerChars ) = self
::getCaseMaps();
2201 return strtr( $matches[1], $wikiLowerChars );
2205 * @param $matches array
2208 function ucwordsCallbackMB( $matches ) {
2209 return mb_strtoupper( $matches[0] );
2213 * @param $matches array
2216 function ucwordsCallbackWiki( $matches ) {
2217 list( $wikiUpperChars ) = self
::getCaseMaps();
2218 return strtr( $matches[0], $wikiUpperChars );
2222 * Make a string's first character uppercase
2224 * @param $str string
2228 function ucfirst( $str ) {
2230 if ( $o < 96 ) { // if already uppercase...
2232 } elseif ( $o < 128 ) {
2233 return ucfirst( $str ); // use PHP's ucfirst()
2235 // fall back to more complex logic in case of multibyte strings
2236 return $this->uc( $str, true );
2241 * Convert a string to uppercase
2243 * @param $str string
2244 * @param $first bool
2248 function uc( $str, $first = false ) {
2249 if ( function_exists( 'mb_strtoupper' ) ) {
2251 if ( $this->isMultibyte( $str ) ) {
2252 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2254 return ucfirst( $str );
2257 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
2260 if ( $this->isMultibyte( $str ) ) {
2261 $x = $first ?
'^' : '';
2262 return preg_replace_callback(
2263 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2264 array( $this, 'ucCallback' ),
2268 return $first ?
ucfirst( $str ) : strtoupper( $str );
2274 * @param $str string
2275 * @return mixed|string
2277 function lcfirst( $str ) {
2280 return strval( $str );
2281 } elseif ( $o >= 128 ) {
2282 return $this->lc( $str, true );
2283 } elseif ( $o > 96 ) {
2286 $str[0] = strtolower( $str[0] );
2292 * @param $str string
2293 * @param $first bool
2294 * @return mixed|string
2296 function lc( $str, $first = false ) {
2297 if ( function_exists( 'mb_strtolower' ) ) {
2299 if ( $this->isMultibyte( $str ) ) {
2300 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2302 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2305 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
2308 if ( $this->isMultibyte( $str ) ) {
2309 $x = $first ?
'^' : '';
2310 return preg_replace_callback(
2311 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2312 array( $this, 'lcCallback' ),
2316 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2322 * @param $str string
2325 function isMultibyte( $str ) {
2326 return (bool)preg_match( '/[\x80-\xff]/', $str );
2330 * @param $str string
2331 * @return mixed|string
2333 function ucwords( $str ) {
2334 if ( $this->isMultibyte( $str ) ) {
2335 $str = $this->lc( $str );
2337 // regexp to find first letter in each word (i.e. after each space)
2338 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2340 // function to use to capitalize a single char
2341 if ( function_exists( 'mb_strtoupper' ) ) {
2342 return preg_replace_callback(
2344 array( $this, 'ucwordsCallbackMB' ),
2348 return preg_replace_callback(
2350 array( $this, 'ucwordsCallbackWiki' ),
2355 return ucwords( strtolower( $str ) );
2360 * capitalize words at word breaks
2362 * @param $str string
2365 function ucwordbreaks( $str ) {
2366 if ( $this->isMultibyte( $str ) ) {
2367 $str = $this->lc( $str );
2369 // since \b doesn't work for UTF-8, we explicitely define word break chars
2370 $breaks = "[ \-\(\)\}\{\.,\?!]";
2372 // find first letter after word break
2373 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2375 if ( function_exists( 'mb_strtoupper' ) ) {
2376 return preg_replace_callback(
2378 array( $this, 'ucwordbreaksCallbackMB' ),
2382 return preg_replace_callback(
2384 array( $this, 'ucwordsCallbackWiki' ),
2389 return preg_replace_callback(
2390 '/\b([\w\x80-\xff]+)\b/',
2391 array( $this, 'ucwordbreaksCallbackAscii' ),
2398 * Return a case-folded representation of $s
2400 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2401 * and $s2 are the same except for the case of their characters. It is not
2402 * necessary for the value returned to make sense when displayed.
2404 * Do *not* perform any other normalisation in this function. If a caller
2405 * uses this function when it should be using a more general normalisation
2406 * function, then fix the caller.
2412 function caseFold( $s ) {
2413 return $this->uc( $s );
2420 function checkTitleEncoding( $s ) {
2421 if ( is_array( $s ) ) {
2422 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2424 # Check for non-UTF-8 URLs
2425 $ishigh = preg_match( '/[\x80-\xff]/', $s );
2430 if ( function_exists( 'mb_check_encoding' ) ) {
2431 $isutf8 = mb_check_encoding( $s, 'UTF-8' );
2433 $isutf8 = preg_match( '/^(?>[\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2434 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
2440 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2446 function fallback8bitEncoding() {
2447 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
2451 * Most writing systems use whitespace to break up words.
2452 * Some languages such as Chinese don't conventionally do this,
2453 * which requires special handling when breaking up words for
2458 function hasWordBreaks() {
2463 * Some languages such as Chinese require word segmentation,
2464 * Specify such segmentation when overridden in derived class.
2466 * @param $string String
2469 function segmentByWord( $string ) {
2474 * Some languages have special punctuation need to be normalized.
2475 * Make such changes here.
2477 * @param $string String
2480 function normalizeForSearch( $string ) {
2481 return self
::convertDoubleWidth( $string );
2485 * convert double-width roman characters to single-width.
2486 * range: ff00-ff5f ~= 0020-007f
2488 * @param $string string
2492 protected static function convertDoubleWidth( $string ) {
2493 static $full = null;
2494 static $half = null;
2496 if ( $full === null ) {
2497 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2498 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2499 $full = str_split( $fullWidth, 3 );
2500 $half = str_split( $halfWidth );
2503 $string = str_replace( $full, $half, $string );
2508 * @param $string string
2509 * @param $pattern string
2512 protected static function insertSpace( $string, $pattern ) {
2513 $string = preg_replace( $pattern, " $1 ", $string );
2514 $string = preg_replace( '/ +/', ' ', $string );
2519 * @param $termsArray array
2522 function convertForSearchResult( $termsArray ) {
2523 # some languages, e.g. Chinese, need to do a conversion
2524 # in order for search results to be displayed correctly
2529 * Get the first character of a string.
2534 function firstChar( $s ) {
2537 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2538 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2543 if ( isset( $matches[1] ) ) {
2544 if ( strlen( $matches[1] ) != 3 ) {
2548 // Break down Hangul syllables to grab the first jamo
2549 $code = utf8ToCodepoint( $matches[1] );
2550 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
2552 } elseif ( $code < 0xb098 ) {
2553 return "\xe3\x84\xb1";
2554 } elseif ( $code < 0xb2e4 ) {
2555 return "\xe3\x84\xb4";
2556 } elseif ( $code < 0xb77c ) {
2557 return "\xe3\x84\xb7";
2558 } elseif ( $code < 0xb9c8 ) {
2559 return "\xe3\x84\xb9";
2560 } elseif ( $code < 0xbc14 ) {
2561 return "\xe3\x85\x81";
2562 } elseif ( $code < 0xc0ac ) {
2563 return "\xe3\x85\x82";
2564 } elseif ( $code < 0xc544 ) {
2565 return "\xe3\x85\x85";
2566 } elseif ( $code < 0xc790 ) {
2567 return "\xe3\x85\x87";
2568 } elseif ( $code < 0xcc28 ) {
2569 return "\xe3\x85\x88";
2570 } elseif ( $code < 0xce74 ) {
2571 return "\xe3\x85\x8a";
2572 } elseif ( $code < 0xd0c0 ) {
2573 return "\xe3\x85\x8b";
2574 } elseif ( $code < 0xd30c ) {
2575 return "\xe3\x85\x8c";
2576 } elseif ( $code < 0xd558 ) {
2577 return "\xe3\x85\x8d";
2579 return "\xe3\x85\x8e";
2586 function initEncoding() {
2587 # Some languages may have an alternate char encoding option
2588 # (Esperanto X-coding, Japanese furigana conversion, etc)
2589 # If this language is used as the primary content language,
2590 # an override to the defaults can be set here on startup.
2597 function recodeForEdit( $s ) {
2598 # For some languages we'll want to explicitly specify
2599 # which characters make it into the edit box raw
2600 # or are converted in some way or another.
2601 global $wgEditEncoding;
2602 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
2605 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2613 function recodeInput( $s ) {
2614 # Take the previous into account.
2615 global $wgEditEncoding;
2616 if ( $wgEditEncoding != '' ) {
2617 $enc = $wgEditEncoding;
2621 if ( $enc == 'UTF-8' ) {
2624 return $this->iconv( $enc, 'UTF-8', $s );
2629 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2630 * also cleans up certain backwards-compatible sequences, converting them
2631 * to the modern Unicode equivalent.
2633 * This is language-specific for performance reasons only.
2639 function normalize( $s ) {
2640 global $wgAllUnicodeFixes;
2641 $s = UtfNormal
::cleanUp( $s );
2642 if ( $wgAllUnicodeFixes ) {
2643 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2644 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2651 * Transform a string using serialized data stored in the given file (which
2652 * must be in the serialized subdirectory of $IP). The file contains pairs
2653 * mapping source characters to destination characters.
2655 * The data is cached in process memory. This will go faster if you have the
2656 * FastStringSearch extension.
2658 * @param $file string
2659 * @param $string string
2661 * @throws MWException
2664 function transformUsingPairFile( $file, $string ) {
2665 if ( !isset( $this->transformData
[$file] ) ) {
2666 $data = wfGetPrecompiledData( $file );
2667 if ( $data === false ) {
2668 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2670 $this->transformData
[$file] = new ReplacementArray( $data );
2672 return $this->transformData
[$file]->replace( $string );
2676 * For right-to-left language support
2681 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2685 * Return the correct HTML 'dir' attribute value for this language.
2689 return $this->isRTL() ?
'rtl' : 'ltr';
2693 * Return 'left' or 'right' as appropriate alignment for line-start
2694 * for this language's text direction.
2696 * Should be equivalent to CSS3 'start' text-align value....
2700 function alignStart() {
2701 return $this->isRTL() ?
'right' : 'left';
2705 * Return 'right' or 'left' as appropriate alignment for line-end
2706 * for this language's text direction.
2708 * Should be equivalent to CSS3 'end' text-align value....
2712 function alignEnd() {
2713 return $this->isRTL() ?
'left' : 'right';
2717 * A hidden direction mark (LRM or RLM), depending on the language direction.
2718 * Unlike getDirMark(), this function returns the character as an HTML entity.
2719 * This function should be used when the output is guaranteed to be HTML,
2720 * because it makes the output HTML source code more readable. When
2721 * the output is plain text or can be escaped, getDirMark() should be used.
2723 * @param $opposite Boolean Get the direction mark opposite to your language
2727 function getDirMarkEntity( $opposite = false ) {
2728 if ( $opposite ) { return $this->isRTL() ?
'‎' : '‏'; }
2729 return $this->isRTL() ?
'‏' : '‎';
2733 * A hidden direction mark (LRM or RLM), depending on the language direction.
2734 * This function produces them as invisible Unicode characters and
2735 * the output may be hard to read and debug, so it should only be used
2736 * when the output is plain text or can be escaped. When the output is
2737 * HTML, use getDirMarkEntity() instead.
2739 * @param $opposite Boolean Get the direction mark opposite to your language
2742 function getDirMark( $opposite = false ) {
2743 $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
2744 $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
2745 if ( $opposite ) { return $this->isRTL() ?
$lrm : $rlm; }
2746 return $this->isRTL() ?
$rlm : $lrm;
2752 function capitalizeAllNouns() {
2753 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2757 * An arrow, depending on the language direction.
2759 * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
2762 function getArrow( $direction = 'forwards' ) {
2763 switch ( $direction ) {
2765 return $this->isRTL() ?
'←' : '→';
2767 return $this->isRTL() ?
'→' : '←';
2780 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2784 function linkPrefixExtension() {
2785 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2791 function getMagicWords() {
2792 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2795 protected function doMagicHook() {
2796 if ( $this->mMagicHookDone
) {
2799 $this->mMagicHookDone
= true;
2800 wfProfileIn( 'LanguageGetMagic' );
2801 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2802 wfProfileOut( 'LanguageGetMagic' );
2806 * Fill a MagicWord object with data from here
2810 function getMagic( $mw ) {
2811 $this->doMagicHook();
2813 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
2814 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
2816 $magicWords = $this->getMagicWords();
2817 if ( isset( $magicWords[$mw->mId
] ) ) {
2818 $rawEntry = $magicWords[$mw->mId
];
2824 if ( !is_array( $rawEntry ) ) {
2825 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
2827 $mw->mCaseSensitive
= $rawEntry[0];
2828 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
2833 * Add magic words to the extension array
2835 * @param $newWords array
2837 function addMagicWordsByLang( $newWords ) {
2838 $fallbackChain = $this->getFallbackLanguages();
2839 $fallbackChain = array_reverse( $fallbackChain );
2840 foreach ( $fallbackChain as $code ) {
2841 if ( isset( $newWords[$code] ) ) {
2842 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
2848 * Get special page names, as an associative array
2849 * case folded alias => real name
2851 function getSpecialPageAliases() {
2852 // Cache aliases because it may be slow to load them
2853 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
2855 $this->mExtendedSpecialPageAliases
=
2856 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
2857 wfRunHooks( 'LanguageGetSpecialPageAliases',
2858 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
2861 return $this->mExtendedSpecialPageAliases
;
2865 * Italic is unsuitable for some languages
2867 * @param $text String: the text to be emphasized.
2870 function emphasize( $text ) {
2871 return "<em>$text</em>";
2875 * Normally we output all numbers in plain en_US style, that is
2876 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2877 * point twohundredthirtyfive. However this is not suitable for all
2878 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2879 * Icelandic just want to use commas instead of dots, and dots instead
2880 * of commas like "293.291,235".
2882 * An example of this function being called:
2884 * wfMessage( 'message' )->numParams( $num )->text()
2887 * See LanguageGu.php for the Gujarati implementation and
2888 * $separatorTransformTable on MessageIs.php for
2889 * the , => . and . => , implementation.
2891 * @todo check if it's viable to use localeconv() for the decimal
2893 * @param $number Mixed: the string to be formatted, should be an integer
2894 * or a floating point number.
2895 * @param $nocommafy Bool: set to true for special numbers like dates
2898 public function formatNum( $number, $nocommafy = false ) {
2899 global $wgTranslateNumerals;
2900 if ( !$nocommafy ) {
2901 $number = $this->commafy( $number );
2902 $s = $this->separatorTransformTable();
2904 $number = strtr( $number, $s );
2908 if ( $wgTranslateNumerals ) {
2909 $s = $this->digitTransformTable();
2911 $number = strtr( $number, $s );
2919 * @param $number string
2922 function parseFormattedNumber( $number ) {
2923 $s = $this->digitTransformTable();
2925 $number = strtr( $number, array_flip( $s ) );
2928 $s = $this->separatorTransformTable();
2930 $number = strtr( $number, array_flip( $s ) );
2933 $number = strtr( $number, array( ',' => '' ) );
2938 * Adds commas to a given number
2943 function commafy( $_ ) {
2944 $digitGroupingPattern = $this->digitGroupingPattern();
2945 if ( $_ === null ) {
2949 if ( !$digitGroupingPattern ||
$digitGroupingPattern === "###,###,###" ) {
2950 // default grouping is at thousands, use the same for ###,###,### pattern too.
2951 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2953 // Ref: http://cldr.unicode.org/translation/number-patterns
2955 if ( intval( $_ ) < 0 ) {
2956 // For negative numbers apply the algorithm like positive number and add sign.
2958 $_ = substr( $_, 1 );
2960 $numberpart = array();
2961 $decimalpart = array();
2962 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
2963 preg_match( "/\d+/", $_, $numberpart );
2964 preg_match( "/\.\d*/", $_, $decimalpart );
2965 $groupedNumber = ( count( $decimalpart ) > 0 ) ?
$decimalpart[0]:"";
2966 if ( $groupedNumber === $_ ) {
2967 // the string does not have any number part. Eg: .12345
2968 return $sign . $groupedNumber;
2970 $start = $end = strlen( $numberpart[0] );
2971 while ( $start > 0 ) {
2972 $match = $matches[0][$numMatches -1] ;
2973 $matchLen = strlen( $match );
2974 $start = $end - $matchLen;
2978 $groupedNumber = substr( $_ , $start, $end -$start ) . $groupedNumber ;
2980 if ( $numMatches > 1 ) {
2981 // use the last pattern for the rest of the number
2985 $groupedNumber = "," . $groupedNumber;
2988 return $sign . $groupedNumber;
2994 function digitGroupingPattern() {
2995 return self
::$dataCache->getItem( $this->mCode
, 'digitGroupingPattern' );
3001 function digitTransformTable() {
3002 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
3008 function separatorTransformTable() {
3009 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
3013 * Take a list of strings and build a locale-friendly comma-separated
3014 * list, using the local comma-separator message.
3015 * The last two strings are chained with an "and".
3016 * NOTE: This function will only work with standard numeric array keys (0, 1, 2…)
3021 function listToText( array $l ) {
3023 $m = count( $l ) - 1;
3027 } elseif ( $m === 1 ) {
3028 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
3030 for ( $i = $m; $i >= 0; $i-- ) {
3033 } elseif ( $i == $m - 1 ) {
3034 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
3036 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
3044 * Take a list of strings and build a locale-friendly comma-separated
3045 * list, using the local comma-separator message.
3046 * @param $list array of strings to put in a comma list
3049 function commaList( array $list ) {
3051 wfMessage( 'comma-separator' )->inLanguage( $this )->escaped(),
3057 * Take a list of strings and build a locale-friendly semicolon-separated
3058 * list, using the local semicolon-separator message.
3059 * @param $list array of strings to put in a semicolon list
3062 function semicolonList( array $list ) {
3064 wfMessage( 'semicolon-separator' )->inLanguage( $this )->escaped(),
3070 * Same as commaList, but separate it with the pipe instead.
3071 * @param $list array of strings to put in a pipe list
3074 function pipeList( array $list ) {
3076 wfMessage( 'pipe-separator' )->inLanguage( $this )->escaped(),
3082 * Truncate a string to a specified length in bytes, appending an optional
3083 * string (e.g. for ellipses)
3085 * The database offers limited byte lengths for some columns in the database;
3086 * multi-byte character sets mean we need to ensure that only whole characters
3087 * are included, otherwise broken characters can be passed to the user
3089 * If $length is negative, the string will be truncated from the beginning
3091 * @param $string String to truncate
3092 * @param $length Int: maximum length (including ellipses)
3093 * @param $ellipsis String to append to the truncated text
3094 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
3095 * $adjustLength was introduced in 1.18, before that behaved as if false.
3098 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
3099 # Use the localized ellipsis character
3100 if ( $ellipsis == '...' ) {
3101 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3103 # Check if there is no need to truncate
3104 if ( $length == 0 ) {
3105 return $ellipsis; // convention
3106 } elseif ( strlen( $string ) <= abs( $length ) ) {
3107 return $string; // no need to truncate
3109 $stringOriginal = $string;
3110 # If ellipsis length is >= $length then we can't apply $adjustLength
3111 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
3112 $string = $ellipsis; // this can be slightly unexpected
3113 # Otherwise, truncate and add ellipsis...
3115 $eLength = $adjustLength ?
strlen( $ellipsis ) : 0;
3116 if ( $length > 0 ) {
3117 $length -= $eLength;
3118 $string = substr( $string, 0, $length ); // xyz...
3119 $string = $this->removeBadCharLast( $string );
3120 $string = $string . $ellipsis;
3122 $length +
= $eLength;
3123 $string = substr( $string, $length ); // ...xyz
3124 $string = $this->removeBadCharFirst( $string );
3125 $string = $ellipsis . $string;
3128 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
3129 # This check is *not* redundant if $adjustLength, due to the single case where
3130 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
3131 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
3134 return $stringOriginal;
3139 * Remove bytes that represent an incomplete Unicode character
3140 * at the end of string (e.g. bytes of the char are missing)
3142 * @param $string String
3145 protected function removeBadCharLast( $string ) {
3146 if ( $string != '' ) {
3147 $char = ord( $string[strlen( $string ) - 1] );
3149 if ( $char >= 0xc0 ) {
3150 # We got the first byte only of a multibyte char; remove it.
3151 $string = substr( $string, 0, -1 );
3152 } elseif ( $char >= 0x80 &&
3153 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
3154 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
3156 # We chopped in the middle of a character; remove it
3164 * Remove bytes that represent an incomplete Unicode character
3165 * at the start of string (e.g. bytes of the char are missing)
3167 * @param $string String
3170 protected function removeBadCharFirst( $string ) {
3171 if ( $string != '' ) {
3172 $char = ord( $string[0] );
3173 if ( $char >= 0x80 && $char < 0xc0 ) {
3174 # We chopped in the middle of a character; remove the whole thing
3175 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
3182 * Truncate a string of valid HTML to a specified length in bytes,
3183 * appending an optional string (e.g. for ellipses), and return valid HTML
3185 * This is only intended for styled/linked text, such as HTML with
3186 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
3187 * Also, this will not detect things like "display:none" CSS.
3189 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
3191 * @param string $text HTML string to truncate
3192 * @param int $length (zero/positive) Maximum length (including ellipses)
3193 * @param string $ellipsis String to append to the truncated text
3196 function truncateHtml( $text, $length, $ellipsis = '...' ) {
3197 # Use the localized ellipsis character
3198 if ( $ellipsis == '...' ) {
3199 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3201 # Check if there is clearly no need to truncate
3202 if ( $length <= 0 ) {
3203 return $ellipsis; // no text shown, nothing to format (convention)
3204 } elseif ( strlen( $text ) <= $length ) {
3205 return $text; // string short enough even *with* HTML (short-circuit)
3208 $dispLen = 0; // innerHTML legth so far
3209 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3210 $tagType = 0; // 0-open, 1-close
3211 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3212 $entityState = 0; // 0-not entity, 1-entity
3213 $tag = $ret = ''; // accumulated tag name, accumulated result string
3214 $openTags = array(); // open tag stack
3215 $maybeState = null; // possible truncation state
3217 $textLen = strlen( $text );
3218 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3219 for ( $pos = 0; true; ++
$pos ) {
3220 # Consider truncation once the display length has reached the maximim.
3221 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3222 # Check that we're not in the middle of a bracket/entity...
3223 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3224 if ( !$testingEllipsis ) {
3225 $testingEllipsis = true;
3226 # Save where we are; we will truncate here unless there turn out to
3227 # be so few remaining characters that truncation is not necessary.
3228 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3229 $maybeState = array( $ret, $openTags ); // save state
3231 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3232 # String in fact does need truncation, the truncation point was OK.
3233 list( $ret, $openTags ) = $maybeState; // reload state
3234 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3235 $ret .= $ellipsis; // add ellipsis
3239 if ( $pos >= $textLen ) break; // extra iteration just for above checks
3241 # Read the next char...
3243 $lastCh = $pos ?
$text[$pos - 1] : '';
3244 $ret .= $ch; // add to result string
3246 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3247 $entityState = 0; // for bad HTML
3248 $bracketState = 1; // tag started (checking for backslash)
3249 } elseif ( $ch == '>' ) {
3250 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3251 $entityState = 0; // for bad HTML
3252 $bracketState = 0; // out of brackets
3253 } elseif ( $bracketState == 1 ) {
3255 $tagType = 1; // close tag (e.g. "</span>")
3257 $tagType = 0; // open tag (e.g. "<span>")
3260 $bracketState = 2; // building tag name
3261 } elseif ( $bracketState == 2 ) {
3265 // Name found (e.g. "<a href=..."), add on tag attributes...
3266 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
3268 } elseif ( $bracketState == 0 ) {
3269 if ( $entityState ) {
3272 $dispLen++
; // entity is one displayed char
3275 if ( $neLength == 0 && !$maybeState ) {
3276 // Save state without $ch. We want to *hit* the first
3277 // display char (to get tags) but not *use* it if truncating.
3278 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3281 $entityState = 1; // entity found, (e.g. " ")
3283 $dispLen++
; // this char is displayed
3284 // Add the next $max display text chars after this in one swoop...
3285 $max = ( $testingEllipsis ?
$length : $neLength ) - $dispLen;
3286 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos +
1, $max );
3287 $dispLen +
= $skipped;
3293 // Close the last tag if left unclosed by bad HTML
3294 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3295 while ( count( $openTags ) > 0 ) {
3296 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3302 * truncateHtml() helper function
3303 * like strcspn() but adds the skipped chars to $ret
3312 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3313 if ( $len === null ) {
3314 $len = -1; // -1 means "no limit" for strcspn
3315 } elseif ( $len < 0 ) {
3319 if ( $start < strlen( $text ) ) {
3320 $skipCount = strcspn( $text, $search, $start, $len );
3321 $ret .= substr( $text, $start, $skipCount );
3327 * truncateHtml() helper function
3328 * (a) push or pop $tag from $openTags as needed
3329 * (b) clear $tag value
3330 * @param &$tag string Current HTML tag name we are looking at
3331 * @param $tagType int (0-open tag, 1-close tag)
3332 * @param $lastCh string Character before the '>' that ended this tag
3333 * @param &$openTags array Open tag stack (not accounting for $tag)
3335 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3336 $tag = ltrim( $tag );
3338 if ( $tagType == 0 && $lastCh != '/' ) {
3339 $openTags[] = $tag; // tag opened (didn't close itself)
3340 } elseif ( $tagType == 1 ) {
3341 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3342 array_pop( $openTags ); // tag closed
3350 * Grammatical transformations, needed for inflected languages
3351 * Invoked by putting {{grammar:case|word}} in a message
3353 * @param $word string
3354 * @param $case string
3357 function convertGrammar( $word, $case ) {
3358 global $wgGrammarForms;
3359 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3360 return $wgGrammarForms[$this->getCode()][$case][$word];
3365 * Get the grammar forms for the content language
3366 * @return array of grammar forms
3369 function getGrammarForms() {
3370 global $wgGrammarForms;
3371 if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
3372 return $wgGrammarForms[$this->getCode()];
3377 * Provides an alternative text depending on specified gender.
3378 * Usage {{gender:username|masculine|feminine|neutral}}.
3379 * username is optional, in which case the gender of current user is used,
3380 * but only in (some) interface messages; otherwise default gender is used.
3382 * If no forms are given, an empty string is returned. If only one form is
3383 * given, it will be returned unconditionally. These details are implied by
3384 * the caller and cannot be overridden in subclasses.
3386 * If more than one form is given, the default is to use the neutral one
3387 * if it is specified, and to use the masculine one otherwise. These
3388 * details can be overridden in subclasses.
3390 * @param $gender string
3391 * @param $forms array
3395 function gender( $gender, $forms ) {
3396 if ( !count( $forms ) ) {
3399 $forms = $this->preConvertPlural( $forms, 2 );
3400 if ( $gender === 'male' ) {
3403 if ( $gender === 'female' ) {
3406 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
3410 * Plural form transformations, needed for some languages.
3411 * For example, there are 3 form of plural in Russian and Polish,
3412 * depending on "count mod 10". See [[w:Plural]]
3413 * For English it is pretty simple.
3415 * Invoked by putting {{plural:count|wordform1|wordform2}}
3416 * or {{plural:count|wordform1|wordform2|wordform3}}
3418 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3420 * @param $count Integer: non-localized number
3421 * @param $forms Array: different plural forms
3422 * @return string Correct form of plural for $count in this language
3424 function convertPlural( $count, $forms ) {
3425 if ( !count( $forms ) ) {
3429 // Handle explicit 0= and 1= forms
3430 foreach ( $forms as $index => $form ) {
3431 if ( isset( $form[1] ) && $form[1] === '=' ) {
3432 if ( $form[0] === (string) $count ) {
3433 return substr( $form, 2 );
3435 unset( $forms[$index] );
3438 $forms = array_values( $forms );
3440 $pluralForm = $this->getPluralForm( $count );
3441 $pluralForm = min( $pluralForm, count( $forms ) - 1 );
3442 return $forms[$pluralForm];
3446 * Checks that convertPlural was given an array and pads it to requested
3447 * amount of forms by copying the last one.
3449 * @param $count Integer: How many forms should there be at least
3450 * @param $forms Array of forms given to convertPlural
3451 * @return array Padded array of forms or an exception if not an array
3453 protected function preConvertPlural( /* Array */ $forms, $count ) {
3454 while ( count( $forms ) < $count ) {
3455 $forms[] = $forms[count( $forms ) - 1];
3461 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3462 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3463 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3464 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3467 * @param $str String: the validated block duration in English
3468 * @return string Somehow translated block duration
3469 * @see LanguageFi.php for example implementation
3471 function translateBlockExpiry( $str ) {
3472 $duration = SpecialBlock
::getSuggestedDurations( $this );
3473 foreach ( $duration as $show => $value ) {
3474 if ( strcmp( $str, $value ) == 0 ) {
3475 return htmlspecialchars( trim( $show ) );
3479 // Since usually only infinite or indefinite is only on list, so try
3480 // equivalents if still here.
3481 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3482 if ( in_array( $str, $indefs ) ) {
3483 foreach ( $indefs as $val ) {
3484 $show = array_search( $val, $duration, true );
3485 if ( $show !== false ) {
3486 return htmlspecialchars( trim( $show ) );
3491 // If all else fails, return a standard duration or timestamp description.
3492 $time = strtotime( $str, 0 );
3493 if ( $time === false ) { // Unknown format. Return it as-is in case.
3495 } elseif ( $time !== strtotime( $str, 1 ) ) { // It's a relative timestamp.
3496 // $time is relative to 0 so it's a duration length.
3497 return $this->formatDuration( $time );
3498 } else { // It's an absolute timestamp.
3499 if ( $time === 0 ) {
3500 // wfTimestamp() handles 0 as current time instead of epoch.
3501 return $this->timeanddate( '19700101000000' );
3503 return $this->timeanddate( $time );
3509 * languages like Chinese need to be segmented in order for the diff
3512 * @param $text String
3515 public function segmentForDiff( $text ) {
3520 * and unsegment to show the result
3522 * @param $text String
3525 public function unsegmentForDiff( $text ) {
3530 * Return the LanguageConverter used in the Language
3533 * @return LanguageConverter
3535 public function getConverter() {
3536 return $this->mConverter
;
3540 * convert text to all supported variants
3542 * @param $text string
3545 public function autoConvertToAllVariants( $text ) {
3546 return $this->mConverter
->autoConvertToAllVariants( $text );
3550 * convert text to different variants of a language.
3552 * @param $text string
3555 public function convert( $text ) {
3556 return $this->mConverter
->convert( $text );
3560 * Convert a Title object to a string in the preferred variant
3562 * @param $title Title
3565 public function convertTitle( $title ) {
3566 return $this->mConverter
->convertTitle( $title );
3570 * Convert a namespace index to a string in the preferred variant
3575 public function convertNamespace( $ns ) {
3576 return $this->mConverter
->convertNamespace( $ns );
3580 * Check if this is a language with variants
3584 public function hasVariants() {
3585 return sizeof( $this->getVariants() ) > 1;
3589 * Check if the language has the specific variant
3592 * @param $variant string
3595 public function hasVariant( $variant ) {
3596 return (bool)$this->mConverter
->validateVariant( $variant );
3600 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3602 * @param $text string
3605 public function armourMath( $text ) {
3606 return $this->mConverter
->armourMath( $text );
3610 * Perform output conversion on a string, and encode for safe HTML output.
3611 * @param $text String text to be converted
3612 * @param $isTitle Bool whether this conversion is for the article title
3614 * @todo this should get integrated somewhere sane
3616 public function convertHtml( $text, $isTitle = false ) {
3617 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3621 * @param $key string
3624 public function convertCategoryKey( $key ) {
3625 return $this->mConverter
->convertCategoryKey( $key );
3629 * Get the list of variants supported by this language
3630 * see sample implementation in LanguageZh.php
3632 * @return array an array of language codes
3634 public function getVariants() {
3635 return $this->mConverter
->getVariants();
3641 public function getPreferredVariant() {
3642 return $this->mConverter
->getPreferredVariant();
3648 public function getDefaultVariant() {
3649 return $this->mConverter
->getDefaultVariant();
3655 public function getURLVariant() {
3656 return $this->mConverter
->getURLVariant();
3660 * If a language supports multiple variants, it is
3661 * possible that non-existing link in one variant
3662 * actually exists in another variant. this function
3663 * tries to find it. See e.g. LanguageZh.php
3665 * @param $link String: the name of the link
3666 * @param $nt Mixed: the title object of the link
3667 * @param $ignoreOtherCond Boolean: to disable other conditions when
3668 * we need to transclude a template or update a category's link
3669 * @return null the input parameters may be modified upon return
3671 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3672 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
3676 * If a language supports multiple variants, converts text
3677 * into an array of all possible variants of the text:
3678 * 'variant' => text in that variant
3680 * @deprecated since 1.17 Use autoConvertToAllVariants()
3682 * @param $text string
3686 public function convertLinkToAllVariants( $text ) {
3687 return $this->mConverter
->convertLinkToAllVariants( $text );
3691 * returns language specific options used by User::getPageRenderHash()
3692 * for example, the preferred language variant
3696 function getExtraHashOptions() {
3697 return $this->mConverter
->getExtraHashOptions();
3701 * For languages that support multiple variants, the title of an
3702 * article may be displayed differently in different variants. this
3703 * function returns the apporiate title defined in the body of the article.
3707 public function getParsedTitle() {
3708 return $this->mConverter
->getParsedTitle();
3712 * Prepare external link text for conversion. When the text is
3713 * a URL, it shouldn't be converted, and it'll be wrapped in
3714 * the "raw" tag (-{R| }-) to prevent conversion.
3716 * This function is called "markNoConversion" for historical
3719 * @param $text String: text to be used for external link
3720 * @param $noParse bool: wrap it without confirming it's a real URL first
3721 * @return string the tagged text
3723 public function markNoConversion( $text, $noParse = false ) {
3724 // Excluding protocal-relative URLs may avoid many false positives.
3725 if ( $noParse ||
preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
3726 return $this->mConverter
->markNoConversion( $text );
3733 * A regular expression to match legal word-trailing characters
3734 * which should be merged onto a link of the form [[foo]]bar.
3738 public function linkTrail() {
3739 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
3745 function getLangObj() {
3750 * Get the RFC 3066 code for this language object
3752 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
3753 * htmlspecialchars() or similar
3757 public function getCode() {
3758 return $this->mCode
;
3762 * Get the code in Bcp47 format which we can use
3763 * inside of html lang="" tags.
3765 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
3766 * htmlspecialchars() or similar.
3771 public function getHtmlCode() {
3772 if ( is_null( $this->mHtmlCode
) ) {
3773 $this->mHtmlCode
= wfBCP47( $this->getCode() );
3775 return $this->mHtmlCode
;
3779 * @param $code string
3781 public function setCode( $code ) {
3782 $this->mCode
= $code;
3783 // Ensure we don't leave an incorrect html code lying around
3784 $this->mHtmlCode
= null;
3788 * Get the name of a file for a certain language code
3789 * @param $prefix string Prepend this to the filename
3790 * @param $code string Language code
3791 * @param $suffix string Append this to the filename
3792 * @throws MWException
3793 * @return string $prefix . $mangledCode . $suffix
3795 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
3796 // Protect against path traversal
3797 if ( !Language
::isValidCode( $code )
3798 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
3800 throw new MWException( "Invalid language code \"$code\"" );
3803 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
3807 * Get the language code from a file name. Inverse of getFileName()
3808 * @param $filename string $prefix . $languageCode . $suffix
3809 * @param $prefix string Prefix before the language code
3810 * @param $suffix string Suffix after the language code
3811 * @return string Language code, or false if $prefix or $suffix isn't found
3813 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
3815 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
3816 preg_quote( $suffix, '/' ) . '/', $filename, $m );
3817 if ( !count( $m ) ) {
3820 return str_replace( '_', '-', strtolower( $m[1] ) );
3824 * @param $code string
3827 public static function getMessagesFileName( $code ) {
3829 $file = self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
3830 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
3835 * @param $code string
3838 public static function getClassFileName( $code ) {
3840 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
3844 * Get the first fallback for a given language.
3846 * @param $code string
3848 * @return bool|string
3850 public static function getFallbackFor( $code ) {
3851 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3854 $fallbacks = self
::getFallbacksFor( $code );
3855 $first = array_shift( $fallbacks );
3861 * Get the ordered list of fallback languages.
3864 * @param $code string Language code
3867 public static function getFallbacksFor( $code ) {
3868 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
3871 $v = self
::getLocalisationCache()->getItem( $code, 'fallback' );
3872 $v = array_map( 'trim', explode( ',', $v ) );
3873 if ( $v[count( $v ) - 1] !== 'en' ) {
3881 * Get all messages for a given language
3882 * WARNING: this may take a long time. If you just need all message *keys*
3883 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
3885 * @param $code string
3889 public static function getMessagesFor( $code ) {
3890 return self
::getLocalisationCache()->getItem( $code, 'messages' );
3894 * Get a message for a given language
3896 * @param $key string
3897 * @param $code string
3901 public static function getMessageFor( $key, $code ) {
3902 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
3906 * Get all message keys for a given language. This is a faster alternative to
3907 * array_keys( Language::getMessagesFor( $code ) )
3910 * @param $code string Language code
3911 * @return array of message keys (strings)
3913 public static function getMessageKeysFor( $code ) {
3914 return self
::getLocalisationCache()->getSubItemList( $code, 'messages' );
3921 function fixVariableInNamespace( $talk ) {
3922 if ( strpos( $talk, '$1' ) === false ) {
3926 global $wgMetaNamespace;
3927 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
3929 # Allow grammar transformations
3930 # Allowing full message-style parsing would make simple requests
3931 # such as action=raw much more expensive than they need to be.
3932 # This will hopefully cover most cases.
3933 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
3934 array( &$this, 'replaceGrammarInNamespace' ), $talk );
3935 return str_replace( ' ', '_', $talk );
3942 function replaceGrammarInNamespace( $m ) {
3943 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
3947 * @throws MWException
3950 static function getCaseMaps() {
3951 static $wikiUpperChars, $wikiLowerChars;
3952 if ( isset( $wikiUpperChars ) ) {
3953 return array( $wikiUpperChars, $wikiLowerChars );
3956 wfProfileIn( __METHOD__
);
3957 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
3958 if ( $arr === false ) {
3959 throw new MWException(
3960 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
3962 $wikiUpperChars = $arr['wikiUpperChars'];
3963 $wikiLowerChars = $arr['wikiLowerChars'];
3964 wfProfileOut( __METHOD__
);
3965 return array( $wikiUpperChars, $wikiLowerChars );
3969 * Decode an expiry (block, protection, etc) which has come from the DB
3971 * @todo FIXME: why are we returnings DBMS-dependent strings???
3973 * @param $expiry String: Database expiry String
3974 * @param $format Bool|Int true to process using language functions, or TS_ constant
3975 * to return the expiry in a given timestamp
3979 public function formatExpiry( $expiry, $format = true ) {
3980 static $infinity, $infinityMsg;
3981 if ( $infinity === null ) {
3982 $infinityMsg = wfMessage( 'infiniteblock' );
3983 $infinity = wfGetDB( DB_SLAVE
)->getInfinity();
3986 if ( $expiry == '' ||
$expiry == $infinity ) {
3987 return $format === true
3991 return $format === true
3992 ?
$this->timeanddate( $expiry, /* User preference timezone */ true )
3993 : wfTimestamp( $format, $expiry );
3999 * @param $seconds int|float
4000 * @param $format Array Optional
4001 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
4002 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
4003 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
4004 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
4007 function formatTimePeriod( $seconds, $format = array() ) {
4008 if ( !is_array( $format ) ) {
4009 $format = array( 'avoid' => $format ); // For backwards compatibility
4011 if ( !isset( $format['avoid'] ) ) {
4012 $format['avoid'] = false;
4014 if ( !isset( $format['noabbrevs' ] ) ) {
4015 $format['noabbrevs'] = false;
4017 $secondsMsg = wfMessage(
4018 $format['noabbrevs'] ?
'seconds' : 'seconds-abbrev' )->inLanguage( $this );
4019 $minutesMsg = wfMessage(
4020 $format['noabbrevs'] ?
'minutes' : 'minutes-abbrev' )->inLanguage( $this );
4021 $hoursMsg = wfMessage(
4022 $format['noabbrevs'] ?
'hours' : 'hours-abbrev' )->inLanguage( $this );
4023 $daysMsg = wfMessage(
4024 $format['noabbrevs'] ?
'days' : 'days-abbrev' )->inLanguage( $this );
4026 if ( round( $seconds * 10 ) < 100 ) {
4027 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
4028 $s = $secondsMsg->params( $s )->text();
4029 } elseif ( round( $seconds ) < 60 ) {
4030 $s = $this->formatNum( round( $seconds ) );
4031 $s = $secondsMsg->params( $s )->text();
4032 } elseif ( round( $seconds ) < 3600 ) {
4033 $minutes = floor( $seconds / 60 );
4034 $secondsPart = round( fmod( $seconds, 60 ) );
4035 if ( $secondsPart == 60 ) {
4039 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4041 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4042 } elseif ( round( $seconds ) <= 2 * 86400 ) {
4043 $hours = floor( $seconds / 3600 );
4044 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
4045 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
4046 if ( $secondsPart == 60 ) {
4050 if ( $minutes == 60 ) {
4054 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
4056 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4057 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
4058 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4061 $days = floor( $seconds / 86400 );
4062 if ( $format['avoid'] === 'avoidminutes' ) {
4063 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
4064 if ( $hours == 24 ) {
4068 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4070 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4071 } elseif ( $format['avoid'] === 'avoidseconds' ) {
4072 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
4073 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
4074 if ( $minutes == 60 ) {
4078 if ( $hours == 24 ) {
4082 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4084 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4086 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4088 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4090 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
4097 * Format a bitrate for output, using an appropriate
4098 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
4100 * This use base 1000. For base 1024 use formatSize(), for another base
4101 * see formatComputingNumbers()
4106 function formatBitrate( $bps ) {
4107 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
4111 * @param $size int Size of the unit
4112 * @param $boundary int Size boundary (1000, or 1024 in most cases)
4113 * @param $messageKey string Message key to be uesd
4116 function formatComputingNumbers( $size, $boundary, $messageKey ) {
4118 return str_replace( '$1', $this->formatNum( $size ),
4119 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
4122 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
4125 $maxIndex = count( $sizes ) - 1;
4126 while ( $size >= $boundary && $index < $maxIndex ) {
4131 // For small sizes no decimal places necessary
4134 // For MB and bigger two decimal places are smarter
4137 $msg = str_replace( '$1', $sizes[$index], $messageKey );
4139 $size = round( $size, $round );
4140 $text = $this->getMessageFromDB( $msg );
4141 return str_replace( '$1', $this->formatNum( $size ), $text );
4145 * Format a size in bytes for output, using an appropriate
4146 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
4148 * This method use base 1024. For base 1000 use formatBitrate(), for
4149 * another base see formatComputingNumbers()
4151 * @param $size int Size to format
4152 * @return string Plain text (not HTML)
4154 function formatSize( $size ) {
4155 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
4159 * Make a list item, used by various special pages
4161 * @param $page String Page link
4162 * @param $details String Text between brackets
4163 * @param $oppositedm Boolean Add the direction mark opposite to your
4164 * language, to display text properly
4167 function specialList( $page, $details, $oppositedm = true ) {
4168 $dirmark = ( $oppositedm ?
$this->getDirMark( true ) : '' ) .
4169 $this->getDirMark();
4170 $details = $details ?
$dirmark . $this->getMessageFromDB( 'word-separator' ) .
4171 wfMessage( 'parentheses' )->rawParams( $details )->inLanguage( $this )->escaped() : '';
4172 return $page . $details;
4176 * Generate (prev x| next x) (20|50|100...) type links for paging
4178 * @param $title Title object to link
4179 * @param $offset Integer offset parameter
4180 * @param $limit Integer limit parameter
4181 * @param $query array|String optional URL query parameter string
4182 * @param $atend Bool optional param for specified if this is the last page
4185 public function viewPrevNext( Title
$title, $offset, $limit, array $query = array(), $atend = false ) {
4186 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
4188 # Make 'previous' link
4189 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4190 if ( $offset > 0 ) {
4191 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
4192 $query, $prev, 'prevn-title', 'mw-prevlink' );
4194 $plink = htmlspecialchars( $prev );
4198 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4200 $nlink = htmlspecialchars( $next );
4202 $nlink = $this->numLink( $title, $offset +
$limit, $limit,
4203 $query, $next, 'prevn-title', 'mw-nextlink' );
4206 # Make links to set number of items per page
4207 $numLinks = array();
4208 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
4209 $numLinks[] = $this->numLink( $title, $offset, $num,
4210 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
4213 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
4214 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
4218 * Helper function for viewPrevNext() that generates links
4220 * @param $title Title object to link
4221 * @param $offset Integer offset parameter
4222 * @param $limit Integer limit parameter
4223 * @param $query Array extra query parameters
4224 * @param $link String text to use for the link; will be escaped
4225 * @param $tooltipMsg String name of the message to use as tooltip
4226 * @param $class String value of the "class" attribute of the link
4227 * @return String HTML fragment
4229 private function numLink( Title
$title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
4230 $query = array( 'limit' => $limit, 'offset' => $offset ) +
$query;
4231 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4232 return Html
::element( 'a', array( 'href' => $title->getLocalURL( $query ),
4233 'title' => $tooltip, 'class' => $class ), $link );
4237 * Get the conversion rule title, if any.
4241 public function getConvRuleTitle() {
4242 return $this->mConverter
->getConvRuleTitle();
4246 * Get the compiled plural rules for the language
4248 * @return array Associative array with plural form, and plural rule as key-value pairs
4250 public function getCompiledPluralRules() {
4251 $pluralRules = self
::$dataCache->getItem( strtolower( $this->mCode
), 'compiledPluralRules' );
4252 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4253 if ( !$pluralRules ) {
4254 foreach ( $fallbacks as $fallbackCode ) {
4255 $pluralRules = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
4256 if ( $pluralRules ) {
4261 return $pluralRules;
4265 * Get the plural rules for the language
4267 * @return array Associative array with plural form, and plural rule as key-value pairs
4269 public function getPluralRules() {
4270 $pluralRules = self
::$dataCache->getItem( strtolower( $this->mCode
), 'pluralRules' );
4271 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4272 if ( !$pluralRules ) {
4273 foreach ( $fallbacks as $fallbackCode ) {
4274 $pluralRules = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRules' );
4275 if ( $pluralRules ) {
4280 return $pluralRules;
4284 * Find the plural form matching to the given number
4285 * It return the form index.
4286 * @return int The index of the plural form
4288 private function getPluralForm( $number ) {
4289 $pluralRules = $this->getCompiledPluralRules();
4290 $form = CLDRPluralRuleEvaluator
::evaluateCompiled( $number, $pluralRules );