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
51 function __construct( $langobj ) { $this->mLang
= $langobj; }
52 function autoConvertToAllVariants( $text ) { return array( $this->mLang
->getCode() => $text ); }
53 function convert( $t ) { return $t; }
54 function convertTo( $text, $variant ) { return $text; }
55 function convertTitle( $t ) { return $t->getPrefixedText(); }
56 function convertNamespace( $ns ) { return $this->mLang
->getFormattedNsText( $ns ); }
57 function getVariants() { return array( $this->mLang
->getCode() ); }
58 function getPreferredVariant() { return $this->mLang
->getCode(); }
59 function getDefaultVariant() { return $this->mLang
->getCode(); }
60 function getURLVariant() { return ''; }
61 function getConvRuleTitle() { return false; }
62 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
63 function getExtraHashOptions() { return ''; }
64 function getParsedTitle() { return ''; }
65 function markNoConversion( $text, $noParse = false ) { return $text; }
66 function convertCategoryKey( $key ) { return $key; }
67 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
68 function armourMath( $text ) { return $text; }
72 * Internationalisation code
78 * @var LanguageConverter
82 public $mVariants, $mCode, $mLoaded = false;
83 public $mMagicExtensions = array(), $mMagicHookDone = false;
84 private $mHtmlCode = null, $mParentLanguage = false;
86 public $dateFormatStrings = array();
87 public $mExtendedSpecialPageAliases;
89 protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
92 * ReplacementArray object caches
94 public $transformData = array();
97 * @var LocalisationCache
99 static public $dataCache;
101 static public $mLangObjCache = array();
103 static public $mWeekdayMsgs = array(
104 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
108 static public $mWeekdayAbbrevMsgs = array(
109 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
112 static public $mMonthMsgs = array(
113 'january', 'february', 'march', 'april', 'may_long', 'june',
114 'july', 'august', 'september', 'october', 'november',
117 static public $mMonthGenMsgs = array(
118 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
119 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
122 static public $mMonthAbbrevMsgs = array(
123 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
124 'sep', 'oct', 'nov', 'dec'
127 static public $mIranianCalendarMonthMsgs = array(
128 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
129 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
130 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
131 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
134 static public $mHebrewCalendarMonthMsgs = array(
135 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
136 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
137 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
138 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
139 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
142 static public $mHebrewCalendarMonthGenMsgs = array(
143 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
144 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
145 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
146 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
147 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
150 static public $mHijriCalendarMonthMsgs = array(
151 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
152 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
153 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
154 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
161 static public $durationIntervals = array(
162 'millennia' => 31556952000,
163 'centuries' => 3155695200,
164 'decades' => 315569520,
165 'years' => 31556952, // 86400 * ( 365 + ( 24 * 3 + 25 ) / 400 )
174 * Cache for language fallbacks.
175 * @see Language::getFallbacksIncludingSiteLanguage
179 static private $fallbackLanguageCache = array();
182 * Get a cached or new language object for a given language code
183 * @param $code String
186 static function factory( $code ) {
187 global $wgDummyLanguageCodes, $wgLangObjCacheSize;
189 if ( isset( $wgDummyLanguageCodes[$code] ) ) {
190 $code = $wgDummyLanguageCodes[$code];
193 // get the language object to process
194 $langObj = isset( self
::$mLangObjCache[$code] )
195 ? self
::$mLangObjCache[$code]
196 : self
::newFromCode( $code );
198 // merge the language object in to get it up front in the cache
199 self
::$mLangObjCache = array_merge( array( $code => $langObj ), self
::$mLangObjCache );
200 // get rid of the oldest ones in case we have an overflow
201 self
::$mLangObjCache = array_slice( self
::$mLangObjCache, 0, $wgLangObjCacheSize, true );
207 * Create a language object for a given language code
208 * @param $code String
209 * @throws MWException
212 protected static function newFromCode( $code ) {
213 // Protect against path traversal below
214 if ( !Language
::isValidCode( $code )
215 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
217 throw new MWException( "Invalid language code \"$code\"" );
220 if ( !Language
::isValidBuiltInCode( $code ) ) {
221 // It's not possible to customise this code with class files, so
222 // just return a Language object. This is to support uselang= hacks.
223 $lang = new Language
;
224 $lang->setCode( $code );
228 // Check if there is a language class for the code
229 $class = self
::classFromCode( $code );
230 self
::preloadLanguageClass( $class );
231 if ( MWInit
::classExists( $class ) ) {
236 // Keep trying the fallback list until we find an existing class
237 $fallbacks = Language
::getFallbacksFor( $code );
238 foreach ( $fallbacks as $fallbackCode ) {
239 if ( !Language
::isValidBuiltInCode( $fallbackCode ) ) {
240 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
243 $class = self
::classFromCode( $fallbackCode );
244 self
::preloadLanguageClass( $class );
245 if ( MWInit
::classExists( $class ) ) {
246 $lang = Language
::newFromCode( $fallbackCode );
247 $lang->setCode( $code );
252 throw new MWException( "Invalid fallback sequence for language '$code'" );
256 * Checks whether any localisation is available for that language tag
257 * in MediaWiki (MessagesXx.php exists).
259 * @param string $code Language tag (in lower case)
260 * @return bool Whether language is supported
263 public static function isSupportedLanguage( $code ) {
264 return $code === strtolower( $code ) && is_readable( self
::getMessagesFileName( $code ) );
268 * Returns true if a language code string is a well-formed language tag
269 * according to RFC 5646.
270 * This function only checks well-formedness; it doesn't check that
271 * language, script or variant codes actually exist in the repositories.
273 * Based on regexes by Mark Davis of the Unicode Consortium:
274 * http://unicode.org/repos/cldr/trunk/tools/java/org/unicode/cldr/util/data/langtagRegex.txt
276 * @param $code string
277 * @param $lenient boolean Whether to allow '_' as separator. The default is only '-'.
282 public static function isWellFormedLanguageTag( $code, $lenient = false ) {
285 $alphanum = '[a-z0-9]';
286 $x = 'x'; # private use singleton
287 $singleton = '[a-wy-z]'; # other singleton
288 $s = $lenient ?
'[-_]' : '-';
290 $language = "$alpha{2,8}|$alpha{2,3}$s$alpha{3}";
291 $script = "$alpha{4}"; # ISO 15924
292 $region = "(?:$alpha{2}|$digit{3})"; # ISO 3166-1 alpha-2 or UN M.49
293 $variant = "(?:$alphanum{5,8}|$digit$alphanum{3})";
294 $extension = "$singleton(?:$s$alphanum{2,8})+";
295 $privateUse = "$x(?:$s$alphanum{1,8})+";
297 # Define certain grandfathered codes, since otherwise the regex is pretty useless.
298 # Since these are limited, this is safe even later changes to the registry --
299 # the only oddity is that it might change the type of the tag, and thus
300 # the results from the capturing groups.
301 # http://www.iana.org/assignments/language-subtag-registry
303 $grandfathered = "en{$s}GB{$s}oed"
304 . "|i{$s}(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)"
305 . "|no{$s}(?:bok|nyn)"
306 . "|sgn{$s}(?:BE{$s}(?:fr|nl)|CH{$s}de)"
307 . "|zh{$s}min{$s}nan";
309 $variantList = "$variant(?:$s$variant)*";
310 $extensionList = "$extension(?:$s$extension)*";
312 $langtag = "(?:($language)"
315 . "(?:$s$variantList)?"
316 . "(?:$s$extensionList)?"
317 . "(?:$s$privateUse)?)";
319 # The final breakdown, with capturing groups for each of these components
320 # The variants, extensions, grandfathered, and private-use may have interior '-'
322 $root = "^(?:$langtag|$privateUse|$grandfathered)$";
324 return (bool)preg_match( "/$root/", strtolower( $code ) );
328 * Returns true if a language code string is of a valid form, whether or
329 * not it exists. This includes codes which are used solely for
330 * customisation via the MediaWiki namespace.
332 * @param $code string
336 public static function isValidCode( $code ) {
337 static $cache = array();
338 if ( isset( $cache[$code] ) ) {
339 return $cache[$code];
341 // People think language codes are html safe, so enforce it.
342 // Ideally we should only allow a-zA-Z0-9-
343 // but, .+ and other chars are often used for {{int:}} hacks
344 // see bugs 37564, 37587, 36938
346 strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code )
347 && !preg_match( Title
::getTitleInvalidRegex(), $code );
349 return $cache[$code];
353 * Returns true if a language code is of a valid form for the purposes of
354 * internal customisation of MediaWiki, via Messages*.php.
356 * @param $code string
358 * @throws MWException
362 public static function isValidBuiltInCode( $code ) {
364 if ( !is_string( $code ) ) {
365 if ( is_object( $code ) ) {
366 $addmsg = " of class " . get_class( $code );
370 $type = gettype( $code );
371 throw new MWException( __METHOD__
. " must be passed a string, $type given$addmsg" );
374 return (bool)preg_match( '/^[a-z0-9-]{2,}$/i', $code );
378 * Returns true if a language code is an IETF tag known to MediaWiki.
380 * @param $code string
385 public static function isKnownLanguageTag( $tag ) {
386 static $coreLanguageNames;
388 // Quick escape for invalid input to avoid exceptions down the line
389 // when code tries to process tags which are not valid at all.
390 if ( !self
::isValidBuiltInCode( $tag ) ) {
394 if ( $coreLanguageNames === null ) {
395 include MWInit
::compiledPath( 'languages/Names.php' );
398 if ( isset( $coreLanguageNames[$tag] )
399 || self
::fetchLanguageName( $tag, $tag ) !== ''
409 * @return String Name of the language class
411 public static function classFromCode( $code ) {
412 if ( $code == 'en' ) {
415 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
420 * Includes language class files
422 * @param $class string Name of the language class
424 public static function preloadLanguageClass( $class ) {
427 if ( $class === 'Language' ) {
431 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
432 include_once "$IP/languages/classes/$class.php";
437 * Get the LocalisationCache instance
439 * @return LocalisationCache
441 public static function getLocalisationCache() {
442 if ( is_null( self
::$dataCache ) ) {
443 global $wgLocalisationCacheConf;
444 $class = $wgLocalisationCacheConf['class'];
445 self
::$dataCache = new $class( $wgLocalisationCacheConf );
447 return self
::$dataCache;
450 function __construct() {
451 $this->mConverter
= new FakeConverter( $this );
452 // Set the code to the name of the descendant
453 if ( get_class( $this ) == 'Language' ) {
456 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
458 self
::getLocalisationCache();
462 * Reduce memory usage
464 function __destruct() {
465 foreach ( $this as $name => $value ) {
466 unset( $this->$name );
471 * Hook which will be called if this is the content language.
472 * Descendants can use this to register hook functions or modify globals
474 function initContLang() { }
477 * Same as getFallbacksFor for current language.
479 * @deprecated in 1.19
481 function getFallbackLanguageCode() {
482 wfDeprecated( __METHOD__
, '1.19' );
483 return self
::getFallbackFor( $this->mCode
);
490 function getFallbackLanguages() {
491 return self
::getFallbacksFor( $this->mCode
);
495 * Exports $wgBookstoreListEn
498 function getBookstoreList() {
499 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
503 * Returns an array of localised namespaces indexed by their numbers. If the namespace is not
504 * available in localised form, it will be included in English.
508 public function getNamespaces() {
509 if ( is_null( $this->namespaceNames
) ) {
510 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
512 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
513 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
515 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
517 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
518 if ( $wgMetaNamespaceTalk ) {
519 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
521 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
522 $this->namespaceNames
[NS_PROJECT_TALK
] =
523 $this->fixVariableInNamespace( $talk );
526 # Sometimes a language will be localised but not actually exist on this wiki.
527 foreach ( $this->namespaceNames
as $key => $text ) {
528 if ( !isset( $validNamespaces[$key] ) ) {
529 unset( $this->namespaceNames
[$key] );
533 # The above mixing may leave namespaces out of canonical order.
534 # Re-order by namespace ID number...
535 ksort( $this->namespaceNames
);
537 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames
) );
539 return $this->namespaceNames
;
543 * Arbitrarily set all of the namespace names at once. Mainly used for testing
544 * @param $namespaces Array of namespaces (id => name)
546 public function setNamespaces( array $namespaces ) {
547 $this->namespaceNames
= $namespaces;
548 $this->mNamespaceIds
= null;
552 * Resets all of the namespace caches. Mainly used for testing
554 public function resetNamespaces() {
555 $this->namespaceNames
= null;
556 $this->mNamespaceIds
= null;
557 $this->namespaceAliases
= null;
561 * A convenience function that returns the same thing as
562 * getNamespaces() except with the array values changed to ' '
563 * where it found '_', useful for producing output to be displayed
564 * e.g. in <select> forms.
568 function getFormattedNamespaces() {
569 $ns = $this->getNamespaces();
570 foreach ( $ns as $k => $v ) {
571 $ns[$k] = strtr( $v, '_', ' ' );
577 * Get a namespace value by key
579 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
580 * echo $mw_ns; // prints 'MediaWiki'
583 * @param $index Int: the array key of the namespace to return
584 * @return mixed, string if the namespace value exists, otherwise false
586 function getNsText( $index ) {
587 $ns = $this->getNamespaces();
588 return isset( $ns[$index] ) ?
$ns[$index] : false;
592 * A convenience function that returns the same thing as
593 * getNsText() except with '_' changed to ' ', useful for
597 * $mw_ns = $wgContLang->getFormattedNsText( NS_MEDIAWIKI_TALK );
598 * echo $mw_ns; // prints 'MediaWiki talk'
601 * @param int $index The array key of the namespace to return
602 * @return string Namespace name without underscores (empty string if namespace does not exist)
604 function getFormattedNsText( $index ) {
605 $ns = $this->getNsText( $index );
606 return strtr( $ns, '_', ' ' );
610 * Returns gender-dependent namespace alias if available.
611 * @param $index Int: namespace index
612 * @param $gender String: gender key (male, female... )
616 function getGenderNsText( $index, $gender ) {
617 global $wgExtraGenderNamespaces;
619 $ns = $wgExtraGenderNamespaces + self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
620 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
624 * Whether this language makes distinguishes genders for example in
629 function needsGenderDistinction() {
630 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
631 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
632 // $wgExtraGenderNamespaces overrides everything
634 } elseif ( isset( $wgExtraNamespaces[NS_USER
] ) && isset( $wgExtraNamespaces[NS_USER_TALK
] ) ) {
635 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
636 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
639 // Check what is in i18n files
640 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
641 return count( $aliases ) > 0;
646 * Get a namespace key by value, case insensitive.
647 * Only matches namespace names for the current language, not the
648 * canonical ones defined in Namespace.php.
650 * @param $text String
651 * @return mixed An integer if $text is a valid value otherwise false
653 function getLocalNsIndex( $text ) {
654 $lctext = $this->lc( $text );
655 $ids = $this->getNamespaceIds();
656 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
662 function getNamespaceAliases() {
663 if ( is_null( $this->namespaceAliases
) ) {
664 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
668 foreach ( $aliases as $name => $index ) {
669 if ( $index === NS_PROJECT_TALK
) {
670 unset( $aliases[$name] );
671 $name = $this->fixVariableInNamespace( $name );
672 $aliases[$name] = $index;
677 global $wgExtraGenderNamespaces;
678 $genders = $wgExtraGenderNamespaces +
(array)self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
679 foreach ( $genders as $index => $forms ) {
680 foreach ( $forms as $alias ) {
681 $aliases[$alias] = $index;
685 # Also add converted namespace names as aliases, to avoid confusion.
686 $convertedNames = array();
687 foreach ( $this->getVariants() as $variant ) {
688 if ( $variant === $this->mCode
) {
691 foreach ( $this->getNamespaces() as $ns => $_ ) {
692 $convertedNames[$this->getConverter()->convertNamespace( $ns, $variant )] = $ns;
696 $this->namespaceAliases
= $aliases +
$convertedNames;
698 return $this->namespaceAliases
;
704 function getNamespaceIds() {
705 if ( is_null( $this->mNamespaceIds
) ) {
706 global $wgNamespaceAliases;
707 # Put namespace names and aliases into a hashtable.
708 # If this is too slow, then we should arrange it so that it is done
709 # before caching. The catch is that at pre-cache time, the above
710 # class-specific fixup hasn't been done.
711 $this->mNamespaceIds
= array();
712 foreach ( $this->getNamespaces() as $index => $name ) {
713 $this->mNamespaceIds
[$this->lc( $name )] = $index;
715 foreach ( $this->getNamespaceAliases() as $name => $index ) {
716 $this->mNamespaceIds
[$this->lc( $name )] = $index;
718 if ( $wgNamespaceAliases ) {
719 foreach ( $wgNamespaceAliases as $name => $index ) {
720 $this->mNamespaceIds
[$this->lc( $name )] = $index;
724 return $this->mNamespaceIds
;
728 * Get a namespace key by value, case insensitive. Canonical namespace
729 * names override custom ones defined for the current language.
731 * @param $text String
732 * @return mixed An integer if $text is a valid value otherwise false
734 function getNsIndex( $text ) {
735 $lctext = $this->lc( $text );
736 $ns = MWNamespace
::getCanonicalIndex( $lctext );
737 if ( $ns !== null ) {
740 $ids = $this->getNamespaceIds();
741 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
745 * short names for language variants used for language conversion links.
747 * @param $code String
748 * @param $usemsg bool Use the "variantname-xyz" message if it exists
751 function getVariantname( $code, $usemsg = true ) {
752 $msg = "variantname-$code";
753 if ( $usemsg && wfMessage( $msg )->exists() ) {
754 return $this->getMessageFromDB( $msg );
756 $name = self
::fetchLanguageName( $code );
758 return $name; # if it's defined as a language name, show that
760 # otherwise, output the language code
766 * @param $name string
769 function specialPage( $name ) {
770 $aliases = $this->getSpecialPageAliases();
771 if ( isset( $aliases[$name][0] ) ) {
772 $name = $aliases[$name][0];
774 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
780 function getDatePreferences() {
781 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
787 function getDateFormats() {
788 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
792 * @return array|string
794 function getDefaultDateFormat() {
795 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
796 if ( $df === 'dmy or mdy' ) {
797 global $wgAmericanDates;
798 return $wgAmericanDates ?
'mdy' : 'dmy';
807 function getDatePreferenceMigrationMap() {
808 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
815 function getImageFile( $image ) {
816 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
822 function getExtraUserToggles() {
823 return (array)self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
830 function getUserToggle( $tog ) {
831 return $this->getMessageFromDB( "tog-$tog" );
835 * Get native language names, indexed by code.
836 * Only those defined in MediaWiki, no other data like CLDR.
837 * If $customisedOnly is true, only returns codes with a messages file
839 * @param $customisedOnly bool
842 * @deprecated in 1.20, use fetchLanguageNames()
844 public static function getLanguageNames( $customisedOnly = false ) {
845 return self
::fetchLanguageNames( null, $customisedOnly ?
'mwfile' : 'mw' );
849 * Get translated language names. This is done on best effort and
850 * by default this is exactly the same as Language::getLanguageNames.
851 * The CLDR extension provides translated names.
852 * @param $code String Language code.
853 * @return Array language code => language name
855 * @deprecated in 1.20, use fetchLanguageNames()
857 public static function getTranslatedLanguageNames( $code ) {
858 return self
::fetchLanguageNames( $code, 'all' );
862 * Get an array of language names, indexed by code.
863 * @param $inLanguage null|string: Code of language in which to return the names
864 * Use null for autonyms (native names)
865 * @param $include string:
866 * 'all' all available languages
867 * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
868 * 'mwfile' only if the language is in 'mw' *and* has a message file
869 * @return array: language code => language name
872 public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
873 global $wgExtraLanguageNames;
874 static $coreLanguageNames;
876 if ( $coreLanguageNames === null ) {
877 include MWInit
::compiledPath( 'languages/Names.php' );
883 # TODO: also include when $inLanguage is null, when this code is more efficient
884 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
887 $mwNames = $wgExtraLanguageNames +
$coreLanguageNames;
888 foreach ( $mwNames as $mwCode => $mwName ) {
889 # - Prefer own MediaWiki native name when not using the hook
890 # - For other names just add if not added through the hook
891 if ( $mwCode === $inLanguage ||
!isset( $names[$mwCode] ) ) {
892 $names[$mwCode] = $mwName;
896 if ( $include === 'all' ) {
901 $coreCodes = array_keys( $mwNames );
902 foreach ( $coreCodes as $coreCode ) {
903 $returnMw[$coreCode] = $names[$coreCode];
906 if ( $include === 'mwfile' ) {
907 $namesMwFile = array();
908 # We do this using a foreach over the codes instead of a directory
909 # loop so that messages files in extensions will work correctly.
910 foreach ( $returnMw as $code => $value ) {
911 if ( is_readable( self
::getMessagesFileName( $code ) ) ) {
912 $namesMwFile[$code] = $names[$code];
917 # 'mw' option; default if it's not one of the other two options (all/mwfile)
922 * @param $code string: The code of the language for which to get the name
923 * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
924 * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
925 * @return string: Language name or empty
928 public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
929 $array = self
::fetchLanguageNames( $inLanguage, $include );
930 return !array_key_exists( $code, $array ) ?
'' : $array[$code];
934 * Get a message from the MediaWiki namespace.
936 * @param $msg String: message name
939 function getMessageFromDB( $msg ) {
940 return wfMessage( $msg )->inLanguage( $this )->text();
944 * Get the native language name of $code.
945 * Only if defined in MediaWiki, no other data like CLDR.
946 * @param $code string
948 * @deprecated in 1.20, use fetchLanguageName()
950 function getLanguageName( $code ) {
951 return self
::fetchLanguageName( $code );
958 function getMonthName( $key ) {
959 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
965 function getMonthNamesArray() {
966 $monthNames = array( '' );
967 for ( $i = 1; $i < 13; $i++
) {
968 $monthNames[] = $this->getMonthName( $i );
977 function getMonthNameGen( $key ) {
978 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
985 function getMonthAbbreviation( $key ) {
986 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
992 function getMonthAbbreviationsArray() {
993 $monthNames = array( '' );
994 for ( $i = 1; $i < 13; $i++
) {
995 $monthNames[] = $this->getMonthAbbreviation( $i );
1001 * @param $key string
1004 function getWeekdayName( $key ) {
1005 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
1009 * @param $key string
1012 function getWeekdayAbbreviation( $key ) {
1013 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
1017 * @param $key string
1020 function getIranianCalendarMonthName( $key ) {
1021 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
1025 * @param $key string
1028 function getHebrewCalendarMonthName( $key ) {
1029 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
1033 * @param $key string
1036 function getHebrewCalendarMonthNameGen( $key ) {
1037 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
1041 * @param $key string
1044 function getHijriCalendarMonthName( $key ) {
1045 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
1049 * This is a workalike of PHP's date() function, but with better
1050 * internationalisation, a reduced set of format characters, and a better
1053 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrUeIOPTZ. See
1054 * the PHP manual for definitions. There are a number of extensions, which
1057 * xn Do not translate digits of the next numeric format character
1058 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
1059 * xr Use roman numerals for the next numeric format character
1060 * xh Use hebrew numerals for the next numeric format character
1062 * xg Genitive month name
1064 * xij j (day number) in Iranian calendar
1065 * xiF F (month name) in Iranian calendar
1066 * xin n (month number) in Iranian calendar
1067 * xiy y (two digit year) in Iranian calendar
1068 * xiY Y (full year) in Iranian calendar
1070 * xjj j (day number) in Hebrew calendar
1071 * xjF F (month name) in Hebrew calendar
1072 * xjt t (days in month) in Hebrew calendar
1073 * xjx xg (genitive month name) in Hebrew calendar
1074 * xjn n (month number) in Hebrew calendar
1075 * xjY Y (full year) in Hebrew calendar
1077 * xmj j (day number) in Hijri calendar
1078 * xmF F (month name) in Hijri calendar
1079 * xmn n (month number) in Hijri calendar
1080 * xmY Y (full year) in Hijri calendar
1082 * xkY Y (full year) in Thai solar calendar. Months and days are
1083 * identical to the Gregorian calendar
1084 * xoY Y (full year) in Minguo calendar or Juche year.
1085 * Months and days are identical to the
1086 * Gregorian calendar
1087 * xtY Y (full year) in Japanese nengo. Months and days are
1088 * identical to the Gregorian calendar
1090 * Characters enclosed in double quotes will be considered literal (with
1091 * the quotes themselves removed). Unmatched quotes will be considered
1092 * literal quotes. Example:
1094 * "The month is" F => The month is January
1097 * Backslash escaping is also supported.
1099 * Input timestamp is assumed to be pre-normalized to the desired local
1100 * time zone, if any. Note that the format characters crUeIOPTZ will assume
1101 * $ts is UTC if $zone is not given.
1103 * @param $format String
1104 * @param $ts String: 14-character timestamp
1107 * @param $zone DateTimeZone: Timezone of $ts
1108 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
1110 * @throws MWException
1113 function sprintfDate( $format, $ts, DateTimeZone
$zone = null ) {
1118 $dateTimeObj = false;
1127 if ( strlen( $ts ) !== 14 ) {
1128 throw new MWException( __METHOD__
. ": The timestamp $ts should have 14 characters" );
1131 if ( !ctype_digit( $ts ) ) {
1132 throw new MWException( __METHOD__
. ": The timestamp $ts should be a number" );
1135 for ( $p = 0; $p < strlen( $format ); $p++
) {
1137 $code = $format[$p];
1138 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
1139 $code .= $format[++
$p];
1142 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
1143 $code .= $format[++
$p];
1154 $rawToggle = !$rawToggle;
1163 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
1167 $hebrew = self
::tsToHebrew( $ts );
1169 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
1172 $num = substr( $ts, 6, 2 );
1175 if ( !$dateTimeObj ) {
1176 $dateTimeObj = DateTime
::createFromFormat(
1177 'YmdHis', $ts, $zone ?
: new DateTimeZone( 'UTC' )
1180 $s .= $this->getWeekdayAbbreviation( $dateTimeObj->format( 'w' ) +
1 );
1183 $num = intval( substr( $ts, 6, 2 ) );
1187 $iranian = self
::tsToIranian( $ts );
1193 $hijri = self
::tsToHijri( $ts );
1199 $hebrew = self
::tsToHebrew( $ts );
1204 if ( !$dateTimeObj ) {
1205 $dateTimeObj = DateTime
::createFromFormat(
1206 'YmdHis', $ts, $zone ?
: new DateTimeZone( 'UTC' )
1209 $s .= $this->getWeekdayName( $dateTimeObj->format( 'w' ) +
1 );
1212 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1216 $iranian = self
::tsToIranian( $ts );
1218 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1222 $hijri = self
::tsToHijri( $ts );
1224 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1228 $hebrew = self
::tsToHebrew( $ts );
1230 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1233 $num = substr( $ts, 4, 2 );
1236 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1239 $num = intval( substr( $ts, 4, 2 ) );
1243 $iranian = self
::tsToIranian( $ts );
1249 $hijri = self
::tsToHijri ( $ts );
1255 $hebrew = self
::tsToHebrew( $ts );
1261 $hebrew = self
::tsToHebrew( $ts );
1266 $num = substr( $ts, 0, 4 );
1270 $iranian = self
::tsToIranian( $ts );
1276 $hijri = self
::tsToHijri( $ts );
1282 $hebrew = self
::tsToHebrew( $ts );
1288 $thai = self
::tsToYear( $ts, 'thai' );
1294 $minguo = self
::tsToYear( $ts, 'minguo' );
1300 $tenno = self
::tsToYear( $ts, 'tenno' );
1305 $num = substr( $ts, 2, 2 );
1309 $iranian = self
::tsToIranian( $ts );
1311 $num = substr( $iranian[0], -2 );
1314 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
1317 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
1320 $h = substr( $ts, 8, 2 );
1321 $num = $h %
12 ?
$h %
12 : 12;
1324 $num = intval( substr( $ts, 8, 2 ) );
1327 $h = substr( $ts, 8, 2 );
1328 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
1331 $num = substr( $ts, 8, 2 );
1334 $num = substr( $ts, 10, 2 );
1337 $num = substr( $ts, 12, 2 );
1345 // Pass through string from $dateTimeObj->format()
1346 if ( !$dateTimeObj ) {
1347 $dateTimeObj = DateTime
::createFromFormat(
1348 'YmdHis', $ts, $zone ?
: new DateTimeZone( 'UTC' )
1351 $s .= $dateTimeObj->format( $code );
1363 // Pass through number from $dateTimeObj->format()
1364 if ( !$dateTimeObj ) {
1365 $dateTimeObj = DateTime
::createFromFormat(
1366 'YmdHis', $ts, $zone ?
: new DateTimeZone( 'UTC' )
1369 $num = $dateTimeObj->format( $code );
1372 # Backslash escaping
1373 if ( $p < strlen( $format ) - 1 ) {
1374 $s .= $format[++
$p];
1381 if ( $p < strlen( $format ) - 1 ) {
1382 $endQuote = strpos( $format, '"', $p +
1 );
1383 if ( $endQuote === false ) {
1384 # No terminating quote, assume literal "
1387 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1391 # Quote at end of string, assume literal "
1398 if ( $num !== false ) {
1399 if ( $rawToggle ||
$raw ) {
1402 } elseif ( $roman ) {
1403 $s .= Language
::romanNumeral( $num );
1405 } elseif ( $hebrewNum ) {
1406 $s .= self
::hebrewNumeral( $num );
1409 $s .= $this->formatNum( $num, true );
1416 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1417 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1420 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1421 * Gregorian dates to Iranian dates. Originally written in C, it
1422 * is released under the terms of GNU Lesser General Public
1423 * License. Conversion to PHP was performed by Niklas Laxström.
1425 * Link: http://www.farsiweb.info/jalali/jalali.c
1431 private static function tsToIranian( $ts ) {
1432 $gy = substr( $ts, 0, 4 ) -1600;
1433 $gm = substr( $ts, 4, 2 ) -1;
1434 $gd = substr( $ts, 6, 2 ) -1;
1436 # Days passed from the beginning (including leap years)
1438 +
floor( ( $gy +
3 ) / 4 )
1439 - floor( ( $gy +
99 ) / 100 )
1440 +
floor( ( $gy +
399 ) / 400 );
1442 // Add days of the past months of this year
1443 for ( $i = 0; $i < $gm; $i++
) {
1444 $gDayNo +
= self
::$GREG_DAYS[$i];
1448 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1452 // Days passed in current month
1453 $gDayNo +
= (int)$gd;
1455 $jDayNo = $gDayNo - 79;
1457 $jNp = floor( $jDayNo / 12053 );
1460 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1463 if ( $jDayNo >= 366 ) {
1464 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1465 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1468 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1469 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1475 return array( $jy, $jm, $jd );
1479 * Converting Gregorian dates to Hijri dates.
1481 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1483 * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1489 private static function tsToHijri( $ts ) {
1490 $year = substr( $ts, 0, 4 );
1491 $month = substr( $ts, 4, 2 );
1492 $day = substr( $ts, 6, 2 );
1500 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1501 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1504 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1505 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1506 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1509 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1510 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1513 $zl = $zjd -1948440 +
10632;
1514 $zn = (int)( ( $zl - 1 ) / 10631 );
1515 $zl = $zl - 10631 * $zn +
354;
1516 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1517 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1518 $zm = (int)( ( 24 * $zl ) / 709 );
1519 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1520 $zy = 30 * $zn +
$zj - 30;
1522 return array( $zy, $zm, $zd );
1526 * Converting Gregorian dates to Hebrew dates.
1528 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1529 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1530 * to translate the relevant functions into PHP and release them under
1533 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1534 * and Adar II is 14. In a non-leap year, Adar is 6.
1540 private static function tsToHebrew( $ts ) {
1542 $year = substr( $ts, 0, 4 );
1543 $month = substr( $ts, 4, 2 );
1544 $day = substr( $ts, 6, 2 );
1546 # Calculate Hebrew year
1547 $hebrewYear = $year +
3760;
1549 # Month number when September = 1, August = 12
1551 if ( $month > 12 ) {
1558 # Calculate day of year from 1 September
1560 for ( $i = 1; $i < $month; $i++
) {
1564 # Check if the year is leap
1565 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1568 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1575 # Calculate the start of the Hebrew year
1576 $start = self
::hebrewYearStart( $hebrewYear );
1578 # Calculate next year's start
1579 if ( $dayOfYear <= $start ) {
1580 # Day is before the start of the year - it is the previous year
1582 $nextStart = $start;
1586 # Add days since previous year's 1 September
1588 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1592 # Start of the new (previous) year
1593 $start = self
::hebrewYearStart( $hebrewYear );
1596 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1599 # Calculate Hebrew day of year
1600 $hebrewDayOfYear = $dayOfYear - $start;
1602 # Difference between year's days
1603 $diff = $nextStart - $start;
1604 # Add 12 (or 13 for leap years) days to ignore the difference between
1605 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1606 # difference is only about the year type
1607 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1613 # Check the year pattern, and is leap year
1614 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1615 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1616 # and non-leap years
1617 $yearPattern = $diff %
30;
1618 # Check if leap year
1619 $isLeap = $diff >= 30;
1621 # Calculate day in the month from number of day in the Hebrew year
1622 # Don't check Adar - if the day is not in Adar, we will stop before;
1623 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1624 $hebrewDay = $hebrewDayOfYear;
1627 while ( $hebrewMonth <= 12 ) {
1628 # Calculate days in this month
1629 if ( $isLeap && $hebrewMonth == 6 ) {
1630 # Adar in a leap year
1632 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1634 if ( $hebrewDay <= $days ) {
1638 # Subtract the days of Adar I
1639 $hebrewDay -= $days;
1642 if ( $hebrewDay <= $days ) {
1648 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1649 # Cheshvan in a complete year (otherwise as the rule below)
1651 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1652 # Kislev in an incomplete year (otherwise as the rule below)
1655 # Odd months have 30 days, even have 29
1656 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1658 if ( $hebrewDay <= $days ) {
1659 # In the current month
1662 # Subtract the days of the current month
1663 $hebrewDay -= $days;
1664 # Try in the next month
1669 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1673 * This calculates the Hebrew year start, as days since 1 September.
1674 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1675 * Used for Hebrew date.
1681 private static function hebrewYearStart( $year ) {
1682 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1683 $b = intval( ( $year - 1 ) %
4 );
1684 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1688 $Mar = intval( $m );
1694 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1695 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1697 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1699 } elseif ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1703 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1708 * Algorithm to convert Gregorian dates to Thai solar dates,
1709 * Minguo dates or Minguo dates.
1711 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1712 * http://en.wikipedia.org/wiki/Minguo_calendar
1713 * http://en.wikipedia.org/wiki/Japanese_era_name
1715 * @param $ts String: 14-character timestamp
1716 * @param $cName String: calender name
1717 * @return Array: converted year, month, day
1719 private static function tsToYear( $ts, $cName ) {
1720 $gy = substr( $ts, 0, 4 );
1721 $gm = substr( $ts, 4, 2 );
1722 $gd = substr( $ts, 6, 2 );
1724 if ( !strcmp( $cName, 'thai' ) ) {
1726 # Add 543 years to the Gregorian calendar
1727 # Months and days are identical
1728 $gy_offset = $gy +
543;
1729 } elseif ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1731 # Deduct 1911 years from the Gregorian calendar
1732 # Months and days are identical
1733 $gy_offset = $gy - 1911;
1734 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1735 # Nengō dates up to Meiji period
1736 # Deduct years from the Gregorian calendar
1737 # depending on the nengo periods
1738 # Months and days are identical
1739 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1741 $gy_gannen = $gy - 1868 +
1;
1742 $gy_offset = $gy_gannen;
1743 if ( $gy_gannen == 1 ) {
1746 $gy_offset = '明治' . $gy_offset;
1748 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1749 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1750 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1751 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1752 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1756 $gy_gannen = $gy - 1912 +
1;
1757 $gy_offset = $gy_gannen;
1758 if ( $gy_gannen == 1 ) {
1761 $gy_offset = '大正' . $gy_offset;
1763 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1764 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1765 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1769 $gy_gannen = $gy - 1926 +
1;
1770 $gy_offset = $gy_gannen;
1771 if ( $gy_gannen == 1 ) {
1774 $gy_offset = '昭和' . $gy_offset;
1777 $gy_gannen = $gy - 1989 +
1;
1778 $gy_offset = $gy_gannen;
1779 if ( $gy_gannen == 1 ) {
1782 $gy_offset = '平成' . $gy_offset;
1788 return array( $gy_offset, $gm, $gd );
1792 * Roman number formatting up to 10000
1798 static function romanNumeral( $num ) {
1799 static $table = array(
1800 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1801 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1802 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1803 array( '', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM', 'MMMMMM', 'MMMMMMM', 'MMMMMMMM', 'MMMMMMMMM', 'MMMMMMMMMM' )
1806 $num = intval( $num );
1807 if ( $num > 10000 ||
$num <= 0 ) {
1812 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1813 if ( $num >= $pow10 ) {
1814 $s .= $table[$i][(int)floor( $num / $pow10 )];
1816 $num = $num %
$pow10;
1822 * Hebrew Gematria number formatting up to 9999
1828 static function hebrewNumeral( $num ) {
1829 static $table = array(
1830 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1831 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1832 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1833 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1836 $num = intval( $num );
1837 if ( $num > 9999 ||
$num <= 0 ) {
1842 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1843 if ( $num >= $pow10 ) {
1844 if ( $num == 15 ||
$num == 16 ) {
1845 $s .= $table[0][9] . $table[0][$num - 9];
1848 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1849 if ( $pow10 == 1000 ) {
1854 $num = $num %
$pow10;
1856 if ( strlen( $s ) == 2 ) {
1859 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1860 $str .= substr( $s, strlen( $s ) - 2, 2 );
1862 $start = substr( $str, 0, strlen( $str ) - 2 );
1863 $end = substr( $str, strlen( $str ) - 2 );
1866 $str = $start . 'ך';
1869 $str = $start . 'ם';
1872 $str = $start . 'ן';
1875 $str = $start . 'ף';
1878 $str = $start . 'ץ';
1885 * Used by date() and time() to adjust the time output.
1887 * @param $ts Int the time in date('YmdHis') format
1888 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1889 * get user timecorrection setting)
1892 function userAdjust( $ts, $tz = false ) {
1893 global $wgUser, $wgLocalTZoffset;
1895 if ( $tz === false ) {
1896 $tz = $wgUser->getOption( 'timecorrection' );
1899 $data = explode( '|', $tz, 3 );
1901 if ( $data[0] == 'ZoneInfo' ) {
1902 wfSuppressWarnings();
1903 $userTZ = timezone_open( $data[2] );
1904 wfRestoreWarnings();
1905 if ( $userTZ !== false ) {
1906 $date = date_create( $ts, timezone_open( 'UTC' ) );
1907 date_timezone_set( $date, $userTZ );
1908 $date = date_format( $date, 'YmdHis' );
1911 # Unrecognized timezone, default to 'Offset' with the stored offset.
1912 $data[0] = 'Offset';
1916 if ( $data[0] == 'System' ||
$tz == '' ) {
1917 # Global offset in minutes.
1918 if ( isset( $wgLocalTZoffset ) ) {
1919 $minDiff = $wgLocalTZoffset;
1921 } elseif ( $data[0] == 'Offset' ) {
1922 $minDiff = intval( $data[1] );
1924 $data = explode( ':', $tz );
1925 if ( count( $data ) == 2 ) {
1926 $data[0] = intval( $data[0] );
1927 $data[1] = intval( $data[1] );
1928 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1929 if ( $data[0] < 0 ) {
1930 $minDiff = -$minDiff;
1933 $minDiff = intval( $data[0] ) * 60;
1937 # No difference ? Return time unchanged
1938 if ( 0 == $minDiff ) {
1942 wfSuppressWarnings(); // E_STRICT system time bitching
1943 # Generate an adjusted date; take advantage of the fact that mktime
1944 # will normalize out-of-range values so we don't have to split $minDiff
1945 # into hours and minutes.
1947 (int)substr( $ts, 8, 2 ) ), # Hours
1948 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
1949 (int)substr( $ts, 12, 2 ), # Seconds
1950 (int)substr( $ts, 4, 2 ), # Month
1951 (int)substr( $ts, 6, 2 ), # Day
1952 (int)substr( $ts, 0, 4 ) ); # Year
1954 $date = date( 'YmdHis', $t );
1955 wfRestoreWarnings();
1961 * This is meant to be used by time(), date(), and timeanddate() to get
1962 * the date preference they're supposed to use, it should be used in
1966 * function timeanddate([...], $format = true) {
1967 * $datePreference = $this->dateFormat($format);
1972 * @param $usePrefs Mixed: if true, the user's preference is used
1973 * if false, the site/language default is used
1974 * if int/string, assumed to be a format.
1977 function dateFormat( $usePrefs = true ) {
1980 if ( is_bool( $usePrefs ) ) {
1982 $datePreference = $wgUser->getDatePreference();
1984 $datePreference = (string)User
::getDefaultOption( 'date' );
1987 $datePreference = (string)$usePrefs;
1991 if ( $datePreference == '' ) {
1995 return $datePreference;
1999 * Get a format string for a given type and preference
2000 * @param $type string May be date, time or both
2001 * @param $pref string The format name as it appears in Messages*.php
2003 * @since 1.22 New type 'pretty' that provides a more readable timestamp format
2007 function getDateFormatString( $type, $pref ) {
2008 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
2009 if ( $pref == 'default' ) {
2010 $pref = $this->getDefaultDateFormat();
2011 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
2013 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
2015 if ( $type === 'pretty' && $df === null ) {
2016 $df = $this->getDateFormatString( 'date', $pref );
2019 if ( $df === null ) {
2020 $pref = $this->getDefaultDateFormat();
2021 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
2024 $this->dateFormatStrings
[$type][$pref] = $df;
2026 return $this->dateFormatStrings
[$type][$pref];
2030 * @param $ts Mixed: the time format which needs to be turned into a
2031 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2032 * @param $adj Bool: whether to adjust the time output according to the
2033 * user configured offset ($timecorrection)
2034 * @param $format Mixed: true to use user's date format preference
2035 * @param $timecorrection String|bool the time offset as returned by
2036 * validateTimeZone() in Special:Preferences
2039 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
2040 $ts = wfTimestamp( TS_MW
, $ts );
2042 $ts = $this->userAdjust( $ts, $timecorrection );
2044 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
2045 return $this->sprintfDate( $df, $ts );
2049 * @param $ts Mixed: the time format which needs to be turned into a
2050 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2051 * @param $adj Bool: whether to adjust the time output according to the
2052 * user configured offset ($timecorrection)
2053 * @param $format Mixed: true to use user's date format preference
2054 * @param $timecorrection String|bool the time offset as returned by
2055 * validateTimeZone() in Special:Preferences
2058 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
2059 $ts = wfTimestamp( TS_MW
, $ts );
2061 $ts = $this->userAdjust( $ts, $timecorrection );
2063 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
2064 return $this->sprintfDate( $df, $ts );
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 $adj Bool: whether to adjust the time output according to the
2071 * user configured offset ($timecorrection)
2072 * @param $format Mixed: what format to return, if it's false output the
2073 * default one (default true)
2074 * @param $timecorrection String|bool the time offset as returned by
2075 * validateTimeZone() in Special:Preferences
2078 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
2079 $ts = wfTimestamp( TS_MW
, $ts );
2081 $ts = $this->userAdjust( $ts, $timecorrection );
2083 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
2084 return $this->sprintfDate( $df, $ts );
2088 * Takes a number of seconds and turns it into a text using values such as hours and minutes.
2092 * @param integer $seconds The amount of seconds.
2093 * @param array $chosenIntervals The intervals to enable.
2097 public function formatDuration( $seconds, array $chosenIntervals = array() ) {
2098 $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
2100 $segments = array();
2102 foreach ( $intervals as $intervalName => $intervalValue ) {
2103 $message = wfMessage( 'duration-' . $intervalName )->numParams( $intervalValue );
2104 $segments[] = $message->inLanguage( $this )->escaped();
2107 return $this->listToText( $segments );
2111 * Takes a number of seconds and returns an array with a set of corresponding intervals.
2112 * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
2116 * @param integer $seconds The amount of seconds.
2117 * @param array $chosenIntervals The intervals to enable.
2121 public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
2122 if ( empty( $chosenIntervals ) ) {
2123 $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
2126 $intervals = array_intersect_key( self
::$durationIntervals, array_flip( $chosenIntervals ) );
2127 $sortedNames = array_keys( $intervals );
2128 $smallestInterval = array_pop( $sortedNames );
2130 $segments = array();
2132 foreach ( $intervals as $name => $length ) {
2133 $value = floor( $seconds / $length );
2135 if ( $value > 0 ||
( $name == $smallestInterval && empty( $segments ) ) ) {
2136 $seconds -= $value * $length;
2137 $segments[$name] = $value;
2145 * Internal helper function for userDate(), userTime() and userTimeAndDate()
2147 * @param $type String: can be 'date', 'time' or 'both'
2148 * @param $ts Mixed: the time format which needs to be turned into a
2149 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2150 * @param $user User object used to get preferences for timezone and format
2151 * @param $options Array, can contain the following keys:
2152 * - 'timecorrection': time correction, can have the following values:
2153 * - true: use user's preference
2154 * - false: don't use time correction
2155 * - integer: value of time correction in minutes
2156 * - 'format': format to use, can have the following values:
2157 * - true: use user's preference
2158 * - false: use default preference
2159 * - string: format to use
2163 private function internalUserTimeAndDate( $type, $ts, User
$user, array $options ) {
2164 $ts = wfTimestamp( TS_MW
, $ts );
2165 $options +
= array( 'timecorrection' => true, 'format' => true );
2166 if ( $options['timecorrection'] !== false ) {
2167 if ( $options['timecorrection'] === true ) {
2168 $offset = $user->getOption( 'timecorrection' );
2170 $offset = $options['timecorrection'];
2172 $ts = $this->userAdjust( $ts, $offset );
2174 if ( $options['format'] === true ) {
2175 $format = $user->getDatePreference();
2177 $format = $options['format'];
2179 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
2180 return $this->sprintfDate( $df, $ts );
2184 * Get the formatted date for the given timestamp and formatted for
2187 * @param $ts Mixed: the time format which needs to be turned into a
2188 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2189 * @param $user User object used to get preferences for timezone and format
2190 * @param $options Array, can contain the following keys:
2191 * - 'timecorrection': time correction, can have the following values:
2192 * - true: use user's preference
2193 * - false: don't use time correction
2194 * - integer: value of time correction in minutes
2195 * - 'format': format to use, can have the following values:
2196 * - true: use user's preference
2197 * - false: use default preference
2198 * - string: format to use
2202 public function userDate( $ts, User
$user, array $options = array() ) {
2203 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
2207 * Get the formatted time for the given timestamp and formatted for
2210 * @param $ts Mixed: the time format which needs to be turned into a
2211 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2212 * @param $user User object used to get preferences for timezone and format
2213 * @param $options Array, can contain the following keys:
2214 * - 'timecorrection': time correction, can have the following values:
2215 * - true: use user's preference
2216 * - false: don't use time correction
2217 * - integer: value of time correction in minutes
2218 * - 'format': format to use, can have the following values:
2219 * - true: use user's preference
2220 * - false: use default preference
2221 * - string: format to use
2225 public function userTime( $ts, User
$user, array $options = array() ) {
2226 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
2230 * Get the formatted date and time for the given timestamp and formatted for
2233 * @param $ts Mixed: the time format which needs to be turned into a
2234 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2235 * @param $user User object used to get preferences for timezone and format
2236 * @param $options Array, can contain the following keys:
2237 * - 'timecorrection': time correction, can have the following values:
2238 * - true: use user's preference
2239 * - false: don't use time correction
2240 * - integer: value of time correction in minutes
2241 * - 'format': format to use, can have the following values:
2242 * - true: use user's preference
2243 * - false: use default preference
2244 * - string: format to use
2248 public function userTimeAndDate( $ts, User
$user, array $options = array() ) {
2249 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
2253 * Convert an MWTimestamp into a pretty human-readable timestamp using
2254 * the given user preferences and relative base time.
2256 * DO NOT USE THIS FUNCTION DIRECTLY. Instead, call MWTimestamp::getHumanTimestamp
2257 * on your timestamp object, which will then call this function. Calling
2258 * this function directly will cause hooks to be skipped over.
2260 * @see MWTimestamp::getHumanTimestamp
2261 * @param MWTimestamp $ts Timestamp to prettify
2262 * @param MWTimestamp $relativeTo Base timestamp
2263 * @param User $user User preferences to use
2264 * @return string Human timestamp
2267 public function getHumanTimestamp( MWTimestamp
$ts, MWTimestamp
$relativeTo, User
$user ) {
2268 $diff = $ts->diff( $relativeTo );
2269 $diffDay = (bool)( (int)$ts->timestamp
->format( 'w' ) - (int)$relativeTo->timestamp
->format( 'w' ) );
2270 $days = $diff->days ?
: (int)$diffDay;
2271 if ( $diff->invert ||
$days > 5 && $ts->timestamp
->format( 'Y' ) !== $relativeTo->timestamp
->format( 'Y' ) ) {
2272 // Timestamps are in different years: use full timestamp
2273 // Also do full timestamp for future dates
2275 * @FIXME Add better handling of future timestamps.
2277 $format = $this->getDateFormatString( 'both', $user->getDatePreference() ?
: 'default' );
2278 $ts = $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) );
2279 } elseif ( $days > 5 ) {
2280 // Timestamps are in same year, but more than 5 days ago: show day and month only.
2281 $format = $this->getDateFormatString( 'pretty', $user->getDatePreference() ?
: 'default' );
2282 $ts = $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) );
2283 } elseif ( $days > 1 ) {
2284 // Timestamp within the past week: show the day of the week and time
2285 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?
: 'default' );
2286 $weekday = self
::$mWeekdayMsgs[$ts->timestamp
->format( 'w' )];
2287 $ts = wfMessage( "$weekday-at" )
2288 ->inLanguage( $this )
2289 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) ) )
2291 } elseif ( $days == 1 ) {
2292 // Timestamp was yesterday: say 'yesterday' and the time.
2293 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?
: 'default' );
2294 $ts = wfMessage( 'yesterday-at' )
2295 ->inLanguage( $this )
2296 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) ) )
2298 } elseif ( $diff->h
> 1 ||
$diff->h
== 1 && $diff->i
> 30 ) {
2299 // Timestamp was today, but more than 90 minutes ago: say 'today' and the time.
2300 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?
: 'default' );
2301 $ts = wfMessage( 'today-at' )
2302 ->inLanguage( $this )
2303 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) ) )
2306 // From here on in, the timestamp was soon enough ago so that we can simply say
2307 // XX units ago, e.g., "2 hours ago" or "5 minutes ago"
2308 } elseif ( $diff->h
== 1 ) {
2309 // Less than 90 minutes, but more than an hour ago.
2310 $ts = wfMessage( 'hours-ago' )->inLanguage( $this )->numParams( 1 )->text();
2311 } elseif ( $diff->i
>= 1 ) {
2312 // A few minutes ago.
2313 $ts = wfMessage( 'minutes-ago' )->inLanguage( $this )->numParams( $diff->i
)->text();
2314 } elseif ( $diff->s
>= 30 ) {
2315 // Less than a minute, but more than 30 sec ago.
2316 $ts = wfMessage( 'seconds-ago' )->inLanguage( $this )->numParams( $diff->s
)->text();
2318 // Less than 30 seconds ago.
2319 $ts = wfMessage( 'just-now' )->text();
2326 * @param $key string
2327 * @return array|null
2329 function getMessage( $key ) {
2330 return self
::$dataCache->getSubitem( $this->mCode
, 'messages', $key );
2336 function getAllMessages() {
2337 return self
::$dataCache->getItem( $this->mCode
, 'messages' );
2346 function iconv( $in, $out, $string ) {
2347 # This is a wrapper for iconv in all languages except esperanto,
2348 # which does some nasty x-conversions beforehand
2350 # Even with //IGNORE iconv can whine about illegal characters in
2351 # *input* string. We just ignore those too.
2352 # REF: http://bugs.php.net/bug.php?id=37166
2353 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
2354 wfSuppressWarnings();
2355 $text = iconv( $in, $out . '//IGNORE', $string );
2356 wfRestoreWarnings();
2360 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
2363 * @param $matches array
2364 * @return mixed|string
2366 function ucwordbreaksCallbackAscii( $matches ) {
2367 return $this->ucfirst( $matches[1] );
2371 * @param $matches array
2374 function ucwordbreaksCallbackMB( $matches ) {
2375 return mb_strtoupper( $matches[0] );
2379 * @param $matches array
2382 function ucCallback( $matches ) {
2383 list( $wikiUpperChars ) = self
::getCaseMaps();
2384 return strtr( $matches[1], $wikiUpperChars );
2388 * @param $matches array
2391 function lcCallback( $matches ) {
2392 list( , $wikiLowerChars ) = self
::getCaseMaps();
2393 return strtr( $matches[1], $wikiLowerChars );
2397 * @param $matches array
2400 function ucwordsCallbackMB( $matches ) {
2401 return mb_strtoupper( $matches[0] );
2405 * @param $matches array
2408 function ucwordsCallbackWiki( $matches ) {
2409 list( $wikiUpperChars ) = self
::getCaseMaps();
2410 return strtr( $matches[0], $wikiUpperChars );
2414 * Make a string's first character uppercase
2416 * @param $str string
2420 function ucfirst( $str ) {
2422 if ( $o < 96 ) { // if already uppercase...
2424 } elseif ( $o < 128 ) {
2425 return ucfirst( $str ); // use PHP's ucfirst()
2427 // fall back to more complex logic in case of multibyte strings
2428 return $this->uc( $str, true );
2433 * Convert a string to uppercase
2435 * @param $str string
2436 * @param $first bool
2440 function uc( $str, $first = false ) {
2441 if ( function_exists( 'mb_strtoupper' ) ) {
2443 if ( $this->isMultibyte( $str ) ) {
2444 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2446 return ucfirst( $str );
2449 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
2452 if ( $this->isMultibyte( $str ) ) {
2453 $x = $first ?
'^' : '';
2454 return preg_replace_callback(
2455 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2456 array( $this, 'ucCallback' ),
2460 return $first ?
ucfirst( $str ) : strtoupper( $str );
2466 * @param $str string
2467 * @return mixed|string
2469 function lcfirst( $str ) {
2472 return strval( $str );
2473 } elseif ( $o >= 128 ) {
2474 return $this->lc( $str, true );
2475 } elseif ( $o > 96 ) {
2478 $str[0] = strtolower( $str[0] );
2484 * @param $str string
2485 * @param $first bool
2486 * @return mixed|string
2488 function lc( $str, $first = false ) {
2489 if ( function_exists( 'mb_strtolower' ) ) {
2491 if ( $this->isMultibyte( $str ) ) {
2492 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2494 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2497 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
2500 if ( $this->isMultibyte( $str ) ) {
2501 $x = $first ?
'^' : '';
2502 return preg_replace_callback(
2503 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2504 array( $this, 'lcCallback' ),
2508 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2514 * @param $str string
2517 function isMultibyte( $str ) {
2518 return (bool)preg_match( '/[\x80-\xff]/', $str );
2522 * @param $str string
2523 * @return mixed|string
2525 function ucwords( $str ) {
2526 if ( $this->isMultibyte( $str ) ) {
2527 $str = $this->lc( $str );
2529 // regexp to find first letter in each word (i.e. after each space)
2530 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2532 // function to use to capitalize a single char
2533 if ( function_exists( 'mb_strtoupper' ) ) {
2534 return preg_replace_callback(
2536 array( $this, 'ucwordsCallbackMB' ),
2540 return preg_replace_callback(
2542 array( $this, 'ucwordsCallbackWiki' ),
2547 return ucwords( strtolower( $str ) );
2552 * capitalize words at word breaks
2554 * @param $str string
2557 function ucwordbreaks( $str ) {
2558 if ( $this->isMultibyte( $str ) ) {
2559 $str = $this->lc( $str );
2561 // since \b doesn't work for UTF-8, we explicitely define word break chars
2562 $breaks = "[ \-\(\)\}\{\.,\?!]";
2564 // find first letter after word break
2565 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2567 if ( function_exists( 'mb_strtoupper' ) ) {
2568 return preg_replace_callback(
2570 array( $this, 'ucwordbreaksCallbackMB' ),
2574 return preg_replace_callback(
2576 array( $this, 'ucwordsCallbackWiki' ),
2581 return preg_replace_callback(
2582 '/\b([\w\x80-\xff]+)\b/',
2583 array( $this, 'ucwordbreaksCallbackAscii' ),
2590 * Return a case-folded representation of $s
2592 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2593 * and $s2 are the same except for the case of their characters. It is not
2594 * necessary for the value returned to make sense when displayed.
2596 * Do *not* perform any other normalisation in this function. If a caller
2597 * uses this function when it should be using a more general normalisation
2598 * function, then fix the caller.
2604 function caseFold( $s ) {
2605 return $this->uc( $s );
2612 function checkTitleEncoding( $s ) {
2613 if ( is_array( $s ) ) {
2614 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2616 if ( StringUtils
::isUtf8( $s ) ) {
2620 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2626 function fallback8bitEncoding() {
2627 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
2631 * Most writing systems use whitespace to break up words.
2632 * Some languages such as Chinese don't conventionally do this,
2633 * which requires special handling when breaking up words for
2638 function hasWordBreaks() {
2643 * Some languages such as Chinese require word segmentation,
2644 * Specify such segmentation when overridden in derived class.
2646 * @param $string String
2649 function segmentByWord( $string ) {
2654 * Some languages have special punctuation need to be normalized.
2655 * Make such changes here.
2657 * @param $string String
2660 function normalizeForSearch( $string ) {
2661 return self
::convertDoubleWidth( $string );
2665 * convert double-width roman characters to single-width.
2666 * range: ff00-ff5f ~= 0020-007f
2668 * @param $string string
2672 protected static function convertDoubleWidth( $string ) {
2673 static $full = null;
2674 static $half = null;
2676 if ( $full === null ) {
2677 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2678 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2679 $full = str_split( $fullWidth, 3 );
2680 $half = str_split( $halfWidth );
2683 $string = str_replace( $full, $half, $string );
2688 * @param $string string
2689 * @param $pattern string
2692 protected static function insertSpace( $string, $pattern ) {
2693 $string = preg_replace( $pattern, " $1 ", $string );
2694 $string = preg_replace( '/ +/', ' ', $string );
2699 * @param $termsArray array
2702 function convertForSearchResult( $termsArray ) {
2703 # some languages, e.g. Chinese, need to do a conversion
2704 # in order for search results to be displayed correctly
2709 * Get the first character of a string.
2714 function firstChar( $s ) {
2717 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2718 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2723 if ( isset( $matches[1] ) ) {
2724 if ( strlen( $matches[1] ) != 3 ) {
2728 // Break down Hangul syllables to grab the first jamo
2729 $code = utf8ToCodepoint( $matches[1] );
2730 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
2732 } elseif ( $code < 0xb098 ) {
2733 return "\xe3\x84\xb1";
2734 } elseif ( $code < 0xb2e4 ) {
2735 return "\xe3\x84\xb4";
2736 } elseif ( $code < 0xb77c ) {
2737 return "\xe3\x84\xb7";
2738 } elseif ( $code < 0xb9c8 ) {
2739 return "\xe3\x84\xb9";
2740 } elseif ( $code < 0xbc14 ) {
2741 return "\xe3\x85\x81";
2742 } elseif ( $code < 0xc0ac ) {
2743 return "\xe3\x85\x82";
2744 } elseif ( $code < 0xc544 ) {
2745 return "\xe3\x85\x85";
2746 } elseif ( $code < 0xc790 ) {
2747 return "\xe3\x85\x87";
2748 } elseif ( $code < 0xcc28 ) {
2749 return "\xe3\x85\x88";
2750 } elseif ( $code < 0xce74 ) {
2751 return "\xe3\x85\x8a";
2752 } elseif ( $code < 0xd0c0 ) {
2753 return "\xe3\x85\x8b";
2754 } elseif ( $code < 0xd30c ) {
2755 return "\xe3\x85\x8c";
2756 } elseif ( $code < 0xd558 ) {
2757 return "\xe3\x85\x8d";
2759 return "\xe3\x85\x8e";
2766 function initEncoding() {
2767 # Some languages may have an alternate char encoding option
2768 # (Esperanto X-coding, Japanese furigana conversion, etc)
2769 # If this language is used as the primary content language,
2770 # an override to the defaults can be set here on startup.
2777 function recodeForEdit( $s ) {
2778 # For some languages we'll want to explicitly specify
2779 # which characters make it into the edit box raw
2780 # or are converted in some way or another.
2781 global $wgEditEncoding;
2782 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
2785 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2793 function recodeInput( $s ) {
2794 # Take the previous into account.
2795 global $wgEditEncoding;
2796 if ( $wgEditEncoding != '' ) {
2797 $enc = $wgEditEncoding;
2801 if ( $enc == 'UTF-8' ) {
2804 return $this->iconv( $enc, 'UTF-8', $s );
2809 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2810 * also cleans up certain backwards-compatible sequences, converting them
2811 * to the modern Unicode equivalent.
2813 * This is language-specific for performance reasons only.
2819 function normalize( $s ) {
2820 global $wgAllUnicodeFixes;
2821 $s = UtfNormal
::cleanUp( $s );
2822 if ( $wgAllUnicodeFixes ) {
2823 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2824 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2831 * Transform a string using serialized data stored in the given file (which
2832 * must be in the serialized subdirectory of $IP). The file contains pairs
2833 * mapping source characters to destination characters.
2835 * The data is cached in process memory. This will go faster if you have the
2836 * FastStringSearch extension.
2838 * @param $file string
2839 * @param $string string
2841 * @throws MWException
2844 function transformUsingPairFile( $file, $string ) {
2845 if ( !isset( $this->transformData
[$file] ) ) {
2846 $data = wfGetPrecompiledData( $file );
2847 if ( $data === false ) {
2848 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2850 $this->transformData
[$file] = new ReplacementArray( $data );
2852 return $this->transformData
[$file]->replace( $string );
2856 * For right-to-left language support
2861 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2865 * Return the correct HTML 'dir' attribute value for this language.
2869 return $this->isRTL() ?
'rtl' : 'ltr';
2873 * Return 'left' or 'right' as appropriate alignment for line-start
2874 * for this language's text direction.
2876 * Should be equivalent to CSS3 'start' text-align value....
2880 function alignStart() {
2881 return $this->isRTL() ?
'right' : 'left';
2885 * Return 'right' or 'left' as appropriate alignment for line-end
2886 * for this language's text direction.
2888 * Should be equivalent to CSS3 'end' text-align value....
2892 function alignEnd() {
2893 return $this->isRTL() ?
'left' : 'right';
2897 * A hidden direction mark (LRM or RLM), depending on the language direction.
2898 * Unlike getDirMark(), this function returns the character as an HTML entity.
2899 * This function should be used when the output is guaranteed to be HTML,
2900 * because it makes the output HTML source code more readable. When
2901 * the output is plain text or can be escaped, getDirMark() should be used.
2903 * @param $opposite Boolean Get the direction mark opposite to your language
2907 function getDirMarkEntity( $opposite = false ) {
2909 return $this->isRTL() ?
'‎' : '‏';
2911 return $this->isRTL() ?
'‏' : '‎';
2915 * A hidden direction mark (LRM or RLM), depending on the language direction.
2916 * This function produces them as invisible Unicode characters and
2917 * the output may be hard to read and debug, so it should only be used
2918 * when the output is plain text or can be escaped. When the output is
2919 * HTML, use getDirMarkEntity() instead.
2921 * @param $opposite Boolean Get the direction mark opposite to your language
2924 function getDirMark( $opposite = false ) {
2925 $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
2926 $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
2928 return $this->isRTL() ?
$lrm : $rlm;
2930 return $this->isRTL() ?
$rlm : $lrm;
2936 function capitalizeAllNouns() {
2937 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2941 * An arrow, depending on the language direction.
2943 * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
2946 function getArrow( $direction = 'forwards' ) {
2947 switch ( $direction ) {
2949 return $this->isRTL() ?
'←' : '→';
2951 return $this->isRTL() ?
'→' : '←';
2964 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2968 function linkPrefixExtension() {
2969 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2973 * Get all magic words from cache.
2976 function getMagicWords() {
2977 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2981 * Run the LanguageGetMagic hook once.
2983 protected function doMagicHook() {
2984 if ( $this->mMagicHookDone
) {
2987 $this->mMagicHookDone
= true;
2988 wfProfileIn( 'LanguageGetMagic' );
2989 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2990 wfProfileOut( 'LanguageGetMagic' );
2994 * Fill a MagicWord object with data from here
2998 function getMagic( $mw ) {
2999 // Saves a function call
3000 if ( ! $this->mMagicHookDone
) {
3001 $this->doMagicHook();
3004 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
3005 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
3007 $rawEntry = self
::$dataCache->getSubitem(
3008 $this->mCode
, 'magicWords', $mw->mId
);
3011 if ( !is_array( $rawEntry ) ) {
3012 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
3014 $mw->mCaseSensitive
= $rawEntry[0];
3015 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
3020 * Add magic words to the extension array
3022 * @param $newWords array
3024 function addMagicWordsByLang( $newWords ) {
3025 $fallbackChain = $this->getFallbackLanguages();
3026 $fallbackChain = array_reverse( $fallbackChain );
3027 foreach ( $fallbackChain as $code ) {
3028 if ( isset( $newWords[$code] ) ) {
3029 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
3035 * Get special page names, as an associative array
3036 * case folded alias => real name
3038 function getSpecialPageAliases() {
3039 // Cache aliases because it may be slow to load them
3040 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
3042 $this->mExtendedSpecialPageAliases
=
3043 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
3044 wfRunHooks( 'LanguageGetSpecialPageAliases',
3045 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
3048 return $this->mExtendedSpecialPageAliases
;
3052 * Italic is unsuitable for some languages
3054 * @param $text String: the text to be emphasized.
3057 function emphasize( $text ) {
3058 return "<em>$text</em>";
3062 * Normally we output all numbers in plain en_US style, that is
3063 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
3064 * point twohundredthirtyfive. However this is not suitable for all
3065 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
3066 * Icelandic just want to use commas instead of dots, and dots instead
3067 * of commas like "293.291,235".
3069 * An example of this function being called:
3071 * wfMessage( 'message' )->numParams( $num )->text()
3074 * See LanguageGu.php for the Gujarati implementation and
3075 * $separatorTransformTable on MessageIs.php for
3076 * the , => . and . => , implementation.
3078 * @todo check if it's viable to use localeconv() for the decimal
3080 * @param $number Mixed: the string to be formatted, should be an integer
3081 * or a floating point number.
3082 * @param $nocommafy Bool: set to true for special numbers like dates
3085 public function formatNum( $number, $nocommafy = false ) {
3086 global $wgTranslateNumerals;
3087 if ( !$nocommafy ) {
3088 $number = $this->commafy( $number );
3089 $s = $this->separatorTransformTable();
3091 $number = strtr( $number, $s );
3095 if ( $wgTranslateNumerals ) {
3096 $s = $this->digitTransformTable();
3098 $number = strtr( $number, $s );
3106 * Front-end for non-commafied formatNum
3108 * @param mixed $number the string to be formatted, should be an integer
3109 * or a floating point number.
3113 public function formatNumNoSeparators( $number ) {
3114 return $this->formatNum( $number, true );
3118 * @param $number string
3121 function parseFormattedNumber( $number ) {
3122 $s = $this->digitTransformTable();
3124 $number = strtr( $number, array_flip( $s ) );
3127 $s = $this->separatorTransformTable();
3129 $number = strtr( $number, array_flip( $s ) );
3132 $number = strtr( $number, array( ',' => '' ) );
3137 * Adds commas to a given number
3139 * @param $number mixed
3142 function commafy( $number ) {
3143 $digitGroupingPattern = $this->digitGroupingPattern();
3144 if ( $number === null ) {
3148 if ( !$digitGroupingPattern ||
$digitGroupingPattern === "###,###,###" ) {
3149 // default grouping is at thousands, use the same for ###,###,### pattern too.
3150 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $number ) ) );
3152 // Ref: http://cldr.unicode.org/translation/number-patterns
3154 if ( intval( $number ) < 0 ) {
3155 // For negative numbers apply the algorithm like positive number and add sign.
3157 $number = substr( $number, 1 );
3159 $integerPart = array();
3160 $decimalPart = array();
3161 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
3162 preg_match( "/\d+/", $number, $integerPart );
3163 preg_match( "/\.\d*/", $number, $decimalPart );
3164 $groupedNumber = ( count( $decimalPart ) > 0 ) ?
$decimalPart[0] : "";
3165 if ( $groupedNumber === $number ) {
3166 // the string does not have any number part. Eg: .12345
3167 return $sign . $groupedNumber;
3169 $start = $end = strlen( $integerPart[0] );
3170 while ( $start > 0 ) {
3171 $match = $matches[0][$numMatches - 1];
3172 $matchLen = strlen( $match );
3173 $start = $end - $matchLen;
3177 $groupedNumber = substr( $number, $start, $end -$start ) . $groupedNumber;
3179 if ( $numMatches > 1 ) {
3180 // use the last pattern for the rest of the number
3184 $groupedNumber = "," . $groupedNumber;
3187 return $sign . $groupedNumber;
3194 function digitGroupingPattern() {
3195 return self
::$dataCache->getItem( $this->mCode
, 'digitGroupingPattern' );
3201 function digitTransformTable() {
3202 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
3208 function separatorTransformTable() {
3209 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
3213 * Take a list of strings and build a locale-friendly comma-separated
3214 * list, using the local comma-separator message.
3215 * The last two strings are chained with an "and".
3216 * NOTE: This function will only work with standard numeric array keys (0, 1, 2…)
3221 function listToText( array $l ) {
3222 $m = count( $l ) - 1;
3227 $and = $this->getMessageFromDB( 'and' );
3228 $space = $this->getMessageFromDB( 'word-separator' );
3230 $comma = $this->getMessageFromDB( 'comma-separator' );
3234 for ( $i = $m - 1; $i >= 0; $i-- ) {
3235 if ( $i == $m - 1 ) {
3236 $s = $l[$i] . $and . $space . $s;
3238 $s = $l[$i] . $comma . $s;
3245 * Take a list of strings and build a locale-friendly comma-separated
3246 * list, using the local comma-separator message.
3247 * @param $list array of strings to put in a comma list
3250 function commaList( array $list ) {
3252 wfMessage( 'comma-separator' )->inLanguage( $this )->escaped(),
3258 * Take a list of strings and build a locale-friendly semicolon-separated
3259 * list, using the local semicolon-separator message.
3260 * @param $list array of strings to put in a semicolon list
3263 function semicolonList( array $list ) {
3265 wfMessage( 'semicolon-separator' )->inLanguage( $this )->escaped(),
3271 * Same as commaList, but separate it with the pipe instead.
3272 * @param $list array of strings to put in a pipe list
3275 function pipeList( array $list ) {
3277 wfMessage( 'pipe-separator' )->inLanguage( $this )->escaped(),
3283 * Truncate a string to a specified length in bytes, appending an optional
3284 * string (e.g. for ellipses)
3286 * The database offers limited byte lengths for some columns in the database;
3287 * multi-byte character sets mean we need to ensure that only whole characters
3288 * are included, otherwise broken characters can be passed to the user
3290 * If $length is negative, the string will be truncated from the beginning
3292 * @param $string String to truncate
3293 * @param $length Int: maximum length (including ellipses)
3294 * @param $ellipsis String to append to the truncated text
3295 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
3296 * $adjustLength was introduced in 1.18, before that behaved as if false.
3299 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
3300 # Use the localized ellipsis character
3301 if ( $ellipsis == '...' ) {
3302 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3304 # Check if there is no need to truncate
3305 if ( $length == 0 ) {
3306 return $ellipsis; // convention
3307 } elseif ( strlen( $string ) <= abs( $length ) ) {
3308 return $string; // no need to truncate
3310 $stringOriginal = $string;
3311 # If ellipsis length is >= $length then we can't apply $adjustLength
3312 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
3313 $string = $ellipsis; // this can be slightly unexpected
3314 # Otherwise, truncate and add ellipsis...
3316 $eLength = $adjustLength ?
strlen( $ellipsis ) : 0;
3317 if ( $length > 0 ) {
3318 $length -= $eLength;
3319 $string = substr( $string, 0, $length ); // xyz...
3320 $string = $this->removeBadCharLast( $string );
3321 $string = $string . $ellipsis;
3323 $length +
= $eLength;
3324 $string = substr( $string, $length ); // ...xyz
3325 $string = $this->removeBadCharFirst( $string );
3326 $string = $ellipsis . $string;
3329 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
3330 # This check is *not* redundant if $adjustLength, due to the single case where
3331 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
3332 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
3335 return $stringOriginal;
3340 * Remove bytes that represent an incomplete Unicode character
3341 * at the end of string (e.g. bytes of the char are missing)
3343 * @param $string String
3346 protected function removeBadCharLast( $string ) {
3347 if ( $string != '' ) {
3348 $char = ord( $string[strlen( $string ) - 1] );
3350 if ( $char >= 0xc0 ) {
3351 # We got the first byte only of a multibyte char; remove it.
3352 $string = substr( $string, 0, -1 );
3353 } elseif ( $char >= 0x80 &&
3354 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
3355 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m )
3357 # We chopped in the middle of a character; remove it
3365 * Remove bytes that represent an incomplete Unicode character
3366 * at the start of string (e.g. bytes of the char are missing)
3368 * @param $string String
3371 protected function removeBadCharFirst( $string ) {
3372 if ( $string != '' ) {
3373 $char = ord( $string[0] );
3374 if ( $char >= 0x80 && $char < 0xc0 ) {
3375 # We chopped in the middle of a character; remove the whole thing
3376 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
3383 * Truncate a string of valid HTML to a specified length in bytes,
3384 * appending an optional string (e.g. for ellipses), and return valid HTML
3386 * This is only intended for styled/linked text, such as HTML with
3387 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
3388 * Also, this will not detect things like "display:none" CSS.
3390 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
3392 * @param string $text HTML string to truncate
3393 * @param int $length (zero/positive) Maximum length (including ellipses)
3394 * @param string $ellipsis String to append to the truncated text
3397 function truncateHtml( $text, $length, $ellipsis = '...' ) {
3398 # Use the localized ellipsis character
3399 if ( $ellipsis == '...' ) {
3400 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3402 # Check if there is clearly no need to truncate
3403 if ( $length <= 0 ) {
3404 return $ellipsis; // no text shown, nothing to format (convention)
3405 } elseif ( strlen( $text ) <= $length ) {
3406 return $text; // string short enough even *with* HTML (short-circuit)
3409 $dispLen = 0; // innerHTML legth so far
3410 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3411 $tagType = 0; // 0-open, 1-close
3412 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3413 $entityState = 0; // 0-not entity, 1-entity
3414 $tag = $ret = ''; // accumulated tag name, accumulated result string
3415 $openTags = array(); // open tag stack
3416 $maybeState = null; // possible truncation state
3418 $textLen = strlen( $text );
3419 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3420 for ( $pos = 0; true; ++
$pos ) {
3421 # Consider truncation once the display length has reached the maximim.
3422 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3423 # Check that we're not in the middle of a bracket/entity...
3424 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3425 if ( !$testingEllipsis ) {
3426 $testingEllipsis = true;
3427 # Save where we are; we will truncate here unless there turn out to
3428 # be so few remaining characters that truncation is not necessary.
3429 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3430 $maybeState = array( $ret, $openTags ); // save state
3432 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3433 # String in fact does need truncation, the truncation point was OK.
3434 list( $ret, $openTags ) = $maybeState; // reload state
3435 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3436 $ret .= $ellipsis; // add ellipsis
3440 if ( $pos >= $textLen ) {
3441 break; // extra iteration just for above checks
3444 # Read the next char...
3446 $lastCh = $pos ?
$text[$pos - 1] : '';
3447 $ret .= $ch; // add to result string
3449 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3450 $entityState = 0; // for bad HTML
3451 $bracketState = 1; // tag started (checking for backslash)
3452 } elseif ( $ch == '>' ) {
3453 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3454 $entityState = 0; // for bad HTML
3455 $bracketState = 0; // out of brackets
3456 } elseif ( $bracketState == 1 ) {
3458 $tagType = 1; // close tag (e.g. "</span>")
3460 $tagType = 0; // open tag (e.g. "<span>")
3463 $bracketState = 2; // building tag name
3464 } elseif ( $bracketState == 2 ) {
3468 // Name found (e.g. "<a href=..."), add on tag attributes...
3469 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
3471 } elseif ( $bracketState == 0 ) {
3472 if ( $entityState ) {
3475 $dispLen++
; // entity is one displayed char
3478 if ( $neLength == 0 && !$maybeState ) {
3479 // Save state without $ch. We want to *hit* the first
3480 // display char (to get tags) but not *use* it if truncating.
3481 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3484 $entityState = 1; // entity found, (e.g. " ")
3486 $dispLen++
; // this char is displayed
3487 // Add the next $max display text chars after this in one swoop...
3488 $max = ( $testingEllipsis ?
$length : $neLength ) - $dispLen;
3489 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos +
1, $max );
3490 $dispLen +
= $skipped;
3496 // Close the last tag if left unclosed by bad HTML
3497 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3498 while ( count( $openTags ) > 0 ) {
3499 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3505 * truncateHtml() helper function
3506 * like strcspn() but adds the skipped chars to $ret
3515 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3516 if ( $len === null ) {
3517 $len = -1; // -1 means "no limit" for strcspn
3518 } elseif ( $len < 0 ) {
3522 if ( $start < strlen( $text ) ) {
3523 $skipCount = strcspn( $text, $search, $start, $len );
3524 $ret .= substr( $text, $start, $skipCount );
3530 * truncateHtml() helper function
3531 * (a) push or pop $tag from $openTags as needed
3532 * (b) clear $tag value
3533 * @param &$tag string Current HTML tag name we are looking at
3534 * @param $tagType int (0-open tag, 1-close tag)
3535 * @param $lastCh string Character before the '>' that ended this tag
3536 * @param &$openTags array Open tag stack (not accounting for $tag)
3538 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3539 $tag = ltrim( $tag );
3541 if ( $tagType == 0 && $lastCh != '/' ) {
3542 $openTags[] = $tag; // tag opened (didn't close itself)
3543 } elseif ( $tagType == 1 ) {
3544 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3545 array_pop( $openTags ); // tag closed
3553 * Grammatical transformations, needed for inflected languages
3554 * Invoked by putting {{grammar:case|word}} in a message
3556 * @param $word string
3557 * @param $case string
3560 function convertGrammar( $word, $case ) {
3561 global $wgGrammarForms;
3562 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3563 return $wgGrammarForms[$this->getCode()][$case][$word];
3568 * Get the grammar forms for the content language
3569 * @return array of grammar forms
3572 function getGrammarForms() {
3573 global $wgGrammarForms;
3574 if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
3575 return $wgGrammarForms[$this->getCode()];
3580 * Provides an alternative text depending on specified gender.
3581 * Usage {{gender:username|masculine|feminine|neutral}}.
3582 * username is optional, in which case the gender of current user is used,
3583 * but only in (some) interface messages; otherwise default gender is used.
3585 * If no forms are given, an empty string is returned. If only one form is
3586 * given, it will be returned unconditionally. These details are implied by
3587 * the caller and cannot be overridden in subclasses.
3589 * If more than one form is given, the default is to use the neutral one
3590 * if it is specified, and to use the masculine one otherwise. These
3591 * details can be overridden in subclasses.
3593 * @param $gender string
3594 * @param $forms array
3598 function gender( $gender, $forms ) {
3599 if ( !count( $forms ) ) {
3602 $forms = $this->preConvertPlural( $forms, 2 );
3603 if ( $gender === 'male' ) {
3606 if ( $gender === 'female' ) {
3609 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
3613 * Plural form transformations, needed for some languages.
3614 * For example, there are 3 form of plural in Russian and Polish,
3615 * depending on "count mod 10". See [[w:Plural]]
3616 * For English it is pretty simple.
3618 * Invoked by putting {{plural:count|wordform1|wordform2}}
3619 * or {{plural:count|wordform1|wordform2|wordform3}}
3621 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3623 * @param $count Integer: non-localized number
3624 * @param $forms Array: different plural forms
3625 * @return string Correct form of plural for $count in this language
3627 function convertPlural( $count, $forms ) {
3628 // Handle explicit n=pluralform cases
3629 foreach ( $forms as $index => $form ) {
3630 if ( preg_match( '/^\d+=/i', $form ) ) {
3631 $pos = strpos( $form, '=' );
3632 if ( substr( $form, 0, $pos ) === (string) $count ) {
3633 return substr( $form, $pos +
1 );
3635 unset( $forms[$index] );
3639 $forms = array_values( $forms );
3640 if ( !count( $forms ) ) {
3644 $pluralForm = $this->getPluralRuleIndexNumber( $count );
3645 $pluralForm = min( $pluralForm, count( $forms ) - 1 );
3646 return $forms[$pluralForm];
3650 * Checks that convertPlural was given an array and pads it to requested
3651 * amount of forms by copying the last one.
3653 * @param $count Integer: How many forms should there be at least
3654 * @param $forms Array of forms given to convertPlural
3655 * @return array Padded array of forms or an exception if not an array
3657 protected function preConvertPlural( /* Array */ $forms, $count ) {
3658 while ( count( $forms ) < $count ) {
3659 $forms[] = $forms[count( $forms ) - 1];
3665 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3666 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3667 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3668 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3671 * @param $str String: the validated block duration in English
3672 * @return string Somehow translated block duration
3673 * @see LanguageFi.php for example implementation
3675 function translateBlockExpiry( $str ) {
3676 $duration = SpecialBlock
::getSuggestedDurations( $this );
3677 foreach ( $duration as $show => $value ) {
3678 if ( strcmp( $str, $value ) == 0 ) {
3679 return htmlspecialchars( trim( $show ) );
3683 // Since usually only infinite or indefinite is only on list, so try
3684 // equivalents if still here.
3685 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3686 if ( in_array( $str, $indefs ) ) {
3687 foreach ( $indefs as $val ) {
3688 $show = array_search( $val, $duration, true );
3689 if ( $show !== false ) {
3690 return htmlspecialchars( trim( $show ) );
3695 // If all else fails, return a standard duration or timestamp description.
3696 $time = strtotime( $str, 0 );
3697 if ( $time === false ) { // Unknown format. Return it as-is in case.
3699 } elseif ( $time !== strtotime( $str, 1 ) ) { // It's a relative timestamp.
3700 // $time is relative to 0 so it's a duration length.
3701 return $this->formatDuration( $time );
3702 } else { // It's an absolute timestamp.
3703 if ( $time === 0 ) {
3704 // wfTimestamp() handles 0 as current time instead of epoch.
3705 return $this->timeanddate( '19700101000000' );
3707 return $this->timeanddate( $time );
3713 * languages like Chinese need to be segmented in order for the diff
3716 * @param $text String
3719 public function segmentForDiff( $text ) {
3724 * and unsegment to show the result
3726 * @param $text String
3729 public function unsegmentForDiff( $text ) {
3734 * Return the LanguageConverter used in the Language
3737 * @return LanguageConverter
3739 public function getConverter() {
3740 return $this->mConverter
;
3744 * convert text to all supported variants
3746 * @param $text string
3749 public function autoConvertToAllVariants( $text ) {
3750 return $this->mConverter
->autoConvertToAllVariants( $text );
3754 * convert text to different variants of a language.
3756 * @param $text string
3759 public function convert( $text ) {
3760 return $this->mConverter
->convert( $text );
3764 * Convert a Title object to a string in the preferred variant
3766 * @param $title Title
3769 public function convertTitle( $title ) {
3770 return $this->mConverter
->convertTitle( $title );
3774 * Convert a namespace index to a string in the preferred variant
3779 public function convertNamespace( $ns ) {
3780 return $this->mConverter
->convertNamespace( $ns );
3784 * Check if this is a language with variants
3788 public function hasVariants() {
3789 return count( $this->getVariants() ) > 1;
3793 * Check if the language has the specific variant
3796 * @param $variant string
3799 public function hasVariant( $variant ) {
3800 return (bool)$this->mConverter
->validateVariant( $variant );
3804 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3806 * @param $text string
3809 public function armourMath( $text ) {
3810 return $this->mConverter
->armourMath( $text );
3814 * Perform output conversion on a string, and encode for safe HTML output.
3815 * @param $text String text to be converted
3816 * @param $isTitle Bool whether this conversion is for the article title
3818 * @todo this should get integrated somewhere sane
3820 public function convertHtml( $text, $isTitle = false ) {
3821 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3825 * @param $key string
3828 public function convertCategoryKey( $key ) {
3829 return $this->mConverter
->convertCategoryKey( $key );
3833 * Get the list of variants supported by this language
3834 * see sample implementation in LanguageZh.php
3836 * @return array an array of language codes
3838 public function getVariants() {
3839 return $this->mConverter
->getVariants();
3845 public function getPreferredVariant() {
3846 return $this->mConverter
->getPreferredVariant();
3852 public function getDefaultVariant() {
3853 return $this->mConverter
->getDefaultVariant();
3859 public function getURLVariant() {
3860 return $this->mConverter
->getURLVariant();
3864 * If a language supports multiple variants, it is
3865 * possible that non-existing link in one variant
3866 * actually exists in another variant. this function
3867 * tries to find it. See e.g. LanguageZh.php
3869 * @param $link String: the name of the link
3870 * @param $nt Mixed: the title object of the link
3871 * @param $ignoreOtherCond Boolean: to disable other conditions when
3872 * we need to transclude a template or update a category's link
3873 * @return null the input parameters may be modified upon return
3875 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3876 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
3880 * If a language supports multiple variants, converts text
3881 * into an array of all possible variants of the text:
3882 * 'variant' => text in that variant
3884 * @deprecated since 1.17 Use autoConvertToAllVariants()
3886 * @param $text string
3890 public function convertLinkToAllVariants( $text ) {
3891 return $this->mConverter
->convertLinkToAllVariants( $text );
3895 * returns language specific options used by User::getPageRenderHash()
3896 * for example, the preferred language variant
3900 function getExtraHashOptions() {
3901 return $this->mConverter
->getExtraHashOptions();
3905 * For languages that support multiple variants, the title of an
3906 * article may be displayed differently in different variants. this
3907 * function returns the apporiate title defined in the body of the article.
3911 public function getParsedTitle() {
3912 return $this->mConverter
->getParsedTitle();
3916 * Prepare external link text for conversion. When the text is
3917 * a URL, it shouldn't be converted, and it'll be wrapped in
3918 * the "raw" tag (-{R| }-) to prevent conversion.
3920 * This function is called "markNoConversion" for historical
3923 * @param $text String: text to be used for external link
3924 * @param $noParse bool: wrap it without confirming it's a real URL first
3925 * @return string the tagged text
3927 public function markNoConversion( $text, $noParse = false ) {
3928 // Excluding protocal-relative URLs may avoid many false positives.
3929 if ( $noParse ||
preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
3930 return $this->mConverter
->markNoConversion( $text );
3937 * A regular expression to match legal word-trailing characters
3938 * which should be merged onto a link of the form [[foo]]bar.
3942 public function linkTrail() {
3943 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
3949 function getLangObj() {
3954 * Get the "parent" language which has a converter to convert a "compatible" language
3955 * (in another variant) to this language (eg. zh for zh-cn, but not en for en-gb).
3957 * @return Language|null
3960 public function getParentLanguage() {
3961 if ( $this->mParentLanguage
!== false ) {
3962 return $this->mParentLanguage
;
3965 $pieces = explode( '-', $this->getCode() );
3967 if ( !in_array( $code, LanguageConverter
::$languagesWithVariants ) ) {
3968 $this->mParentLanguage
= null;
3971 $lang = Language
::factory( $code );
3972 if ( !$lang->hasVariant( $this->getCode() ) ) {
3973 $this->mParentLanguage
= null;
3977 $this->mParentLanguage
= $lang;
3982 * Get the RFC 3066 code for this language object
3984 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
3985 * htmlspecialchars() or similar
3989 public function getCode() {
3990 return $this->mCode
;
3994 * Get the code in Bcp47 format which we can use
3995 * inside of html lang="" tags.
3997 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
3998 * htmlspecialchars() or similar.
4003 public function getHtmlCode() {
4004 if ( is_null( $this->mHtmlCode
) ) {
4005 $this->mHtmlCode
= wfBCP47( $this->getCode() );
4007 return $this->mHtmlCode
;
4011 * @param $code string
4013 public function setCode( $code ) {
4014 $this->mCode
= $code;
4015 // Ensure we don't leave incorrect cached data lying around
4016 $this->mHtmlCode
= null;
4017 $this->mParentLanguage
= false;
4021 * Get the name of a file for a certain language code
4022 * @param $prefix string Prepend this to the filename
4023 * @param $code string Language code
4024 * @param $suffix string Append this to the filename
4025 * @throws MWException
4026 * @return string $prefix . $mangledCode . $suffix
4028 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
4029 // Protect against path traversal
4030 if ( !Language
::isValidCode( $code )
4031 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
4033 throw new MWException( "Invalid language code \"$code\"" );
4036 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
4040 * Get the language code from a file name. Inverse of getFileName()
4041 * @param $filename string $prefix . $languageCode . $suffix
4042 * @param $prefix string Prefix before the language code
4043 * @param $suffix string Suffix after the language code
4044 * @return string Language code, or false if $prefix or $suffix isn't found
4046 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
4048 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
4049 preg_quote( $suffix, '/' ) . '/', $filename, $m );
4050 if ( !count( $m ) ) {
4053 return str_replace( '_', '-', strtolower( $m[1] ) );
4057 * @param $code string
4060 public static function getMessagesFileName( $code ) {
4062 $file = self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
4063 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
4068 * @param $code string
4071 public static function getClassFileName( $code ) {
4073 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
4077 * Get the first fallback for a given language.
4079 * @param $code string
4081 * @return bool|string
4083 public static function getFallbackFor( $code ) {
4084 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
4087 $fallbacks = self
::getFallbacksFor( $code );
4088 $first = array_shift( $fallbacks );
4094 * Get the ordered list of fallback languages.
4097 * @param $code string Language code
4100 public static function getFallbacksFor( $code ) {
4101 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
4104 $v = self
::getLocalisationCache()->getItem( $code, 'fallback' );
4105 $v = array_map( 'trim', explode( ',', $v ) );
4106 if ( $v[count( $v ) - 1] !== 'en' ) {
4114 * Get the ordered list of fallback languages, ending with the fallback
4115 * language chain for the site language.
4118 * @param string $code Language code
4119 * @return array array( fallbacks, site fallbacks )
4121 public static function getFallbacksIncludingSiteLanguage( $code ) {
4122 global $wgLanguageCode;
4124 // Usually, we will only store a tiny number of fallback chains, so we
4125 // keep them in static memory.
4126 $cacheKey = "{$code}-{$wgLanguageCode}";
4128 if ( !array_key_exists( $cacheKey, self
::$fallbackLanguageCache ) ) {
4129 $fallbacks = self
::getFallbacksFor( $code );
4131 // Append the site's fallback chain, including the site language itself
4132 $siteFallbacks = self
::getFallbacksFor( $wgLanguageCode );
4133 array_unshift( $siteFallbacks, $wgLanguageCode );
4135 // Eliminate any languages already included in the chain
4136 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
4138 self
::$fallbackLanguageCache[$cacheKey] = array( $fallbacks, $siteFallbacks );
4140 return self
::$fallbackLanguageCache[$cacheKey];
4144 * Get all messages for a given language
4145 * WARNING: this may take a long time. If you just need all message *keys*
4146 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
4148 * @param $code string
4152 public static function getMessagesFor( $code ) {
4153 return self
::getLocalisationCache()->getItem( $code, 'messages' );
4157 * Get a message for a given language
4159 * @param $key string
4160 * @param $code string
4164 public static function getMessageFor( $key, $code ) {
4165 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
4169 * Get all message keys for a given language. This is a faster alternative to
4170 * array_keys( Language::getMessagesFor( $code ) )
4173 * @param $code string Language code
4174 * @return array of message keys (strings)
4176 public static function getMessageKeysFor( $code ) {
4177 return self
::getLocalisationCache()->getSubItemList( $code, 'messages' );
4184 function fixVariableInNamespace( $talk ) {
4185 if ( strpos( $talk, '$1' ) === false ) {
4189 global $wgMetaNamespace;
4190 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
4192 # Allow grammar transformations
4193 # Allowing full message-style parsing would make simple requests
4194 # such as action=raw much more expensive than they need to be.
4195 # This will hopefully cover most cases.
4196 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
4197 array( &$this, 'replaceGrammarInNamespace' ), $talk );
4198 return str_replace( ' ', '_', $talk );
4205 function replaceGrammarInNamespace( $m ) {
4206 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
4210 * @throws MWException
4213 static function getCaseMaps() {
4214 static $wikiUpperChars, $wikiLowerChars;
4215 if ( isset( $wikiUpperChars ) ) {
4216 return array( $wikiUpperChars, $wikiLowerChars );
4219 wfProfileIn( __METHOD__
);
4220 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
4221 if ( $arr === false ) {
4222 throw new MWException(
4223 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
4225 $wikiUpperChars = $arr['wikiUpperChars'];
4226 $wikiLowerChars = $arr['wikiLowerChars'];
4227 wfProfileOut( __METHOD__
);
4228 return array( $wikiUpperChars, $wikiLowerChars );
4232 * Decode an expiry (block, protection, etc) which has come from the DB
4234 * @todo FIXME: why are we returnings DBMS-dependent strings???
4236 * @param $expiry String: Database expiry String
4237 * @param $format Bool|Int true to process using language functions, or TS_ constant
4238 * to return the expiry in a given timestamp
4242 public function formatExpiry( $expiry, $format = true ) {
4244 if ( $infinity === null ) {
4245 $infinity = wfGetDB( DB_SLAVE
)->getInfinity();
4248 if ( $expiry == '' ||
$expiry == $infinity ) {
4249 return $format === true
4250 ?
$this->getMessageFromDB( 'infiniteblock' )
4253 return $format === true
4254 ?
$this->timeanddate( $expiry, /* User preference timezone */ true )
4255 : wfTimestamp( $format, $expiry );
4261 * @param $seconds int|float
4262 * @param $format Array Optional
4263 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
4264 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
4265 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
4266 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
4269 function formatTimePeriod( $seconds, $format = array() ) {
4270 if ( !is_array( $format ) ) {
4271 $format = array( 'avoid' => $format ); // For backwards compatibility
4273 if ( !isset( $format['avoid'] ) ) {
4274 $format['avoid'] = false;
4276 if ( !isset( $format['noabbrevs' ] ) ) {
4277 $format['noabbrevs'] = false;
4279 $secondsMsg = wfMessage(
4280 $format['noabbrevs'] ?
'seconds' : 'seconds-abbrev' )->inLanguage( $this );
4281 $minutesMsg = wfMessage(
4282 $format['noabbrevs'] ?
'minutes' : 'minutes-abbrev' )->inLanguage( $this );
4283 $hoursMsg = wfMessage(
4284 $format['noabbrevs'] ?
'hours' : 'hours-abbrev' )->inLanguage( $this );
4285 $daysMsg = wfMessage(
4286 $format['noabbrevs'] ?
'days' : 'days-abbrev' )->inLanguage( $this );
4288 if ( round( $seconds * 10 ) < 100 ) {
4289 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
4290 $s = $secondsMsg->params( $s )->text();
4291 } elseif ( round( $seconds ) < 60 ) {
4292 $s = $this->formatNum( round( $seconds ) );
4293 $s = $secondsMsg->params( $s )->text();
4294 } elseif ( round( $seconds ) < 3600 ) {
4295 $minutes = floor( $seconds / 60 );
4296 $secondsPart = round( fmod( $seconds, 60 ) );
4297 if ( $secondsPart == 60 ) {
4301 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4303 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4304 } elseif ( round( $seconds ) <= 2 * 86400 ) {
4305 $hours = floor( $seconds / 3600 );
4306 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
4307 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
4308 if ( $secondsPart == 60 ) {
4312 if ( $minutes == 60 ) {
4316 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
4318 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4319 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
4320 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4323 $days = floor( $seconds / 86400 );
4324 if ( $format['avoid'] === 'avoidminutes' ) {
4325 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
4326 if ( $hours == 24 ) {
4330 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4332 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4333 } elseif ( $format['avoid'] === 'avoidseconds' ) {
4334 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
4335 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
4336 if ( $minutes == 60 ) {
4340 if ( $hours == 24 ) {
4344 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4346 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4348 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4350 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4352 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
4359 * Format a bitrate for output, using an appropriate
4360 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
4362 * This use base 1000. For base 1024 use formatSize(), for another base
4363 * see formatComputingNumbers()
4368 function formatBitrate( $bps ) {
4369 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
4373 * @param $size int Size of the unit
4374 * @param $boundary int Size boundary (1000, or 1024 in most cases)
4375 * @param $messageKey string Message key to be uesd
4378 function formatComputingNumbers( $size, $boundary, $messageKey ) {
4380 return str_replace( '$1', $this->formatNum( $size ),
4381 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
4384 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
4387 $maxIndex = count( $sizes ) - 1;
4388 while ( $size >= $boundary && $index < $maxIndex ) {
4393 // For small sizes no decimal places necessary
4396 // For MB and bigger two decimal places are smarter
4399 $msg = str_replace( '$1', $sizes[$index], $messageKey );
4401 $size = round( $size, $round );
4402 $text = $this->getMessageFromDB( $msg );
4403 return str_replace( '$1', $this->formatNum( $size ), $text );
4407 * Format a size in bytes for output, using an appropriate
4408 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
4410 * This method use base 1024. For base 1000 use formatBitrate(), for
4411 * another base see formatComputingNumbers()
4413 * @param $size int Size to format
4414 * @return string Plain text (not HTML)
4416 function formatSize( $size ) {
4417 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
4421 * Make a list item, used by various special pages
4423 * @param $page String Page link
4424 * @param $details String Text between brackets
4425 * @param $oppositedm Boolean Add the direction mark opposite to your
4426 * language, to display text properly
4429 function specialList( $page, $details, $oppositedm = true ) {
4430 $dirmark = ( $oppositedm ?
$this->getDirMark( true ) : '' ) .
4431 $this->getDirMark();
4432 $details = $details ?
$dirmark . $this->getMessageFromDB( 'word-separator' ) .
4433 wfMessage( 'parentheses' )->rawParams( $details )->inLanguage( $this )->escaped() : '';
4434 return $page . $details;
4438 * Generate (prev x| next x) (20|50|100...) type links for paging
4440 * @param $title Title object to link
4441 * @param $offset Integer offset parameter
4442 * @param $limit Integer limit parameter
4443 * @param $query array|String optional URL query parameter string
4444 * @param $atend Bool optional param for specified if this is the last page
4447 public function viewPrevNext( Title
$title, $offset, $limit, array $query = array(), $atend = false ) {
4448 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
4450 # Make 'previous' link
4451 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4452 if ( $offset > 0 ) {
4453 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
4454 $query, $prev, 'prevn-title', 'mw-prevlink' );
4456 $plink = htmlspecialchars( $prev );
4460 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4462 $nlink = htmlspecialchars( $next );
4464 $nlink = $this->numLink( $title, $offset +
$limit, $limit,
4465 $query, $next, 'prevn-title', 'mw-nextlink' );
4468 # Make links to set number of items per page
4469 $numLinks = array();
4470 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
4471 $numLinks[] = $this->numLink( $title, $offset, $num,
4472 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
4475 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
4476 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
4480 * Helper function for viewPrevNext() that generates links
4482 * @param $title Title object to link
4483 * @param $offset Integer offset parameter
4484 * @param $limit Integer limit parameter
4485 * @param $query Array extra query parameters
4486 * @param $link String text to use for the link; will be escaped
4487 * @param $tooltipMsg String name of the message to use as tooltip
4488 * @param $class String value of the "class" attribute of the link
4489 * @return String HTML fragment
4491 private function numLink( Title
$title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
4492 $query = array( 'limit' => $limit, 'offset' => $offset ) +
$query;
4493 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4494 return Html
::element( 'a', array( 'href' => $title->getLocalURL( $query ),
4495 'title' => $tooltip, 'class' => $class ), $link );
4499 * Get the conversion rule title, if any.
4503 public function getConvRuleTitle() {
4504 return $this->mConverter
->getConvRuleTitle();
4508 * Get the compiled plural rules for the language
4510 * @return array Associative array with plural form, and plural rule as key-value pairs
4512 public function getCompiledPluralRules() {
4513 $pluralRules = self
::$dataCache->getItem( strtolower( $this->mCode
), 'compiledPluralRules' );
4514 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4515 if ( !$pluralRules ) {
4516 foreach ( $fallbacks as $fallbackCode ) {
4517 $pluralRules = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
4518 if ( $pluralRules ) {
4523 return $pluralRules;
4527 * Get the plural rules for the language
4529 * @return array Associative array with plural form number and plural rule as key-value pairs
4531 public function getPluralRules() {
4532 $pluralRules = self
::$dataCache->getItem( strtolower( $this->mCode
), 'pluralRules' );
4533 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4534 if ( !$pluralRules ) {
4535 foreach ( $fallbacks as $fallbackCode ) {
4536 $pluralRules = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRules' );
4537 if ( $pluralRules ) {
4542 return $pluralRules;
4546 * Get the plural rule types for the language
4548 * @return array Associative array with plural form number and plural rule type as key-value pairs
4550 public function getPluralRuleTypes() {
4551 $pluralRuleTypes = self
::$dataCache->getItem( strtolower( $this->mCode
), 'pluralRuleTypes' );
4552 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4553 if ( !$pluralRuleTypes ) {
4554 foreach ( $fallbacks as $fallbackCode ) {
4555 $pluralRuleTypes = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRuleTypes' );
4556 if ( $pluralRuleTypes ) {
4561 return $pluralRuleTypes;
4565 * Find the index number of the plural rule appropriate for the given number
4566 * @return int The index number of the plural rule
4568 public function getPluralRuleIndexNumber( $number ) {
4569 $pluralRules = $this->getCompiledPluralRules();
4570 $form = CLDRPluralRuleEvaluator
::evaluateCompiled( $number, $pluralRules );
4575 * Find the plural rule type appropriate for the given number
4576 * For example, if the language is set to Arabic, getPluralType(5) should
4579 * @return string The name of the plural rule type, e.g. one, two, few, many
4581 public function getPluralRuleType( $number ) {
4582 $index = $this->getPluralRuleIndexNumber( $number );
4583 $pluralRuleTypes = $this->getPluralRuleTypes();
4584 if ( isset( $pluralRuleTypes[$index] ) ) {
4585 return $pluralRuleTypes[$index];