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 autoConvert( $text, $variant = false ) { return $text; }
53 function autoConvertToAllVariants( $text ) { return array( $this->mLang
->getCode() => $text ); }
54 function convert( $t ) { return $t; }
55 function convertTo( $text, $variant ) { return $text; }
56 function convertTitle( $t ) { return $t->getPrefixedText(); }
57 function convertNamespace( $ns ) { return $this->mLang
->getFormattedNsText( $ns ); }
58 function getVariants() { return array( $this->mLang
->getCode() ); }
59 function getVariantFallbacks( $variant ) { return $this->mLang
->getCode(); }
60 function getPreferredVariant() { return $this->mLang
->getCode(); }
61 function getDefaultVariant() { return $this->mLang
->getCode(); }
62 function getURLVariant() { return ''; }
63 function getConvRuleTitle() { return false; }
64 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
65 function getExtraHashOptions() { return ''; }
66 function getParsedTitle() { return ''; }
67 function markNoConversion( $text, $noParse = false ) { return $text; }
68 function convertCategoryKey( $key ) { return $key; }
69 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
70 function armourMath( $text ) { return $text; }
71 function validateVariant( $variant = null ) { return $variant === $this->mLang
->getCode() ?
$variant : null; }
72 function translate( $text, $variant ) { return $text; }
76 * Internationalisation code
82 * @var LanguageConverter
86 public $mVariants, $mCode, $mLoaded = false;
87 public $mMagicExtensions = array(), $mMagicHookDone = false;
88 private $mHtmlCode = null, $mParentLanguage = false;
90 public $dateFormatStrings = array();
91 public $mExtendedSpecialPageAliases;
93 protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
96 * ReplacementArray object caches
98 public $transformData = array();
101 * @var LocalisationCache
103 static public $dataCache;
105 static public $mLangObjCache = array();
107 static public $mWeekdayMsgs = array(
108 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
112 static public $mWeekdayAbbrevMsgs = array(
113 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
116 static public $mMonthMsgs = array(
117 'january', 'february', 'march', 'april', 'may_long', 'june',
118 'july', 'august', 'september', 'october', 'november',
121 static public $mMonthGenMsgs = array(
122 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
123 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
126 static public $mMonthAbbrevMsgs = array(
127 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
128 'sep', 'oct', 'nov', 'dec'
131 static public $mIranianCalendarMonthMsgs = array(
132 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
133 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
134 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
135 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
138 static public $mHebrewCalendarMonthMsgs = array(
139 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
140 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
141 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
142 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
143 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
146 static public $mHebrewCalendarMonthGenMsgs = array(
147 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
148 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
149 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
150 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
151 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
154 static public $mHijriCalendarMonthMsgs = array(
155 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
156 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
157 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
158 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
165 static public $durationIntervals = array(
166 'millennia' => 31556952000,
167 'centuries' => 3155695200,
168 'decades' => 315569520,
169 'years' => 31556952, // 86400 * ( 365 + ( 24 * 3 + 25 ) / 400 )
178 * Cache for language fallbacks.
179 * @see Language::getFallbacksIncludingSiteLanguage
183 static private $fallbackLanguageCache = array();
186 * Get a cached or new language object for a given language code
187 * @param $code String
190 static function factory( $code ) {
191 global $wgDummyLanguageCodes, $wgLangObjCacheSize;
193 if ( isset( $wgDummyLanguageCodes[$code] ) ) {
194 $code = $wgDummyLanguageCodes[$code];
197 // get the language object to process
198 $langObj = isset( self
::$mLangObjCache[$code] )
199 ? self
::$mLangObjCache[$code]
200 : self
::newFromCode( $code );
202 // merge the language object in to get it up front in the cache
203 self
::$mLangObjCache = array_merge( array( $code => $langObj ), self
::$mLangObjCache );
204 // get rid of the oldest ones in case we have an overflow
205 self
::$mLangObjCache = array_slice( self
::$mLangObjCache, 0, $wgLangObjCacheSize, true );
211 * Create a language object for a given language code
212 * @param $code String
213 * @throws MWException
216 protected static function newFromCode( $code ) {
217 // Protect against path traversal below
218 if ( !Language
::isValidCode( $code )
219 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
221 throw new MWException( "Invalid language code \"$code\"" );
224 if ( !Language
::isValidBuiltInCode( $code ) ) {
225 // It's not possible to customise this code with class files, so
226 // just return a Language object. This is to support uselang= hacks.
227 $lang = new Language
;
228 $lang->setCode( $code );
232 // Check if there is a language class for the code
233 $class = self
::classFromCode( $code );
234 self
::preloadLanguageClass( $class );
235 if ( MWInit
::classExists( $class ) ) {
240 // Keep trying the fallback list until we find an existing class
241 $fallbacks = Language
::getFallbacksFor( $code );
242 foreach ( $fallbacks as $fallbackCode ) {
243 if ( !Language
::isValidBuiltInCode( $fallbackCode ) ) {
244 throw new MWException( "Invalid fallback '$fallbackCode' in fallback sequence for '$code'" );
247 $class = self
::classFromCode( $fallbackCode );
248 self
::preloadLanguageClass( $class );
249 if ( MWInit
::classExists( $class ) ) {
250 $lang = Language
::newFromCode( $fallbackCode );
251 $lang->setCode( $code );
256 throw new MWException( "Invalid fallback sequence for language '$code'" );
260 * Checks whether any localisation is available for that language tag
261 * in MediaWiki (MessagesXx.php exists).
263 * @param string $code Language tag (in lower case)
264 * @return bool Whether language is supported
267 public static function isSupportedLanguage( $code ) {
268 return $code === strtolower( $code ) && is_readable( self
::getMessagesFileName( $code ) );
272 * Returns true if a language code string is a well-formed language tag
273 * according to RFC 5646.
274 * This function only checks well-formedness; it doesn't check that
275 * language, script or variant codes actually exist in the repositories.
277 * Based on regexes by Mark Davis of the Unicode Consortium:
278 * http://unicode.org/repos/cldr/trunk/tools/java/org/unicode/cldr/util/data/langtagRegex.txt
280 * @param $code string
281 * @param $lenient boolean Whether to allow '_' as separator. The default is only '-'.
286 public static function isWellFormedLanguageTag( $code, $lenient = false ) {
289 $alphanum = '[a-z0-9]';
290 $x = 'x'; # private use singleton
291 $singleton = '[a-wy-z]'; # other singleton
292 $s = $lenient ?
'[-_]' : '-';
294 $language = "$alpha{2,8}|$alpha{2,3}$s$alpha{3}";
295 $script = "$alpha{4}"; # ISO 15924
296 $region = "(?:$alpha{2}|$digit{3})"; # ISO 3166-1 alpha-2 or UN M.49
297 $variant = "(?:$alphanum{5,8}|$digit$alphanum{3})";
298 $extension = "$singleton(?:$s$alphanum{2,8})+";
299 $privateUse = "$x(?:$s$alphanum{1,8})+";
301 # Define certain grandfathered codes, since otherwise the regex is pretty useless.
302 # Since these are limited, this is safe even later changes to the registry --
303 # the only oddity is that it might change the type of the tag, and thus
304 # the results from the capturing groups.
305 # http://www.iana.org/assignments/language-subtag-registry
307 $grandfathered = "en{$s}GB{$s}oed"
308 . "|i{$s}(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)"
309 . "|no{$s}(?:bok|nyn)"
310 . "|sgn{$s}(?:BE{$s}(?:fr|nl)|CH{$s}de)"
311 . "|zh{$s}min{$s}nan";
313 $variantList = "$variant(?:$s$variant)*";
314 $extensionList = "$extension(?:$s$extension)*";
316 $langtag = "(?:($language)"
319 . "(?:$s$variantList)?"
320 . "(?:$s$extensionList)?"
321 . "(?:$s$privateUse)?)";
323 # The final breakdown, with capturing groups for each of these components
324 # The variants, extensions, grandfathered, and private-use may have interior '-'
326 $root = "^(?:$langtag|$privateUse|$grandfathered)$";
328 return (bool)preg_match( "/$root/", strtolower( $code ) );
332 * Returns true if a language code string is of a valid form, whether or
333 * not it exists. This includes codes which are used solely for
334 * customisation via the MediaWiki namespace.
336 * @param $code string
340 public static function isValidCode( $code ) {
341 static $cache = array();
342 if ( isset( $cache[$code] ) ) {
343 return $cache[$code];
345 // People think language codes are html safe, so enforce it.
346 // Ideally we should only allow a-zA-Z0-9-
347 // but, .+ and other chars are often used for {{int:}} hacks
348 // see bugs 37564, 37587, 36938
350 strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code )
351 && !preg_match( Title
::getTitleInvalidRegex(), $code );
353 return $cache[$code];
357 * Returns true if a language code is of a valid form for the purposes of
358 * internal customisation of MediaWiki, via Messages*.php.
360 * @param $code string
362 * @throws MWException
366 public static function isValidBuiltInCode( $code ) {
368 if ( !is_string( $code ) ) {
369 if ( is_object( $code ) ) {
370 $addmsg = " of class " . get_class( $code );
374 $type = gettype( $code );
375 throw new MWException( __METHOD__
. " must be passed a string, $type given$addmsg" );
378 return (bool)preg_match( '/^[a-z0-9-]{2,}$/i', $code );
382 * Returns true if a language code is an IETF tag known to MediaWiki.
384 * @param $code string
389 public static function isKnownLanguageTag( $tag ) {
390 static $coreLanguageNames;
392 // Quick escape for invalid input to avoid exceptions down the line
393 // when code tries to process tags which are not valid at all.
394 if ( !self
::isValidBuiltInCode( $tag ) ) {
398 if ( $coreLanguageNames === null ) {
399 include MWInit
::compiledPath( 'languages/Names.php' );
402 if ( isset( $coreLanguageNames[$tag] )
403 || self
::fetchLanguageName( $tag, $tag ) !== ''
413 * @return String Name of the language class
415 public static function classFromCode( $code ) {
416 if ( $code == 'en' ) {
419 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
424 * Includes language class files
426 * @param $class string Name of the language class
428 public static function preloadLanguageClass( $class ) {
431 if ( $class === 'Language' ) {
435 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
436 include_once "$IP/languages/classes/$class.php";
441 * Get the LocalisationCache instance
443 * @return LocalisationCache
445 public static function getLocalisationCache() {
446 if ( is_null( self
::$dataCache ) ) {
447 global $wgLocalisationCacheConf;
448 $class = $wgLocalisationCacheConf['class'];
449 self
::$dataCache = new $class( $wgLocalisationCacheConf );
451 return self
::$dataCache;
454 function __construct() {
455 $this->mConverter
= new FakeConverter( $this );
456 // Set the code to the name of the descendant
457 if ( get_class( $this ) == 'Language' ) {
460 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
462 self
::getLocalisationCache();
466 * Reduce memory usage
468 function __destruct() {
469 foreach ( $this as $name => $value ) {
470 unset( $this->$name );
475 * Hook which will be called if this is the content language.
476 * Descendants can use this to register hook functions or modify globals
478 function initContLang() { }
481 * Same as getFallbacksFor for current language.
483 * @deprecated in 1.19
485 function getFallbackLanguageCode() {
486 wfDeprecated( __METHOD__
, '1.19' );
487 return self
::getFallbackFor( $this->mCode
);
494 function getFallbackLanguages() {
495 return self
::getFallbacksFor( $this->mCode
);
499 * Exports $wgBookstoreListEn
502 function getBookstoreList() {
503 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
507 * Returns an array of localised namespaces indexed by their numbers. If the namespace is not
508 * available in localised form, it will be included in English.
512 public function getNamespaces() {
513 if ( is_null( $this->namespaceNames
) ) {
514 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
516 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
517 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
519 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
521 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
522 if ( $wgMetaNamespaceTalk ) {
523 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
525 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
526 $this->namespaceNames
[NS_PROJECT_TALK
] =
527 $this->fixVariableInNamespace( $talk );
530 # Sometimes a language will be localised but not actually exist on this wiki.
531 foreach ( $this->namespaceNames
as $key => $text ) {
532 if ( !isset( $validNamespaces[$key] ) ) {
533 unset( $this->namespaceNames
[$key] );
537 # The above mixing may leave namespaces out of canonical order.
538 # Re-order by namespace ID number...
539 ksort( $this->namespaceNames
);
541 wfRunHooks( 'LanguageGetNamespaces', array( &$this->namespaceNames
) );
543 return $this->namespaceNames
;
547 * Arbitrarily set all of the namespace names at once. Mainly used for testing
548 * @param $namespaces Array of namespaces (id => name)
550 public function setNamespaces( array $namespaces ) {
551 $this->namespaceNames
= $namespaces;
552 $this->mNamespaceIds
= null;
556 * Resets all of the namespace caches. Mainly used for testing
558 public function resetNamespaces() {
559 $this->namespaceNames
= null;
560 $this->mNamespaceIds
= null;
561 $this->namespaceAliases
= null;
565 * A convenience function that returns the same thing as
566 * getNamespaces() except with the array values changed to ' '
567 * where it found '_', useful for producing output to be displayed
568 * e.g. in <select> forms.
572 function getFormattedNamespaces() {
573 $ns = $this->getNamespaces();
574 foreach ( $ns as $k => $v ) {
575 $ns[$k] = strtr( $v, '_', ' ' );
581 * Get a namespace value by key
583 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
584 * echo $mw_ns; // prints 'MediaWiki'
587 * @param $index Int: the array key of the namespace to return
588 * @return mixed, string if the namespace value exists, otherwise false
590 function getNsText( $index ) {
591 $ns = $this->getNamespaces();
592 return isset( $ns[$index] ) ?
$ns[$index] : false;
596 * A convenience function that returns the same thing as
597 * getNsText() except with '_' changed to ' ', useful for
601 * $mw_ns = $wgContLang->getFormattedNsText( NS_MEDIAWIKI_TALK );
602 * echo $mw_ns; // prints 'MediaWiki talk'
605 * @param int $index The array key of the namespace to return
606 * @return string Namespace name without underscores (empty string if namespace does not exist)
608 function getFormattedNsText( $index ) {
609 $ns = $this->getNsText( $index );
610 return strtr( $ns, '_', ' ' );
614 * Returns gender-dependent namespace alias if available.
615 * @param $index Int: namespace index
616 * @param $gender String: gender key (male, female... )
620 function getGenderNsText( $index, $gender ) {
621 global $wgExtraGenderNamespaces;
623 $ns = $wgExtraGenderNamespaces + self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
624 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
628 * Whether this language makes distinguishes genders for example in
633 function needsGenderDistinction() {
634 global $wgExtraGenderNamespaces, $wgExtraNamespaces;
635 if ( count( $wgExtraGenderNamespaces ) > 0 ) {
636 // $wgExtraGenderNamespaces overrides everything
638 } elseif ( isset( $wgExtraNamespaces[NS_USER
] ) && isset( $wgExtraNamespaces[NS_USER_TALK
] ) ) {
639 /// @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
640 // $wgExtraNamespaces overrides any gender aliases specified in i18n files
643 // Check what is in i18n files
644 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
645 return count( $aliases ) > 0;
650 * Get a namespace key by value, case insensitive.
651 * Only matches namespace names for the current language, not the
652 * canonical ones defined in Namespace.php.
654 * @param $text String
655 * @return mixed An integer if $text is a valid value otherwise false
657 function getLocalNsIndex( $text ) {
658 $lctext = $this->lc( $text );
659 $ids = $this->getNamespaceIds();
660 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
666 function getNamespaceAliases() {
667 if ( is_null( $this->namespaceAliases
) ) {
668 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
672 foreach ( $aliases as $name => $index ) {
673 if ( $index === NS_PROJECT_TALK
) {
674 unset( $aliases[$name] );
675 $name = $this->fixVariableInNamespace( $name );
676 $aliases[$name] = $index;
681 global $wgExtraGenderNamespaces;
682 $genders = $wgExtraGenderNamespaces +
(array)self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
683 foreach ( $genders as $index => $forms ) {
684 foreach ( $forms as $alias ) {
685 $aliases[$alias] = $index;
689 $this->namespaceAliases
= $aliases;
691 return $this->namespaceAliases
;
697 function getNamespaceIds() {
698 if ( is_null( $this->mNamespaceIds
) ) {
699 global $wgNamespaceAliases;
700 # Put namespace names and aliases into a hashtable.
701 # If this is too slow, then we should arrange it so that it is done
702 # before caching. The catch is that at pre-cache time, the above
703 # class-specific fixup hasn't been done.
704 $this->mNamespaceIds
= array();
705 foreach ( $this->getNamespaces() as $index => $name ) {
706 $this->mNamespaceIds
[$this->lc( $name )] = $index;
708 foreach ( $this->getNamespaceAliases() as $name => $index ) {
709 $this->mNamespaceIds
[$this->lc( $name )] = $index;
711 if ( $wgNamespaceAliases ) {
712 foreach ( $wgNamespaceAliases as $name => $index ) {
713 $this->mNamespaceIds
[$this->lc( $name )] = $index;
717 return $this->mNamespaceIds
;
721 * Get a namespace key by value, case insensitive. Canonical namespace
722 * names override custom ones defined for the current language.
724 * @param $text String
725 * @return mixed An integer if $text is a valid value otherwise false
727 function getNsIndex( $text ) {
728 $lctext = $this->lc( $text );
729 $ns = MWNamespace
::getCanonicalIndex( $lctext );
730 if ( $ns !== null ) {
733 $ids = $this->getNamespaceIds();
734 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
738 * short names for language variants used for language conversion links.
740 * @param $code String
741 * @param $usemsg bool Use the "variantname-xyz" message if it exists
744 function getVariantname( $code, $usemsg = true ) {
745 $msg = "variantname-$code";
746 if ( $usemsg && wfMessage( $msg )->exists() ) {
747 return $this->getMessageFromDB( $msg );
749 $name = self
::fetchLanguageName( $code );
751 return $name; # if it's defined as a language name, show that
753 # otherwise, output the language code
759 * @param $name string
762 function specialPage( $name ) {
763 $aliases = $this->getSpecialPageAliases();
764 if ( isset( $aliases[$name][0] ) ) {
765 $name = $aliases[$name][0];
767 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
773 function getDatePreferences() {
774 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
780 function getDateFormats() {
781 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
785 * @return array|string
787 function getDefaultDateFormat() {
788 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
789 if ( $df === 'dmy or mdy' ) {
790 global $wgAmericanDates;
791 return $wgAmericanDates ?
'mdy' : 'dmy';
800 function getDatePreferenceMigrationMap() {
801 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
808 function getImageFile( $image ) {
809 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
815 function getExtraUserToggles() {
816 return (array)self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
823 function getUserToggle( $tog ) {
824 return $this->getMessageFromDB( "tog-$tog" );
828 * Get native language names, indexed by code.
829 * Only those defined in MediaWiki, no other data like CLDR.
830 * If $customisedOnly is true, only returns codes with a messages file
832 * @param $customisedOnly bool
835 * @deprecated in 1.20, use fetchLanguageNames()
837 public static function getLanguageNames( $customisedOnly = false ) {
838 return self
::fetchLanguageNames( null, $customisedOnly ?
'mwfile' : 'mw' );
842 * Get translated language names. This is done on best effort and
843 * by default this is exactly the same as Language::getLanguageNames.
844 * The CLDR extension provides translated names.
845 * @param $code String Language code.
846 * @return Array language code => language name
848 * @deprecated in 1.20, use fetchLanguageNames()
850 public static function getTranslatedLanguageNames( $code ) {
851 return self
::fetchLanguageNames( $code, 'all' );
855 * Get an array of language names, indexed by code.
856 * @param $inLanguage null|string: Code of language in which to return the names
857 * Use null for autonyms (native names)
858 * @param $include string:
859 * 'all' all available languages
860 * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
861 * 'mwfile' only if the language is in 'mw' *and* has a message file
862 * @return array: language code => language name
865 public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
866 global $wgExtraLanguageNames;
867 static $coreLanguageNames;
869 if ( $coreLanguageNames === null ) {
870 include MWInit
::compiledPath( 'languages/Names.php' );
876 # TODO: also include when $inLanguage is null, when this code is more efficient
877 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
880 $mwNames = $wgExtraLanguageNames +
$coreLanguageNames;
881 foreach ( $mwNames as $mwCode => $mwName ) {
882 # - Prefer own MediaWiki native name when not using the hook
883 # - For other names just add if not added through the hook
884 if ( $mwCode === $inLanguage ||
!isset( $names[$mwCode] ) ) {
885 $names[$mwCode] = $mwName;
889 if ( $include === 'all' ) {
894 $coreCodes = array_keys( $mwNames );
895 foreach ( $coreCodes as $coreCode ) {
896 $returnMw[$coreCode] = $names[$coreCode];
899 if ( $include === 'mwfile' ) {
900 $namesMwFile = array();
901 # We do this using a foreach over the codes instead of a directory
902 # loop so that messages files in extensions will work correctly.
903 foreach ( $returnMw as $code => $value ) {
904 if ( is_readable( self
::getMessagesFileName( $code ) ) ) {
905 $namesMwFile[$code] = $names[$code];
910 # 'mw' option; default if it's not one of the other two options (all/mwfile)
915 * @param $code string: The code of the language for which to get the name
916 * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
917 * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
918 * @return string: Language name or empty
921 public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
922 $array = self
::fetchLanguageNames( $inLanguage, $include );
923 return !array_key_exists( $code, $array ) ?
'' : $array[$code];
927 * Get a message from the MediaWiki namespace.
929 * @param $msg String: message name
932 function getMessageFromDB( $msg ) {
933 return wfMessage( $msg )->inLanguage( $this )->text();
937 * Get the native language name of $code.
938 * Only if defined in MediaWiki, no other data like CLDR.
939 * @param $code string
941 * @deprecated in 1.20, use fetchLanguageName()
943 function getLanguageName( $code ) {
944 return self
::fetchLanguageName( $code );
951 function getMonthName( $key ) {
952 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
958 function getMonthNamesArray() {
959 $monthNames = array( '' );
960 for ( $i = 1; $i < 13; $i++
) {
961 $monthNames[] = $this->getMonthName( $i );
970 function getMonthNameGen( $key ) {
971 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
978 function getMonthAbbreviation( $key ) {
979 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
985 function getMonthAbbreviationsArray() {
986 $monthNames = array( '' );
987 for ( $i = 1; $i < 13; $i++
) {
988 $monthNames[] = $this->getMonthAbbreviation( $i );
997 function getWeekdayName( $key ) {
998 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
1002 * @param $key string
1005 function getWeekdayAbbreviation( $key ) {
1006 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
1010 * @param $key string
1013 function getIranianCalendarMonthName( $key ) {
1014 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
1018 * @param $key string
1021 function getHebrewCalendarMonthName( $key ) {
1022 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
1026 * @param $key string
1029 function getHebrewCalendarMonthNameGen( $key ) {
1030 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
1034 * @param $key string
1037 function getHijriCalendarMonthName( $key ) {
1038 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
1042 * This is a workalike of PHP's date() function, but with better
1043 * internationalisation, a reduced set of format characters, and a better
1046 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrUeIOPTZ. See
1047 * the PHP manual for definitions. There are a number of extensions, which
1050 * xn Do not translate digits of the next numeric format character
1051 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
1052 * xr Use roman numerals for the next numeric format character
1053 * xh Use hebrew numerals for the next numeric format character
1055 * xg Genitive month name
1057 * xij j (day number) in Iranian calendar
1058 * xiF F (month name) in Iranian calendar
1059 * xin n (month number) in Iranian calendar
1060 * xiy y (two digit year) in Iranian calendar
1061 * xiY Y (full year) in Iranian calendar
1063 * xjj j (day number) in Hebrew calendar
1064 * xjF F (month name) in Hebrew calendar
1065 * xjt t (days in month) in Hebrew calendar
1066 * xjx xg (genitive month name) in Hebrew calendar
1067 * xjn n (month number) in Hebrew calendar
1068 * xjY Y (full year) in Hebrew calendar
1070 * xmj j (day number) in Hijri calendar
1071 * xmF F (month name) in Hijri calendar
1072 * xmn n (month number) in Hijri calendar
1073 * xmY Y (full year) in Hijri calendar
1075 * xkY Y (full year) in Thai solar calendar. Months and days are
1076 * identical to the Gregorian calendar
1077 * xoY Y (full year) in Minguo calendar or Juche year.
1078 * Months and days are identical to the
1079 * Gregorian calendar
1080 * xtY Y (full year) in Japanese nengo. Months and days are
1081 * identical to the Gregorian calendar
1083 * Characters enclosed in double quotes will be considered literal (with
1084 * the quotes themselves removed). Unmatched quotes will be considered
1085 * literal quotes. Example:
1087 * "The month is" F => The month is January
1090 * Backslash escaping is also supported.
1092 * Input timestamp is assumed to be pre-normalized to the desired local
1093 * time zone, if any. Note that the format characters crUeIOPTZ will assume
1094 * $ts is UTC if $zone is not given.
1096 * @param $format String
1097 * @param $ts String: 14-character timestamp
1100 * @param $zone DateTimeZone: Timezone of $ts
1101 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
1103 * @throws MWException
1106 function sprintfDate( $format, $ts, DateTimeZone
$zone = null ) {
1111 $dateTimeObj = false;
1120 if ( strlen( $ts ) !== 14 ) {
1121 throw new MWException( __METHOD__
. ": The timestamp $ts should have 14 characters" );
1124 if ( !ctype_digit( $ts ) ) {
1125 throw new MWException( __METHOD__
. ": The timestamp $ts should be a number" );
1128 for ( $p = 0; $p < strlen( $format ); $p++
) {
1130 $code = $format[$p];
1131 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
1132 $code .= $format[++
$p];
1135 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
1136 $code .= $format[++
$p];
1147 $rawToggle = !$rawToggle;
1156 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
1160 $hebrew = self
::tsToHebrew( $ts );
1162 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
1165 $num = substr( $ts, 6, 2 );
1168 if ( !$dateTimeObj ) {
1169 $dateTimeObj = DateTime
::createFromFormat(
1170 'YmdHis', $ts, $zone ?
: new DateTimeZone( 'UTC' )
1173 $s .= $this->getWeekdayAbbreviation( $dateTimeObj->format( 'w' ) +
1 );
1176 $num = intval( substr( $ts, 6, 2 ) );
1180 $iranian = self
::tsToIranian( $ts );
1186 $hijri = self
::tsToHijri( $ts );
1192 $hebrew = self
::tsToHebrew( $ts );
1197 if ( !$dateTimeObj ) {
1198 $dateTimeObj = DateTime
::createFromFormat(
1199 'YmdHis', $ts, $zone ?
: new DateTimeZone( 'UTC' )
1202 $s .= $this->getWeekdayName( $dateTimeObj->format( 'w' ) +
1 );
1205 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
1209 $iranian = self
::tsToIranian( $ts );
1211 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
1215 $hijri = self
::tsToHijri( $ts );
1217 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
1221 $hebrew = self
::tsToHebrew( $ts );
1223 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
1226 $num = substr( $ts, 4, 2 );
1229 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
1232 $num = intval( substr( $ts, 4, 2 ) );
1236 $iranian = self
::tsToIranian( $ts );
1242 $hijri = self
::tsToHijri ( $ts );
1248 $hebrew = self
::tsToHebrew( $ts );
1254 $hebrew = self
::tsToHebrew( $ts );
1259 $num = substr( $ts, 0, 4 );
1263 $iranian = self
::tsToIranian( $ts );
1269 $hijri = self
::tsToHijri( $ts );
1275 $hebrew = self
::tsToHebrew( $ts );
1281 $thai = self
::tsToYear( $ts, 'thai' );
1287 $minguo = self
::tsToYear( $ts, 'minguo' );
1293 $tenno = self
::tsToYear( $ts, 'tenno' );
1298 $num = substr( $ts, 2, 2 );
1302 $iranian = self
::tsToIranian( $ts );
1304 $num = substr( $iranian[0], -2 );
1307 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
1310 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
1313 $h = substr( $ts, 8, 2 );
1314 $num = $h %
12 ?
$h %
12 : 12;
1317 $num = intval( substr( $ts, 8, 2 ) );
1320 $h = substr( $ts, 8, 2 );
1321 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
1324 $num = substr( $ts, 8, 2 );
1327 $num = substr( $ts, 10, 2 );
1330 $num = substr( $ts, 12, 2 );
1338 // Pass through string from $dateTimeObj->format()
1339 if ( !$dateTimeObj ) {
1340 $dateTimeObj = DateTime
::createFromFormat(
1341 'YmdHis', $ts, $zone ?
: new DateTimeZone( 'UTC' )
1344 $s .= $dateTimeObj->format( $code );
1356 // Pass through number from $dateTimeObj->format()
1357 if ( !$dateTimeObj ) {
1358 $dateTimeObj = DateTime
::createFromFormat(
1359 'YmdHis', $ts, $zone ?
: new DateTimeZone( 'UTC' )
1362 $num = $dateTimeObj->format( $code );
1365 # Backslash escaping
1366 if ( $p < strlen( $format ) - 1 ) {
1367 $s .= $format[++
$p];
1374 if ( $p < strlen( $format ) - 1 ) {
1375 $endQuote = strpos( $format, '"', $p +
1 );
1376 if ( $endQuote === false ) {
1377 # No terminating quote, assume literal "
1380 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1384 # Quote at end of string, assume literal "
1391 if ( $num !== false ) {
1392 if ( $rawToggle ||
$raw ) {
1395 } elseif ( $roman ) {
1396 $s .= Language
::romanNumeral( $num );
1398 } elseif ( $hebrewNum ) {
1399 $s .= self
::hebrewNumeral( $num );
1402 $s .= $this->formatNum( $num, true );
1409 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1410 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1413 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1414 * Gregorian dates to Iranian dates. Originally written in C, it
1415 * is released under the terms of GNU Lesser General Public
1416 * License. Conversion to PHP was performed by Niklas Laxström.
1418 * Link: http://www.farsiweb.info/jalali/jalali.c
1424 private static function tsToIranian( $ts ) {
1425 $gy = substr( $ts, 0, 4 ) -1600;
1426 $gm = substr( $ts, 4, 2 ) -1;
1427 $gd = substr( $ts, 6, 2 ) -1;
1429 # Days passed from the beginning (including leap years)
1431 +
floor( ( $gy +
3 ) / 4 )
1432 - floor( ( $gy +
99 ) / 100 )
1433 +
floor( ( $gy +
399 ) / 400 );
1435 // Add days of the past months of this year
1436 for ( $i = 0; $i < $gm; $i++
) {
1437 $gDayNo +
= self
::$GREG_DAYS[$i];
1441 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1445 // Days passed in current month
1446 $gDayNo +
= (int)$gd;
1448 $jDayNo = $gDayNo - 79;
1450 $jNp = floor( $jDayNo / 12053 );
1453 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1456 if ( $jDayNo >= 366 ) {
1457 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1458 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1461 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1462 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1468 return array( $jy, $jm, $jd );
1472 * Converting Gregorian dates to Hijri dates.
1474 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1476 * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1482 private static function tsToHijri( $ts ) {
1483 $year = substr( $ts, 0, 4 );
1484 $month = substr( $ts, 4, 2 );
1485 $day = substr( $ts, 6, 2 );
1493 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1494 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1497 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1498 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1499 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1502 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1503 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1506 $zl = $zjd -1948440 +
10632;
1507 $zn = (int)( ( $zl - 1 ) / 10631 );
1508 $zl = $zl - 10631 * $zn +
354;
1509 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1510 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1511 $zm = (int)( ( 24 * $zl ) / 709 );
1512 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1513 $zy = 30 * $zn +
$zj - 30;
1515 return array( $zy, $zm, $zd );
1519 * Converting Gregorian dates to Hebrew dates.
1521 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1522 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1523 * to translate the relevant functions into PHP and release them under
1526 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1527 * and Adar II is 14. In a non-leap year, Adar is 6.
1533 private static function tsToHebrew( $ts ) {
1535 $year = substr( $ts, 0, 4 );
1536 $month = substr( $ts, 4, 2 );
1537 $day = substr( $ts, 6, 2 );
1539 # Calculate Hebrew year
1540 $hebrewYear = $year +
3760;
1542 # Month number when September = 1, August = 12
1544 if ( $month > 12 ) {
1551 # Calculate day of year from 1 September
1553 for ( $i = 1; $i < $month; $i++
) {
1557 # Check if the year is leap
1558 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1561 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1568 # Calculate the start of the Hebrew year
1569 $start = self
::hebrewYearStart( $hebrewYear );
1571 # Calculate next year's start
1572 if ( $dayOfYear <= $start ) {
1573 # Day is before the start of the year - it is the previous year
1575 $nextStart = $start;
1579 # Add days since previous year's 1 September
1581 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1585 # Start of the new (previous) year
1586 $start = self
::hebrewYearStart( $hebrewYear );
1589 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1592 # Calculate Hebrew day of year
1593 $hebrewDayOfYear = $dayOfYear - $start;
1595 # Difference between year's days
1596 $diff = $nextStart - $start;
1597 # Add 12 (or 13 for leap years) days to ignore the difference between
1598 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1599 # difference is only about the year type
1600 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1606 # Check the year pattern, and is leap year
1607 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1608 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1609 # and non-leap years
1610 $yearPattern = $diff %
30;
1611 # Check if leap year
1612 $isLeap = $diff >= 30;
1614 # Calculate day in the month from number of day in the Hebrew year
1615 # Don't check Adar - if the day is not in Adar, we will stop before;
1616 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1617 $hebrewDay = $hebrewDayOfYear;
1620 while ( $hebrewMonth <= 12 ) {
1621 # Calculate days in this month
1622 if ( $isLeap && $hebrewMonth == 6 ) {
1623 # Adar in a leap year
1625 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1627 if ( $hebrewDay <= $days ) {
1631 # Subtract the days of Adar I
1632 $hebrewDay -= $days;
1635 if ( $hebrewDay <= $days ) {
1641 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1642 # Cheshvan in a complete year (otherwise as the rule below)
1644 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1645 # Kislev in an incomplete year (otherwise as the rule below)
1648 # Odd months have 30 days, even have 29
1649 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1651 if ( $hebrewDay <= $days ) {
1652 # In the current month
1655 # Subtract the days of the current month
1656 $hebrewDay -= $days;
1657 # Try in the next month
1662 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1666 * This calculates the Hebrew year start, as days since 1 September.
1667 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1668 * Used for Hebrew date.
1674 private static function hebrewYearStart( $year ) {
1675 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1676 $b = intval( ( $year - 1 ) %
4 );
1677 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1681 $Mar = intval( $m );
1687 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1688 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1690 } elseif ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1692 } elseif ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1696 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1701 * Algorithm to convert Gregorian dates to Thai solar dates,
1702 * Minguo dates or Minguo dates.
1704 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1705 * http://en.wikipedia.org/wiki/Minguo_calendar
1706 * http://en.wikipedia.org/wiki/Japanese_era_name
1708 * @param $ts String: 14-character timestamp
1709 * @param $cName String: calender name
1710 * @return Array: converted year, month, day
1712 private static function tsToYear( $ts, $cName ) {
1713 $gy = substr( $ts, 0, 4 );
1714 $gm = substr( $ts, 4, 2 );
1715 $gd = substr( $ts, 6, 2 );
1717 if ( !strcmp( $cName, 'thai' ) ) {
1719 # Add 543 years to the Gregorian calendar
1720 # Months and days are identical
1721 $gy_offset = $gy +
543;
1722 } elseif ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1724 # Deduct 1911 years from the Gregorian calendar
1725 # Months and days are identical
1726 $gy_offset = $gy - 1911;
1727 } elseif ( !strcmp( $cName, 'tenno' ) ) {
1728 # Nengō dates up to Meiji period
1729 # Deduct years from the Gregorian calendar
1730 # depending on the nengo periods
1731 # Months and days are identical
1732 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1734 $gy_gannen = $gy - 1868 +
1;
1735 $gy_offset = $gy_gannen;
1736 if ( $gy_gannen == 1 ) {
1739 $gy_offset = '明治' . $gy_offset;
1741 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1742 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1743 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1744 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1745 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1749 $gy_gannen = $gy - 1912 +
1;
1750 $gy_offset = $gy_gannen;
1751 if ( $gy_gannen == 1 ) {
1754 $gy_offset = '大正' . $gy_offset;
1756 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1757 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1758 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1762 $gy_gannen = $gy - 1926 +
1;
1763 $gy_offset = $gy_gannen;
1764 if ( $gy_gannen == 1 ) {
1767 $gy_offset = '昭和' . $gy_offset;
1770 $gy_gannen = $gy - 1989 +
1;
1771 $gy_offset = $gy_gannen;
1772 if ( $gy_gannen == 1 ) {
1775 $gy_offset = '平成' . $gy_offset;
1781 return array( $gy_offset, $gm, $gd );
1785 * Roman number formatting up to 10000
1791 static function romanNumeral( $num ) {
1792 static $table = array(
1793 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1794 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1795 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1796 array( '', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM', 'MMMMMM', 'MMMMMMM', 'MMMMMMMM', 'MMMMMMMMM', 'MMMMMMMMMM' )
1799 $num = intval( $num );
1800 if ( $num > 10000 ||
$num <= 0 ) {
1805 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1806 if ( $num >= $pow10 ) {
1807 $s .= $table[$i][(int)floor( $num / $pow10 )];
1809 $num = $num %
$pow10;
1815 * Hebrew Gematria number formatting up to 9999
1821 static function hebrewNumeral( $num ) {
1822 static $table = array(
1823 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1824 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1825 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1826 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1829 $num = intval( $num );
1830 if ( $num > 9999 ||
$num <= 0 ) {
1835 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1836 if ( $num >= $pow10 ) {
1837 if ( $num == 15 ||
$num == 16 ) {
1838 $s .= $table[0][9] . $table[0][$num - 9];
1841 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1842 if ( $pow10 == 1000 ) {
1847 $num = $num %
$pow10;
1849 if ( strlen( $s ) == 2 ) {
1852 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1853 $str .= substr( $s, strlen( $s ) - 2, 2 );
1855 $start = substr( $str, 0, strlen( $str ) - 2 );
1856 $end = substr( $str, strlen( $str ) - 2 );
1859 $str = $start . 'ך';
1862 $str = $start . 'ם';
1865 $str = $start . 'ן';
1868 $str = $start . 'ף';
1871 $str = $start . 'ץ';
1878 * Used by date() and time() to adjust the time output.
1880 * @param $ts Int the time in date('YmdHis') format
1881 * @param $tz Mixed: adjust the time by this amount (default false, mean we
1882 * get user timecorrection setting)
1885 function userAdjust( $ts, $tz = false ) {
1886 global $wgUser, $wgLocalTZoffset;
1888 if ( $tz === false ) {
1889 $tz = $wgUser->getOption( 'timecorrection' );
1892 $data = explode( '|', $tz, 3 );
1894 if ( $data[0] == 'ZoneInfo' ) {
1895 wfSuppressWarnings();
1896 $userTZ = timezone_open( $data[2] );
1897 wfRestoreWarnings();
1898 if ( $userTZ !== false ) {
1899 $date = date_create( $ts, timezone_open( 'UTC' ) );
1900 date_timezone_set( $date, $userTZ );
1901 $date = date_format( $date, 'YmdHis' );
1904 # Unrecognized timezone, default to 'Offset' with the stored offset.
1905 $data[0] = 'Offset';
1909 if ( $data[0] == 'System' ||
$tz == '' ) {
1910 # Global offset in minutes.
1911 if ( isset( $wgLocalTZoffset ) ) {
1912 $minDiff = $wgLocalTZoffset;
1914 } elseif ( $data[0] == 'Offset' ) {
1915 $minDiff = intval( $data[1] );
1917 $data = explode( ':', $tz );
1918 if ( count( $data ) == 2 ) {
1919 $data[0] = intval( $data[0] );
1920 $data[1] = intval( $data[1] );
1921 $minDiff = abs( $data[0] ) * 60 +
$data[1];
1922 if ( $data[0] < 0 ) {
1923 $minDiff = -$minDiff;
1926 $minDiff = intval( $data[0] ) * 60;
1930 # No difference ? Return time unchanged
1931 if ( 0 == $minDiff ) {
1935 wfSuppressWarnings(); // E_STRICT system time bitching
1936 # Generate an adjusted date; take advantage of the fact that mktime
1937 # will normalize out-of-range values so we don't have to split $minDiff
1938 # into hours and minutes.
1940 (int)substr( $ts, 8, 2 ) ), # Hours
1941 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
1942 (int)substr( $ts, 12, 2 ), # Seconds
1943 (int)substr( $ts, 4, 2 ), # Month
1944 (int)substr( $ts, 6, 2 ), # Day
1945 (int)substr( $ts, 0, 4 ) ); # Year
1947 $date = date( 'YmdHis', $t );
1948 wfRestoreWarnings();
1954 * This is meant to be used by time(), date(), and timeanddate() to get
1955 * the date preference they're supposed to use, it should be used in
1959 * function timeanddate([...], $format = true) {
1960 * $datePreference = $this->dateFormat($format);
1965 * @param $usePrefs Mixed: if true, the user's preference is used
1966 * if false, the site/language default is used
1967 * if int/string, assumed to be a format.
1970 function dateFormat( $usePrefs = true ) {
1973 if ( is_bool( $usePrefs ) ) {
1975 $datePreference = $wgUser->getDatePreference();
1977 $datePreference = (string)User
::getDefaultOption( 'date' );
1980 $datePreference = (string)$usePrefs;
1984 if ( $datePreference == '' ) {
1988 return $datePreference;
1992 * Get a format string for a given type and preference
1993 * @param $type string May be date, time or both
1994 * @param $pref string The format name as it appears in Messages*.php
1996 * @since 1.22 New type 'pretty' that provides a more readable timestamp format
2000 function getDateFormatString( $type, $pref ) {
2001 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
2002 if ( $pref == 'default' ) {
2003 $pref = $this->getDefaultDateFormat();
2004 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
2006 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
2008 if ( $type === 'pretty' && $df === null ) {
2009 $df = $this->getDateFormatString( 'date', $pref );
2012 if ( $df === null ) {
2013 $pref = $this->getDefaultDateFormat();
2014 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
2017 $this->dateFormatStrings
[$type][$pref] = $df;
2019 return $this->dateFormatStrings
[$type][$pref];
2023 * @param $ts Mixed: the time format which needs to be turned into a
2024 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2025 * @param $adj Bool: whether to adjust the time output according to the
2026 * user configured offset ($timecorrection)
2027 * @param $format Mixed: true to use user's date format preference
2028 * @param $timecorrection String|bool the time offset as returned by
2029 * validateTimeZone() in Special:Preferences
2032 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
2033 $ts = wfTimestamp( TS_MW
, $ts );
2035 $ts = $this->userAdjust( $ts, $timecorrection );
2037 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
2038 return $this->sprintfDate( $df, $ts );
2042 * @param $ts Mixed: the time format which needs to be turned into a
2043 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2044 * @param $adj Bool: whether to adjust the time output according to the
2045 * user configured offset ($timecorrection)
2046 * @param $format Mixed: true to use user's date format preference
2047 * @param $timecorrection String|bool the time offset as returned by
2048 * validateTimeZone() in Special:Preferences
2051 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
2052 $ts = wfTimestamp( TS_MW
, $ts );
2054 $ts = $this->userAdjust( $ts, $timecorrection );
2056 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
2057 return $this->sprintfDate( $df, $ts );
2061 * @param $ts Mixed: the time format which needs to be turned into a
2062 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2063 * @param $adj Bool: whether to adjust the time output according to the
2064 * user configured offset ($timecorrection)
2065 * @param $format Mixed: what format to return, if it's false output the
2066 * default one (default true)
2067 * @param $timecorrection String|bool the time offset as returned by
2068 * validateTimeZone() in Special:Preferences
2071 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
2072 $ts = wfTimestamp( TS_MW
, $ts );
2074 $ts = $this->userAdjust( $ts, $timecorrection );
2076 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
2077 return $this->sprintfDate( $df, $ts );
2081 * Takes a number of seconds and turns it into a text using values such as hours and minutes.
2085 * @param integer $seconds The amount of seconds.
2086 * @param array $chosenIntervals The intervals to enable.
2090 public function formatDuration( $seconds, array $chosenIntervals = array() ) {
2091 $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
2093 $segments = array();
2095 foreach ( $intervals as $intervalName => $intervalValue ) {
2096 // Give grep a chance to find the usages:
2097 // duration-centuries, duration-decades, duration-years, duration-days,
2098 // duration-hours, duration-minutes, duration-seconds
2099 $message = wfMessage( 'duration-' . $intervalName )->numParams( $intervalValue );
2100 $segments[] = $message->inLanguage( $this )->escaped();
2103 return $this->listToText( $segments );
2107 * Takes a number of seconds and returns an array with a set of corresponding intervals.
2108 * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
2112 * @param integer $seconds The amount of seconds.
2113 * @param array $chosenIntervals The intervals to enable.
2117 public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
2118 if ( empty( $chosenIntervals ) ) {
2119 $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
2122 $intervals = array_intersect_key( self
::$durationIntervals, array_flip( $chosenIntervals ) );
2123 $sortedNames = array_keys( $intervals );
2124 $smallestInterval = array_pop( $sortedNames );
2126 $segments = array();
2128 foreach ( $intervals as $name => $length ) {
2129 $value = floor( $seconds / $length );
2131 if ( $value > 0 ||
( $name == $smallestInterval && empty( $segments ) ) ) {
2132 $seconds -= $value * $length;
2133 $segments[$name] = $value;
2141 * Internal helper function for userDate(), userTime() and userTimeAndDate()
2143 * @param $type String: can be 'date', 'time' or 'both'
2144 * @param $ts Mixed: the time format which needs to be turned into a
2145 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2146 * @param $user User object used to get preferences for timezone and format
2147 * @param $options Array, can contain the following keys:
2148 * - 'timecorrection': time correction, can have the following values:
2149 * - true: use user's preference
2150 * - false: don't use time correction
2151 * - integer: value of time correction in minutes
2152 * - 'format': format to use, can have the following values:
2153 * - true: use user's preference
2154 * - false: use default preference
2155 * - string: format to use
2159 private function internalUserTimeAndDate( $type, $ts, User
$user, array $options ) {
2160 $ts = wfTimestamp( TS_MW
, $ts );
2161 $options +
= array( 'timecorrection' => true, 'format' => true );
2162 if ( $options['timecorrection'] !== false ) {
2163 if ( $options['timecorrection'] === true ) {
2164 $offset = $user->getOption( 'timecorrection' );
2166 $offset = $options['timecorrection'];
2168 $ts = $this->userAdjust( $ts, $offset );
2170 if ( $options['format'] === true ) {
2171 $format = $user->getDatePreference();
2173 $format = $options['format'];
2175 $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
2176 return $this->sprintfDate( $df, $ts );
2180 * Get the formatted date for the given timestamp and formatted for
2183 * @param $ts Mixed: the time format which needs to be turned into a
2184 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2185 * @param $user User object used to get preferences for timezone and format
2186 * @param $options Array, can contain the following keys:
2187 * - 'timecorrection': time correction, can have the following values:
2188 * - true: use user's preference
2189 * - false: don't use time correction
2190 * - integer: value of time correction in minutes
2191 * - 'format': format to use, can have the following values:
2192 * - true: use user's preference
2193 * - false: use default preference
2194 * - string: format to use
2198 public function userDate( $ts, User
$user, array $options = array() ) {
2199 return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
2203 * Get the formatted time for the given timestamp and formatted for
2206 * @param $ts Mixed: the time format which needs to be turned into a
2207 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2208 * @param $user User object used to get preferences for timezone and format
2209 * @param $options Array, can contain the following keys:
2210 * - 'timecorrection': time correction, can have the following values:
2211 * - true: use user's preference
2212 * - false: don't use time correction
2213 * - integer: value of time correction in minutes
2214 * - 'format': format to use, can have the following values:
2215 * - true: use user's preference
2216 * - false: use default preference
2217 * - string: format to use
2221 public function userTime( $ts, User
$user, array $options = array() ) {
2222 return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
2226 * Get the formatted date and time for the given timestamp and formatted for
2229 * @param $ts Mixed: the time format which needs to be turned into a
2230 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
2231 * @param $user User object used to get preferences for timezone and format
2232 * @param $options Array, can contain the following keys:
2233 * - 'timecorrection': time correction, can have the following values:
2234 * - true: use user's preference
2235 * - false: don't use time correction
2236 * - integer: value of time correction in minutes
2237 * - 'format': format to use, can have the following values:
2238 * - true: use user's preference
2239 * - false: use default preference
2240 * - string: format to use
2244 public function userTimeAndDate( $ts, User
$user, array $options = array() ) {
2245 return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
2249 * Convert an MWTimestamp into a pretty human-readable timestamp using
2250 * the given user preferences and relative base time.
2252 * DO NOT USE THIS FUNCTION DIRECTLY. Instead, call MWTimestamp::getHumanTimestamp
2253 * on your timestamp object, which will then call this function. Calling
2254 * this function directly will cause hooks to be skipped over.
2256 * @see MWTimestamp::getHumanTimestamp
2257 * @param MWTimestamp $ts Timestamp to prettify
2258 * @param MWTimestamp $relativeTo Base timestamp
2259 * @param User $user User preferences to use
2260 * @return string Human timestamp
2263 public function getHumanTimestamp( MWTimestamp
$ts, MWTimestamp
$relativeTo, User
$user ) {
2264 $diff = $ts->diff( $relativeTo );
2265 $diffDay = (bool)( (int)$ts->timestamp
->format( 'w' ) - (int)$relativeTo->timestamp
->format( 'w' ) );
2266 $days = $diff->days ?
: (int)$diffDay;
2267 if ( $diff->invert ||
$days > 5 && $ts->timestamp
->format( 'Y' ) !== $relativeTo->timestamp
->format( 'Y' ) ) {
2268 // Timestamps are in different years: use full timestamp
2269 // Also do full timestamp for future dates
2271 * @FIXME Add better handling of future timestamps.
2273 $format = $this->getDateFormatString( 'both', $user->getDatePreference() ?
: 'default' );
2274 $ts = $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) );
2275 } elseif ( $days > 5 ) {
2276 // Timestamps are in same year, but more than 5 days ago: show day and month only.
2277 $format = $this->getDateFormatString( 'pretty', $user->getDatePreference() ?
: 'default' );
2278 $ts = $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) );
2279 } elseif ( $days > 1 ) {
2280 // Timestamp within the past week: show the day of the week and time
2281 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?
: 'default' );
2282 $weekday = self
::$mWeekdayMsgs[$ts->timestamp
->format( 'w' )];
2283 $ts = wfMessage( "$weekday-at" )
2284 ->inLanguage( $this )
2285 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) ) )
2287 } elseif ( $days == 1 ) {
2288 // Timestamp was yesterday: say 'yesterday' and the time.
2289 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?
: 'default' );
2290 $ts = wfMessage( 'yesterday-at' )
2291 ->inLanguage( $this )
2292 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) ) )
2294 } elseif ( $diff->h
> 1 ||
$diff->h
== 1 && $diff->i
> 30 ) {
2295 // Timestamp was today, but more than 90 minutes ago: say 'today' and the time.
2296 $format = $this->getDateFormatString( 'time', $user->getDatePreference() ?
: 'default' );
2297 $ts = wfMessage( 'today-at' )
2298 ->inLanguage( $this )
2299 ->params( $this->sprintfDate( $format, $ts->getTimestamp( TS_MW
) ) )
2302 // From here on in, the timestamp was soon enough ago so that we can simply say
2303 // XX units ago, e.g., "2 hours ago" or "5 minutes ago"
2304 } elseif ( $diff->h
== 1 ) {
2305 // Less than 90 minutes, but more than an hour ago.
2306 $ts = wfMessage( 'hours-ago' )->inLanguage( $this )->numParams( 1 )->text();
2307 } elseif ( $diff->i
>= 1 ) {
2308 // A few minutes ago.
2309 $ts = wfMessage( 'minutes-ago' )->inLanguage( $this )->numParams( $diff->i
)->text();
2310 } elseif ( $diff->s
>= 30 ) {
2311 // Less than a minute, but more than 30 sec ago.
2312 $ts = wfMessage( 'seconds-ago' )->inLanguage( $this )->numParams( $diff->s
)->text();
2314 // Less than 30 seconds ago.
2315 $ts = wfMessage( 'just-now' )->text();
2322 * @param $key string
2323 * @return array|null
2325 function getMessage( $key ) {
2326 return self
::$dataCache->getSubitem( $this->mCode
, 'messages', $key );
2332 function getAllMessages() {
2333 return self
::$dataCache->getItem( $this->mCode
, 'messages' );
2342 function iconv( $in, $out, $string ) {
2343 # This is a wrapper for iconv in all languages except esperanto,
2344 # which does some nasty x-conversions beforehand
2346 # Even with //IGNORE iconv can whine about illegal characters in
2347 # *input* string. We just ignore those too.
2348 # REF: http://bugs.php.net/bug.php?id=37166
2349 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
2350 wfSuppressWarnings();
2351 $text = iconv( $in, $out . '//IGNORE', $string );
2352 wfRestoreWarnings();
2356 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
2359 * @param $matches array
2360 * @return mixed|string
2362 function ucwordbreaksCallbackAscii( $matches ) {
2363 return $this->ucfirst( $matches[1] );
2367 * @param $matches array
2370 function ucwordbreaksCallbackMB( $matches ) {
2371 return mb_strtoupper( $matches[0] );
2375 * @param $matches array
2378 function ucCallback( $matches ) {
2379 list( $wikiUpperChars ) = self
::getCaseMaps();
2380 return strtr( $matches[1], $wikiUpperChars );
2384 * @param $matches array
2387 function lcCallback( $matches ) {
2388 list( , $wikiLowerChars ) = self
::getCaseMaps();
2389 return strtr( $matches[1], $wikiLowerChars );
2393 * @param $matches array
2396 function ucwordsCallbackMB( $matches ) {
2397 return mb_strtoupper( $matches[0] );
2401 * @param $matches array
2404 function ucwordsCallbackWiki( $matches ) {
2405 list( $wikiUpperChars ) = self
::getCaseMaps();
2406 return strtr( $matches[0], $wikiUpperChars );
2410 * Make a string's first character uppercase
2412 * @param $str string
2416 function ucfirst( $str ) {
2418 if ( $o < 96 ) { // if already uppercase...
2420 } elseif ( $o < 128 ) {
2421 return ucfirst( $str ); // use PHP's ucfirst()
2423 // fall back to more complex logic in case of multibyte strings
2424 return $this->uc( $str, true );
2429 * Convert a string to uppercase
2431 * @param $str string
2432 * @param $first bool
2436 function uc( $str, $first = false ) {
2437 if ( function_exists( 'mb_strtoupper' ) ) {
2439 if ( $this->isMultibyte( $str ) ) {
2440 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2442 return ucfirst( $str );
2445 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
2448 if ( $this->isMultibyte( $str ) ) {
2449 $x = $first ?
'^' : '';
2450 return preg_replace_callback(
2451 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2452 array( $this, 'ucCallback' ),
2456 return $first ?
ucfirst( $str ) : strtoupper( $str );
2462 * @param $str string
2463 * @return mixed|string
2465 function lcfirst( $str ) {
2468 return strval( $str );
2469 } elseif ( $o >= 128 ) {
2470 return $this->lc( $str, true );
2471 } elseif ( $o > 96 ) {
2474 $str[0] = strtolower( $str[0] );
2480 * @param $str string
2481 * @param $first bool
2482 * @return mixed|string
2484 function lc( $str, $first = false ) {
2485 if ( function_exists( 'mb_strtolower' ) ) {
2487 if ( $this->isMultibyte( $str ) ) {
2488 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
2490 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
2493 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
2496 if ( $this->isMultibyte( $str ) ) {
2497 $x = $first ?
'^' : '';
2498 return preg_replace_callback(
2499 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
2500 array( $this, 'lcCallback' ),
2504 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
2510 * @param $str string
2513 function isMultibyte( $str ) {
2514 return (bool)preg_match( '/[\x80-\xff]/', $str );
2518 * @param $str string
2519 * @return mixed|string
2521 function ucwords( $str ) {
2522 if ( $this->isMultibyte( $str ) ) {
2523 $str = $this->lc( $str );
2525 // regexp to find first letter in each word (i.e. after each space)
2526 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2528 // function to use to capitalize a single char
2529 if ( function_exists( 'mb_strtoupper' ) ) {
2530 return preg_replace_callback(
2532 array( $this, 'ucwordsCallbackMB' ),
2536 return preg_replace_callback(
2538 array( $this, 'ucwordsCallbackWiki' ),
2543 return ucwords( strtolower( $str ) );
2548 * capitalize words at word breaks
2550 * @param $str string
2553 function ucwordbreaks( $str ) {
2554 if ( $this->isMultibyte( $str ) ) {
2555 $str = $this->lc( $str );
2557 // since \b doesn't work for UTF-8, we explicitely define word break chars
2558 $breaks = "[ \-\(\)\}\{\.,\?!]";
2560 // find first letter after word break
2561 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
2563 if ( function_exists( 'mb_strtoupper' ) ) {
2564 return preg_replace_callback(
2566 array( $this, 'ucwordbreaksCallbackMB' ),
2570 return preg_replace_callback(
2572 array( $this, 'ucwordsCallbackWiki' ),
2577 return preg_replace_callback(
2578 '/\b([\w\x80-\xff]+)\b/',
2579 array( $this, 'ucwordbreaksCallbackAscii' ),
2586 * Return a case-folded representation of $s
2588 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
2589 * and $s2 are the same except for the case of their characters. It is not
2590 * necessary for the value returned to make sense when displayed.
2592 * Do *not* perform any other normalisation in this function. If a caller
2593 * uses this function when it should be using a more general normalisation
2594 * function, then fix the caller.
2600 function caseFold( $s ) {
2601 return $this->uc( $s );
2608 function checkTitleEncoding( $s ) {
2609 if ( is_array( $s ) ) {
2610 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
2612 if ( StringUtils
::isUtf8( $s ) ) {
2616 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
2622 function fallback8bitEncoding() {
2623 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
2627 * Most writing systems use whitespace to break up words.
2628 * Some languages such as Chinese don't conventionally do this,
2629 * which requires special handling when breaking up words for
2634 function hasWordBreaks() {
2639 * Some languages such as Chinese require word segmentation,
2640 * Specify such segmentation when overridden in derived class.
2642 * @param $string String
2645 function segmentByWord( $string ) {
2650 * Some languages have special punctuation need to be normalized.
2651 * Make such changes here.
2653 * @param $string String
2656 function normalizeForSearch( $string ) {
2657 return self
::convertDoubleWidth( $string );
2661 * convert double-width roman characters to single-width.
2662 * range: ff00-ff5f ~= 0020-007f
2664 * @param $string string
2668 protected static function convertDoubleWidth( $string ) {
2669 static $full = null;
2670 static $half = null;
2672 if ( $full === null ) {
2673 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2674 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2675 $full = str_split( $fullWidth, 3 );
2676 $half = str_split( $halfWidth );
2679 $string = str_replace( $full, $half, $string );
2684 * @param $string string
2685 * @param $pattern string
2688 protected static function insertSpace( $string, $pattern ) {
2689 $string = preg_replace( $pattern, " $1 ", $string );
2690 $string = preg_replace( '/ +/', ' ', $string );
2695 * @param $termsArray array
2698 function convertForSearchResult( $termsArray ) {
2699 # some languages, e.g. Chinese, need to do a conversion
2700 # in order for search results to be displayed correctly
2705 * Get the first character of a string.
2710 function firstChar( $s ) {
2713 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
2714 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
2719 if ( isset( $matches[1] ) ) {
2720 if ( strlen( $matches[1] ) != 3 ) {
2724 // Break down Hangul syllables to grab the first jamo
2725 $code = utf8ToCodepoint( $matches[1] );
2726 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
2728 } elseif ( $code < 0xb098 ) {
2729 return "\xe3\x84\xb1";
2730 } elseif ( $code < 0xb2e4 ) {
2731 return "\xe3\x84\xb4";
2732 } elseif ( $code < 0xb77c ) {
2733 return "\xe3\x84\xb7";
2734 } elseif ( $code < 0xb9c8 ) {
2735 return "\xe3\x84\xb9";
2736 } elseif ( $code < 0xbc14 ) {
2737 return "\xe3\x85\x81";
2738 } elseif ( $code < 0xc0ac ) {
2739 return "\xe3\x85\x82";
2740 } elseif ( $code < 0xc544 ) {
2741 return "\xe3\x85\x85";
2742 } elseif ( $code < 0xc790 ) {
2743 return "\xe3\x85\x87";
2744 } elseif ( $code < 0xcc28 ) {
2745 return "\xe3\x85\x88";
2746 } elseif ( $code < 0xce74 ) {
2747 return "\xe3\x85\x8a";
2748 } elseif ( $code < 0xd0c0 ) {
2749 return "\xe3\x85\x8b";
2750 } elseif ( $code < 0xd30c ) {
2751 return "\xe3\x85\x8c";
2752 } elseif ( $code < 0xd558 ) {
2753 return "\xe3\x85\x8d";
2755 return "\xe3\x85\x8e";
2762 function initEncoding() {
2763 # Some languages may have an alternate char encoding option
2764 # (Esperanto X-coding, Japanese furigana conversion, etc)
2765 # If this language is used as the primary content language,
2766 # an override to the defaults can be set here on startup.
2773 function recodeForEdit( $s ) {
2774 # For some languages we'll want to explicitly specify
2775 # which characters make it into the edit box raw
2776 # or are converted in some way or another.
2777 global $wgEditEncoding;
2778 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
2781 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2789 function recodeInput( $s ) {
2790 # Take the previous into account.
2791 global $wgEditEncoding;
2792 if ( $wgEditEncoding != '' ) {
2793 $enc = $wgEditEncoding;
2797 if ( $enc == 'UTF-8' ) {
2800 return $this->iconv( $enc, 'UTF-8', $s );
2805 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2806 * also cleans up certain backwards-compatible sequences, converting them
2807 * to the modern Unicode equivalent.
2809 * This is language-specific for performance reasons only.
2815 function normalize( $s ) {
2816 global $wgAllUnicodeFixes;
2817 $s = UtfNormal
::cleanUp( $s );
2818 if ( $wgAllUnicodeFixes ) {
2819 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2820 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2827 * Transform a string using serialized data stored in the given file (which
2828 * must be in the serialized subdirectory of $IP). The file contains pairs
2829 * mapping source characters to destination characters.
2831 * The data is cached in process memory. This will go faster if you have the
2832 * FastStringSearch extension.
2834 * @param $file string
2835 * @param $string string
2837 * @throws MWException
2840 function transformUsingPairFile( $file, $string ) {
2841 if ( !isset( $this->transformData
[$file] ) ) {
2842 $data = wfGetPrecompiledData( $file );
2843 if ( $data === false ) {
2844 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2846 $this->transformData
[$file] = new ReplacementArray( $data );
2848 return $this->transformData
[$file]->replace( $string );
2852 * For right-to-left language support
2857 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2861 * Return the correct HTML 'dir' attribute value for this language.
2865 return $this->isRTL() ?
'rtl' : 'ltr';
2869 * Return 'left' or 'right' as appropriate alignment for line-start
2870 * for this language's text direction.
2872 * Should be equivalent to CSS3 'start' text-align value....
2876 function alignStart() {
2877 return $this->isRTL() ?
'right' : 'left';
2881 * Return 'right' or 'left' as appropriate alignment for line-end
2882 * for this language's text direction.
2884 * Should be equivalent to CSS3 'end' text-align value....
2888 function alignEnd() {
2889 return $this->isRTL() ?
'left' : 'right';
2893 * A hidden direction mark (LRM or RLM), depending on the language direction.
2894 * Unlike getDirMark(), this function returns the character as an HTML entity.
2895 * This function should be used when the output is guaranteed to be HTML,
2896 * because it makes the output HTML source code more readable. When
2897 * the output is plain text or can be escaped, getDirMark() should be used.
2899 * @param $opposite Boolean Get the direction mark opposite to your language
2903 function getDirMarkEntity( $opposite = false ) {
2905 return $this->isRTL() ?
'‎' : '‏';
2907 return $this->isRTL() ?
'‏' : '‎';
2911 * A hidden direction mark (LRM or RLM), depending on the language direction.
2912 * This function produces them as invisible Unicode characters and
2913 * the output may be hard to read and debug, so it should only be used
2914 * when the output is plain text or can be escaped. When the output is
2915 * HTML, use getDirMarkEntity() instead.
2917 * @param $opposite Boolean Get the direction mark opposite to your language
2920 function getDirMark( $opposite = false ) {
2921 $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
2922 $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
2924 return $this->isRTL() ?
$lrm : $rlm;
2926 return $this->isRTL() ?
$rlm : $lrm;
2932 function capitalizeAllNouns() {
2933 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2937 * An arrow, depending on the language direction.
2939 * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
2942 function getArrow( $direction = 'forwards' ) {
2943 switch ( $direction ) {
2945 return $this->isRTL() ?
'←' : '→';
2947 return $this->isRTL() ?
'→' : '←';
2960 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2964 function linkPrefixExtension() {
2965 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2969 * Get all magic words from cache.
2972 function getMagicWords() {
2973 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2977 * Run the LanguageGetMagic hook once.
2979 protected function doMagicHook() {
2980 if ( $this->mMagicHookDone
) {
2983 $this->mMagicHookDone
= true;
2984 wfProfileIn( 'LanguageGetMagic' );
2985 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2986 wfProfileOut( 'LanguageGetMagic' );
2990 * Fill a MagicWord object with data from here
2994 function getMagic( $mw ) {
2995 // Saves a function call
2996 if ( ! $this->mMagicHookDone
) {
2997 $this->doMagicHook();
3000 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
3001 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
3003 $rawEntry = self
::$dataCache->getSubitem(
3004 $this->mCode
, 'magicWords', $mw->mId
);
3007 if ( !is_array( $rawEntry ) ) {
3008 error_log( "\"$rawEntry\" is not a valid magic word for \"$mw->mId\"" );
3010 $mw->mCaseSensitive
= $rawEntry[0];
3011 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
3016 * Add magic words to the extension array
3018 * @param $newWords array
3020 function addMagicWordsByLang( $newWords ) {
3021 $fallbackChain = $this->getFallbackLanguages();
3022 $fallbackChain = array_reverse( $fallbackChain );
3023 foreach ( $fallbackChain as $code ) {
3024 if ( isset( $newWords[$code] ) ) {
3025 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
3031 * Get special page names, as an associative array
3032 * case folded alias => real name
3034 function getSpecialPageAliases() {
3035 // Cache aliases because it may be slow to load them
3036 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
3038 $this->mExtendedSpecialPageAliases
=
3039 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
3040 wfRunHooks( 'LanguageGetSpecialPageAliases',
3041 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
3044 return $this->mExtendedSpecialPageAliases
;
3048 * Italic is unsuitable for some languages
3050 * @param $text String: the text to be emphasized.
3053 function emphasize( $text ) {
3054 return "<em>$text</em>";
3058 * Normally we output all numbers in plain en_US style, that is
3059 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
3060 * point twohundredthirtyfive. However this is not suitable for all
3061 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
3062 * Icelandic just want to use commas instead of dots, and dots instead
3063 * of commas like "293.291,235".
3065 * An example of this function being called:
3067 * wfMessage( 'message' )->numParams( $num )->text()
3070 * See LanguageGu.php for the Gujarati implementation and
3071 * $separatorTransformTable on MessageIs.php for
3072 * the , => . and . => , implementation.
3074 * @todo check if it's viable to use localeconv() for the decimal
3076 * @param $number Mixed: the string to be formatted, should be an integer
3077 * or a floating point number.
3078 * @param $nocommafy Bool: set to true for special numbers like dates
3081 public function formatNum( $number, $nocommafy = false ) {
3082 global $wgTranslateNumerals;
3083 if ( !$nocommafy ) {
3084 $number = $this->commafy( $number );
3085 $s = $this->separatorTransformTable();
3087 $number = strtr( $number, $s );
3091 if ( $wgTranslateNumerals ) {
3092 $s = $this->digitTransformTable();
3094 $number = strtr( $number, $s );
3102 * Front-end for non-commafied formatNum
3104 * @param mixed $number the string to be formatted, should be an integer
3105 * or a floating point number.
3109 public function formatNumNoSeparators( $number ) {
3110 return $this->formatNum( $number, true );
3114 * @param $number string
3117 function parseFormattedNumber( $number ) {
3118 $s = $this->digitTransformTable();
3120 $number = strtr( $number, array_flip( $s ) );
3123 $s = $this->separatorTransformTable();
3125 $number = strtr( $number, array_flip( $s ) );
3128 $number = strtr( $number, array( ',' => '' ) );
3133 * Adds commas to a given number
3135 * @param $number mixed
3138 function commafy( $number ) {
3139 $digitGroupingPattern = $this->digitGroupingPattern();
3140 if ( $number === null ) {
3144 if ( !$digitGroupingPattern ||
$digitGroupingPattern === "###,###,###" ) {
3145 // default grouping is at thousands, use the same for ###,###,### pattern too.
3146 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $number ) ) );
3148 // Ref: http://cldr.unicode.org/translation/number-patterns
3150 if ( intval( $number ) < 0 ) {
3151 // For negative numbers apply the algorithm like positive number and add sign.
3153 $number = substr( $number, 1 );
3155 $integerPart = array();
3156 $decimalPart = array();
3157 $numMatches = preg_match_all( "/(#+)/", $digitGroupingPattern, $matches );
3158 preg_match( "/\d+/", $number, $integerPart );
3159 preg_match( "/\.\d*/", $number, $decimalPart );
3160 $groupedNumber = ( count( $decimalPart ) > 0 ) ?
$decimalPart[0] : "";
3161 if ( $groupedNumber === $number ) {
3162 // the string does not have any number part. Eg: .12345
3163 return $sign . $groupedNumber;
3165 $start = $end = strlen( $integerPart[0] );
3166 while ( $start > 0 ) {
3167 $match = $matches[0][$numMatches - 1];
3168 $matchLen = strlen( $match );
3169 $start = $end - $matchLen;
3173 $groupedNumber = substr( $number, $start, $end -$start ) . $groupedNumber;
3175 if ( $numMatches > 1 ) {
3176 // use the last pattern for the rest of the number
3180 $groupedNumber = "," . $groupedNumber;
3183 return $sign . $groupedNumber;
3190 function digitGroupingPattern() {
3191 return self
::$dataCache->getItem( $this->mCode
, 'digitGroupingPattern' );
3197 function digitTransformTable() {
3198 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
3204 function separatorTransformTable() {
3205 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
3209 * Take a list of strings and build a locale-friendly comma-separated
3210 * list, using the local comma-separator message.
3211 * The last two strings are chained with an "and".
3212 * NOTE: This function will only work with standard numeric array keys (0, 1, 2…)
3217 function listToText( array $l ) {
3218 $m = count( $l ) - 1;
3223 $and = $this->getMessageFromDB( 'and' );
3224 $space = $this->getMessageFromDB( 'word-separator' );
3226 $comma = $this->getMessageFromDB( 'comma-separator' );
3230 for ( $i = $m - 1; $i >= 0; $i-- ) {
3231 if ( $i == $m - 1 ) {
3232 $s = $l[$i] . $and . $space . $s;
3234 $s = $l[$i] . $comma . $s;
3241 * Take a list of strings and build a locale-friendly comma-separated
3242 * list, using the local comma-separator message.
3243 * @param $list array of strings to put in a comma list
3246 function commaList( array $list ) {
3248 wfMessage( 'comma-separator' )->inLanguage( $this )->escaped(),
3254 * Take a list of strings and build a locale-friendly semicolon-separated
3255 * list, using the local semicolon-separator message.
3256 * @param $list array of strings to put in a semicolon list
3259 function semicolonList( array $list ) {
3261 wfMessage( 'semicolon-separator' )->inLanguage( $this )->escaped(),
3267 * Same as commaList, but separate it with the pipe instead.
3268 * @param $list array of strings to put in a pipe list
3271 function pipeList( array $list ) {
3273 wfMessage( 'pipe-separator' )->inLanguage( $this )->escaped(),
3279 * Truncate a string to a specified length in bytes, appending an optional
3280 * string (e.g. for ellipses)
3282 * The database offers limited byte lengths for some columns in the database;
3283 * multi-byte character sets mean we need to ensure that only whole characters
3284 * are included, otherwise broken characters can be passed to the user
3286 * If $length is negative, the string will be truncated from the beginning
3288 * @param $string String to truncate
3289 * @param $length Int: maximum length (including ellipses)
3290 * @param $ellipsis String to append to the truncated text
3291 * @param $adjustLength Boolean: Subtract length of ellipsis from $length.
3292 * $adjustLength was introduced in 1.18, before that behaved as if false.
3295 function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
3296 # Use the localized ellipsis character
3297 if ( $ellipsis == '...' ) {
3298 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3300 # Check if there is no need to truncate
3301 if ( $length == 0 ) {
3302 return $ellipsis; // convention
3303 } elseif ( strlen( $string ) <= abs( $length ) ) {
3304 return $string; // no need to truncate
3306 $stringOriginal = $string;
3307 # If ellipsis length is >= $length then we can't apply $adjustLength
3308 if ( $adjustLength && strlen( $ellipsis ) >= abs( $length ) ) {
3309 $string = $ellipsis; // this can be slightly unexpected
3310 # Otherwise, truncate and add ellipsis...
3312 $eLength = $adjustLength ?
strlen( $ellipsis ) : 0;
3313 if ( $length > 0 ) {
3314 $length -= $eLength;
3315 $string = substr( $string, 0, $length ); // xyz...
3316 $string = $this->removeBadCharLast( $string );
3317 $string = $string . $ellipsis;
3319 $length +
= $eLength;
3320 $string = substr( $string, $length ); // ...xyz
3321 $string = $this->removeBadCharFirst( $string );
3322 $string = $ellipsis . $string;
3325 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181).
3326 # This check is *not* redundant if $adjustLength, due to the single case where
3327 # LEN($ellipsis) > ABS($limit arg); $stringOriginal could be shorter than $string.
3328 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
3331 return $stringOriginal;
3336 * Remove bytes that represent an incomplete Unicode character
3337 * at the end of string (e.g. bytes of the char are missing)
3339 * @param $string String
3342 protected function removeBadCharLast( $string ) {
3343 if ( $string != '' ) {
3344 $char = ord( $string[strlen( $string ) - 1] );
3346 if ( $char >= 0xc0 ) {
3347 # We got the first byte only of a multibyte char; remove it.
3348 $string = substr( $string, 0, -1 );
3349 } elseif ( $char >= 0x80 &&
3350 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
3351 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m )
3353 # We chopped in the middle of a character; remove it
3361 * Remove bytes that represent an incomplete Unicode character
3362 * at the start of string (e.g. bytes of the char are missing)
3364 * @param $string String
3367 protected function removeBadCharFirst( $string ) {
3368 if ( $string != '' ) {
3369 $char = ord( $string[0] );
3370 if ( $char >= 0x80 && $char < 0xc0 ) {
3371 # We chopped in the middle of a character; remove the whole thing
3372 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
3379 * Truncate a string of valid HTML to a specified length in bytes,
3380 * appending an optional string (e.g. for ellipses), and return valid HTML
3382 * This is only intended for styled/linked text, such as HTML with
3383 * tags like <span> and <a>, were the tags are self-contained (valid HTML).
3384 * Also, this will not detect things like "display:none" CSS.
3386 * Note: since 1.18 you do not need to leave extra room in $length for ellipses.
3388 * @param string $text HTML string to truncate
3389 * @param int $length (zero/positive) Maximum length (including ellipses)
3390 * @param string $ellipsis String to append to the truncated text
3393 function truncateHtml( $text, $length, $ellipsis = '...' ) {
3394 # Use the localized ellipsis character
3395 if ( $ellipsis == '...' ) {
3396 $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
3398 # Check if there is clearly no need to truncate
3399 if ( $length <= 0 ) {
3400 return $ellipsis; // no text shown, nothing to format (convention)
3401 } elseif ( strlen( $text ) <= $length ) {
3402 return $text; // string short enough even *with* HTML (short-circuit)
3405 $dispLen = 0; // innerHTML legth so far
3406 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
3407 $tagType = 0; // 0-open, 1-close
3408 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
3409 $entityState = 0; // 0-not entity, 1-entity
3410 $tag = $ret = ''; // accumulated tag name, accumulated result string
3411 $openTags = array(); // open tag stack
3412 $maybeState = null; // possible truncation state
3414 $textLen = strlen( $text );
3415 $neLength = max( 0, $length - strlen( $ellipsis ) ); // non-ellipsis len if truncated
3416 for ( $pos = 0; true; ++
$pos ) {
3417 # Consider truncation once the display length has reached the maximim.
3418 # We check if $dispLen > 0 to grab tags for the $neLength = 0 case.
3419 # Check that we're not in the middle of a bracket/entity...
3420 if ( $dispLen && $dispLen >= $neLength && $bracketState == 0 && !$entityState ) {
3421 if ( !$testingEllipsis ) {
3422 $testingEllipsis = true;
3423 # Save where we are; we will truncate here unless there turn out to
3424 # be so few remaining characters that truncation is not necessary.
3425 if ( !$maybeState ) { // already saved? ($neLength = 0 case)
3426 $maybeState = array( $ret, $openTags ); // save state
3428 } elseif ( $dispLen > $length && $dispLen > strlen( $ellipsis ) ) {
3429 # String in fact does need truncation, the truncation point was OK.
3430 list( $ret, $openTags ) = $maybeState; // reload state
3431 $ret = $this->removeBadCharLast( $ret ); // multi-byte char fix
3432 $ret .= $ellipsis; // add ellipsis
3436 if ( $pos >= $textLen ) {
3437 break; // extra iteration just for above checks
3440 # Read the next char...
3442 $lastCh = $pos ?
$text[$pos - 1] : '';
3443 $ret .= $ch; // add to result string
3445 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
3446 $entityState = 0; // for bad HTML
3447 $bracketState = 1; // tag started (checking for backslash)
3448 } elseif ( $ch == '>' ) {
3449 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
3450 $entityState = 0; // for bad HTML
3451 $bracketState = 0; // out of brackets
3452 } elseif ( $bracketState == 1 ) {
3454 $tagType = 1; // close tag (e.g. "</span>")
3456 $tagType = 0; // open tag (e.g. "<span>")
3459 $bracketState = 2; // building tag name
3460 } elseif ( $bracketState == 2 ) {
3464 // Name found (e.g. "<a href=..."), add on tag attributes...
3465 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
3467 } elseif ( $bracketState == 0 ) {
3468 if ( $entityState ) {
3471 $dispLen++
; // entity is one displayed char
3474 if ( $neLength == 0 && !$maybeState ) {
3475 // Save state without $ch. We want to *hit* the first
3476 // display char (to get tags) but not *use* it if truncating.
3477 $maybeState = array( substr( $ret, 0, -1 ), $openTags );
3480 $entityState = 1; // entity found, (e.g. " ")
3482 $dispLen++
; // this char is displayed
3483 // Add the next $max display text chars after this in one swoop...
3484 $max = ( $testingEllipsis ?
$length : $neLength ) - $dispLen;
3485 $skipped = $this->truncate_skip( $ret, $text, "<>&", $pos +
1, $max );
3486 $dispLen +
= $skipped;
3492 // Close the last tag if left unclosed by bad HTML
3493 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
3494 while ( count( $openTags ) > 0 ) {
3495 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
3501 * truncateHtml() helper function
3502 * like strcspn() but adds the skipped chars to $ret
3511 private function truncate_skip( &$ret, $text, $search, $start, $len = null ) {
3512 if ( $len === null ) {
3513 $len = -1; // -1 means "no limit" for strcspn
3514 } elseif ( $len < 0 ) {
3518 if ( $start < strlen( $text ) ) {
3519 $skipCount = strcspn( $text, $search, $start, $len );
3520 $ret .= substr( $text, $start, $skipCount );
3526 * truncateHtml() helper function
3527 * (a) push or pop $tag from $openTags as needed
3528 * (b) clear $tag value
3529 * @param &$tag string Current HTML tag name we are looking at
3530 * @param $tagType int (0-open tag, 1-close tag)
3531 * @param $lastCh string Character before the '>' that ended this tag
3532 * @param &$openTags array Open tag stack (not accounting for $tag)
3534 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
3535 $tag = ltrim( $tag );
3537 if ( $tagType == 0 && $lastCh != '/' ) {
3538 $openTags[] = $tag; // tag opened (didn't close itself)
3539 } elseif ( $tagType == 1 ) {
3540 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
3541 array_pop( $openTags ); // tag closed
3549 * Grammatical transformations, needed for inflected languages
3550 * Invoked by putting {{grammar:case|word}} in a message
3552 * @param $word string
3553 * @param $case string
3556 function convertGrammar( $word, $case ) {
3557 global $wgGrammarForms;
3558 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
3559 return $wgGrammarForms[$this->getCode()][$case][$word];
3564 * Get the grammar forms for the content language
3565 * @return array of grammar forms
3568 function getGrammarForms() {
3569 global $wgGrammarForms;
3570 if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
3571 return $wgGrammarForms[$this->getCode()];
3576 * Provides an alternative text depending on specified gender.
3577 * Usage {{gender:username|masculine|feminine|neutral}}.
3578 * username is optional, in which case the gender of current user is used,
3579 * but only in (some) interface messages; otherwise default gender is used.
3581 * If no forms are given, an empty string is returned. If only one form is
3582 * given, it will be returned unconditionally. These details are implied by
3583 * the caller and cannot be overridden in subclasses.
3585 * If more than one form is given, the default is to use the neutral one
3586 * if it is specified, and to use the masculine one otherwise. These
3587 * details can be overridden in subclasses.
3589 * @param $gender string
3590 * @param $forms array
3594 function gender( $gender, $forms ) {
3595 if ( !count( $forms ) ) {
3598 $forms = $this->preConvertPlural( $forms, 2 );
3599 if ( $gender === 'male' ) {
3602 if ( $gender === 'female' ) {
3605 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
3609 * Plural form transformations, needed for some languages.
3610 * For example, there are 3 form of plural in Russian and Polish,
3611 * depending on "count mod 10". See [[w:Plural]]
3612 * For English it is pretty simple.
3614 * Invoked by putting {{plural:count|wordform1|wordform2}}
3615 * or {{plural:count|wordform1|wordform2|wordform3}}
3617 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
3619 * @param $count Integer: non-localized number
3620 * @param $forms Array: different plural forms
3621 * @return string Correct form of plural for $count in this language
3623 function convertPlural( $count, $forms ) {
3624 // Handle explicit n=pluralform cases
3625 foreach ( $forms as $index => $form ) {
3626 if ( preg_match( '/^\d+=/i', $form ) ) {
3627 $pos = strpos( $form, '=' );
3628 if ( substr( $form, 0, $pos ) === (string) $count ) {
3629 return substr( $form, $pos +
1 );
3631 unset( $forms[$index] );
3635 $forms = array_values( $forms );
3636 if ( !count( $forms ) ) {
3640 $pluralForm = $this->getPluralRuleIndexNumber( $count );
3641 $pluralForm = min( $pluralForm, count( $forms ) - 1 );
3642 return $forms[$pluralForm];
3646 * Checks that convertPlural was given an array and pads it to requested
3647 * amount of forms by copying the last one.
3649 * @param $count Integer: How many forms should there be at least
3650 * @param $forms Array of forms given to convertPlural
3651 * @return array Padded array of forms or an exception if not an array
3653 protected function preConvertPlural( /* Array */ $forms, $count ) {
3654 while ( count( $forms ) < $count ) {
3655 $forms[] = $forms[count( $forms ) - 1];
3661 * @todo Maybe translate block durations. Note that this function is somewhat misnamed: it
3662 * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time
3663 * (which is an absolute timestamp). Please note: do NOT add this blindly, as it is used
3664 * on old expiry lengths recorded in log entries. You'd need to provide the start date to
3667 * @param $str String: the validated block duration in English
3668 * @return string Somehow translated block duration
3669 * @see LanguageFi.php for example implementation
3671 function translateBlockExpiry( $str ) {
3672 $duration = SpecialBlock
::getSuggestedDurations( $this );
3673 foreach ( $duration as $show => $value ) {
3674 if ( strcmp( $str, $value ) == 0 ) {
3675 return htmlspecialchars( trim( $show ) );
3679 // Since usually only infinite or indefinite is only on list, so try
3680 // equivalents if still here.
3681 $indefs = array( 'infinite', 'infinity', 'indefinite' );
3682 if ( in_array( $str, $indefs ) ) {
3683 foreach ( $indefs as $val ) {
3684 $show = array_search( $val, $duration, true );
3685 if ( $show !== false ) {
3686 return htmlspecialchars( trim( $show ) );
3691 // If all else fails, return a standard duration or timestamp description.
3692 $time = strtotime( $str, 0 );
3693 if ( $time === false ) { // Unknown format. Return it as-is in case.
3695 } elseif ( $time !== strtotime( $str, 1 ) ) { // It's a relative timestamp.
3696 // $time is relative to 0 so it's a duration length.
3697 return $this->formatDuration( $time );
3698 } else { // It's an absolute timestamp.
3699 if ( $time === 0 ) {
3700 // wfTimestamp() handles 0 as current time instead of epoch.
3701 return $this->timeanddate( '19700101000000' );
3703 return $this->timeanddate( $time );
3709 * languages like Chinese need to be segmented in order for the diff
3712 * @param $text String
3715 public function segmentForDiff( $text ) {
3720 * and unsegment to show the result
3722 * @param $text String
3725 public function unsegmentForDiff( $text ) {
3730 * Return the LanguageConverter used in the Language
3733 * @return LanguageConverter
3735 public function getConverter() {
3736 return $this->mConverter
;
3740 * convert text to all supported variants
3742 * @param $text string
3745 public function autoConvertToAllVariants( $text ) {
3746 return $this->mConverter
->autoConvertToAllVariants( $text );
3750 * convert text to different variants of a language.
3752 * @param $text string
3755 public function convert( $text ) {
3756 return $this->mConverter
->convert( $text );
3760 * Convert a Title object to a string in the preferred variant
3762 * @param $title Title
3765 public function convertTitle( $title ) {
3766 return $this->mConverter
->convertTitle( $title );
3770 * Convert a namespace index to a string in the preferred variant
3775 public function convertNamespace( $ns ) {
3776 return $this->mConverter
->convertNamespace( $ns );
3780 * Check if this is a language with variants
3784 public function hasVariants() {
3785 return count( $this->getVariants() ) > 1;
3789 * Check if the language has the specific variant
3792 * @param $variant string
3795 public function hasVariant( $variant ) {
3796 return (bool)$this->mConverter
->validateVariant( $variant );
3800 * Put custom tags (e.g. -{ }-) around math to prevent conversion
3802 * @param $text string
3805 public function armourMath( $text ) {
3806 return $this->mConverter
->armourMath( $text );
3810 * Perform output conversion on a string, and encode for safe HTML output.
3811 * @param $text String text to be converted
3812 * @param $isTitle Bool whether this conversion is for the article title
3814 * @todo this should get integrated somewhere sane
3816 public function convertHtml( $text, $isTitle = false ) {
3817 return htmlspecialchars( $this->convert( $text, $isTitle ) );
3821 * @param $key string
3824 public function convertCategoryKey( $key ) {
3825 return $this->mConverter
->convertCategoryKey( $key );
3829 * Get the list of variants supported by this language
3830 * see sample implementation in LanguageZh.php
3832 * @return array an array of language codes
3834 public function getVariants() {
3835 return $this->mConverter
->getVariants();
3841 public function getPreferredVariant() {
3842 return $this->mConverter
->getPreferredVariant();
3848 public function getDefaultVariant() {
3849 return $this->mConverter
->getDefaultVariant();
3855 public function getURLVariant() {
3856 return $this->mConverter
->getURLVariant();
3860 * If a language supports multiple variants, it is
3861 * possible that non-existing link in one variant
3862 * actually exists in another variant. this function
3863 * tries to find it. See e.g. LanguageZh.php
3865 * @param $link String: the name of the link
3866 * @param $nt Mixed: the title object of the link
3867 * @param $ignoreOtherCond Boolean: to disable other conditions when
3868 * we need to transclude a template or update a category's link
3869 * @return null the input parameters may be modified upon return
3871 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
3872 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
3876 * If a language supports multiple variants, converts text
3877 * into an array of all possible variants of the text:
3878 * 'variant' => text in that variant
3880 * @deprecated since 1.17 Use autoConvertToAllVariants()
3882 * @param $text string
3886 public function convertLinkToAllVariants( $text ) {
3887 return $this->mConverter
->convertLinkToAllVariants( $text );
3891 * returns language specific options used by User::getPageRenderHash()
3892 * for example, the preferred language variant
3896 function getExtraHashOptions() {
3897 return $this->mConverter
->getExtraHashOptions();
3901 * For languages that support multiple variants, the title of an
3902 * article may be displayed differently in different variants. this
3903 * function returns the apporiate title defined in the body of the article.
3907 public function getParsedTitle() {
3908 return $this->mConverter
->getParsedTitle();
3912 * Prepare external link text for conversion. When the text is
3913 * a URL, it shouldn't be converted, and it'll be wrapped in
3914 * the "raw" tag (-{R| }-) to prevent conversion.
3916 * This function is called "markNoConversion" for historical
3919 * @param $text String: text to be used for external link
3920 * @param $noParse bool: wrap it without confirming it's a real URL first
3921 * @return string the tagged text
3923 public function markNoConversion( $text, $noParse = false ) {
3924 // Excluding protocal-relative URLs may avoid many false positives.
3925 if ( $noParse ||
preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
3926 return $this->mConverter
->markNoConversion( $text );
3933 * A regular expression to match legal word-trailing characters
3934 * which should be merged onto a link of the form [[foo]]bar.
3938 public function linkTrail() {
3939 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
3945 function getLangObj() {
3950 * Get the "parent" language which has a converter to convert a "compatible" language
3951 * (in another variant) to this language (eg. zh for zh-cn, but not en for en-gb).
3953 * @return Language|null
3956 public function getParentLanguage() {
3957 if ( $this->mParentLanguage
!== false ) {
3958 return $this->mParentLanguage
;
3961 $pieces = explode( '-', $this->getCode() );
3963 if ( !in_array( $code, LanguageConverter
::$languagesWithVariants ) ) {
3964 $this->mParentLanguage
= null;
3967 $lang = Language
::factory( $code );
3968 if ( !$lang->hasVariant( $this->getCode() ) ) {
3969 $this->mParentLanguage
= null;
3973 $this->mParentLanguage
= $lang;
3978 * Get the RFC 3066 code for this language object
3980 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
3981 * htmlspecialchars() or similar
3985 public function getCode() {
3986 return $this->mCode
;
3990 * Get the code in Bcp47 format which we can use
3991 * inside of html lang="" tags.
3993 * NOTE: The return value of this function is NOT HTML-safe and must be escaped with
3994 * htmlspecialchars() or similar.
3999 public function getHtmlCode() {
4000 if ( is_null( $this->mHtmlCode
) ) {
4001 $this->mHtmlCode
= wfBCP47( $this->getCode() );
4003 return $this->mHtmlCode
;
4007 * @param $code string
4009 public function setCode( $code ) {
4010 $this->mCode
= $code;
4011 // Ensure we don't leave incorrect cached data lying around
4012 $this->mHtmlCode
= null;
4013 $this->mParentLanguage
= false;
4017 * Get the name of a file for a certain language code
4018 * @param $prefix string Prepend this to the filename
4019 * @param $code string Language code
4020 * @param $suffix string Append this to the filename
4021 * @throws MWException
4022 * @return string $prefix . $mangledCode . $suffix
4024 public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
4025 // Protect against path traversal
4026 if ( !Language
::isValidCode( $code )
4027 ||
strcspn( $code, ":/\\\000" ) !== strlen( $code ) )
4029 throw new MWException( "Invalid language code \"$code\"" );
4032 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
4036 * Get the language code from a file name. Inverse of getFileName()
4037 * @param $filename string $prefix . $languageCode . $suffix
4038 * @param $prefix string Prefix before the language code
4039 * @param $suffix string Suffix after the language code
4040 * @return string Language code, or false if $prefix or $suffix isn't found
4042 public static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
4044 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
4045 preg_quote( $suffix, '/' ) . '/', $filename, $m );
4046 if ( !count( $m ) ) {
4049 return str_replace( '_', '-', strtolower( $m[1] ) );
4053 * @param $code string
4056 public static function getMessagesFileName( $code ) {
4058 $file = self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
4059 wfRunHooks( 'Language::getMessagesFileName', array( $code, &$file ) );
4064 * @param $code string
4067 public static function getClassFileName( $code ) {
4069 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
4073 * Get the first fallback for a given language.
4075 * @param $code string
4077 * @return bool|string
4079 public static function getFallbackFor( $code ) {
4080 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
4083 $fallbacks = self
::getFallbacksFor( $code );
4084 $first = array_shift( $fallbacks );
4090 * Get the ordered list of fallback languages.
4093 * @param $code string Language code
4096 public static function getFallbacksFor( $code ) {
4097 if ( $code === 'en' ||
!Language
::isValidBuiltInCode( $code ) ) {
4100 $v = self
::getLocalisationCache()->getItem( $code, 'fallback' );
4101 $v = array_map( 'trim', explode( ',', $v ) );
4102 if ( $v[count( $v ) - 1] !== 'en' ) {
4110 * Get the ordered list of fallback languages, ending with the fallback
4111 * language chain for the site language.
4114 * @param string $code Language code
4115 * @return array array( fallbacks, site fallbacks )
4117 public static function getFallbacksIncludingSiteLanguage( $code ) {
4118 global $wgLanguageCode;
4120 // Usually, we will only store a tiny number of fallback chains, so we
4121 // keep them in static memory.
4122 $cacheKey = "{$code}-{$wgLanguageCode}";
4124 if ( !array_key_exists( $cacheKey, self
::$fallbackLanguageCache ) ) {
4125 $fallbacks = self
::getFallbacksFor( $code );
4127 // Append the site's fallback chain, including the site language itself
4128 $siteFallbacks = self
::getFallbacksFor( $wgLanguageCode );
4129 array_unshift( $siteFallbacks, $wgLanguageCode );
4131 // Eliminate any languages already included in the chain
4132 $siteFallbacks = array_diff( $siteFallbacks, $fallbacks );
4134 self
::$fallbackLanguageCache[$cacheKey] = array( $fallbacks, $siteFallbacks );
4136 return self
::$fallbackLanguageCache[$cacheKey];
4140 * Get all messages for a given language
4141 * WARNING: this may take a long time. If you just need all message *keys*
4142 * but need the *contents* of only a few messages, consider using getMessageKeysFor().
4144 * @param $code string
4148 public static function getMessagesFor( $code ) {
4149 return self
::getLocalisationCache()->getItem( $code, 'messages' );
4153 * Get a message for a given language
4155 * @param $key string
4156 * @param $code string
4160 public static function getMessageFor( $key, $code ) {
4161 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
4165 * Get all message keys for a given language. This is a faster alternative to
4166 * array_keys( Language::getMessagesFor( $code ) )
4169 * @param $code string Language code
4170 * @return array of message keys (strings)
4172 public static function getMessageKeysFor( $code ) {
4173 return self
::getLocalisationCache()->getSubItemList( $code, 'messages' );
4180 function fixVariableInNamespace( $talk ) {
4181 if ( strpos( $talk, '$1' ) === false ) {
4185 global $wgMetaNamespace;
4186 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
4188 # Allow grammar transformations
4189 # Allowing full message-style parsing would make simple requests
4190 # such as action=raw much more expensive than they need to be.
4191 # This will hopefully cover most cases.
4192 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
4193 array( &$this, 'replaceGrammarInNamespace' ), $talk );
4194 return str_replace( ' ', '_', $talk );
4201 function replaceGrammarInNamespace( $m ) {
4202 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
4206 * @throws MWException
4209 static function getCaseMaps() {
4210 static $wikiUpperChars, $wikiLowerChars;
4211 if ( isset( $wikiUpperChars ) ) {
4212 return array( $wikiUpperChars, $wikiLowerChars );
4215 wfProfileIn( __METHOD__
);
4216 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
4217 if ( $arr === false ) {
4218 throw new MWException(
4219 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
4221 $wikiUpperChars = $arr['wikiUpperChars'];
4222 $wikiLowerChars = $arr['wikiLowerChars'];
4223 wfProfileOut( __METHOD__
);
4224 return array( $wikiUpperChars, $wikiLowerChars );
4228 * Decode an expiry (block, protection, etc) which has come from the DB
4230 * @todo FIXME: why are we returnings DBMS-dependent strings???
4232 * @param $expiry String: Database expiry String
4233 * @param $format Bool|Int true to process using language functions, or TS_ constant
4234 * to return the expiry in a given timestamp
4238 public function formatExpiry( $expiry, $format = true ) {
4240 if ( $infinity === null ) {
4241 $infinity = wfGetDB( DB_SLAVE
)->getInfinity();
4244 if ( $expiry == '' ||
$expiry == $infinity ) {
4245 return $format === true
4246 ?
$this->getMessageFromDB( 'infiniteblock' )
4249 return $format === true
4250 ?
$this->timeanddate( $expiry, /* User preference timezone */ true )
4251 : wfTimestamp( $format, $expiry );
4257 * @param $seconds int|float
4258 * @param $format Array Optional
4259 * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour
4260 * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours
4261 * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends
4262 * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes'
4265 function formatTimePeriod( $seconds, $format = array() ) {
4266 if ( !is_array( $format ) ) {
4267 $format = array( 'avoid' => $format ); // For backwards compatibility
4269 if ( !isset( $format['avoid'] ) ) {
4270 $format['avoid'] = false;
4272 if ( !isset( $format['noabbrevs' ] ) ) {
4273 $format['noabbrevs'] = false;
4275 $secondsMsg = wfMessage(
4276 $format['noabbrevs'] ?
'seconds' : 'seconds-abbrev' )->inLanguage( $this );
4277 $minutesMsg = wfMessage(
4278 $format['noabbrevs'] ?
'minutes' : 'minutes-abbrev' )->inLanguage( $this );
4279 $hoursMsg = wfMessage(
4280 $format['noabbrevs'] ?
'hours' : 'hours-abbrev' )->inLanguage( $this );
4281 $daysMsg = wfMessage(
4282 $format['noabbrevs'] ?
'days' : 'days-abbrev' )->inLanguage( $this );
4284 if ( round( $seconds * 10 ) < 100 ) {
4285 $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
4286 $s = $secondsMsg->params( $s )->text();
4287 } elseif ( round( $seconds ) < 60 ) {
4288 $s = $this->formatNum( round( $seconds ) );
4289 $s = $secondsMsg->params( $s )->text();
4290 } elseif ( round( $seconds ) < 3600 ) {
4291 $minutes = floor( $seconds / 60 );
4292 $secondsPart = round( fmod( $seconds, 60 ) );
4293 if ( $secondsPart == 60 ) {
4297 $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4299 $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4300 } elseif ( round( $seconds ) <= 2 * 86400 ) {
4301 $hours = floor( $seconds / 3600 );
4302 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
4303 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
4304 if ( $secondsPart == 60 ) {
4308 if ( $minutes == 60 ) {
4312 $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
4314 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4315 if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) {
4316 $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
4319 $days = floor( $seconds / 86400 );
4320 if ( $format['avoid'] === 'avoidminutes' ) {
4321 $hours = round( ( $seconds - $days * 86400 ) / 3600 );
4322 if ( $hours == 24 ) {
4326 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4328 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4329 } elseif ( $format['avoid'] === 'avoidseconds' ) {
4330 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
4331 $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
4332 if ( $minutes == 60 ) {
4336 if ( $hours == 24 ) {
4340 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4342 $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
4344 $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
4346 $s = $daysMsg->params( $this->formatNum( $days ) )->text();
4348 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
4355 * Format a bitrate for output, using an appropriate
4356 * unit (bps, kbps, Mbps, Gbps, Tbps, Pbps, Ebps, Zbps or Ybps) according to the magnitude in question
4358 * This use base 1000. For base 1024 use formatSize(), for another base
4359 * see formatComputingNumbers()
4364 function formatBitrate( $bps ) {
4365 return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" );
4369 * @param $size int Size of the unit
4370 * @param $boundary int Size boundary (1000, or 1024 in most cases)
4371 * @param $messageKey string Message key to be uesd
4374 function formatComputingNumbers( $size, $boundary, $messageKey ) {
4376 return str_replace( '$1', $this->formatNum( $size ),
4377 $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) )
4380 $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
4383 $maxIndex = count( $sizes ) - 1;
4384 while ( $size >= $boundary && $index < $maxIndex ) {
4389 // For small sizes no decimal places necessary
4392 // For MB and bigger two decimal places are smarter
4395 $msg = str_replace( '$1', $sizes[$index], $messageKey );
4397 $size = round( $size, $round );
4398 $text = $this->getMessageFromDB( $msg );
4399 return str_replace( '$1', $this->formatNum( $size ), $text );
4403 * Format a size in bytes for output, using an appropriate
4404 * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
4406 * This method use base 1024. For base 1000 use formatBitrate(), for
4407 * another base see formatComputingNumbers()
4409 * @param $size int Size to format
4410 * @return string Plain text (not HTML)
4412 function formatSize( $size ) {
4413 return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" );
4417 * Make a list item, used by various special pages
4419 * @param $page String Page link
4420 * @param $details String Text between brackets
4421 * @param $oppositedm Boolean Add the direction mark opposite to your
4422 * language, to display text properly
4425 function specialList( $page, $details, $oppositedm = true ) {
4426 $dirmark = ( $oppositedm ?
$this->getDirMark( true ) : '' ) .
4427 $this->getDirMark();
4428 $details = $details ?
$dirmark . $this->getMessageFromDB( 'word-separator' ) .
4429 wfMessage( 'parentheses' )->rawParams( $details )->inLanguage( $this )->escaped() : '';
4430 return $page . $details;
4434 * Generate (prev x| next x) (20|50|100...) type links for paging
4436 * @param $title Title object to link
4437 * @param $offset Integer offset parameter
4438 * @param $limit Integer limit parameter
4439 * @param $query array|String optional URL query parameter string
4440 * @param $atend Bool optional param for specified if this is the last page
4443 public function viewPrevNext( Title
$title, $offset, $limit, array $query = array(), $atend = false ) {
4444 // @todo FIXME: Why on earth this needs one message for the text and another one for tooltip?
4446 # Make 'previous' link
4447 $prev = wfMessage( 'prevn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4448 if ( $offset > 0 ) {
4449 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit,
4450 $query, $prev, 'prevn-title', 'mw-prevlink' );
4452 $plink = htmlspecialchars( $prev );
4456 $next = wfMessage( 'nextn' )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4458 $nlink = htmlspecialchars( $next );
4460 $nlink = $this->numLink( $title, $offset +
$limit, $limit,
4461 $query, $next, 'prevn-title', 'mw-nextlink' );
4464 # Make links to set number of items per page
4465 $numLinks = array();
4466 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
4467 $numLinks[] = $this->numLink( $title, $offset, $num,
4468 $query, $this->formatNum( $num ), 'shown-title', 'mw-numlink' );
4471 return wfMessage( 'viewprevnext' )->inLanguage( $this )->title( $title
4472 )->rawParams( $plink, $nlink, $this->pipeList( $numLinks ) )->escaped();
4476 * Helper function for viewPrevNext() that generates links
4478 * @param $title Title object to link
4479 * @param $offset Integer offset parameter
4480 * @param $limit Integer limit parameter
4481 * @param $query Array extra query parameters
4482 * @param $link String text to use for the link; will be escaped
4483 * @param $tooltipMsg String name of the message to use as tooltip
4484 * @param $class String value of the "class" attribute of the link
4485 * @return String HTML fragment
4487 private function numLink( Title
$title, $offset, $limit, array $query, $link, $tooltipMsg, $class ) {
4488 $query = array( 'limit' => $limit, 'offset' => $offset ) +
$query;
4489 $tooltip = wfMessage( $tooltipMsg )->inLanguage( $this )->title( $title )->numParams( $limit )->text();
4490 return Html
::element( 'a', array( 'href' => $title->getLocalURL( $query ),
4491 'title' => $tooltip, 'class' => $class ), $link );
4495 * Get the conversion rule title, if any.
4499 public function getConvRuleTitle() {
4500 return $this->mConverter
->getConvRuleTitle();
4504 * Get the compiled plural rules for the language
4506 * @return array Associative array with plural form, and plural rule as key-value pairs
4508 public function getCompiledPluralRules() {
4509 $pluralRules = self
::$dataCache->getItem( strtolower( $this->mCode
), 'compiledPluralRules' );
4510 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4511 if ( !$pluralRules ) {
4512 foreach ( $fallbacks as $fallbackCode ) {
4513 $pluralRules = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
4514 if ( $pluralRules ) {
4519 return $pluralRules;
4523 * Get the plural rules for the language
4525 * @return array Associative array with plural form number and plural rule as key-value pairs
4527 public function getPluralRules() {
4528 $pluralRules = self
::$dataCache->getItem( strtolower( $this->mCode
), 'pluralRules' );
4529 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4530 if ( !$pluralRules ) {
4531 foreach ( $fallbacks as $fallbackCode ) {
4532 $pluralRules = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRules' );
4533 if ( $pluralRules ) {
4538 return $pluralRules;
4542 * Get the plural rule types for the language
4544 * @return array Associative array with plural form number and plural rule type as key-value pairs
4546 public function getPluralRuleTypes() {
4547 $pluralRuleTypes = self
::$dataCache->getItem( strtolower( $this->mCode
), 'pluralRuleTypes' );
4548 $fallbacks = Language
::getFallbacksFor( $this->mCode
);
4549 if ( !$pluralRuleTypes ) {
4550 foreach ( $fallbacks as $fallbackCode ) {
4551 $pluralRuleTypes = self
::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRuleTypes' );
4552 if ( $pluralRuleTypes ) {
4557 return $pluralRuleTypes;
4561 * Find the index number of the plural rule appropriate for the given number
4562 * @return int The index number of the plural rule
4564 public function getPluralRuleIndexNumber( $number ) {
4565 $pluralRules = $this->getCompiledPluralRules();
4566 $form = CLDRPluralRuleEvaluator
::evaluateCompiled( $number, $pluralRules );
4571 * Find the plural rule type appropriate for the given number
4572 * For example, if the language is set to Arabic, getPluralType(5) should
4575 * @return string The name of the plural rule type, e.g. one, two, few, many
4577 public function getPluralRuleType( $number ) {
4578 $index = $this->getPluralRuleIndexNumber( $number );
4579 $pluralRuleTypes = $this->getPluralRuleTypes();
4580 if ( isset( $pluralRuleTypes[$index] ) ) {
4581 return $pluralRuleTypes[$index];