3 * Internationalisation code
10 * @defgroup Language Language
13 if ( !defined( 'MEDIAWIKI' ) ) {
14 echo "This file is part of MediaWiki, it is not a valid entry point.\n";
19 global $wgLanguageNames;
20 require_once( dirname( __FILE__
) . '/Names.php' );
22 global $wgInputEncoding, $wgOutputEncoding;
25 * These are always UTF-8, they exist only for backwards compatibility
27 $wgInputEncoding = 'UTF-8';
28 $wgOutputEncoding = 'UTF-8';
30 if ( function_exists( 'mb_strtoupper' ) ) {
31 mb_internal_encoding( 'UTF-8' );
35 * a fake language converter
41 function __construct( $langobj ) { $this->mLang
= $langobj; }
42 function autoConvertToAllVariants( $text ) { return array( $this->mLang
->getCode() => $text ); }
43 function convert( $t ) { return $t; }
44 function convertTitle( $t ) { return $t->getPrefixedText(); }
45 function getVariants() { return array( $this->mLang
->getCode() ); }
46 function getPreferredVariant() { return $this->mLang
->getCode(); }
47 function getDefaultVariant() { return $this->mLang
->getCode(); }
48 function getURLVariant() { return ''; }
49 function getConvRuleTitle() { return false; }
50 function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
51 function getExtraHashOptions() { return ''; }
52 function getParsedTitle() { return ''; }
53 function markNoConversion( $text, $noParse = false ) { return $text; }
54 function convertCategoryKey( $key ) { return $key; }
55 function convertLinkToAllVariants( $text ) { return $this->autoConvertToAllVariants( $text ); }
56 function armourMath( $text ) { return $text; }
60 * Internationalisation code
64 var $mConverter, $mVariants, $mCode, $mLoaded = false;
65 var $mMagicExtensions = array(), $mMagicHookDone = false;
67 var $mNamespaceIds, $namespaceNames, $namespaceAliases;
68 var $dateFormatStrings = array();
69 var $mExtendedSpecialPageAliases;
72 * ReplacementArray object caches
74 var $transformData = array();
76 static public $dataCache;
77 static public $mLangObjCache = array();
79 static public $mWeekdayMsgs = array(
80 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
84 static public $mWeekdayAbbrevMsgs = array(
85 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
88 static public $mMonthMsgs = array(
89 'january', 'february', 'march', 'april', 'may_long', 'june',
90 'july', 'august', 'september', 'october', 'november',
93 static public $mMonthGenMsgs = array(
94 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
95 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
98 static public $mMonthAbbrevMsgs = array(
99 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
100 'sep', 'oct', 'nov', 'dec'
103 static public $mIranianCalendarMonthMsgs = array(
104 'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
105 'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
106 'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
107 'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
110 static public $mHebrewCalendarMonthMsgs = array(
111 'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
112 'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
113 'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
114 'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
115 'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
118 static public $mHebrewCalendarMonthGenMsgs = array(
119 'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
120 'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
121 'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
122 'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
123 'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
126 static public $mHijriCalendarMonthMsgs = array(
127 'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
128 'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
129 'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
130 'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
134 * Get a cached language object for a given language code
135 * @param $code String
138 static function factory( $code ) {
139 if ( !isset( self
::$mLangObjCache[$code] ) ) {
140 if ( count( self
::$mLangObjCache ) > 10 ) {
141 // Don't keep a billion objects around, that's stupid.
142 self
::$mLangObjCache = array();
144 self
::$mLangObjCache[$code] = self
::newFromCode( $code );
146 return self
::$mLangObjCache[$code];
150 * Create a language object for a given language code
151 * @param $code String
154 protected static function newFromCode( $code ) {
156 static $recursionLevel = 0;
158 // Protect against path traversal below
159 if ( !Language
::isValidCode( $code ) ) {
160 throw new MWException( "Invalid language code \"$code\"" );
163 if ( $code == 'en' ) {
166 $class = 'Language' . str_replace( '-', '_', ucfirst( $code ) );
167 // Preload base classes to work around APC/PHP5 bug
168 if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
169 include_once( "$IP/languages/classes/$class.deps.php" );
171 if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
172 include_once( "$IP/languages/classes/$class.php" );
176 if ( $recursionLevel > 5 ) {
177 throw new MWException( "Language fallback loop detected when creating class $class\n" );
180 if ( !class_exists( $class ) ) {
181 $fallback = Language
::getFallbackFor( $code );
183 $lang = Language
::newFromCode( $fallback );
185 $lang->setCode( $code );
193 * Returns true if a language code string is of a valid form, whether or
196 public static function isValidCode( $code ) {
197 return strcspn( $code, "/\\\000" ) === strlen( $code );
201 * Get the LocalisationCache instance
203 public static function getLocalisationCache() {
204 if ( is_null( self
::$dataCache ) ) {
205 global $wgLocalisationCacheConf;
206 $class = $wgLocalisationCacheConf['class'];
207 self
::$dataCache = new $class( $wgLocalisationCacheConf );
209 return self
::$dataCache;
212 function __construct() {
213 $this->mConverter
= new FakeConverter( $this );
214 // Set the code to the name of the descendant
215 if ( get_class( $this ) == 'Language' ) {
218 $this->mCode
= str_replace( '_', '-', strtolower( substr( get_class( $this ), 8 ) ) );
220 self
::getLocalisationCache();
224 * Reduce memory usage
226 function __destruct() {
227 foreach ( $this as $name => $value ) {
228 unset( $this->$name );
233 * Hook which will be called if this is the content language.
234 * Descendants can use this to register hook functions or modify globals
236 function initContLang() { }
239 * @deprecated Use User::getDefaultOptions()
242 function getDefaultUserOptions() {
243 wfDeprecated( __METHOD__
);
244 return User
::getDefaultOptions();
247 function getFallbackLanguageCode() {
248 if ( $this->mCode
=== 'en' ) {
251 return self
::$dataCache->getItem( $this->mCode
, 'fallback' );
256 * Exports $wgBookstoreListEn
259 function getBookstoreList() {
260 return self
::$dataCache->getItem( $this->mCode
, 'bookstoreList' );
266 function getNamespaces() {
267 if ( is_null( $this->namespaceNames
) ) {
268 global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
270 $this->namespaceNames
= self
::$dataCache->getItem( $this->mCode
, 'namespaceNames' );
271 $validNamespaces = MWNamespace
::getCanonicalNamespaces();
273 $this->namespaceNames
= $wgExtraNamespaces +
$this->namespaceNames +
$validNamespaces;
275 $this->namespaceNames
[NS_PROJECT
] = $wgMetaNamespace;
276 if ( $wgMetaNamespaceTalk ) {
277 $this->namespaceNames
[NS_PROJECT_TALK
] = $wgMetaNamespaceTalk;
279 $talk = $this->namespaceNames
[NS_PROJECT_TALK
];
280 $this->namespaceNames
[NS_PROJECT_TALK
] =
281 $this->fixVariableInNamespace( $talk );
284 # Sometimes a language will be localised but not actually exist on this wiki.
285 foreach( $this->namespaceNames
as $key => $text ) {
286 if ( !isset( $validNamespaces[$key] ) ) {
287 unset( $this->namespaceNames
[$key] );
291 # The above mixing may leave namespaces out of canonical order.
292 # Re-order by namespace ID number...
293 ksort( $this->namespaceNames
);
295 return $this->namespaceNames
;
299 * A convenience function that returns the same thing as
300 * getNamespaces() except with the array values changed to ' '
301 * where it found '_', useful for producing output to be displayed
302 * e.g. in <select> forms.
306 function getFormattedNamespaces() {
307 $ns = $this->getNamespaces();
308 foreach ( $ns as $k => $v ) {
309 $ns[$k] = strtr( $v, '_', ' ' );
315 * Get a namespace value by key
317 * $mw_ns = $wgContLang->getNsText( NS_MEDIAWIKI );
318 * echo $mw_ns; // prints 'MediaWiki'
321 * @param $index Int: the array key of the namespace to return
322 * @return mixed, string if the namespace value exists, otherwise false
324 function getNsText( $index ) {
325 $ns = $this->getNamespaces();
326 return isset( $ns[$index] ) ?
$ns[$index] : false;
330 * A convenience function that returns the same thing as
331 * getNsText() except with '_' changed to ' ', useful for
336 function getFormattedNsText( $index ) {
337 $ns = $this->getNsText( $index );
338 return strtr( $ns, '_', ' ' );
342 * Returns gender-dependent namespace alias if available.
343 * @param $index Int: namespace index
344 * @param $gender String: gender key (male, female... )
348 function getGenderNsText( $index, $gender ) {
349 $ns = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
350 return isset( $ns[$index][$gender] ) ?
$ns[$index][$gender] : $this->getNsText( $index );
354 * Whether this language makes distinguishes genders for example in
359 function needsGenderDistinction() {
360 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
361 return count( $aliases ) > 0;
365 * Get a namespace key by value, case insensitive.
366 * Only matches namespace names for the current language, not the
367 * canonical ones defined in Namespace.php.
369 * @param $text String
370 * @return mixed An integer if $text is a valid value otherwise false
372 function getLocalNsIndex( $text ) {
373 $lctext = $this->lc( $text );
374 $ids = $this->getNamespaceIds();
375 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
378 function getNamespaceAliases() {
379 if ( is_null( $this->namespaceAliases
) ) {
380 $aliases = self
::$dataCache->getItem( $this->mCode
, 'namespaceAliases' );
384 foreach ( $aliases as $name => $index ) {
385 if ( $index === NS_PROJECT_TALK
) {
386 unset( $aliases[$name] );
387 $name = $this->fixVariableInNamespace( $name );
388 $aliases[$name] = $index;
393 $genders = self
::$dataCache->getItem( $this->mCode
, 'namespaceGenderAliases' );
394 foreach ( $genders as $index => $forms ) {
395 foreach ( $forms as $alias ) {
396 $aliases[$alias] = $index;
400 $this->namespaceAliases
= $aliases;
402 return $this->namespaceAliases
;
405 function getNamespaceIds() {
406 if ( is_null( $this->mNamespaceIds
) ) {
407 global $wgNamespaceAliases;
408 # Put namespace names and aliases into a hashtable.
409 # If this is too slow, then we should arrange it so that it is done
410 # before caching. The catch is that at pre-cache time, the above
411 # class-specific fixup hasn't been done.
412 $this->mNamespaceIds
= array();
413 foreach ( $this->getNamespaces() as $index => $name ) {
414 $this->mNamespaceIds
[$this->lc( $name )] = $index;
416 foreach ( $this->getNamespaceAliases() as $name => $index ) {
417 $this->mNamespaceIds
[$this->lc( $name )] = $index;
419 if ( $wgNamespaceAliases ) {
420 foreach ( $wgNamespaceAliases as $name => $index ) {
421 $this->mNamespaceIds
[$this->lc( $name )] = $index;
425 return $this->mNamespaceIds
;
430 * Get a namespace key by value, case insensitive. Canonical namespace
431 * names override custom ones defined for the current language.
433 * @param $text String
434 * @return mixed An integer if $text is a valid value otherwise false
436 function getNsIndex( $text ) {
437 $lctext = $this->lc( $text );
438 if ( ( $ns = MWNamespace
::getCanonicalIndex( $lctext ) ) !== null ) {
441 $ids = $this->getNamespaceIds();
442 return isset( $ids[$lctext] ) ?
$ids[$lctext] : false;
446 * short names for language variants used for language conversion links.
448 * @param $code String
451 function getVariantname( $code ) {
452 return $this->getMessageFromDB( "variantname-$code" );
455 function specialPage( $name ) {
456 $aliases = $this->getSpecialPageAliases();
457 if ( isset( $aliases[$name][0] ) ) {
458 $name = $aliases[$name][0];
460 return $this->getNsText( NS_SPECIAL
) . ':' . $name;
463 function getQuickbarSettings() {
465 $this->getMessage( 'qbsettings-none' ),
466 $this->getMessage( 'qbsettings-fixedleft' ),
467 $this->getMessage( 'qbsettings-fixedright' ),
468 $this->getMessage( 'qbsettings-floatingleft' ),
469 $this->getMessage( 'qbsettings-floatingright' )
473 function getMathNames() {
474 return self
::$dataCache->getItem( $this->mCode
, 'mathNames' );
477 function getDatePreferences() {
478 return self
::$dataCache->getItem( $this->mCode
, 'datePreferences' );
481 function getDateFormats() {
482 return self
::$dataCache->getItem( $this->mCode
, 'dateFormats' );
485 function getDefaultDateFormat() {
486 $df = self
::$dataCache->getItem( $this->mCode
, 'defaultDateFormat' );
487 if ( $df === 'dmy or mdy' ) {
488 global $wgAmericanDates;
489 return $wgAmericanDates ?
'mdy' : 'dmy';
495 function getDatePreferenceMigrationMap() {
496 return self
::$dataCache->getItem( $this->mCode
, 'datePreferenceMigrationMap' );
499 function getImageFile( $image ) {
500 return self
::$dataCache->getSubitem( $this->mCode
, 'imageFiles', $image );
503 function getDefaultUserOptionOverrides() {
504 return self
::$dataCache->getItem( $this->mCode
, 'defaultUserOptionOverrides' );
507 function getExtraUserToggles() {
508 return self
::$dataCache->getItem( $this->mCode
, 'extraUserToggles' );
511 function getUserToggle( $tog ) {
512 return $this->getMessageFromDB( "tog-$tog" );
516 * Get language names, indexed by code.
517 * If $customisedOnly is true, only returns codes with a messages file
519 public static function getLanguageNames( $customisedOnly = false ) {
520 global $wgLanguageNames, $wgExtraLanguageNames;
521 $allNames = $wgExtraLanguageNames +
$wgLanguageNames;
522 if ( !$customisedOnly ) {
528 $dir = opendir( "$IP/languages/messages" );
529 while ( false !== ( $file = readdir( $dir ) ) ) {
530 $code = self
::getCodeFromFileName( $file, 'Messages' );
531 if ( $code && isset( $allNames[$code] ) ) {
532 $names[$code] = $allNames[$code];
540 * Get translated language names. This is done on best effort and
541 * by default this is exactly the same as Language::getLanguageNames.
542 * The CLDR extension provides translated names.
543 * @param $code String Language code.
544 * @return Array language code => language name
547 public static function getTranslatedLanguageNames( $code ) {
549 wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
551 foreach ( self
::getLanguageNames() as $code => $name ) {
552 if ( !isset( $names[$code] ) ) $names[$code] = $name;
559 * Get a message from the MediaWiki namespace.
561 * @param $msg String: message name
564 function getMessageFromDB( $msg ) {
565 return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
568 function getLanguageName( $code ) {
569 $names = self
::getLanguageNames();
570 if ( !array_key_exists( $code, $names ) ) {
573 return $names[$code];
576 function getMonthName( $key ) {
577 return $this->getMessageFromDB( self
::$mMonthMsgs[$key - 1] );
580 function getMonthNameGen( $key ) {
581 return $this->getMessageFromDB( self
::$mMonthGenMsgs[$key - 1] );
584 function getMonthAbbreviation( $key ) {
585 return $this->getMessageFromDB( self
::$mMonthAbbrevMsgs[$key - 1] );
588 function getWeekdayName( $key ) {
589 return $this->getMessageFromDB( self
::$mWeekdayMsgs[$key - 1] );
592 function getWeekdayAbbreviation( $key ) {
593 return $this->getMessageFromDB( self
::$mWeekdayAbbrevMsgs[$key - 1] );
596 function getIranianCalendarMonthName( $key ) {
597 return $this->getMessageFromDB( self
::$mIranianCalendarMonthMsgs[$key - 1] );
600 function getHebrewCalendarMonthName( $key ) {
601 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthMsgs[$key - 1] );
604 function getHebrewCalendarMonthNameGen( $key ) {
605 return $this->getMessageFromDB( self
::$mHebrewCalendarMonthGenMsgs[$key - 1] );
608 function getHijriCalendarMonthName( $key ) {
609 return $this->getMessageFromDB( self
::$mHijriCalendarMonthMsgs[$key - 1] );
613 * Used by date() and time() to adjust the time output.
615 * @param $ts Int the time in date('YmdHis') format
616 * @param $tz Mixed: adjust the time by this amount (default false, mean we
617 * get user timecorrection setting)
620 function userAdjust( $ts, $tz = false ) {
621 global $wgUser, $wgLocalTZoffset;
623 if ( $tz === false ) {
624 $tz = $wgUser->getOption( 'timecorrection' );
627 $data = explode( '|', $tz, 3 );
629 if ( $data[0] == 'ZoneInfo' ) {
630 if ( function_exists( 'timezone_open' ) && @timezone_open
( $data[2] ) !== false ) {
631 $date = date_create( $ts, timezone_open( 'UTC' ) );
632 date_timezone_set( $date, timezone_open( $data[2] ) );
633 $date = date_format( $date, 'YmdHis' );
636 # Unrecognized timezone, default to 'Offset' with the stored offset.
641 if ( $data[0] == 'System' ||
$tz == '' ) {
642 # Global offset in minutes.
643 if ( isset( $wgLocalTZoffset ) ) {
644 $minDiff = $wgLocalTZoffset;
646 } else if ( $data[0] == 'Offset' ) {
647 $minDiff = intval( $data[1] );
649 $data = explode( ':', $tz );
650 if ( count( $data ) == 2 ) {
651 $data[0] = intval( $data[0] );
652 $data[1] = intval( $data[1] );
653 $minDiff = abs( $data[0] ) * 60 +
$data[1];
654 if ( $data[0] < 0 ) {
655 $minDiff = -$minDiff;
658 $minDiff = intval( $data[0] ) * 60;
662 # No difference ? Return time unchanged
663 if ( 0 == $minDiff ) {
667 wfSuppressWarnings(); // E_STRICT system time bitching
668 # Generate an adjusted date; take advantage of the fact that mktime
669 # will normalize out-of-range values so we don't have to split $minDiff
670 # into hours and minutes.
672 (int)substr( $ts, 8, 2 ) ), # Hours
673 (int)substr( $ts, 10, 2 ) +
$minDiff, # Minutes
674 (int)substr( $ts, 12, 2 ), # Seconds
675 (int)substr( $ts, 4, 2 ), # Month
676 (int)substr( $ts, 6, 2 ), # Day
677 (int)substr( $ts, 0, 4 ) ); # Year
679 $date = date( 'YmdHis', $t );
686 * This is a workalike of PHP's date() function, but with better
687 * internationalisation, a reduced set of format characters, and a better
690 * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrU. See the
691 * PHP manual for definitions. There are a number of extensions, which
694 * xn Do not translate digits of the next numeric format character
695 * xN Toggle raw digit (xn) flag, stays set until explicitly unset
696 * xr Use roman numerals for the next numeric format character
697 * xh Use hebrew numerals for the next numeric format character
699 * xg Genitive month name
701 * xij j (day number) in Iranian calendar
702 * xiF F (month name) in Iranian calendar
703 * xin n (month number) in Iranian calendar
704 * xiY Y (full year) in Iranian calendar
706 * xjj j (day number) in Hebrew calendar
707 * xjF F (month name) in Hebrew calendar
708 * xjt t (days in month) in Hebrew calendar
709 * xjx xg (genitive month name) in Hebrew calendar
710 * xjn n (month number) in Hebrew calendar
711 * xjY Y (full year) in Hebrew calendar
713 * xmj j (day number) in Hijri calendar
714 * xmF F (month name) in Hijri calendar
715 * xmn n (month number) in Hijri calendar
716 * xmY Y (full year) in Hijri calendar
718 * xkY Y (full year) in Thai solar calendar. Months and days are
719 * identical to the Gregorian calendar
720 * xoY Y (full year) in Minguo calendar or Juche year.
721 * Months and days are identical to the
723 * xtY Y (full year) in Japanese nengo. Months and days are
724 * identical to the Gregorian calendar
726 * Characters enclosed in double quotes will be considered literal (with
727 * the quotes themselves removed). Unmatched quotes will be considered
728 * literal quotes. Example:
730 * "The month is" F => The month is January
733 * Backslash escaping is also supported.
735 * Input timestamp is assumed to be pre-normalized to the desired local
738 * @param $format String
739 * @param $ts String: 14-character timestamp
742 * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
744 function sprintfDate( $format, $ts ) {
757 for ( $p = 0; $p < strlen( $format ); $p++
) {
760 if ( $code == 'x' && $p < strlen( $format ) - 1 ) {
761 $code .= $format[++
$p];
764 if ( ( $code === 'xi' ||
$code == 'xj' ||
$code == 'xk' ||
$code == 'xm' ||
$code == 'xo' ||
$code == 'xt' ) && $p < strlen( $format ) - 1 ) {
765 $code .= $format[++
$p];
776 $rawToggle = !$rawToggle;
785 $s .= $this->getMonthNameGen( substr( $ts, 4, 2 ) );
788 if ( !$hebrew ) $hebrew = self
::tsToHebrew( $ts );
789 $s .= $this->getHebrewCalendarMonthNameGen( $hebrew[1] );
792 $num = substr( $ts, 6, 2 );
795 if ( !$unix ) $unix = wfTimestamp( TS_UNIX
, $ts );
796 $s .= $this->getWeekdayAbbreviation( gmdate( 'w', $unix ) +
1 );
799 $num = intval( substr( $ts, 6, 2 ) );
803 $iranian = self
::tsToIranian( $ts );
809 $hijri = self
::tsToHijri( $ts );
815 $hebrew = self
::tsToHebrew( $ts );
821 $unix = wfTimestamp( TS_UNIX
, $ts );
823 $s .= $this->getWeekdayName( gmdate( 'w', $unix ) +
1 );
827 $unix = wfTimestamp( TS_UNIX
, $ts );
829 $w = gmdate( 'w', $unix );
834 $unix = wfTimestamp( TS_UNIX
, $ts );
836 $num = gmdate( 'w', $unix );
840 $unix = wfTimestamp( TS_UNIX
, $ts );
842 $num = gmdate( 'z', $unix );
846 $unix = wfTimestamp( TS_UNIX
, $ts );
848 $num = gmdate( 'W', $unix );
851 $s .= $this->getMonthName( substr( $ts, 4, 2 ) );
855 $iranian = self
::tsToIranian( $ts );
857 $s .= $this->getIranianCalendarMonthName( $iranian[1] );
861 $hijri = self
::tsToHijri( $ts );
863 $s .= $this->getHijriCalendarMonthName( $hijri[1] );
867 $hebrew = self
::tsToHebrew( $ts );
869 $s .= $this->getHebrewCalendarMonthName( $hebrew[1] );
872 $num = substr( $ts, 4, 2 );
875 $s .= $this->getMonthAbbreviation( substr( $ts, 4, 2 ) );
878 $num = intval( substr( $ts, 4, 2 ) );
882 $iranian = self
::tsToIranian( $ts );
888 $hijri = self
::tsToHijri ( $ts );
894 $hebrew = self
::tsToHebrew( $ts );
900 $unix = wfTimestamp( TS_UNIX
, $ts );
902 $num = gmdate( 't', $unix );
906 $hebrew = self
::tsToHebrew( $ts );
912 $unix = wfTimestamp( TS_UNIX
, $ts );
914 $num = gmdate( 'L', $unix );
918 $unix = wfTimestamp( TS_UNIX
, $ts );
920 $num = date( 'o', $unix );
923 $num = substr( $ts, 0, 4 );
927 $iranian = self
::tsToIranian( $ts );
933 $hijri = self
::tsToHijri( $ts );
939 $hebrew = self
::tsToHebrew( $ts );
945 $thai = self
::tsToYear( $ts, 'thai' );
951 $minguo = self
::tsToYear( $ts, 'minguo' );
957 $tenno = self
::tsToYear( $ts, 'tenno' );
962 $num = substr( $ts, 2, 2 );
965 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'am' : 'pm';
968 $s .= intval( substr( $ts, 8, 2 ) ) < 12 ?
'AM' : 'PM';
971 $h = substr( $ts, 8, 2 );
972 $num = $h %
12 ?
$h %
12 : 12;
975 $num = intval( substr( $ts, 8, 2 ) );
978 $h = substr( $ts, 8, 2 );
979 $num = sprintf( '%02d', $h %
12 ?
$h %
12 : 12 );
982 $num = substr( $ts, 8, 2 );
985 $num = substr( $ts, 10, 2 );
988 $num = substr( $ts, 12, 2 );
992 $unix = wfTimestamp( TS_UNIX
, $ts );
994 $s .= gmdate( 'c', $unix );
998 $unix = wfTimestamp( TS_UNIX
, $ts );
1000 $s .= gmdate( 'r', $unix );
1004 $unix = wfTimestamp( TS_UNIX
, $ts );
1009 # Backslash escaping
1010 if ( $p < strlen( $format ) - 1 ) {
1011 $s .= $format[++
$p];
1018 if ( $p < strlen( $format ) - 1 ) {
1019 $endQuote = strpos( $format, '"', $p +
1 );
1020 if ( $endQuote === false ) {
1021 # No terminating quote, assume literal "
1024 $s .= substr( $format, $p +
1, $endQuote - $p - 1 );
1028 # Quote at end of string, assume literal "
1035 if ( $num !== false ) {
1036 if ( $rawToggle ||
$raw ) {
1039 } elseif ( $roman ) {
1040 $s .= self
::romanNumeral( $num );
1042 } elseif ( $hebrewNum ) {
1043 $s .= self
::hebrewNumeral( $num );
1046 $s .= $this->formatNum( $num, true );
1053 private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
1054 private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
1056 * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
1057 * Gregorian dates to Iranian dates. Originally written in C, it
1058 * is released under the terms of GNU Lesser General Public
1059 * License. Conversion to PHP was performed by Niklas Laxström.
1061 * Link: http://www.farsiweb.info/jalali/jalali.c
1063 private static function tsToIranian( $ts ) {
1064 $gy = substr( $ts, 0, 4 ) -1600;
1065 $gm = substr( $ts, 4, 2 ) -1;
1066 $gd = substr( $ts, 6, 2 ) -1;
1068 # Days passed from the beginning (including leap years)
1070 +
floor( ( $gy +
3 ) / 4 )
1071 - floor( ( $gy +
99 ) / 100 )
1072 +
floor( ( $gy +
399 ) / 400 );
1075 // Add days of the past months of this year
1076 for ( $i = 0; $i < $gm; $i++
) {
1077 $gDayNo +
= self
::$GREG_DAYS[$i];
1081 if ( $gm > 1 && ( ( $gy %
4 === 0 && $gy %
100 !== 0 ||
( $gy %
400 == 0 ) ) ) ) {
1085 // Days passed in current month
1088 $jDayNo = $gDayNo - 79;
1090 $jNp = floor( $jDayNo / 12053 );
1093 $jy = 979 +
33 * $jNp +
4 * floor( $jDayNo / 1461 );
1096 if ( $jDayNo >= 366 ) {
1097 $jy +
= floor( ( $jDayNo - 1 ) / 365 );
1098 $jDayNo = floor( ( $jDayNo - 1 ) %
365 );
1101 for ( $i = 0; $i < 11 && $jDayNo >= self
::$IRANIAN_DAYS[$i]; $i++
) {
1102 $jDayNo -= self
::$IRANIAN_DAYS[$i];
1108 return array( $jy, $jm, $jd );
1112 * Converting Gregorian dates to Hijri dates.
1114 * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
1116 * @link http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
1118 private static function tsToHijri( $ts ) {
1119 $year = substr( $ts, 0, 4 );
1120 $month = substr( $ts, 4, 2 );
1121 $day = substr( $ts, 6, 2 );
1129 ( $zy > 1582 ) ||
( ( $zy == 1582 ) && ( $zm > 10 ) ) ||
1130 ( ( $zy == 1582 ) && ( $zm == 10 ) && ( $zd > 14 ) )
1133 $zjd = (int)( ( 1461 * ( $zy +
4800 +
(int)( ( $zm - 14 ) / 12 ) ) ) / 4 ) +
1134 (int)( ( 367 * ( $zm - 2 - 12 * ( (int)( ( $zm - 14 ) / 12 ) ) ) ) / 12 ) -
1135 (int)( ( 3 * (int)( ( ( $zy +
4900 +
(int)( ( $zm - 14 ) / 12 ) ) / 100 ) ) ) / 4 ) +
1138 $zjd = 367 * $zy - (int)( ( 7 * ( $zy +
5001 +
(int)( ( $zm - 9 ) / 7 ) ) ) / 4 ) +
1139 (int)( ( 275 * $zm ) / 9 ) +
$zd +
1729777;
1142 $zl = $zjd -1948440 +
10632;
1143 $zn = (int)( ( $zl - 1 ) / 10631 );
1144 $zl = $zl - 10631 * $zn +
354;
1145 $zj = ( (int)( ( 10985 - $zl ) / 5316 ) ) * ( (int)( ( 50 * $zl ) / 17719 ) ) +
( (int)( $zl / 5670 ) ) * ( (int)( ( 43 * $zl ) / 15238 ) );
1146 $zl = $zl - ( (int)( ( 30 - $zj ) / 15 ) ) * ( (int)( ( 17719 * $zj ) / 50 ) ) - ( (int)( $zj / 16 ) ) * ( (int)( ( 15238 * $zj ) / 43 ) ) +
29;
1147 $zm = (int)( ( 24 * $zl ) / 709 );
1148 $zd = $zl - (int)( ( 709 * $zm ) / 24 );
1149 $zy = 30 * $zn +
$zj - 30;
1151 return array( $zy, $zm, $zd );
1155 * Converting Gregorian dates to Hebrew dates.
1157 * Based on a JavaScript code by Abu Mami and Yisrael Hersch
1158 * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
1159 * to translate the relevant functions into PHP and release them under
1162 * The months are counted from Tishrei = 1. In a leap year, Adar I is 13
1163 * and Adar II is 14. In a non-leap year, Adar is 6.
1165 private static function tsToHebrew( $ts ) {
1167 $year = substr( $ts, 0, 4 );
1168 $month = substr( $ts, 4, 2 );
1169 $day = substr( $ts, 6, 2 );
1171 # Calculate Hebrew year
1172 $hebrewYear = $year +
3760;
1174 # Month number when September = 1, August = 12
1176 if ( $month > 12 ) {
1183 # Calculate day of year from 1 September
1185 for ( $i = 1; $i < $month; $i++
) {
1189 # Check if the year is leap
1190 if ( $year %
400 == 0 ||
( $year %
4 == 0 && $year %
100 > 0 ) ) {
1193 } elseif ( $i == 8 ||
$i == 10 ||
$i == 1 ||
$i == 3 ) {
1200 # Calculate the start of the Hebrew year
1201 $start = self
::hebrewYearStart( $hebrewYear );
1203 # Calculate next year's start
1204 if ( $dayOfYear <= $start ) {
1205 # Day is before the start of the year - it is the previous year
1207 $nextStart = $start;
1211 # Add days since previous year's 1 September
1213 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1217 # Start of the new (previous) year
1218 $start = self
::hebrewYearStart( $hebrewYear );
1221 $nextStart = self
::hebrewYearStart( $hebrewYear +
1 );
1224 # Calculate Hebrew day of year
1225 $hebrewDayOfYear = $dayOfYear - $start;
1227 # Difference between year's days
1228 $diff = $nextStart - $start;
1229 # Add 12 (or 13 for leap years) days to ignore the difference between
1230 # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
1231 # difference is only about the year type
1232 if ( ( $year %
400 == 0 ) ||
( $year %
100 != 0 && $year %
4 == 0 ) ) {
1238 # Check the year pattern, and is leap year
1239 # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
1240 # This is mod 30, to work on both leap years (which add 30 days of Adar I)
1241 # and non-leap years
1242 $yearPattern = $diff %
30;
1243 # Check if leap year
1244 $isLeap = $diff >= 30;
1246 # Calculate day in the month from number of day in the Hebrew year
1247 # Don't check Adar - if the day is not in Adar, we will stop before;
1248 # if it is in Adar, we will use it to check if it is Adar I or Adar II
1249 $hebrewDay = $hebrewDayOfYear;
1252 while ( $hebrewMonth <= 12 ) {
1253 # Calculate days in this month
1254 if ( $isLeap && $hebrewMonth == 6 ) {
1255 # Adar in a leap year
1257 # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
1259 if ( $hebrewDay <= $days ) {
1263 # Subtract the days of Adar I
1264 $hebrewDay -= $days;
1267 if ( $hebrewDay <= $days ) {
1273 } elseif ( $hebrewMonth == 2 && $yearPattern == 2 ) {
1274 # Cheshvan in a complete year (otherwise as the rule below)
1276 } elseif ( $hebrewMonth == 3 && $yearPattern == 0 ) {
1277 # Kislev in an incomplete year (otherwise as the rule below)
1280 # Odd months have 30 days, even have 29
1281 $days = 30 - ( $hebrewMonth - 1 ) %
2;
1283 if ( $hebrewDay <= $days ) {
1284 # In the current month
1287 # Subtract the days of the current month
1288 $hebrewDay -= $days;
1289 # Try in the next month
1294 return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
1298 * This calculates the Hebrew year start, as days since 1 September.
1299 * Based on Carl Friedrich Gauss algorithm for finding Easter date.
1300 * Used for Hebrew date.
1302 private static function hebrewYearStart( $year ) {
1303 $a = intval( ( 12 * ( $year - 1 ) +
17 ) %
19 );
1304 $b = intval( ( $year - 1 ) %
4 );
1305 $m = 32.044093161144 +
1.5542417966212 * $a +
$b / 4.0 - 0.0031777940220923 * ( $year - 1 );
1309 $Mar = intval( $m );
1315 $c = intval( ( $Mar +
3 * ( $year - 1 ) +
5 * $b +
5 ) %
7 );
1316 if ( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
1318 } else if ( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
1320 } else if ( $c == 2 ||
$c == 4 ||
$c == 6 ) {
1324 $Mar +
= intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
1329 * Algorithm to convert Gregorian dates to Thai solar dates,
1330 * Minguo dates or Minguo dates.
1332 * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
1333 * http://en.wikipedia.org/wiki/Minguo_calendar
1334 * http://en.wikipedia.org/wiki/Japanese_era_name
1336 * @param $ts String: 14-character timestamp
1337 * @param $cName String: calender name
1338 * @return Array: converted year, month, day
1340 private static function tsToYear( $ts, $cName ) {
1341 $gy = substr( $ts, 0, 4 );
1342 $gm = substr( $ts, 4, 2 );
1343 $gd = substr( $ts, 6, 2 );
1345 if ( !strcmp( $cName, 'thai' ) ) {
1347 # Add 543 years to the Gregorian calendar
1348 # Months and days are identical
1349 $gy_offset = $gy +
543;
1350 } else if ( ( !strcmp( $cName, 'minguo' ) ) ||
!strcmp( $cName, 'juche' ) ) {
1352 # Deduct 1911 years from the Gregorian calendar
1353 # Months and days are identical
1354 $gy_offset = $gy - 1911;
1355 } else if ( !strcmp( $cName, 'tenno' ) ) {
1356 # Nengō dates up to Meiji period
1357 # Deduct years from the Gregorian calendar
1358 # depending on the nengo periods
1359 # Months and days are identical
1360 if ( ( $gy < 1912 ) ||
( ( $gy == 1912 ) && ( $gm < 7 ) ) ||
( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd < 31 ) ) ) {
1362 $gy_gannen = $gy - 1868 +
1;
1363 $gy_offset = $gy_gannen;
1364 if ( $gy_gannen == 1 ) {
1367 $gy_offset = '明治' . $gy_offset;
1369 ( ( $gy == 1912 ) && ( $gm == 7 ) && ( $gd == 31 ) ) ||
1370 ( ( $gy == 1912 ) && ( $gm >= 8 ) ) ||
1371 ( ( $gy > 1912 ) && ( $gy < 1926 ) ) ||
1372 ( ( $gy == 1926 ) && ( $gm < 12 ) ) ||
1373 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd < 26 ) )
1377 $gy_gannen = $gy - 1912 +
1;
1378 $gy_offset = $gy_gannen;
1379 if ( $gy_gannen == 1 ) {
1382 $gy_offset = '大正' . $gy_offset;
1384 ( ( $gy == 1926 ) && ( $gm == 12 ) && ( $gd >= 26 ) ) ||
1385 ( ( $gy > 1926 ) && ( $gy < 1989 ) ) ||
1386 ( ( $gy == 1989 ) && ( $gm == 1 ) && ( $gd < 8 ) )
1390 $gy_gannen = $gy - 1926 +
1;
1391 $gy_offset = $gy_gannen;
1392 if ( $gy_gannen == 1 ) {
1395 $gy_offset = '昭和' . $gy_offset;
1398 $gy_gannen = $gy - 1989 +
1;
1399 $gy_offset = $gy_gannen;
1400 if ( $gy_gannen == 1 ) {
1403 $gy_offset = '平成' . $gy_offset;
1409 return array( $gy_offset, $gm, $gd );
1413 * Roman number formatting up to 3000
1415 static function romanNumeral( $num ) {
1416 static $table = array(
1417 array( '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X' ),
1418 array( '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C' ),
1419 array( '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M' ),
1420 array( '', 'M', 'MM', 'MMM' )
1423 $num = intval( $num );
1424 if ( $num > 3000 ||
$num <= 0 ) {
1429 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1430 if ( $num >= $pow10 ) {
1431 $s .= $table[$i][floor( $num / $pow10 )];
1433 $num = $num %
$pow10;
1439 * Hebrew Gematria number formatting up to 9999
1441 static function hebrewNumeral( $num ) {
1442 static $table = array(
1443 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' ),
1444 array( '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ', 'ק' ),
1445 array( '', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר', 'תש', 'תת', 'תתק', 'תתר' ),
1446 array( '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט', 'י' )
1449 $num = intval( $num );
1450 if ( $num > 9999 ||
$num <= 0 ) {
1455 for ( $pow10 = 1000, $i = 3; $i >= 0; $pow10 /= 10, $i-- ) {
1456 if ( $num >= $pow10 ) {
1457 if ( $num == 15 ||
$num == 16 ) {
1458 $s .= $table[0][9] . $table[0][$num - 9];
1461 $s .= $table[$i][intval( ( $num / $pow10 ) )];
1462 if ( $pow10 == 1000 ) {
1467 $num = $num %
$pow10;
1469 if ( strlen( $s ) == 2 ) {
1472 $str = substr( $s, 0, strlen( $s ) - 2 ) . '"';
1473 $str .= substr( $s, strlen( $s ) - 2, 2 );
1475 $start = substr( $str, 0, strlen( $str ) - 2 );
1476 $end = substr( $str, strlen( $str ) - 2 );
1479 $str = $start . 'ך';
1482 $str = $start . 'ם';
1485 $str = $start . 'ן';
1488 $str = $start . 'ף';
1491 $str = $start . 'ץ';
1498 * This is meant to be used by time(), date(), and timeanddate() to get
1499 * the date preference they're supposed to use, it should be used in
1503 * function timeanddate([...], $format = true) {
1504 * $datePreference = $this->dateFormat($format);
1509 * @param $usePrefs Mixed: if true, the user's preference is used
1510 * if false, the site/language default is used
1511 * if int/string, assumed to be a format.
1514 function dateFormat( $usePrefs = true ) {
1517 if ( is_bool( $usePrefs ) ) {
1519 $datePreference = $wgUser->getDatePreference();
1521 $datePreference = (string)User
::getDefaultOption( 'date' );
1524 $datePreference = (string)$usePrefs;
1528 if ( $datePreference == '' ) {
1532 return $datePreference;
1536 * Get a format string for a given type and preference
1537 * @param $type May be date, time or both
1538 * @param $pref The format name as it appears in Messages*.php
1540 function getDateFormatString( $type, $pref ) {
1541 if ( !isset( $this->dateFormatStrings
[$type][$pref] ) ) {
1542 if ( $pref == 'default' ) {
1543 $pref = $this->getDefaultDateFormat();
1544 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1546 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1547 if ( is_null( $df ) ) {
1548 $pref = $this->getDefaultDateFormat();
1549 $df = self
::$dataCache->getSubitem( $this->mCode
, 'dateFormats', "$pref $type" );
1552 $this->dateFormatStrings
[$type][$pref] = $df;
1554 return $this->dateFormatStrings
[$type][$pref];
1558 * @param $ts Mixed: the time format which needs to be turned into a
1559 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1560 * @param $adj Bool: whether to adjust the time output according to the
1561 * user configured offset ($timecorrection)
1562 * @param $format Mixed: true to use user's date format preference
1563 * @param $timecorrection String: the time offset as returned by
1564 * validateTimeZone() in Special:Preferences
1567 function date( $ts, $adj = false, $format = true, $timecorrection = false ) {
1568 $ts = wfTimestamp( TS_MW
, $ts );
1570 $ts = $this->userAdjust( $ts, $timecorrection );
1572 $df = $this->getDateFormatString( 'date', $this->dateFormat( $format ) );
1573 return $this->sprintfDate( $df, $ts );
1577 * @param $ts Mixed: the time format which needs to be turned into a
1578 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1579 * @param $adj Bool: whether to adjust the time output according to the
1580 * user configured offset ($timecorrection)
1581 * @param $format Mixed: true to use user's date format preference
1582 * @param $timecorrection String: the time offset as returned by
1583 * validateTimeZone() in Special:Preferences
1586 function time( $ts, $adj = false, $format = true, $timecorrection = false ) {
1587 $ts = wfTimestamp( TS_MW
, $ts );
1589 $ts = $this->userAdjust( $ts, $timecorrection );
1591 $df = $this->getDateFormatString( 'time', $this->dateFormat( $format ) );
1592 return $this->sprintfDate( $df, $ts );
1596 * @param $ts Mixed: the time format which needs to be turned into a
1597 * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
1598 * @param $adj Bool: whether to adjust the time output according to the
1599 * user configured offset ($timecorrection)
1600 * @param $format Mixed: what format to return, if it's false output the
1601 * default one (default true)
1602 * @param $timecorrection String: the time offset as returned by
1603 * validateTimeZone() in Special:Preferences
1606 function timeanddate( $ts, $adj = false, $format = true, $timecorrection = false ) {
1607 $ts = wfTimestamp( TS_MW
, $ts );
1609 $ts = $this->userAdjust( $ts, $timecorrection );
1611 $df = $this->getDateFormatString( 'both', $this->dateFormat( $format ) );
1612 return $this->sprintfDate( $df, $ts );
1615 function getMessage( $key ) {
1616 // Don't change getPreferredVariant() to getCode() / mCode, because:
1618 // 1. Some language like Chinese has multiple variant languages. Only
1619 // getPreferredVariant() (in LanguageConverter) could return a
1620 // sub-language which would be more suitable for the user.
1621 // 2. To languages without multiple variants, getPreferredVariant()
1622 // (in FakeConverter) functions exactly same as getCode() / mCode,
1623 // it won't break anything.
1626 return self
::$dataCache->getSubitem( $this->getPreferredVariant(), 'messages', $key );
1629 function getAllMessages() {
1630 return self
::$dataCache->getItem( $this->getPreferredVariant(), 'messages' );
1633 function iconv( $in, $out, $string ) {
1634 # This is a wrapper for iconv in all languages except esperanto,
1635 # which does some nasty x-conversions beforehand
1637 # Even with //IGNORE iconv can whine about illegal characters in
1638 # *input* string. We just ignore those too.
1639 # REF: http://bugs.php.net/bug.php?id=37166
1640 # REF: https://bugzilla.wikimedia.org/show_bug.cgi?id=16885
1641 wfSuppressWarnings();
1642 $text = iconv( $in, $out . '//IGNORE', $string );
1643 wfRestoreWarnings();
1647 // callback functions for uc(), lc(), ucwords(), ucwordbreaks()
1648 function ucwordbreaksCallbackAscii( $matches ) {
1649 return $this->ucfirst( $matches[1] );
1652 function ucwordbreaksCallbackMB( $matches ) {
1653 return mb_strtoupper( $matches[0] );
1656 function ucCallback( $matches ) {
1657 list( $wikiUpperChars ) = self
::getCaseMaps();
1658 return strtr( $matches[1], $wikiUpperChars );
1661 function lcCallback( $matches ) {
1662 list( , $wikiLowerChars ) = self
::getCaseMaps();
1663 return strtr( $matches[1], $wikiLowerChars );
1666 function ucwordsCallbackMB( $matches ) {
1667 return mb_strtoupper( $matches[0] );
1670 function ucwordsCallbackWiki( $matches ) {
1671 list( $wikiUpperChars ) = self
::getCaseMaps();
1672 return strtr( $matches[0], $wikiUpperChars );
1676 * Make a string's first character uppercase
1678 function ucfirst( $str ) {
1680 if ( $o < 96 ) { // if already uppercase...
1682 } elseif ( $o < 128 ) {
1683 return ucfirst( $str ); // use PHP's ucfirst()
1685 // fall back to more complex logic in case of multibyte strings
1686 return $this->uc( $str, true );
1691 * Convert a string to uppercase
1693 function uc( $str, $first = false ) {
1694 if ( function_exists( 'mb_strtoupper' ) ) {
1696 if ( $this->isMultibyte( $str ) ) {
1697 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1699 return ucfirst( $str );
1702 return $this->isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
1705 if ( $this->isMultibyte( $str ) ) {
1706 $x = $first ?
'^' : '';
1707 return preg_replace_callback(
1708 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1709 array( $this, 'ucCallback' ),
1713 return $first ?
ucfirst( $str ) : strtoupper( $str );
1718 function lcfirst( $str ) {
1721 return strval( $str );
1722 } elseif ( $o >= 128 ) {
1723 return $this->lc( $str, true );
1724 } elseif ( $o > 96 ) {
1727 $str[0] = strtolower( $str[0] );
1732 function lc( $str, $first = false ) {
1733 if ( function_exists( 'mb_strtolower' ) ) {
1735 if ( $this->isMultibyte( $str ) ) {
1736 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
1738 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
1741 return $this->isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
1744 if ( $this->isMultibyte( $str ) ) {
1745 $x = $first ?
'^' : '';
1746 return preg_replace_callback(
1747 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/",
1748 array( $this, 'lcCallback' ),
1752 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
1757 function isMultibyte( $str ) {
1758 return (bool)preg_match( '/[\x80-\xff]/', $str );
1761 function ucwords( $str ) {
1762 if ( $this->isMultibyte( $str ) ) {
1763 $str = $this->lc( $str );
1765 // regexp to find first letter in each word (i.e. after each space)
1766 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)| ([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1768 // function to use to capitalize a single char
1769 if ( function_exists( 'mb_strtoupper' ) ) {
1770 return preg_replace_callback(
1772 array( $this, 'ucwordsCallbackMB' ),
1776 return preg_replace_callback(
1778 array( $this, 'ucwordsCallbackWiki' ),
1783 return ucwords( strtolower( $str ) );
1787 # capitalize words at word breaks
1788 function ucwordbreaks( $str ) {
1789 if ( $this->isMultibyte( $str ) ) {
1790 $str = $this->lc( $str );
1792 // since \b doesn't work for UTF-8, we explicitely define word break chars
1793 $breaks = "[ \-\(\)\}\{\.,\?!]";
1795 // find first letter after word break
1796 $replaceRegexp = "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)|$breaks([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/";
1798 if ( function_exists( 'mb_strtoupper' ) ) {
1799 return preg_replace_callback(
1801 array( $this, 'ucwordbreaksCallbackMB' ),
1805 return preg_replace_callback(
1807 array( $this, 'ucwordsCallbackWiki' ),
1812 return preg_replace_callback(
1813 '/\b([\w\x80-\xff]+)\b/',
1814 array( $this, 'ucwordbreaksCallbackAscii' ),
1821 * Return a case-folded representation of $s
1823 * This is a representation such that caseFold($s1)==caseFold($s2) if $s1
1824 * and $s2 are the same except for the case of their characters. It is not
1825 * necessary for the value returned to make sense when displayed.
1827 * Do *not* perform any other normalisation in this function. If a caller
1828 * uses this function when it should be using a more general normalisation
1829 * function, then fix the caller.
1831 function caseFold( $s ) {
1832 return $this->uc( $s );
1835 function checkTitleEncoding( $s ) {
1836 if ( is_array( $s ) ) {
1837 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
1839 # Check for non-UTF-8 URLs
1840 $ishigh = preg_match( '/[\x80-\xff]/', $s );
1845 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1846 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
1851 return $this->iconv( $this->fallback8bitEncoding(), 'utf-8', $s );
1854 function fallback8bitEncoding() {
1855 return self
::$dataCache->getItem( $this->mCode
, 'fallback8bitEncoding' );
1859 * Most writing systems use whitespace to break up words.
1860 * Some languages such as Chinese don't conventionally do this,
1861 * which requires special handling when breaking up words for
1864 function hasWordBreaks() {
1869 * Some languages such as Chinese require word segmentation,
1870 * Specify such segmentation when overridden in derived class.
1872 * @param $string String
1875 function segmentByWord( $string ) {
1880 * Some languages have special punctuation need to be normalized.
1881 * Make such changes here.
1883 * @param $string String
1886 function normalizeForSearch( $string ) {
1887 return self
::convertDoubleWidth( $string );
1891 * convert double-width roman characters to single-width.
1892 * range: ff00-ff5f ~= 0020-007f
1894 protected static function convertDoubleWidth( $string ) {
1895 static $full = null;
1896 static $half = null;
1898 if ( $full === null ) {
1899 $fullWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1900 $halfWidth = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1901 $full = str_split( $fullWidth, 3 );
1902 $half = str_split( $halfWidth );
1905 $string = str_replace( $full, $half, $string );
1909 protected static function insertSpace( $string, $pattern ) {
1910 $string = preg_replace( $pattern, " $1 ", $string );
1911 $string = preg_replace( '/ +/', ' ', $string );
1915 function convertForSearchResult( $termsArray ) {
1916 # some languages, e.g. Chinese, need to do a conversion
1917 # in order for search results to be displayed correctly
1922 * Get the first character of a string.
1927 function firstChar( $s ) {
1930 '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
1931 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/',
1936 if ( isset( $matches[1] ) ) {
1937 if ( strlen( $matches[1] ) != 3 ) {
1941 // Break down Hangul syllables to grab the first jamo
1942 $code = utf8ToCodepoint( $matches[1] );
1943 if ( $code < 0xac00 ||
0xd7a4 <= $code ) {
1945 } elseif ( $code < 0xb098 ) {
1946 return "\xe3\x84\xb1";
1947 } elseif ( $code < 0xb2e4 ) {
1948 return "\xe3\x84\xb4";
1949 } elseif ( $code < 0xb77c ) {
1950 return "\xe3\x84\xb7";
1951 } elseif ( $code < 0xb9c8 ) {
1952 return "\xe3\x84\xb9";
1953 } elseif ( $code < 0xbc14 ) {
1954 return "\xe3\x85\x81";
1955 } elseif ( $code < 0xc0ac ) {
1956 return "\xe3\x85\x82";
1957 } elseif ( $code < 0xc544 ) {
1958 return "\xe3\x85\x85";
1959 } elseif ( $code < 0xc790 ) {
1960 return "\xe3\x85\x87";
1961 } elseif ( $code < 0xcc28 ) {
1962 return "\xe3\x85\x88";
1963 } elseif ( $code < 0xce74 ) {
1964 return "\xe3\x85\x8a";
1965 } elseif ( $code < 0xd0c0 ) {
1966 return "\xe3\x85\x8b";
1967 } elseif ( $code < 0xd30c ) {
1968 return "\xe3\x85\x8c";
1969 } elseif ( $code < 0xd558 ) {
1970 return "\xe3\x85\x8d";
1972 return "\xe3\x85\x8e";
1979 function initEncoding() {
1980 # Some languages may have an alternate char encoding option
1981 # (Esperanto X-coding, Japanese furigana conversion, etc)
1982 # If this language is used as the primary content language,
1983 # an override to the defaults can be set here on startup.
1986 function recodeForEdit( $s ) {
1987 # For some languages we'll want to explicitly specify
1988 # which characters make it into the edit box raw
1989 # or are converted in some way or another.
1990 # Note that if wgOutputEncoding is different from
1991 # wgInputEncoding, this text will be further converted
1992 # to wgOutputEncoding.
1993 global $wgEditEncoding;
1994 if ( $wgEditEncoding == '' ||
$wgEditEncoding == 'UTF-8' ) {
1997 return $this->iconv( 'UTF-8', $wgEditEncoding, $s );
2001 function recodeInput( $s ) {
2002 # Take the previous into account.
2003 global $wgEditEncoding;
2004 if ( $wgEditEncoding != '' ) {
2005 $enc = $wgEditEncoding;
2009 if ( $enc == 'UTF-8' ) {
2012 return $this->iconv( $enc, 'UTF-8', $s );
2017 * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
2018 * also cleans up certain backwards-compatible sequences, converting them
2019 * to the modern Unicode equivalent.
2021 * This is language-specific for performance reasons only.
2023 function normalize( $s ) {
2024 global $wgAllUnicodeFixes;
2025 $s = UtfNormal
::cleanUp( $s );
2026 if ( $wgAllUnicodeFixes ) {
2027 $s = $this->transformUsingPairFile( 'normalize-ar.ser', $s );
2028 $s = $this->transformUsingPairFile( 'normalize-ml.ser', $s );
2035 * Transform a string using serialized data stored in the given file (which
2036 * must be in the serialized subdirectory of $IP). The file contains pairs
2037 * mapping source characters to destination characters.
2039 * The data is cached in process memory. This will go faster if you have the
2040 * FastStringSearch extension.
2042 function transformUsingPairFile( $file, $string ) {
2043 if ( !isset( $this->transformData
[$file] ) ) {
2044 $data = wfGetPrecompiledData( $file );
2045 if ( $data === false ) {
2046 throw new MWException( __METHOD__
. ": The transformation file $file is missing" );
2048 $this->transformData
[$file] = new ReplacementArray( $data );
2050 return $this->transformData
[$file]->replace( $string );
2054 * For right-to-left language support
2059 return self
::$dataCache->getItem( $this->mCode
, 'rtl' );
2063 * Return the correct HTML 'dir' attribute value for this language.
2067 return $this->isRTL() ?
'rtl' : 'ltr';
2071 * Return 'left' or 'right' as appropriate alignment for line-start
2072 * for this language's text direction.
2074 * Should be equivalent to CSS3 'start' text-align value....
2078 function alignStart() {
2079 return $this->isRTL() ?
'right' : 'left';
2083 * Return 'right' or 'left' as appropriate alignment for line-end
2084 * for this language's text direction.
2086 * Should be equivalent to CSS3 'end' text-align value....
2090 function alignEnd() {
2091 return $this->isRTL() ?
'left' : 'right';
2095 * A hidden direction mark (LRM or RLM), depending on the language direction
2099 function getDirMark() {
2100 return $this->isRTL() ?
"\xE2\x80\x8F" : "\xE2\x80\x8E";
2103 function capitalizeAllNouns() {
2104 return self
::$dataCache->getItem( $this->mCode
, 'capitalizeAllNouns' );
2108 * An arrow, depending on the language direction
2112 function getArrow() {
2113 return $this->isRTL() ?
'←' : '→';
2117 * To allow "foo[[bar]]" to extend the link over the whole word "foobar"
2121 function linkPrefixExtension() {
2122 return self
::$dataCache->getItem( $this->mCode
, 'linkPrefixExtension' );
2125 function getMagicWords() {
2126 return self
::$dataCache->getItem( $this->mCode
, 'magicWords' );
2129 protected function doMagicHook() {
2130 if ( $this->mMagicHookDone
) {
2133 $this->mMagicHookDone
= true;
2134 wfProfileIn( 'LanguageGetMagic' );
2135 wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions
, $this->getCode() ) );
2136 wfProfileOut( 'LanguageGetMagic' );
2139 # Fill a MagicWord object with data from here
2140 function getMagic( $mw ) {
2141 $this->doMagicHook();
2143 if ( isset( $this->mMagicExtensions
[$mw->mId
] ) ) {
2144 $rawEntry = $this->mMagicExtensions
[$mw->mId
];
2146 $magicWords = $this->getMagicWords();
2147 if ( isset( $magicWords[$mw->mId
] ) ) {
2148 $rawEntry = $magicWords[$mw->mId
];
2154 if ( !is_array( $rawEntry ) ) {
2155 error_log( "\"$rawEntry\" is not a valid magic thingie for \"$mw->mId\"" );
2157 $mw->mCaseSensitive
= $rawEntry[0];
2158 $mw->mSynonyms
= array_slice( $rawEntry, 1 );
2163 * Add magic words to the extension array
2165 function addMagicWordsByLang( $newWords ) {
2166 $code = $this->getCode();
2167 $fallbackChain = array();
2168 while ( $code && !in_array( $code, $fallbackChain ) ) {
2169 $fallbackChain[] = $code;
2170 $code = self
::getFallbackFor( $code );
2172 if ( !in_array( 'en', $fallbackChain ) ) {
2173 $fallbackChain[] = 'en';
2175 $fallbackChain = array_reverse( $fallbackChain );
2176 foreach ( $fallbackChain as $code ) {
2177 if ( isset( $newWords[$code] ) ) {
2178 $this->mMagicExtensions
= $newWords[$code] +
$this->mMagicExtensions
;
2184 * Get special page names, as an associative array
2185 * case folded alias => real name
2187 function getSpecialPageAliases() {
2188 // Cache aliases because it may be slow to load them
2189 if ( is_null( $this->mExtendedSpecialPageAliases
) ) {
2191 $this->mExtendedSpecialPageAliases
=
2192 self
::$dataCache->getItem( $this->mCode
, 'specialPageAliases' );
2193 wfRunHooks( 'LanguageGetSpecialPageAliases',
2194 array( &$this->mExtendedSpecialPageAliases
, $this->getCode() ) );
2197 return $this->mExtendedSpecialPageAliases
;
2201 * Italic is unsuitable for some languages
2203 * @param $text String: the text to be emphasized.
2206 function emphasize( $text ) {
2207 return "<em>$text</em>";
2211 * Normally we output all numbers in plain en_US style, that is
2212 * 293,291.235 for twohundredninetythreethousand-twohundredninetyone
2213 * point twohundredthirtyfive. However this is not sutable for all
2214 * languages, some such as Pakaran want ੨੯੩,੨੯੫.੨੩੫ and others such as
2215 * Icelandic just want to use commas instead of dots, and dots instead
2216 * of commas like "293.291,235".
2218 * An example of this function being called:
2220 * wfMsg( 'message', $wgLang->formatNum( $num ) )
2223 * See LanguageGu.php for the Gujarati implementation and
2224 * $separatorTransformTable on MessageIs.php for
2225 * the , => . and . => , implementation.
2227 * @todo check if it's viable to use localeconv() for the decimal
2229 * @param $number Mixed: the string to be formatted, should be an integer
2230 * or a floating point number.
2231 * @param $nocommafy Bool: set to true for special numbers like dates
2234 function formatNum( $number, $nocommafy = false ) {
2235 global $wgTranslateNumerals;
2236 if ( !$nocommafy ) {
2237 $number = $this->commafy( $number );
2238 $s = $this->separatorTransformTable();
2240 $number = strtr( $number, $s );
2244 if ( $wgTranslateNumerals ) {
2245 $s = $this->digitTransformTable();
2247 $number = strtr( $number, $s );
2254 function parseFormattedNumber( $number ) {
2255 $s = $this->digitTransformTable();
2257 $number = strtr( $number, array_flip( $s ) );
2260 $s = $this->separatorTransformTable();
2262 $number = strtr( $number, array_flip( $s ) );
2265 $number = strtr( $number, array( ',' => '' ) );
2270 * Adds commas to a given number
2275 function commafy( $_ ) {
2276 return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,', strrev( $_ ) ) );
2279 function digitTransformTable() {
2280 return self
::$dataCache->getItem( $this->mCode
, 'digitTransformTable' );
2283 function separatorTransformTable() {
2284 return self
::$dataCache->getItem( $this->mCode
, 'separatorTransformTable' );
2288 * Take a list of strings and build a locale-friendly comma-separated
2289 * list, using the local comma-separator message.
2290 * The last two strings are chained with an "and".
2295 function listToText( $l ) {
2297 $m = count( $l ) - 1;
2299 return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
2301 for ( $i = $m; $i >= 0; $i-- ) {
2304 } else if ( $i == $m - 1 ) {
2305 $s = $l[$i] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $s;
2307 $s = $l[$i] . $this->getMessageFromDB( 'comma-separator' ) . $s;
2315 * Take a list of strings and build a locale-friendly comma-separated
2316 * list, using the local comma-separator message.
2317 * @param $list array of strings to put in a comma list
2320 function commaList( $list ) {
2325 array( 'parsemag', 'escapenoentities', 'language' => $this )
2331 * Take a list of strings and build a locale-friendly semicolon-separated
2332 * list, using the local semicolon-separator message.
2333 * @param $list array of strings to put in a semicolon list
2336 function semicolonList( $list ) {
2340 'semicolon-separator',
2341 array( 'parsemag', 'escapenoentities', 'language' => $this )
2347 * Same as commaList, but separate it with the pipe instead.
2348 * @param $list array of strings to put in a pipe list
2351 function pipeList( $list ) {
2356 array( 'escapenoentities', 'language' => $this )
2362 * Truncate a string to a specified length in bytes, appending an optional
2363 * string (e.g. for ellipses)
2365 * The database offers limited byte lengths for some columns in the database;
2366 * multi-byte character sets mean we need to ensure that only whole characters
2367 * are included, otherwise broken characters can be passed to the user
2369 * If $length is negative, the string will be truncated from the beginning
2371 * @param $string String to truncate
2372 * @param $length Int: maximum length (excluding ellipses)
2373 * @param $ellipsis String to append to the truncated text
2376 function truncate( $string, $length, $ellipsis = '...' ) {
2377 # Use the localized ellipsis character
2378 if ( $ellipsis == '...' ) {
2379 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
2381 # Check if there is no need to truncate
2382 if ( $length == 0 ) {
2384 } elseif ( strlen( $string ) <= abs( $length ) ) {
2387 $stringOriginal = $string;
2388 if ( $length > 0 ) {
2389 $string = substr( $string, 0, $length ); // xyz...
2390 $string = $this->removeBadCharLast( $string );
2391 $string = $string . $ellipsis;
2393 $string = substr( $string, $length ); // ...xyz
2394 $string = $this->removeBadCharFirst( $string );
2395 $string = $ellipsis . $string;
2397 # Do not truncate if the ellipsis makes the string longer/equal (bug 22181)
2398 if ( strlen( $string ) < strlen( $stringOriginal ) ) {
2401 return $stringOriginal;
2406 * Remove bytes that represent an incomplete Unicode character
2407 * at the end of string (e.g. bytes of the char are missing)
2409 * @param $string String
2412 protected function removeBadCharLast( $string ) {
2413 $char = ord( $string[strlen( $string ) - 1] );
2415 if ( $char >= 0xc0 ) {
2416 # We got the first byte only of a multibyte char; remove it.
2417 $string = substr( $string, 0, -1 );
2418 } elseif ( $char >= 0x80 &&
2419 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
2420 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) )
2422 # We chopped in the middle of a character; remove it
2429 * Remove bytes that represent an incomplete Unicode character
2430 * at the start of string (e.g. bytes of the char are missing)
2432 * @param $string String
2435 protected function removeBadCharFirst( $string ) {
2436 $char = ord( $string[0] );
2437 if ( $char >= 0x80 && $char < 0xc0 ) {
2438 # We chopped in the middle of a character; remove the whole thing
2439 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
2445 * Truncate a string of valid HTML to a specified length in bytes,
2446 * appending an optional string (e.g. for ellipses), and return valid HTML
2448 * This is only intended for styled/linked text, such as HTML with
2449 * tags like <span> and <a>, were the tags are self-contained (valid HTML)
2451 * Note: tries to fix broken HTML with MWTidy
2453 * @param string $text HTML string to truncate
2454 * @param int $length (zero/positive) Maximum length (excluding ellipses)
2455 * @param string $ellipsis String to append to the truncated text
2458 function truncateHtml( $text, $length, $ellipsis = '...' ) {
2459 # Use the localized ellipsis character
2460 if ( $ellipsis == '...' ) {
2461 $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
2463 # Check if there is no need to truncate
2464 if ( $length <= 0 ) {
2465 return $ellipsis; // no text shown, nothing to format
2466 } elseif ( strlen( $text ) <= $length ) {
2467 return $text; // string short enough even *with* HTML
2469 $text = MWTidy
::tidy( $text ); // fix tags
2470 $displayLen = 0; // innerHTML legth so far
2471 $testingEllipsis = false; // checking if ellipses will make string longer/equal?
2472 $tagType = 0; // 0-open, 1-close
2473 $bracketState = 0; // 1-tag start, 2-tag name, 0-neither
2474 $entityState = 0; // 0-not entity, 1-entity
2476 $openTags = array(); // open tag stack
2477 $textLen = strlen( $text );
2478 for ( $pos = 0; $pos < $textLen; ++
$pos ) {
2480 $lastCh = $pos ?
$text[$pos - 1] : '';
2481 $ret .= $ch; // add to result string
2483 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags ); // for bad HTML
2484 $entityState = 0; // for bad HTML
2485 $bracketState = 1; // tag started (checking for backslash)
2486 } elseif ( $ch == '>' ) {
2487 $this->truncate_endBracket( $tag, $tagType, $lastCh, $openTags );
2488 $entityState = 0; // for bad HTML
2489 $bracketState = 0; // out of brackets
2490 } elseif ( $bracketState == 1 ) {
2492 $tagType = 1; // close tag (e.g. "</span>")
2494 $tagType = 0; // open tag (e.g. "<span>")
2497 $bracketState = 2; // building tag name
2498 } elseif ( $bracketState == 2 ) {
2502 // Name found (e.g. "<a href=..."), add on tag attributes...
2503 $pos +
= $this->truncate_skip( $ret, $text, "<>", $pos +
1 );
2505 } elseif ( $bracketState == 0 ) {
2506 if ( $entityState ) {
2509 $displayLen++
; // entity is one displayed char
2513 $entityState = 1; // entity found, (e.g. " ")
2515 $displayLen++
; // this char is displayed
2516 // Add on the other display text after this...
2517 $skipped = $this->truncate_skip(
2518 $ret, $text, "<>&", $pos +
1, $length - $displayLen );
2519 $displayLen +
= $skipped;
2524 # Consider truncation once the display length has reached the maximim.
2525 # Double-check that we're not in the middle of a bracket/entity...
2526 if ( $displayLen >= $length && $bracketState == 0 && $entityState == 0 ) {
2527 if ( !$testingEllipsis ) {
2528 $testingEllipsis = true;
2529 # Save where we are; we will truncate here unless
2530 # the ellipsis actually makes the string longer.
2531 $pOpenTags = $openTags; // save state
2532 $pRet = $ret; // save state
2533 } elseif ( $displayLen > ( $length +
strlen( $ellipsis ) ) ) {
2534 # Ellipsis won't make string longer/equal, the truncation point was OK.
2535 $openTags = $pOpenTags; // reload state
2536 $ret = $this->removeBadCharLast( $pRet ); // reload state, multi-byte char fix
2537 $ret .= $ellipsis; // add ellipsis
2542 if ( $displayLen == 0 ) {
2543 return ''; // no text shown, nothing to format
2545 // Close the last tag if left unclosed by bad HTML
2546 $this->truncate_endBracket( $tag, $text[$textLen - 1], $tagType, $openTags );
2547 while ( count( $openTags ) > 0 ) {
2548 $ret .= '</' . array_pop( $openTags ) . '>'; // close open tags
2553 // truncateHtml() helper function
2554 // like strcspn() but adds the skipped chars to $ret
2555 private function truncate_skip( &$ret, $text, $search, $start, $len = -1 ) {
2557 if ( $start < strlen( $text ) ) {
2558 $skipCount = strcspn( $text, $search, $start, $len );
2559 $ret .= substr( $text, $start, $skipCount );
2565 * truncateHtml() helper function
2566 * (a) push or pop $tag from $openTags as needed
2567 * (b) clear $tag value
2568 * @param String &$tag Current HTML tag name we are looking at
2569 * @param int $tagType (0-open tag, 1-close tag)
2570 * @param char $lastCh Character before the '>' that ended this tag
2571 * @param array &$openTags Open tag stack (not accounting for $tag)
2573 private function truncate_endBracket( &$tag, $tagType, $lastCh, &$openTags ) {
2574 $tag = ltrim( $tag );
2576 if ( $tagType == 0 && $lastCh != '/' ) {
2577 $openTags[] = $tag; // tag opened (didn't close itself)
2578 } else if ( $tagType == 1 ) {
2579 if ( $openTags && $tag == $openTags[count( $openTags ) - 1] ) {
2580 array_pop( $openTags ); // tag closed
2588 * Grammatical transformations, needed for inflected languages
2589 * Invoked by putting {{grammar:case|word}} in a message
2591 * @param $word string
2592 * @param $case string
2595 function convertGrammar( $word, $case ) {
2596 global $wgGrammarForms;
2597 if ( isset( $wgGrammarForms[$this->getCode()][$case][$word] ) ) {
2598 return $wgGrammarForms[$this->getCode()][$case][$word];
2604 * Provides an alternative text depending on specified gender.
2605 * Usage {{gender:username|masculine|feminine|neutral}}.
2606 * username is optional, in which case the gender of current user is used,
2607 * but only in (some) interface messages; otherwise default gender is used.
2608 * If second or third parameter are not specified, masculine is used.
2609 * These details may be overriden per language.
2611 function gender( $gender, $forms ) {
2612 if ( !count( $forms ) ) {
2615 $forms = $this->preConvertPlural( $forms, 2 );
2616 if ( $gender === 'male' ) {
2619 if ( $gender === 'female' ) {
2622 return isset( $forms[2] ) ?
$forms[2] : $forms[0];
2626 * Plural form transformations, needed for some languages.
2627 * For example, there are 3 form of plural in Russian and Polish,
2628 * depending on "count mod 10". See [[w:Plural]]
2629 * For English it is pretty simple.
2631 * Invoked by putting {{plural:count|wordform1|wordform2}}
2632 * or {{plural:count|wordform1|wordform2|wordform3}}
2634 * Example: {{plural:{{NUMBEROFARTICLES}}|article|articles}}
2636 * @param $count Integer: non-localized number
2637 * @param $forms Array: different plural forms
2638 * @return string Correct form of plural for $count in this language
2640 function convertPlural( $count, $forms ) {
2641 if ( !count( $forms ) ) {
2644 $forms = $this->preConvertPlural( $forms, 2 );
2646 return ( $count == 1 ) ?
$forms[0] : $forms[1];
2650 * Checks that convertPlural was given an array and pads it to requested
2651 * amound of forms by copying the last one.
2653 * @param $count Integer: How many forms should there be at least
2654 * @param $forms Array of forms given to convertPlural
2655 * @return array Padded array of forms or an exception if not an array
2657 protected function preConvertPlural( /* Array */ $forms, $count ) {
2658 while ( count( $forms ) < $count ) {
2659 $forms[] = $forms[count( $forms ) - 1];
2665 * For translating of expiry times
2666 * @param $str String: the validated block time in English
2667 * @return Somehow translated block time
2668 * @see LanguageFi.php for example implementation
2670 function translateBlockExpiry( $str ) {
2671 $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' );
2673 if ( $scBlockExpiryOptions == '-' ) {
2677 foreach ( explode( ',', $scBlockExpiryOptions ) as $option ) {
2678 if ( strpos( $option, ':' ) === false ) {
2681 list( $show, $value ) = explode( ':', $option );
2682 if ( strcmp( $str, $value ) == 0 ) {
2683 return htmlspecialchars( trim( $show ) );
2691 * languages like Chinese need to be segmented in order for the diff
2694 * @param $text String
2697 function segmentForDiff( $text ) {
2702 * and unsegment to show the result
2704 * @param $text String
2707 function unsegmentForDiff( $text ) {
2711 # convert text to all supported variants
2712 function autoConvertToAllVariants( $text ) {
2713 return $this->mConverter
->autoConvertToAllVariants( $text );
2716 # convert text to different variants of a language.
2717 function convert( $text ) {
2718 return $this->mConverter
->convert( $text );
2721 # Convert a Title object to a string in the preferred variant
2722 function convertTitle( $title ) {
2723 return $this->mConverter
->convertTitle( $title );
2726 # Check if this is a language with variants
2727 function hasVariants() {
2728 return sizeof( $this->getVariants() ) > 1;
2731 # Put custom tags (e.g. -{ }-) around math to prevent conversion
2732 function armourMath( $text ) {
2733 return $this->mConverter
->armourMath( $text );
2737 * Perform output conversion on a string, and encode for safe HTML output.
2738 * @param $text String text to be converted
2739 * @param $isTitle Bool whether this conversion is for the article title
2741 * @todo this should get integrated somewhere sane
2743 function convertHtml( $text, $isTitle = false ) {
2744 return htmlspecialchars( $this->convert( $text, $isTitle ) );
2747 function convertCategoryKey( $key ) {
2748 return $this->mConverter
->convertCategoryKey( $key );
2752 * Get the list of variants supported by this language
2753 * see sample implementation in LanguageZh.php
2755 * @return array an array of language codes
2757 function getVariants() {
2758 return $this->mConverter
->getVariants();
2761 function getPreferredVariant() {
2762 return $this->mConverter
->getPreferredVariant();
2765 function getDefaultVariant() {
2766 return $this->mConverter
->getDefaultVariant();
2769 function getURLVariant() {
2770 return $this->mConverter
->getURLVariant();
2774 * If a language supports multiple variants, it is
2775 * possible that non-existing link in one variant
2776 * actually exists in another variant. this function
2777 * tries to find it. See e.g. LanguageZh.php
2779 * @param $link String: the name of the link
2780 * @param $nt Mixed: the title object of the link
2781 * @param $ignoreOtherCond Boolean: to disable other conditions when
2782 * we need to transclude a template or update a category's link
2783 * @return null the input parameters may be modified upon return
2785 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
2786 $this->mConverter
->findVariantLink( $link, $nt, $ignoreOtherCond );
2790 * If a language supports multiple variants, converts text
2791 * into an array of all possible variants of the text:
2792 * 'variant' => text in that variant
2794 * @deprecated Use autoConvertToAllVariants()
2796 function convertLinkToAllVariants( $text ) {
2797 return $this->mConverter
->convertLinkToAllVariants( $text );
2801 * returns language specific options used by User::getPageRenderHash()
2802 * for example, the preferred language variant
2806 function getExtraHashOptions() {
2807 return $this->mConverter
->getExtraHashOptions();
2811 * For languages that support multiple variants, the title of an
2812 * article may be displayed differently in different variants. this
2813 * function returns the apporiate title defined in the body of the article.
2817 function getParsedTitle() {
2818 return $this->mConverter
->getParsedTitle();
2822 * Enclose a string with the "no conversion" tag. This is used by
2823 * various functions in the Parser
2825 * @param $text String: text to be tagged for no conversion
2827 * @return string the tagged text
2829 function markNoConversion( $text, $noParse = false ) {
2830 return $this->mConverter
->markNoConversion( $text, $noParse );
2834 * A regular expression to match legal word-trailing characters
2835 * which should be merged onto a link of the form [[foo]]bar.
2839 function linkTrail() {
2840 return self
::$dataCache->getItem( $this->mCode
, 'linkTrail' );
2843 function getLangObj() {
2848 * Get the RFC 3066 code for this language object
2850 function getCode() {
2851 return $this->mCode
;
2854 function setCode( $code ) {
2855 $this->mCode
= $code;
2859 * Get the name of a file for a certain language code
2860 * @param $prefix string Prepend this to the filename
2861 * @param $code string Language code
2862 * @param $suffix string Append this to the filename
2863 * @return string $prefix . $mangledCode . $suffix
2865 static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
2866 // Protect against path traversal
2867 if ( !Language
::isValidCode( $code ) ) {
2868 throw new MWException( "Invalid language code \"$code\"" );
2871 return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
2875 * Get the language code from a file name. Inverse of getFileName()
2876 * @param $filename string $prefix . $languageCode . $suffix
2877 * @param $prefix string Prefix before the language code
2878 * @param $suffix string Suffix after the language code
2879 * @return Language code, or false if $prefix or $suffix isn't found
2881 static function getCodeFromFileName( $filename, $prefix = 'Language', $suffix = '.php' ) {
2883 preg_match( '/' . preg_quote( $prefix, '/' ) . '([A-Z][a-z_]+)' .
2884 preg_quote( $suffix, '/' ) . '/', $filename, $m );
2885 if ( !count( $m ) ) {
2888 return str_replace( '_', '-', strtolower( $m[1] ) );
2891 static function getMessagesFileName( $code ) {
2893 return self
::getFileName( "$IP/languages/messages/Messages", $code, '.php' );
2896 static function getClassFileName( $code ) {
2898 return self
::getFileName( "$IP/languages/classes/Language", $code, '.php' );
2902 * Get the fallback for a given language
2904 static function getFallbackFor( $code ) {
2905 if ( $code === 'en' ) {
2909 return self
::getLocalisationCache()->getItem( $code, 'fallback' );
2914 * Get all messages for a given language
2915 * WARNING: this may take a long time
2917 static function getMessagesFor( $code ) {
2918 return self
::getLocalisationCache()->getItem( $code, 'messages' );
2922 * Get a message for a given language
2924 static function getMessageFor( $key, $code ) {
2925 return self
::getLocalisationCache()->getSubitem( $code, 'messages', $key );
2928 function fixVariableInNamespace( $talk ) {
2929 if ( strpos( $talk, '$1' ) === false ) {
2933 global $wgMetaNamespace;
2934 $talk = str_replace( '$1', $wgMetaNamespace, $talk );
2936 # Allow grammar transformations
2937 # Allowing full message-style parsing would make simple requests
2938 # such as action=raw much more expensive than they need to be.
2939 # This will hopefully cover most cases.
2940 $talk = preg_replace_callback( '/{{grammar:(.*?)\|(.*?)}}/i',
2941 array( &$this, 'replaceGrammarInNamespace' ), $talk );
2942 return str_replace( ' ', '_', $talk );
2945 function replaceGrammarInNamespace( $m ) {
2946 return $this->convertGrammar( trim( $m[2] ), trim( $m[1] ) );
2949 static function getCaseMaps() {
2950 static $wikiUpperChars, $wikiLowerChars;
2951 if ( isset( $wikiUpperChars ) ) {
2952 return array( $wikiUpperChars, $wikiLowerChars );
2955 wfProfileIn( __METHOD__
);
2956 $arr = wfGetPrecompiledData( 'Utf8Case.ser' );
2957 if ( $arr === false ) {
2958 throw new MWException(
2959 "Utf8Case.ser is missing, please run \"make\" in the serialized directory\n" );
2961 $wikiUpperChars = $arr['wikiUpperChars'];
2962 $wikiLowerChars = $arr['wikiLowerChars'];
2963 wfProfileOut( __METHOD__
);
2964 return array( $wikiUpperChars, $wikiLowerChars );
2967 function formatTimePeriod( $seconds ) {
2968 if ( round( $seconds * 10 ) < 100 ) {
2969 return $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) ) . $this->getMessageFromDB( 'seconds-abbrev' );
2970 } elseif ( round( $seconds ) < 60 ) {
2971 return $this->formatNum( round( $seconds ) ) . $this->getMessageFromDB( 'seconds-abbrev' );
2972 } elseif ( round( $seconds ) < 3600 ) {
2973 $minutes = floor( $seconds / 60 );
2974 $secondsPart = round( fmod( $seconds, 60 ) );
2975 if ( $secondsPart == 60 ) {
2979 return $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' ) . ' ' .
2980 $this->formatNum( $secondsPart ) . $this->getMessageFromDB( 'seconds-abbrev' );
2982 $hours = floor( $seconds / 3600 );
2983 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
2984 $secondsPart = round( $seconds - $hours * 3600 - $minutes * 60 );
2985 if ( $secondsPart == 60 ) {
2989 if ( $minutes == 60 ) {
2993 return $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' ) . ' ' .
2994 $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' ) . ' ' .
2995 $this->formatNum( $secondsPart ) . $this->getMessageFromDB( 'seconds-abbrev' );
2999 function formatBitrate( $bps ) {
3000 $units = array( 'bps', 'kbps', 'Mbps', 'Gbps' );
3002 return $this->formatNum( $bps ) . $units[0];
3004 $unitIndex = floor( log10( $bps ) / 3 );
3005 $mantissa = $bps / pow( 1000, $unitIndex );
3006 if ( $mantissa < 10 ) {
3007 $mantissa = round( $mantissa, 1 );
3009 $mantissa = round( $mantissa );
3011 return $this->formatNum( $mantissa ) . $units[$unitIndex];
3015 * Format a size in bytes for output, using an appropriate
3016 * unit (B, KB, MB or GB) according to the magnitude in question
3018 * @param $size Size to format
3019 * @return string Plain text (not HTML)
3021 function formatSize( $size ) {
3022 // For small sizes no decimal places necessary
3024 if ( $size > 1024 ) {
3025 $size = $size / 1024;
3026 if ( $size > 1024 ) {
3027 $size = $size / 1024;
3028 // For MB and bigger two decimal places are smarter
3030 if ( $size > 1024 ) {
3031 $size = $size / 1024;
3032 $msg = 'size-gigabytes';
3034 $msg = 'size-megabytes';
3037 $msg = 'size-kilobytes';
3040 $msg = 'size-bytes';
3042 $size = round( $size, $round );
3043 $text = $this->getMessageFromDB( $msg );
3044 return str_replace( '$1', $this->formatNum( $size ), $text );
3048 * Get the conversion rule title, if any.
3050 function getConvRuleTitle() {
3051 return $this->mConverter
->getConvRuleTitle();