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