Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.57% covered (warning)
77.57%
1525 / 1966
33.97% covered (danger)
33.97%
53 / 156
CRAP
0.00% covered (danger)
0.00%
0 / 1
Language
77.61% covered (warning)
77.61%
1525 / 1965
33.97% covered (danger)
33.97%
53 / 156
7759.44
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFallbackLanguages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBookstoreList
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getNamespaces
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
5
 setNamespaces
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 resetNamespaces
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getFormattedNamespaces
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getNsText
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFormattedNsText
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getGenderNsText
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 needsGenderDistinction
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
 getLocalNsIndex
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getNamespaceAliases
90.00% covered (success)
90.00%
27 / 30
0.00% covered (danger)
0.00%
0 / 1
10.10
 getNamespaceIds
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 getNsIndex
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getVariantname
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getDatePreferences
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDateFormats
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultDateFormat
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 getDatePreferenceMigrationMap
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessageFromDB
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 msg
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMonthName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMonthNamesArray
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getMonthNameGen
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMonthAbbreviation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMonthAbbreviationsArray
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getWeekdayName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWeekdayAbbreviation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dateTimeObj
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 dateTimeObjFormat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sprintfDate
93.26% covered (success)
93.26%
346 / 371
0.00% covered (danger)
0.00%
0 / 1
139.49
 computeUnitTimestampDeadline
93.33% covered (success)
93.33%
28 / 30
0.00% covered (danger)
0.00%
0 / 1
9.02
 tsToIranian
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 1
9
 tsToHijri
92.31% covered (success)
92.31%
24 / 26
0.00% covered (danger)
0.00%
0 / 1
7.02
 tsToHebrew
59.65% covered (warning)
59.65%
34 / 57
0.00% covered (danger)
0.00%
0 / 1
79.51
 hebrewYearStart
77.78% covered (warning)
77.78%
14 / 18
0.00% covered (danger)
0.00%
0 / 1
13.58
 tsToYear
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
8.43
 tsToJapaneseGengo
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
10
 tsToJapaneseGengoCalculate
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 strongDirFromContent
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 romanNumeral
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
5.01
 hebrewNumeral
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
1 / 1
13
 userAdjust
73.68% covered (warning)
73.68%
14 / 19
0.00% covered (danger)
0.00%
0 / 1
4.29
 convertDateFormatToJs
76.67% covered (warning)
76.67%
184 / 240
0.00% covered (danger)
0.00%
0 / 1
155.29
 makeMediaWikiTimestamp
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 dateFormat
63.64% covered (warning)
63.64%
7 / 11
0.00% covered (danger)
0.00%
0 / 1
4.77
 getDateFormatString
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
7.93
 date
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 time
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 timeanddate
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 formatDuration
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 formatDurationBetweenTimestamps
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
11
 getDurationIntervals
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
6
 internalUserTimeAndDate
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 userDate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 userTime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 userTimeAndDate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHumanTimestamp
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
3.00
 getHumanTimestampInternal
89.47% covered (warning)
89.47%
34 / 38
0.00% covered (danger)
0.00%
0 / 1
20.47
 getGroupName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getGroupMemberName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getMessage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllMessages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 iconv
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 ucfirst
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
6.07
 uc
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 lcfirst
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
5.12
 lc
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 isMultibyte
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ucwords
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 ucwordbreaks
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
2
 caseFold
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 checkTitleEncoding
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 fallback8bitEncoding
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasWordBreaks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 segmentByWord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchIndexVariant
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalizeForSearch
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 convertDoubleWidth
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 insertSpace
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 convertForSearchResult
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 firstChar
42.42% covered (danger)
42.42%
14 / 33
0.00% covered (danger)
0.00%
0 / 1
79.84
 normalize
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
2.15
 transformUsingPairFile
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 isRTL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDir
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 alignStart
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 alignEnd
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getDirMarkEntity
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 getDirMark
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
 getArrow
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
90
 linkPrefixExtension
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMagicWords
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMagic
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 getSpecialPageAliases
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 emphasize
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 formatNum
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 formatNumInternal
98.15% covered (success)
98.15%
53 / 54
0.00% covered (danger)
0.00%
0 / 1
19
 formatNumNoSeparators
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseFormattedNumber
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
6
 digitGroupingPattern
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 digitTransformTable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 separatorTransformTable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 minimumGroupingDigits
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 listToText
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 commaList
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 semicolonList
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 pipeList
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 truncateForDatabase
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 truncateForVisual
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 truncateInternal
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
9
 removeBadCharLast
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
5.20
 truncateHtml
97.06% covered (success)
97.06%
66 / 68
0.00% covered (danger)
0.00%
0 / 1
29
 truncate_skip
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 truncate_endBracket
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
7
 convertGrammar
77.78% covered (warning)
77.78%
21 / 27
0.00% covered (danger)
0.00%
0 / 1
9.89
 getGrammarForms
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getGrammarTransformations
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
5.12
 gender
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 convertPlural
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
 handleExplicitPluralForms
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 preConvertPlural
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFormalityIndex
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 embedBidi
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
4.02
 getBlockDurations
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 translateBlockExpiry
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
10
 segmentForDiff
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 unsegmentForDiff
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 linkTrail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 linkPrefixCharset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 equals
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHtmlCode
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 toBcp47Code
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSameCodeAs
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
3.58
 getCodeFromFileName
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 fixVariableInNamespace
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 formatExpiry
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
 formatTimePeriod
100.00% covered (success)
100.00%
73 / 73
100.00% covered (success)
100.00%
1 / 1
23
 formatBitrate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 formatComputingNumbers
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
 formatSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 specialList
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getNumberFormatter
52.63% covered (warning)
52.63%
10 / 19
0.00% covered (danger)
0.00%
0 / 1
14.80
 createNumberFormatter
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
3.71
 getCompiledPluralRules
46.15% covered (danger)
46.15%
6 / 13
0.00% covered (danger)
0.00%
0 / 1
8.90
 getPluralRules
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 getPluralRuleTypes
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 getPluralRuleIndexNumber
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 getPluralRuleType
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 getConverterInternal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHookContainer
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHookRunner
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getJsData
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getJsDateFormats
86.21% covered (warning)
86.21%
25 / 29
0.00% covered (danger)
0.00%
0 / 1
10.26
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7/**
8 * @defgroup Language Internationalisation
9 *
10 * See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more information.
11 */
12
13/**
14 * @defgroup Languages Languages
15 * @ingroup Language
16 */
17
18namespace MediaWiki\Language;
19
20use CLDRPluralRuleParser\Evaluator;
21use DateTime;
22use DateTimeImmutable;
23use DateTimeInterface;
24use DateTimeZone;
25use InvalidArgumentException;
26use Locale;
27use MediaWiki\Config\Config;
28use MediaWiki\Context\RequestContext;
29use MediaWiki\HookContainer\HookContainer;
30use MediaWiki\HookContainer\HookRunner;
31use MediaWiki\Html\Html;
32use MediaWiki\Json\FormatJson;
33use MediaWiki\Languages\Data\NormalizeAr;
34use MediaWiki\Languages\Data\NormalizeMl;
35use MediaWiki\Logger\LoggerFactory;
36use MediaWiki\MainConfigNames;
37use MediaWiki\MediaWikiServices;
38use MediaWiki\Message\Message;
39use MediaWiki\Parser\MagicWord;
40use MediaWiki\Title\NamespaceInfo;
41use MediaWiki\User\User;
42use MediaWiki\User\UserIdentity;
43use MediaWiki\User\UserTimeCorrection;
44use MediaWiki\Utils\MWTimestamp;
45use MediaWiki\Xml\XmlSelect;
46use NumberFormatter;
47use RuntimeException;
48use UtfNormal\Validator as UtfNormalValidator;
49use Wikimedia\Bcp47Code\Bcp47Code;
50use Wikimedia\DebugInfo\DebugInfoTrait;
51use Wikimedia\Message\MessageParam;
52use Wikimedia\Message\MessageSpecifier;
53use Wikimedia\ReplacementArray;
54use Wikimedia\StringUtils\StringUtils;
55use Wikimedia\Timestamp\ConvertibleTimestamp;
56use Wikimedia\Timestamp\TimestampFormat as TS;
57
58/**
59 * Base class for language-specific code.
60 *
61 * See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more information.
62 *
63 * @ingroup Language
64 */
65class Language implements Bcp47Code {
66    use DebugInfoTrait;
67
68    public string $mCode;
69
70    /**
71     * @deprecated since 1.35, use LocalisationCache with custom language config
72     */
73    public $mMagicExtensions = [];
74
75    /** @var string|null */
76    private $mHtmlCode = null;
77
78    /**
79     * memoize
80     * @var string[][]
81     * @deprecated since 1.35, must be private
82     */
83    public $dateFormatStrings = [];
84
85    /**
86     * memoize
87     * @var string[][]|null
88     * @deprecated since 1.35, must be protected
89     */
90    public $mExtendedSpecialPageAliases;
91
92    /** @var array<int,string>|null Indexed by numeric namespace ID */
93    protected $namespaceNames;
94    /** @var array<string,int>|null Indexed by localized lower-cased namespace name */
95    protected $mNamespaceIds;
96    /** @var array<string,int>|null Map from alias to namespace ID */
97    protected $namespaceAliases;
98
99    /**
100     * @var ReplacementArray[]
101     * @noVarDump
102     */
103    private $transformData = [];
104
105    /**
106     * @var array[]|null
107     * @noVarDump
108     */
109    private $grammarTransformCache;
110
111    /** @noVarDump */
112    private HookRunner $hookRunner;
113
114    /**
115     * @var array|null
116     */
117    private $overrideUcfirstCharacters;
118
119    /**
120     * @var NumberFormatter|null
121     * @noVarDump
122     */
123    private $numberFormatter = null;
124
125    /**
126     * @since 1.35
127     */
128    public const WEEKDAY_MESSAGES = [
129        'sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
130        'friday', 'saturday'
131    ];
132
133    /**
134     * @since 1.35
135     */
136    public const WEEKDAY_ABBREVIATED_MESSAGES = [
137        'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
138    ];
139
140    /**
141     * @since 1.35
142     */
143    public const MONTH_MESSAGES = [
144        'january', 'february', 'march', 'april', 'may_long', 'june',
145        'july', 'august', 'september', 'october', 'november',
146        'december'
147    ];
148
149    /**
150     * @deprecated since 1.35, use the MONTH_MESSAGES constant
151     */
152    public static $mMonthMsgs = self::MONTH_MESSAGES;
153
154    /**
155     * @since 1.35
156     */
157    public const MONTH_GENITIVE_MESSAGES = [
158        'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
159        'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen',
160        'december-gen'
161    ];
162
163    /**
164     * @since 1.35
165     */
166    public const MONTH_ABBREVIATED_MESSAGES = [
167        'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
168        'sep', 'oct', 'nov', 'dec'
169    ];
170
171    /**
172     * @deprecated since 1.35, use the MONTH_ABBREVIATED_MESSAGES constant
173     */
174    public static $mMonthAbbrevMsgs = self::MONTH_ABBREVIATED_MESSAGES;
175
176    /**
177     * @since 1.35
178     */
179    public const IRANIAN_CALENDAR_MONTHS_MESSAGES = [
180        'iranian-calendar-m1', 'iranian-calendar-m2', 'iranian-calendar-m3',
181        'iranian-calendar-m4', 'iranian-calendar-m5', 'iranian-calendar-m6',
182        'iranian-calendar-m7', 'iranian-calendar-m8', 'iranian-calendar-m9',
183        'iranian-calendar-m10', 'iranian-calendar-m11', 'iranian-calendar-m12'
184    ];
185
186    /**
187     * @since 1.35
188     */
189    public const HEBREW_CALENDAR_MONTHS_MESSAGES = [
190        'hebrew-calendar-m1', 'hebrew-calendar-m2', 'hebrew-calendar-m3',
191        'hebrew-calendar-m4', 'hebrew-calendar-m5', 'hebrew-calendar-m6',
192        'hebrew-calendar-m7', 'hebrew-calendar-m8', 'hebrew-calendar-m9',
193        'hebrew-calendar-m10', 'hebrew-calendar-m11', 'hebrew-calendar-m12',
194        'hebrew-calendar-m6a', 'hebrew-calendar-m6b'
195    ];
196
197    /**
198     * @since 1.35
199     */
200    public const HEBREW_CALENDAR_MONTH_GENITIVE_MESSAGES = [
201        'hebrew-calendar-m1-gen', 'hebrew-calendar-m2-gen', 'hebrew-calendar-m3-gen',
202        'hebrew-calendar-m4-gen', 'hebrew-calendar-m5-gen', 'hebrew-calendar-m6-gen',
203        'hebrew-calendar-m7-gen', 'hebrew-calendar-m8-gen', 'hebrew-calendar-m9-gen',
204        'hebrew-calendar-m10-gen', 'hebrew-calendar-m11-gen', 'hebrew-calendar-m12-gen',
205        'hebrew-calendar-m6a-gen', 'hebrew-calendar-m6b-gen'
206    ];
207
208    /**
209     * @since 1.35
210     */
211    public const HIJRI_CALENDAR_MONTH_MESSAGES = [
212        'hijri-calendar-m1', 'hijri-calendar-m2', 'hijri-calendar-m3',
213        'hijri-calendar-m4', 'hijri-calendar-m5', 'hijri-calendar-m6',
214        'hijri-calendar-m7', 'hijri-calendar-m8', 'hijri-calendar-m9',
215        'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
216    ];
217
218    /**
219     * @since 1.35
220     */
221    protected const DURATION_INTERVALS = [
222        'millennia' => 1000 * 31_556_952,
223        'centuries' => 100 * 31_556_952,
224        'decades' => 10 * 31_556_952,
225        // The average year is 365.2425 days (365 + (24 * 3 + 25) / 400)
226        'years' => 31_556_952, // 365.2425 * 24 * 3600
227        // To simplify, we consider a month to be 1/12 of a year
228        'months' => 365.2425 * 24 * 3600 / 12,
229        'days' => 24 * 3600,
230        'hours' => 3600,
231        'minutes' => 60,
232        'seconds' => 1,
233    ];
234
235    /**
236     * @deprecated since 1.35, use the DURATION_INTERVALS constant
237     * @since 1.20
238     * @var int[]
239     */
240    public static $durationIntervals = self::DURATION_INTERVALS;
241
242    /** Map of (time unit => relative datetime specifier) */
243    private const DEADLINE_DATE_SPEC_BY_UNIT = [
244        'Y' => 'first day of January next year midnight',
245        'M' => 'first day of next month midnight',
246        'W' => 'monday next week midnight',
247        'D' => 'next day midnight',
248        // Note that this may be before the current time, in which case
249        // we advance by a calendar year; the ISOY is actually 1 week past
250        // this, since dec 28 is always in "the last week of the ISO year".
251        'ISOY' => 'december 28 midnight',
252        // Note that this may be before the current time, in which case 'D'
253        // is used.
254        'AM' => 'today noon',
255        // Note that this relative datetime specifier does not zero out
256        // minutes/seconds, but we will do so manually in
257        // ::computeUnitTimestampDeadline() when given the unit 'H'/'m'/'s'
258        'H' => 'next hour',
259        'm' => 'next minute',
260        's' => 'next second',
261    ];
262
263    /**
264     * Unicode directional formatting characters
265     */
266    private const LRM = "\u{200E}"; // U+200E LEFT-TO-RIGHT MARK
267    private const RLM = "\u{200F}"; // U+200F RIGHT-TO-LEFT MARK
268    private const LRE = "\u{202A}"; // U+202A LEFT-TO-RIGHT EMBEDDING
269    private const RLE = "\u{202B}"; // U+202B RIGHT-TO-LEFT EMBEDDING
270    private const PDF = "\u{202C}"; // U+202C POP DIRECTIONAL FORMATTING
271    // https://en.wikipedia.org/wiki/Arabic_letter_mark (Unicode 6.3.0)
272    private const ALM = "\u{061C}"; // U+061C ARABIC LETTER MARK
273
274    /**
275     * Directionality test regex for embedBidi(). Matches the first strong directionality codepoint:
276     * - in group 1 if it is LTR
277     * - in group 2 if it is RTL
278     * Does not match if there is no strong directionality codepoint.
279     *
280     * The form is '/(?:([strong ltr codepoint])|([strong rtl codepoint]))/u'.
281     *
282     * Generated by UnicodeJS (see tools/strongDir) from the UCD; see
283     * https://gerrit.wikimedia.org/g/unicodejs .
284     * @var string
285     */
286    // @codeCoverageIgnoreStart
287    // phpcs:ignore Generic.Files.LineLength,MediaWiki.Commenting.PropertyDocumentation.MissingDocumentationPrivate
288    private static $strongDirRegex = '/(?:([\x{41}-\x{5a}\x{61}-\x{7a}\x{aa}\x{b5}\x{ba}\x{c0}-\x{d6}\x{d8}-\x{f6}\x{f8}-\x{2b8}\x{2bb}-\x{2c1}\x{2d0}\x{2d1}\x{2e0}-\x{2e4}\x{2ee}\x{370}-\x{373}\x{376}\x{377}\x{37a}-\x{37d}\x{37f}\x{386}\x{388}-\x{38a}\x{38c}\x{38e}-\x{3a1}\x{3a3}-\x{3f5}\x{3f7}-\x{482}\x{48a}-\x{52f}\x{531}-\x{556}\x{559}-\x{55f}\x{561}-\x{587}\x{589}\x{903}-\x{939}\x{93b}\x{93d}-\x{940}\x{949}-\x{94c}\x{94e}-\x{950}\x{958}-\x{961}\x{964}-\x{980}\x{982}\x{983}\x{985}-\x{98c}\x{98f}\x{990}\x{993}-\x{9a8}\x{9aa}-\x{9b0}\x{9b2}\x{9b6}-\x{9b9}\x{9bd}-\x{9c0}\x{9c7}\x{9c8}\x{9cb}\x{9cc}\x{9ce}\x{9d7}\x{9dc}\x{9dd}\x{9df}-\x{9e1}\x{9e6}-\x{9f1}\x{9f4}-\x{9fa}\x{a03}\x{a05}-\x{a0a}\x{a0f}\x{a10}\x{a13}-\x{a28}\x{a2a}-\x{a30}\x{a32}\x{a33}\x{a35}\x{a36}\x{a38}\x{a39}\x{a3e}-\x{a40}\x{a59}-\x{a5c}\x{a5e}\x{a66}-\x{a6f}\x{a72}-\x{a74}\x{a83}\x{a85}-\x{a8d}\x{a8f}-\x{a91}\x{a93}-\x{aa8}\x{aaa}-\x{ab0}\x{ab2}\x{ab3}\x{ab5}-\x{ab9}\x{abd}-\x{ac0}\x{ac9}\x{acb}\x{acc}\x{ad0}\x{ae0}\x{ae1}\x{ae6}-\x{af0}\x{af9}\x{b02}\x{b03}\x{b05}-\x{b0c}\x{b0f}\x{b10}\x{b13}-\x{b28}\x{b2a}-\x{b30}\x{b32}\x{b33}\x{b35}-\x{b39}\x{b3d}\x{b3e}\x{b40}\x{b47}\x{b48}\x{b4b}\x{b4c}\x{b57}\x{b5c}\x{b5d}\x{b5f}-\x{b61}\x{b66}-\x{b77}\x{b83}\x{b85}-\x{b8a}\x{b8e}-\x{b90}\x{b92}-\x{b95}\x{b99}\x{b9a}\x{b9c}\x{b9e}\x{b9f}\x{ba3}\x{ba4}\x{ba8}-\x{baa}\x{bae}-\x{bb9}\x{bbe}\x{bbf}\x{bc1}\x{bc2}\x{bc6}-\x{bc8}\x{bca}-\x{bcc}\x{bd0}\x{bd7}\x{be6}-\x{bf2}\x{c01}-\x{c03}\x{c05}-\x{c0c}\x{c0e}-\x{c10}\x{c12}-\x{c28}\x{c2a}-\x{c39}\x{c3d}\x{c41}-\x{c44}\x{c58}-\x{c5a}\x{c60}\x{c61}\x{c66}-\x{c6f}\x{c7f}\x{c82}\x{c83}\x{c85}-\x{c8c}\x{c8e}-\x{c90}\x{c92}-\x{ca8}\x{caa}-\x{cb3}\x{cb5}-\x{cb9}\x{cbd}-\x{cc4}\x{cc6}-\x{cc8}\x{cca}\x{ccb}\x{cd5}\x{cd6}\x{cde}\x{ce0}\x{ce1}\x{ce6}-\x{cef}\x{cf1}\x{cf2}\x{d02}\x{d03}\x{d05}-\x{d0c}\x{d0e}-\x{d10}\x{d12}-\x{d3a}\x{d3d}-\x{d40}\x{d46}-\x{d48}\x{d4a}-\x{d4c}\x{d4e}\x{d57}\x{d5f}-\x{d61}\x{d66}-\x{d75}\x{d79}-\x{d7f}\x{d82}\x{d83}\x{d85}-\x{d96}\x{d9a}-\x{db1}\x{db3}-\x{dbb}\x{dbd}\x{dc0}-\x{dc6}\x{dcf}-\x{dd1}\x{dd8}-\x{ddf}\x{de6}-\x{def}\x{df2}-\x{df4}\x{e01}-\x{e30}\x{e32}\x{e33}\x{e40}-\x{e46}\x{e4f}-\x{e5b}\x{e81}\x{e82}\x{e84}\x{e87}\x{e88}\x{e8a}\x{e8d}\x{e94}-\x{e97}\x{e99}-\x{e9f}\x{ea1}-\x{ea3}\x{ea5}\x{ea7}\x{eaa}\x{eab}\x{ead}-\x{eb0}\x{eb2}\x{eb3}\x{ebd}\x{ec0}-\x{ec4}\x{ec6}\x{ed0}-\x{ed9}\x{edc}-\x{edf}\x{f00}-\x{f17}\x{f1a}-\x{f34}\x{f36}\x{f38}\x{f3e}-\x{f47}\x{f49}-\x{f6c}\x{f7f}\x{f85}\x{f88}-\x{f8c}\x{fbe}-\x{fc5}\x{fc7}-\x{fcc}\x{fce}-\x{fda}\x{1000}-\x{102c}\x{1031}\x{1038}\x{103b}\x{103c}\x{103f}-\x{1057}\x{105a}-\x{105d}\x{1061}-\x{1070}\x{1075}-\x{1081}\x{1083}\x{1084}\x{1087}-\x{108c}\x{108e}-\x{109c}\x{109e}-\x{10c5}\x{10c7}\x{10cd}\x{10d0}-\x{1248}\x{124a}-\x{124d}\x{1250}-\x{1256}\x{1258}\x{125a}-\x{125d}\x{1260}-\x{1288}\x{128a}-\x{128d}\x{1290}-\x{12b0}\x{12b2}-\x{12b5}\x{12b8}-\x{12be}\x{12c0}\x{12c2}-\x{12c5}\x{12c8}-\x{12d6}\x{12d8}-\x{1310}\x{1312}-\x{1315}\x{1318}-\x{135a}\x{1360}-\x{137c}\x{1380}-\x{138f}\x{13a0}-\x{13f5}\x{13f8}-\x{13fd}\x{1401}-\x{167f}\x{1681}-\x{169a}\x{16a0}-\x{16f8}\x{1700}-\x{170c}\x{170e}-\x{1711}\x{1720}-\x{1731}\x{1735}\x{1736}\x{1740}-\x{1751}\x{1760}-\x{176c}\x{176e}-\x{1770}\x{1780}-\x{17b3}\x{17b6}\x{17be}-\x{17c5}\x{17c7}\x{17c8}\x{17d4}-\x{17da}\x{17dc}\x{17e0}-\x{17e9}\x{1810}-\x{1819}\x{1820}-\x{1877}\x{1880}-\x{18a8}\x{18aa}\x{18b0}-\x{18f5}\x{1900}-\x{191e}\x{1923}-\x{1926}\x{1929}-\x{192b}\x{1930}\x{1931}\x{1933}-\x{1938}\x{1946}-\x{196d}\x{1970}-\x{1974}\x{1980}-\x{19ab}\x{19b0}-\x{19c9}\x{19d0}-\x{19da}\x{1a00}-\x{1a16}\x{1a19}\x{1a1a}\x{1a1e}-\x{1a55}\x{1a57}\x{1a61}\x{1a63}\x{1a64}\x{1a6d}-\x{1a72}\x{1a80}-\x{1a89}\x{1a90}-\x{1a99}\x{1aa0}-\x{1aad}\x{1b04}-\x{1b33}\x{1b35}\x{1b3b}\x{1b3d}-\x{1b41}\x{1b43}-\x{1b4b}\x{1b50}-\x{1b6a}\x{1b74}-\x{1b7c}\x{1b82}-\x{1ba1}\x{1ba6}\x{1ba7}\x{1baa}\x{1bae}-\x{1be5}\x{1be7}\x{1bea}-\x{1bec}\x{1bee}\x{1bf2}\x{1bf3}\x{1bfc}-\x{1c2b}\x{1c34}\x{1c35}\x{1c3b}-\x{1c49}\x{1c4d}-\x{1c7f}\x{1cc0}-\x{1cc7}\x{1cd3}\x{1ce1}\x{1ce9}-\x{1cec}\x{1cee}-\x{1cf3}\x{1cf5}\x{1cf6}\x{1d00}-\x{1dbf}\x{1e00}-\x{1f15}\x{1f18}-\x{1f1d}\x{1f20}-\x{1f45}\x{1f48}-\x{1f4d}\x{1f50}-\x{1f57}\x{1f59}\x{1f5b}\x{1f5d}\x{1f5f}-\x{1f7d}\x{1f80}-\x{1fb4}\x{1fb6}-\x{1fbc}\x{1fbe}\x{1fc2}-\x{1fc4}\x{1fc6}-\x{1fcc}\x{1fd0}-\x{1fd3}\x{1fd6}-\x{1fdb}\x{1fe0}-\x{1fec}\x{1ff2}-\x{1ff4}\x{1ff6}-\x{1ffc}\x{200e}\x{2071}\x{207f}\x{2090}-\x{209c}\x{2102}\x{2107}\x{210a}-\x{2113}\x{2115}\x{2119}-\x{211d}\x{2124}\x{2126}\x{2128}\x{212a}-\x{212d}\x{212f}-\x{2139}\x{213c}-\x{213f}\x{2145}-\x{2149}\x{214e}\x{214f}\x{2160}-\x{2188}\x{2336}-\x{237a}\x{2395}\x{249c}-\x{24e9}\x{26ac}\x{2800}-\x{28ff}\x{2c00}-\x{2c2e}\x{2c30}-\x{2c5e}\x{2c60}-\x{2ce4}\x{2ceb}-\x{2cee}\x{2cf2}\x{2cf3}\x{2d00}-\x{2d25}\x{2d27}\x{2d2d}\x{2d30}-\x{2d67}\x{2d6f}\x{2d70}\x{2d80}-\x{2d96}\x{2da0}-\x{2da6}\x{2da8}-\x{2dae}\x{2db0}-\x{2db6}\x{2db8}-\x{2dbe}\x{2dc0}-\x{2dc6}\x{2dc8}-\x{2dce}\x{2dd0}-\x{2dd6}\x{2dd8}-\x{2dde}\x{3005}-\x{3007}\x{3021}-\x{3029}\x{302e}\x{302f}\x{3031}-\x{3035}\x{3038}-\x{303c}\x{3041}-\x{3096}\x{309d}-\x{309f}\x{30a1}-\x{30fa}\x{30fc}-\x{30ff}\x{3105}-\x{312d}\x{3131}-\x{318e}\x{3190}-\x{31ba}\x{31f0}-\x{321c}\x{3220}-\x{324f}\x{3260}-\x{327b}\x{327f}-\x{32b0}\x{32c0}-\x{32cb}\x{32d0}-\x{32fe}\x{3300}-\x{3376}\x{337b}-\x{33dd}\x{33e0}-\x{33fe}\x{3400}-\x{4db5}\x{4e00}-\x{9fd5}\x{a000}-\x{a48c}\x{a4d0}-\x{a60c}\x{a610}-\x{a62b}\x{a640}-\x{a66e}\x{a680}-\x{a69d}\x{a6a0}-\x{a6ef}\x{a6f2}-\x{a6f7}\x{a722}-\x{a787}\x{a789}-\x{a7ad}\x{a7b0}-\x{a7b7}\x{a7f7}-\x{a801}\x{a803}-\x{a805}\x{a807}-\x{a80a}\x{a80c}-\x{a824}\x{a827}\x{a830}-\x{a837}\x{a840}-\x{a873}\x{a880}-\x{a8c3}\x{a8ce}-\x{a8d9}\x{a8f2}-\x{a8fd}\x{a900}-\x{a925}\x{a92e}-\x{a946}\x{a952}\x{a953}\x{a95f}-\x{a97c}\x{a983}-\x{a9b2}\x{a9b4}\x{a9b5}\x{a9ba}\x{a9bb}\x{a9bd}-\x{a9cd}\x{a9cf}-\x{a9d9}\x{a9de}-\x{a9e4}\x{a9e6}-\x{a9fe}\x{aa00}-\x{aa28}\x{aa2f}\x{aa30}\x{aa33}\x{aa34}\x{aa40}-\x{aa42}\x{aa44}-\x{aa4b}\x{aa4d}\x{aa50}-\x{aa59}\x{aa5c}-\x{aa7b}\x{aa7d}-\x{aaaf}\x{aab1}\x{aab5}\x{aab6}\x{aab9}-\x{aabd}\x{aac0}\x{aac2}\x{aadb}-\x{aaeb}\x{aaee}-\x{aaf5}\x{ab01}-\x{ab06}\x{ab09}-\x{ab0e}\x{ab11}-\x{ab16}\x{ab20}-\x{ab26}\x{ab28}-\x{ab2e}\x{ab30}-\x{ab65}\x{ab70}-\x{abe4}\x{abe6}\x{abe7}\x{abe9}-\x{abec}\x{abf0}-\x{abf9}\x{ac00}-\x{d7a3}\x{d7b0}-\x{d7c6}\x{d7cb}-\x{d7fb}\x{e000}-\x{fa6d}\x{fa70}-\x{fad9}\x{fb00}-\x{fb06}\x{fb13}-\x{fb17}\x{ff21}-\x{ff3a}\x{ff41}-\x{ff5a}\x{ff66}-\x{ffbe}\x{ffc2}-\x{ffc7}\x{ffca}-\x{ffcf}\x{ffd2}-\x{ffd7}\x{ffda}-\x{ffdc}\x{10000}-\x{1000b}\x{1000d}-\x{10026}\x{10028}-\x{1003a}\x{1003c}\x{1003d}\x{1003f}-\x{1004d}\x{10050}-\x{1005d}\x{10080}-\x{100fa}\x{10100}\x{10102}\x{10107}-\x{10133}\x{10137}-\x{1013f}\x{101d0}-\x{101fc}\x{10280}-\x{1029c}\x{102a0}-\x{102d0}\x{10300}-\x{10323}\x{10330}-\x{1034a}\x{10350}-\x{10375}\x{10380}-\x{1039d}\x{1039f}-\x{103c3}\x{103c8}-\x{103d5}\x{10400}-\x{1049d}\x{104a0}-\x{104a9}\x{10500}-\x{10527}\x{10530}-\x{10563}\x{1056f}\x{10600}-\x{10736}\x{10740}-\x{10755}\x{10760}-\x{10767}\x{11000}\x{11002}-\x{11037}\x{11047}-\x{1104d}\x{11066}-\x{1106f}\x{11082}-\x{110b2}\x{110b7}\x{110b8}\x{110bb}-\x{110c1}\x{110d0}-\x{110e8}\x{110f0}-\x{110f9}\x{11103}-\x{11126}\x{1112c}\x{11136}-\x{11143}\x{11150}-\x{11172}\x{11174}-\x{11176}\x{11182}-\x{111b5}\x{111bf}-\x{111c9}\x{111cd}\x{111d0}-\x{111df}\x{111e1}-\x{111f4}\x{11200}-\x{11211}\x{11213}-\x{1122e}\x{11232}\x{11233}\x{11235}\x{11238}-\x{1123d}\x{11280}-\x{11286}\x{11288}\x{1128a}-\x{1128d}\x{1128f}-\x{1129d}\x{1129f}-\x{112a9}\x{112b0}-\x{112de}\x{112e0}-\x{112e2}\x{112f0}-\x{112f9}\x{11302}\x{11303}\x{11305}-\x{1130c}\x{1130f}\x{11310}\x{11313}-\x{11328}\x{1132a}-\x{11330}\x{11332}\x{11333}\x{11335}-\x{11339}\x{1133d}-\x{1133f}\x{11341}-\x{11344}\x{11347}\x{11348}\x{1134b}-\x{1134d}\x{11350}\x{11357}\x{1135d}-\x{11363}\x{11480}-\x{114b2}\x{114b9}\x{114bb}-\x{114be}\x{114c1}\x{114c4}-\x{114c7}\x{114d0}-\x{114d9}\x{11580}-\x{115b1}\x{115b8}-\x{115bb}\x{115be}\x{115c1}-\x{115db}\x{11600}-\x{11632}\x{1163b}\x{1163c}\x{1163e}\x{11641}-\x{11644}\x{11650}-\x{11659}\x{11680}-\x{116aa}\x{116ac}\x{116ae}\x{116af}\x{116b6}\x{116c0}-\x{116c9}\x{11700}-\x{11719}\x{11720}\x{11721}\x{11726}\x{11730}-\x{1173f}\x{118a0}-\x{118f2}\x{118ff}\x{11ac0}-\x{11af8}\x{12000}-\x{12399}\x{12400}-\x{1246e}\x{12470}-\x{12474}\x{12480}-\x{12543}\x{13000}-\x{1342e}\x{14400}-\x{14646}\x{16800}-\x{16a38}\x{16a40}-\x{16a5e}\x{16a60}-\x{16a69}\x{16a6e}\x{16a6f}\x{16ad0}-\x{16aed}\x{16af5}\x{16b00}-\x{16b2f}\x{16b37}-\x{16b45}\x{16b50}-\x{16b59}\x{16b5b}-\x{16b61}\x{16b63}-\x{16b77}\x{16b7d}-\x{16b8f}\x{16f00}-\x{16f44}\x{16f50}-\x{16f7e}\x{16f93}-\x{16f9f}\x{1b000}\x{1b001}\x{1bc00}-\x{1bc6a}\x{1bc70}-\x{1bc7c}\x{1bc80}-\x{1bc88}\x{1bc90}-\x{1bc99}\x{1bc9c}\x{1bc9f}\x{1d000}-\x{1d0f5}\x{1d100}-\x{1d126}\x{1d129}-\x{1d166}\x{1d16a}-\x{1d172}\x{1d183}\x{1d184}\x{1d18c}-\x{1d1a9}\x{1d1ae}-\x{1d1e8}\x{1d360}-\x{1d371}\x{1d400}-\x{1d454}\x{1d456}-\x{1d49c}\x{1d49e}\x{1d49f}\x{1d4a2}\x{1d4a5}\x{1d4a6}\x{1d4a9}-\x{1d4ac}\x{1d4ae}-\x{1d4b9}\x{1d4bb}\x{1d4bd}-\x{1d4c3}\x{1d4c5}-\x{1d505}\x{1d507}-\x{1d50a}\x{1d50d}-\x{1d514}\x{1d516}-\x{1d51c}\x{1d51e}-\x{1d539}\x{1d53b}-\x{1d53e}\x{1d540}-\x{1d544}\x{1d546}\x{1d54a}-\x{1d550}\x{1d552}-\x{1d6a5}\x{1d6a8}-\x{1d6da}\x{1d6dc}-\x{1d714}\x{1d716}-\x{1d74e}\x{1d750}-\x{1d788}\x{1d78a}-\x{1d7c2}\x{1d7c4}-\x{1d7cb}\x{1d800}-\x{1d9ff}\x{1da37}-\x{1da3a}\x{1da6d}-\x{1da74}\x{1da76}-\x{1da83}\x{1da85}-\x{1da8b}\x{1f110}-\x{1f12e}\x{1f130}-\x{1f169}\x{1f170}-\x{1f19a}\x{1f1e6}-\x{1f202}\x{1f210}-\x{1f23a}\x{1f240}-\x{1f248}\x{1f250}\x{1f251}\x{20000}-\x{2a6d6}\x{2a700}-\x{2b734}\x{2b740}-\x{2b81d}\x{2b820}-\x{2cea1}\x{2f800}-\x{2fa1d}\x{f0000}-\x{ffffd}\x{100000}-\x{10fffd}])|([\x{590}\x{5be}\x{5c0}\x{5c3}\x{5c6}\x{5c8}-\x{5ff}\x{7c0}-\x{7ea}\x{7f4}\x{7f5}\x{7fa}-\x{815}\x{81a}\x{824}\x{828}\x{82e}-\x{858}\x{85c}-\x{89f}\x{200f}\x{fb1d}\x{fb1f}-\x{fb28}\x{fb2a}-\x{fb4f}\x{10800}-\x{1091e}\x{10920}-\x{10a00}\x{10a04}\x{10a07}-\x{10a0b}\x{10a10}-\x{10a37}\x{10a3b}-\x{10a3e}\x{10a40}-\x{10ae4}\x{10ae7}-\x{10b38}\x{10b40}-\x{10e5f}\x{10e7f}-\x{10fff}\x{1e800}-\x{1e8cf}\x{1e8d7}-\x{1edff}\x{1ef00}-\x{1efff}\x{608}\x{60b}\x{60d}\x{61b}-\x{64a}\x{66d}-\x{66f}\x{671}-\x{6d5}\x{6e5}\x{6e6}\x{6ee}\x{6ef}\x{6fa}-\x{710}\x{712}-\x{72f}\x{74b}-\x{7a5}\x{7b1}-\x{7bf}\x{8a0}-\x{8e2}\x{fb50}-\x{fd3d}\x{fd40}-\x{fdcf}\x{fdf0}-\x{fdfc}\x{fdfe}\x{fdff}\x{fe70}-\x{fefe}\x{1ee00}-\x{1eeef}\x{1eef2}-\x{1eeff}]))/u';
289    // @codeCoverageIgnoreEnd
290
291    /**
292     * @internal Use LanguageFactory instead.
293     */
294    public function __construct(
295        string $code,
296        /** @noVarDump */
297        private readonly NamespaceInfo $namespaceInfo,
298        /** @noVarDump */
299        private LocalisationCache $localisationCache,
300        /** @noVarDump */
301        private readonly LanguageNameUtils $langNameUtils,
302        /** @noVarDump */
303        private readonly LanguageFallback $langFallback,
304        /** @noVarDump */
305        private readonly LanguageConverterFactory $converterFactory,
306        /** @noVarDump */
307        private readonly HookContainer $hookContainer,
308        /** @noVarDump */
309        private readonly Config $config,
310        /** @noVarDump */
311        private readonly LeximorphFactory $leximorphFactory,
312    ) {
313        $this->mCode = $code;
314        $this->hookRunner = new HookRunner( $hookContainer );
315    }
316
317    /**
318     * @return array
319     * @since 1.19
320     */
321    public function getFallbackLanguages() {
322        return $this->langFallback->getAll( $this->mCode );
323    }
324
325    /**
326     * Exports $wgBookstoreListEn
327     * @return array
328     */
329    public function getBookstoreList() {
330        return $this->localisationCache->getItem( $this->mCode, 'bookstoreList' );
331    }
332
333    /**
334     * Returns an array of localised namespaces (with underscores, without considering language
335     * variants) indexed by their numbers. If the namespace is not available in localised form, it
336     * will be included in English.
337     *
338     * @return array<int,string> List of localized namespace names, indexed by numeric namespace ID.
339     */
340    public function getNamespaces() {
341        if ( $this->namespaceNames === null ) {
342            $metaNamespace = $this->config->get( MainConfigNames::MetaNamespace );
343            $metaNamespaceTalk = $this->config->get( MainConfigNames::MetaNamespaceTalk );
344            $extraNamespaces = $this->config->get( MainConfigNames::ExtraNamespaces );
345            $validNamespaces = $this->namespaceInfo->getCanonicalNamespaces();
346
347            // @phan-suppress-next-line PhanTypeMismatchProperty
348            $this->namespaceNames = $extraNamespaces +
349                $this->localisationCache->getItem( $this->mCode, 'namespaceNames' );
350            // @phan-suppress-next-line PhanTypeInvalidLeftOperand
351            $this->namespaceNames += $validNamespaces;
352
353            $this->namespaceNames[NS_PROJECT] = $metaNamespace;
354            if ( $metaNamespaceTalk ) {
355                $this->namespaceNames[NS_PROJECT_TALK] = $metaNamespaceTalk;
356            } else {
357                $talk = $this->namespaceNames[NS_PROJECT_TALK];
358                $this->namespaceNames[NS_PROJECT_TALK] =
359                    $this->fixVariableInNamespace( $talk );
360            }
361
362            # Sometimes a language will be localised but not actually exist on this wiki.
363            foreach ( $this->namespaceNames as $key => $text ) {
364                if ( !isset( $validNamespaces[$key] ) ) {
365                    unset( $this->namespaceNames[$key] );
366                }
367            }
368
369            # The above mixing may leave namespaces out of canonical order.
370            # Re-order by namespace ID number...
371            ksort( $this->namespaceNames );
372
373            $this->getHookRunner()->onLanguageGetNamespaces( $this->namespaceNames );
374        }
375
376        return $this->namespaceNames;
377    }
378
379    /**
380     * Arbitrarily set all the namespace names at once. Mainly used for testing
381     * @param string[] $namespaces Array of namespaces (id => name)
382     */
383    public function setNamespaces( array $namespaces ) {
384        $this->namespaceNames = $namespaces;
385        $this->mNamespaceIds = null;
386    }
387
388    /**
389     * Resets all the namespace caches. Mainly used for testing
390     * @deprecated since 1.39 Use MediaWikiServices::resetServiceForTesting() instead.
391     */
392    public function resetNamespaces() {
393        $this->namespaceNames = null;
394        $this->mNamespaceIds = null;
395        $this->namespaceAliases = null;
396    }
397
398    /**
399     * A convenience function that returns {@see getNamespaces} with spaces instead of underscores
400     * in values.
401     *
402     * NOTE: This is not suitable for UI text, as language variants of namespace names defined via
403     * system messages are ignored. Use {@see LanguageConverter::convertNamespace} instead.
404     *
405     * @return array<int,string>
406     */
407    public function getFormattedNamespaces() {
408        $ns = $this->getNamespaces();
409        foreach ( $ns as $k => $v ) {
410            $ns[$k] = strtr( $v, '_', ' ' );
411        }
412        return $ns;
413    }
414
415    /**
416     * Get a namespace value by key
417     *
418     * Namespace name uses underscores (not spaces), e.g. 'MediaWiki_talk'.
419     *
420     * <code>
421     * $mw_ns = $lang->getNsText( NS_MEDIAWIKI_TALK );
422     * echo $mw_ns; // prints 'MediaWiki_talk'
423     * </code>
424     *
425     * @param int $index The array key of the namespace to return
426     * @return string|false String if the namespace value exists, otherwise false
427     */
428    public function getNsText( $index ) {
429        $ns = $this->getNamespaces();
430        return $ns[$index] ?? false;
431    }
432
433    /**
434     * A convenience function that returns the same thing as
435     * getNsText() except with '_' changed to ' ', useful for
436     * producing output.
437     *
438     * <code>
439     * $mw_ns = $lang->getFormattedNsText( NS_MEDIAWIKI_TALK );
440     * echo $mw_ns; // prints 'MediaWiki talk'
441     * </code>
442     *
443     * @param int $index The array key of the namespace to return
444     * @return string Namespace name without underscores (empty string if namespace does not exist)
445     */
446    public function getFormattedNsText( $index ) {
447        $ns = $this->getNsText( $index );
448        return $ns === false ? '' : strtr( $ns, '_', ' ' );
449    }
450
451    /**
452     * Returns gender-dependent namespace alias if available.
453     * See https://www.mediawiki.org/wiki/Manual:$wgExtraGenderNamespaces
454     * @param int $index Namespace index
455     * @param string $gender Gender key (male, female... )
456     * @return string|false
457     * @since 1.18
458     */
459    public function getGenderNsText( $index, $gender ) {
460        $extraGenderNamespaces = $this->config->get( MainConfigNames::ExtraGenderNamespaces );
461
462        $ns = $extraGenderNamespaces +
463            (array)$this->localisationCache->getItem( $this->mCode, 'namespaceGenderAliases' );
464
465        return $ns[$index][$gender] ?? $this->getNsText( $index );
466    }
467
468    /**
469     * Whether this language uses gender-dependent namespace aliases.
470     * See https://www.mediawiki.org/wiki/Manual:$wgExtraGenderNamespaces
471     * @return bool
472     * @since 1.18
473     */
474    public function needsGenderDistinction() {
475        $extraGenderNamespaces = $this->config->get( MainConfigNames::ExtraGenderNamespaces );
476        $extraNamespaces = $this->config->get( MainConfigNames::ExtraNamespaces );
477        if ( count( $extraGenderNamespaces ) > 0 ) {
478            // $wgExtraGenderNamespaces overrides everything
479            return true;
480        } elseif ( isset( $extraNamespaces[NS_USER] ) && isset( $extraNamespaces[NS_USER_TALK] ) ) {
481            // @todo There may be other gender namespace than NS_USER & NS_USER_TALK in the future
482            // $wgExtraNamespaces overrides any gender aliases specified in i18n files
483            return false;
484        } else {
485            // Check what is in i18n files
486            $aliases = (array)$this->localisationCache->getItem( $this->mCode, 'namespaceGenderAliases' );
487            return count( $aliases ) > 0;
488        }
489    }
490
491    /**
492     * Get a namespace key by case-insensitive value.
493     * Only matches namespace names for the current language, not the
494     * canonical ones defined in Namespace.php.
495     *
496     * @param string $text
497     * @return int|false An integer if $text is a valid value otherwise false
498     */
499    public function getLocalNsIndex( $text ) {
500        $lctext = $this->lc( $text );
501        $ids = $this->getNamespaceIds();
502        return $ids[$lctext] ?? false;
503    }
504
505    /**
506     * @return array<string,int> Map from names to namespace IDs. Note that each
507     * namespace ID can have multiple alias.
508     */
509    public function getNamespaceAliases() {
510        if ( $this->namespaceAliases === null ) {
511            $aliases = $this->localisationCache->getItem( $this->mCode, 'namespaceAliases' );
512            if ( !$aliases ) {
513                $aliases = [];
514            } else {
515                foreach ( $aliases as $name => $index ) {
516                    if ( $index === NS_PROJECT_TALK ) {
517                        unset( $aliases[$name] );
518                        $name = $this->fixVariableInNamespace( $name );
519                        $aliases[$name] = $index;
520                    }
521                }
522            }
523
524            $extraGenderNamespaces = $this->config->get( MainConfigNames::ExtraGenderNamespaces );
525            $genders = $extraGenderNamespaces + (array)$this->localisationCache
526                ->getItem( $this->mCode, 'namespaceGenderAliases' );
527            foreach ( $genders as $index => $forms ) {
528                foreach ( $forms as $alias ) {
529                    $aliases[$alias] = $index;
530                }
531            }
532
533            $langConverter = $this->getConverterInternal();
534            # Also add converted namespace names as aliases, to avoid confusion.
535            $convertedNames = [];
536            foreach ( $langConverter->getVariants() as $variant ) {
537                if ( $variant === $this->mCode ) {
538                    continue;
539                }
540                foreach ( $this->getNamespaces() as $ns => $_ ) {
541                    $convertedNames[$langConverter->convertNamespace( $ns, $variant )] = $ns;
542                }
543            }
544
545            $this->namespaceAliases = $aliases + $convertedNames;
546
547            // In the case of conflicts between $wgNamespaceAliases and other sources
548            // of aliasing, $wgNamespaceAliases wins.
549            $this->namespaceAliases = $this->config->get( MainConfigNames::NamespaceAliases ) +
550                $this->namespaceAliases;
551
552            # Filter out aliases to namespaces that don't exist, e.g. from extensions
553            # that aren't loaded here but are included in the l10n cache.
554            # (array_intersect preserves keys from its first argument)
555            $this->namespaceAliases = array_intersect(
556                $this->namespaceAliases,
557                array_keys( $this->getNamespaces() )
558            );
559        }
560
561        return $this->namespaceAliases;
562    }
563
564    /**
565     * @return array<string,int> indexed by localized lower-cased namespace name
566     */
567    public function getNamespaceIds() {
568        if ( $this->mNamespaceIds === null ) {
569            # Put namespace names and aliases into a hashtable.
570            # If this is too slow, then we should arrange it so that it is done
571            # before caching. The catch is that at pre-cache time, the above
572            # class-specific fixup hasn't been done.
573            $this->mNamespaceIds = [];
574            foreach ( $this->getNamespaces() as $index => $name ) {
575                $this->mNamespaceIds[$this->lc( $name )] = $index;
576            }
577            foreach ( $this->getNamespaceAliases() as $name => $index ) {
578                $this->mNamespaceIds[$this->lc( $name )] = $index;
579            }
580        }
581        return $this->mNamespaceIds;
582    }
583
584    /**
585     * Get a namespace key by case-insensitive value. Canonical namespace
586     * names override custom ones defined for the current language.
587     *
588     * @param string $text
589     * @return int|false An integer if $text is a valid value otherwise false
590     */
591    public function getNsIndex( $text ) {
592        $lctext = $this->lc( $text );
593        $ns = $this->namespaceInfo->getCanonicalIndex( $lctext );
594        if ( $ns !== null ) {
595            return $ns;
596        }
597        $ids = $this->getNamespaceIds();
598        return $ids[$lctext] ?? false;
599    }
600
601    /**
602     * Short names for language variants used for language conversion links.
603     *
604     * @param string $code
605     * @param bool $usemsg Use the "variantname-xyz" message if it exists
606     * @return string
607     */
608    public function getVariantname( $code, $usemsg = true ) {
609        if ( $usemsg ) {
610            $msg = $this->msg( "variantname-$code" );
611            if ( $msg->exists() ) {
612                return $msg->text();
613            }
614        }
615        $name = $this->langNameUtils->getLanguageName( $code );
616        if ( $name ) {
617            return $name; # if it's defined as a language name, show that
618        } else {
619            # otherwise, output the language code
620            return $code;
621        }
622    }
623
624    /**
625     * @return string[]|false List of date format preference keys, or false if disabled.
626     */
627    public function getDatePreferences() {
628        return $this->localisationCache->getItem( $this->mCode, 'datePreferences' );
629    }
630
631    /**
632     * @return string[]
633     */
634    public function getDateFormats() {
635        return $this->localisationCache->getItem( $this->mCode, 'dateFormats' );
636    }
637
638    /**
639     * @return string
640     */
641    public function getDefaultDateFormat() {
642        $df = $this->localisationCache->getItem( $this->mCode, 'defaultDateFormat' );
643        if ( $df === 'dmy or mdy' ) {
644            return $this->config->get( MainConfigNames::AmericanDates ) ? 'mdy' : 'dmy';
645        } else {
646            return $df;
647        }
648    }
649
650    /**
651     * @return string[]
652     */
653    public function getDatePreferenceMigrationMap() {
654        return $this->localisationCache->getItem( $this->mCode, 'datePreferenceMigrationMap' );
655    }
656
657    /**
658     * Get a message from the MediaWiki namespace.
659     *
660     * @param string $msg Message name
661     * @return string
662     */
663    public function getMessageFromDB( $msg ) {
664        return $this->msg( $msg )->text();
665    }
666
667    /**
668     * Gets the Message object from this language. Only for use inside this class.
669     *
670     * @param string $msg Message name
671     * @phpcs:ignore Generic.Files.LineLength
672     * @param MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float> ...$params
673     *   See Message::params()
674     * @return Message
675     */
676    protected function msg( $msg, ...$params ) {
677        return wfMessage( $msg, ...$params )->inLanguage( $this );
678    }
679
680    /**
681     * @param int $key Number from 1 to 12
682     * @return string
683     */
684    public function getMonthName( $key ) {
685        return $this->getMessageFromDB( self::MONTH_MESSAGES[$key - 1] );
686    }
687
688    /**
689     * @return string[] Indexed from 0 to 11
690     */
691    public function getMonthNamesArray() {
692        $monthNames = [ '' ];
693        for ( $i = 1; $i <= 12; $i++ ) {
694            $monthNames[] = $this->getMonthName( $i );
695        }
696        return $monthNames;
697    }
698
699    /**
700     * @param int $key Number from 1 to 12
701     * @return string
702     */
703    public function getMonthNameGen( $key ) {
704        return $this->getMessageFromDB( self::MONTH_GENITIVE_MESSAGES[$key - 1] );
705    }
706
707    /**
708     * @param int $key Number from 1 to 12
709     * @return string
710     */
711    public function getMonthAbbreviation( $key ) {
712        return $this->getMessageFromDB( self::MONTH_ABBREVIATED_MESSAGES[$key - 1] );
713    }
714
715    /**
716     * @return string[] Indexed from 0 to 11
717     */
718    public function getMonthAbbreviationsArray() {
719        $monthNames = [ '' ];
720        for ( $i = 1; $i <= 12; $i++ ) {
721            $monthNames[] = $this->getMonthAbbreviation( $i );
722        }
723        return $monthNames;
724    }
725
726    /**
727     * @param int $key Number from 1 to 7
728     * @return string
729     */
730    public function getWeekdayName( $key ) {
731        return $this->getMessageFromDB( self::WEEKDAY_MESSAGES[$key - 1] );
732    }
733
734    /**
735     * @param int $key Number from 1 to 7
736     * @return string
737     */
738    public function getWeekdayAbbreviation( $key ) {
739        return $this->getMessageFromDB( self::WEEKDAY_ABBREVIATED_MESSAGES[$key - 1] );
740    }
741
742    /**
743     * Create a DateTime object, if it doesn't already exist.
744     *
745     * @param DateTime|false|null &$dateTimeObj
746     * @param string $ts
747     * @param DateTimeZone|false|null $zone
748     * @return DateTime
749     */
750    private static function dateTimeObj( &$dateTimeObj, $ts, $zone ): DateTime {
751        if ( !$dateTimeObj ) {
752            $dateTimeObj = DateTime::createFromFormat(
753                'YmdHis', $ts, $zone ?: new DateTimeZone( 'UTC' )
754            );
755        }
756        return $dateTimeObj;
757    }
758
759    /**
760     * Pass through the result from $dateTimeObj->format()
761     *
762     * @param DateTime|false|null &$dateTimeObj
763     * @param string $ts
764     * @param DateTimeZone|false|null $zone
765     * @param string $code
766     * @return string
767     */
768    private static function dateTimeObjFormat( &$dateTimeObj, $ts, $zone, $code ) {
769        return self::dateTimeObj( $dateTimeObj, $ts, $zone )->format( $code );
770    }
771
772    /**
773     * This is a workalike of PHP's date() function, but with better
774     * internationalisation, a reduced set of format characters, and a better
775     * escaping format.
776     *
777     * Supported format characters are dDjlNwzWFmMntLoYyaAgGhHiscrUeIOPTZ. See
778     * the PHP manual for definitions. There are a number of extensions, which
779     * start with "x":
780     *
781     *    xn   Do not translate digits of the next numeric format character
782     *    xN   Toggle raw digit (xn) flag, stays set until explicitly unset
783     *    xr   Use roman numerals for the next numeric format character
784     *    xh   Use hebrew numerals for the next numeric format character
785     *    xx   Literal x
786     *    xg   Genitive month name
787     *
788     *    xij  j (day number) in Iranian calendar
789     *    xiF  F (month name) in Iranian calendar
790     *    xin  n (month number) in Iranian calendar
791     *    xiy  y (two digit year) in Iranian calendar
792     *    xiY  Y (full year) in Iranian calendar
793     *    xit  t (days in month) in Iranian calendar
794     *    xiz  z (day of the year) in Iranian calendar
795     *
796     *    xjj  j (day number) in Hebrew calendar
797     *    xjF  F (month name) in Hebrew calendar
798     *    xjt  t (days in month) in Hebrew calendar
799     *    xjx  xg (genitive month name) in Hebrew calendar
800     *    xjn  n (month number) in Hebrew calendar
801     *    xjY  Y (full year) in Hebrew calendar
802     *
803     *    xmj  j (day number) in Hijri calendar
804     *    xmF  F (month name) in Hijri calendar
805     *    xmn  n (month number) in Hijri calendar
806     *    xmY  Y (full year) in Hijri calendar
807     *
808     *    xkY  Y (full year) in Thai solar calendar. Months and days are
809     *                       identical to the Gregorian calendar
810     *    xoY  Y (full year) in Minguo calendar or Juche year.
811     *                       Months and days are identical to the
812     *                       Gregorian calendar
813     *    xtY  Y (full year) in Japanese nengo. Months and days are
814     *                       identical to the Gregorian calendar
815     *
816     * Characters enclosed in double quotes will be considered literal (with
817     * the quotes themselves removed). Unmatched quotes will be considered
818     * literal quotes. Example:
819     *
820     * "The month is" F       => The month is January
821     * i's"                   => 20'11"
822     *
823     * Backslash escaping is also supported.
824     *
825     * Input timestamp is assumed to be pre-normalized to the desired local
826     * time zone, if any. Note that the format characters crUeIOPTZ will assume
827     * $ts is UTC if $zone is not given.
828     *
829     * @param string $format
830     * @param string $ts 14-character timestamp
831     *      YYYYMMDDHHMMSS
832     *      01234567890123
833     * @param DateTimeZone|null $zone Timezone of $ts
834     * @param int|null &$ttl The amount of time (in seconds) the output may be cached for.
835     *   Only makes sense if $ts is the current time.
836     * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
837     *
838     * @return string
839     * @return-taint tainted
840     */
841    public function sprintfDate( $format, $ts, ?DateTimeZone $zone = null, &$ttl = 'unused' ) {
842        // @phan-suppress-previous-line PhanTypeMismatchDefault Type mismatch on pass-by-ref args
843        $s = '';
844        $raw = false;
845        $roman = false;
846        $hebrewNum = false;
847        $dateTimeObj = false;
848        $rawToggle = false;
849        $iranian = false;
850        $hebrew = false;
851        $hijri = false;
852        $thai = false;
853        $minguo = false;
854        $tenno = false;
855
856        $usedSecond = false;
857        $usedMinute = false;
858        $usedHour = false;
859        $usedAMPM = false;
860        $usedDay = false;
861        $usedWeek = false;
862        $usedMonth = false;
863        $usedYear = false;
864        $usedISOYear = false;
865        $usedIsLeapYear = false;
866
867        $usedHebrewMonth = false;
868        $usedIranianMonth = false;
869        $usedHijriMonth = false;
870        $usedHebrewYear = false;
871        $usedIranianYear = false;
872        $usedHijriYear = false;
873        $usedTennoYear = false;
874
875        if ( strlen( $ts ) !== 14 ) {
876            throw new InvalidArgumentException( __METHOD__ . ": The timestamp $ts should have 14 characters" );
877        }
878
879        if ( !ctype_digit( $ts ) ) {
880            throw new InvalidArgumentException( __METHOD__ . ": The timestamp $ts should be a number" );
881        }
882
883        $formatLength = strlen( $format );
884        for ( $p = 0; $p < $formatLength; $p++ ) {
885            $num = false;
886            $code = $format[$p];
887            if ( $code == 'x' && $p < $formatLength - 1 ) {
888                $code .= $format[++$p];
889            }
890
891            if ( ( $code === 'xi'
892                    || $code === 'xj'
893                    || $code === 'xk'
894                    || $code === 'xm'
895                    || $code === 'xo'
896                    || $code === 'xt' )
897                && $p < $formatLength - 1
898            ) {
899                $code .= $format[++$p];
900            }
901
902            switch ( $code ) {
903                case 'xx':
904                    $s .= 'x';
905                    break;
906
907                case 'xn':
908                    $raw = true;
909                    break;
910
911                case 'xN':
912                    $rawToggle = !$rawToggle;
913                    break;
914
915                case 'xr':
916                    $roman = true;
917                    break;
918
919                case 'xh':
920                    $hebrewNum = true;
921                    break;
922
923                case 'xg':
924                    $usedMonth = true;
925                    $s .= $this->getMonthNameGen( (int)substr( $ts, 4, 2 ) );
926                    break;
927
928                case 'xjx':
929                    $usedHebrewMonth = true;
930                    if ( !$hebrew ) {
931                        $hebrew = self::tsToHebrew( $ts );
932                    }
933                    $s .= $this->getMessageFromDB( self::HEBREW_CALENDAR_MONTH_GENITIVE_MESSAGES[$hebrew[1] - 1] );
934                    break;
935
936                case 'd':
937                    $usedDay = true;
938                    $num = substr( $ts, 6, 2 );
939                    break;
940
941                case 'D':
942                    $usedDay = true;
943                    $s .= $this->getWeekdayAbbreviation(
944                        (int)self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, 'w' ) + 1
945                    );
946                    break;
947
948                case 'j':
949                    $usedDay = true;
950                    $num = intval( substr( $ts, 6, 2 ) );
951                    break;
952
953                case 'xij':
954                    $usedDay = true;
955                    if ( !$iranian ) {
956                        $iranian = self::tsToIranian( $ts );
957                    }
958                    $num = $iranian[2];
959                    break;
960
961                case 'xmj':
962                    $usedDay = true;
963                    if ( !$hijri ) {
964                        $hijri = self::tsToHijri( $ts );
965                    }
966                    $num = $hijri[2];
967                    break;
968
969                case 'xjj':
970                    $usedDay = true;
971                    if ( !$hebrew ) {
972                        $hebrew = self::tsToHebrew( $ts );
973                    }
974                    $num = $hebrew[2];
975                    break;
976
977                case 'l':
978                    $usedDay = true;
979                    $s .= $this->getWeekdayName(
980                        (int)self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, 'w' ) + 1
981                    );
982                    break;
983
984                case 'F':
985                    $usedMonth = true;
986                    $s .= $this->getMonthName( (int)substr( $ts, 4, 2 ) );
987                    break;
988
989                case 'xiF':
990                    $usedIranianMonth = true;
991                    if ( !$iranian ) {
992                        $iranian = self::tsToIranian( $ts );
993                    }
994                    $s .= $this->getMessageFromDB( self::IRANIAN_CALENDAR_MONTHS_MESSAGES[$iranian[1] - 1] );
995                    break;
996
997                case 'xmF':
998                    $usedHijriMonth = true;
999                    if ( !$hijri ) {
1000                        $hijri = self::tsToHijri( $ts );
1001                    }
1002                    $s .= $this->getMessageFromDB( self::HIJRI_CALENDAR_MONTH_MESSAGES[$hijri[1] - 1] );
1003                    break;
1004
1005                case 'xjF':
1006                    $usedHebrewMonth = true;
1007                    if ( !$hebrew ) {
1008                        $hebrew = self::tsToHebrew( $ts );
1009                    }
1010                    $s .= $this->getMessageFromDB( self::HEBREW_CALENDAR_MONTHS_MESSAGES[$hebrew[1] - 1] );
1011                    break;
1012
1013                case 'm':
1014                    $usedMonth = true;
1015                    $num = substr( $ts, 4, 2 );
1016                    break;
1017
1018                case 'M':
1019                    $usedMonth = true;
1020                    $s .= $this->getMonthAbbreviation( (int)substr( $ts, 4, 2 ) );
1021                    break;
1022
1023                case 'n':
1024                    $usedMonth = true;
1025                    $num = intval( substr( $ts, 4, 2 ) );
1026                    break;
1027
1028                case 'xin':
1029                    $usedIranianMonth = true;
1030                    if ( !$iranian ) {
1031                        $iranian = self::tsToIranian( $ts );
1032                    }
1033                    $num = $iranian[1];
1034                    break;
1035
1036                case 'xmn':
1037                    $usedHijriMonth = true;
1038                    if ( !$hijri ) {
1039                        $hijri = self::tsToHijri( $ts );
1040                    }
1041                    $num = $hijri[1];
1042                    break;
1043
1044                case 'xjn':
1045                    $usedHebrewMonth = true;
1046                    if ( !$hebrew ) {
1047                        $hebrew = self::tsToHebrew( $ts );
1048                    }
1049                    $num = $hebrew[1];
1050                    break;
1051
1052                case 'xjt':
1053                    $usedHebrewMonth = true;
1054                    if ( !$hebrew ) {
1055                        $hebrew = self::tsToHebrew( $ts );
1056                    }
1057                    $num = $hebrew[3];
1058                    break;
1059
1060                case 'Y':
1061                    $usedYear = true;
1062                    $num = substr( $ts, 0, 4 );
1063                    break;
1064
1065                case 'xiY':
1066                    $usedIranianYear = true;
1067                    if ( !$iranian ) {
1068                        $iranian = self::tsToIranian( $ts );
1069                    }
1070                    $num = $iranian[0];
1071                    break;
1072
1073                case 'xmY':
1074                    $usedHijriYear = true;
1075                    if ( !$hijri ) {
1076                        $hijri = self::tsToHijri( $ts );
1077                    }
1078                    $num = $hijri[0];
1079                    break;
1080
1081                case 'xjY':
1082                    $usedHebrewYear = true;
1083                    if ( !$hebrew ) {
1084                        $hebrew = self::tsToHebrew( $ts );
1085                    }
1086                    $num = $hebrew[0];
1087                    break;
1088
1089                case 'xkY':
1090                    $usedYear = true;
1091                    if ( !$thai ) {
1092                        $thai = self::tsToYear( $ts, 'thai' );
1093                    }
1094                    $num = $thai[0];
1095                    break;
1096
1097                case 'xoY':
1098                    $usedYear = true;
1099                    if ( !$minguo ) {
1100                        $minguo = self::tsToYear( $ts, 'minguo' );
1101                    }
1102                    $num = $minguo[0];
1103                    break;
1104
1105                case 'xtY':
1106                    $usedTennoYear = true;
1107                    if ( !$tenno ) {
1108                        $tenno = self::tsToJapaneseGengo( $ts );
1109                    }
1110                    $num = $tenno;
1111                    break;
1112
1113                case 'y':
1114                    $usedYear = true;
1115                    $num = substr( $ts, 2, 2 );
1116                    break;
1117
1118                case 'xiy':
1119                    $usedIranianYear = true;
1120                    if ( !$iranian ) {
1121                        $iranian = self::tsToIranian( $ts );
1122                    }
1123                    $num = substr( (string)$iranian[0], -2 );
1124                    break;
1125
1126                case 'xit':
1127                    $usedIranianYear = true;
1128                    if ( !$iranian ) {
1129                        $iranian = self::tsToIranian( $ts );
1130                    }
1131                    $num = self::IRANIAN_DAYS[$iranian[1] - 1];
1132                    break;
1133
1134                case 'xiz':
1135                    $usedIranianYear = true;
1136                    if ( !$iranian ) {
1137                        $iranian = self::tsToIranian( $ts );
1138                    }
1139                    $num = $iranian[3];
1140                    break;
1141
1142                case 'a':
1143                    $usedAMPM = true;
1144                    $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
1145                    break;
1146
1147                case 'A':
1148                    $usedAMPM = true;
1149                    $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'AM' : 'PM';
1150                    break;
1151
1152                case 'g':
1153                    $usedHour = true;
1154                    $h = (int)substr( $ts, 8, 2 );
1155                    $num = $h % 12 ?: 12;
1156                    break;
1157
1158                case 'G':
1159                    $usedHour = true;
1160                    $num = intval( substr( $ts, 8, 2 ) );
1161                    break;
1162
1163                case 'h':
1164                    $usedHour = true;
1165                    $h = (int)substr( $ts, 8, 2 );
1166                    $num = sprintf( '%02d', $h % 12 ?: 12 );
1167                    break;
1168
1169                case 'H':
1170                    $usedHour = true;
1171                    $num = substr( $ts, 8, 2 );
1172                    break;
1173
1174                case 'i':
1175                    $usedMinute = true;
1176                    $num = substr( $ts, 10, 2 );
1177                    break;
1178
1179                case 's':
1180                    $usedSecond = true;
1181                    $num = substr( $ts, 12, 2 );
1182                    break;
1183
1184                case 'c':
1185                case 'r':
1186                    $usedSecond = true;
1187                    // fall through
1188                case 'e':
1189                case 'O':
1190                case 'P':
1191                case 'T':
1192                    $s .= self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, $code );
1193                    break;
1194
1195                case 'w':
1196                case 'N':
1197                case 'z':
1198                    $usedDay = true;
1199                    $num = self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, $code );
1200                    break;
1201
1202                case 'W':
1203                    $usedWeek = true;
1204                    $num = self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, $code );
1205                    break;
1206
1207                case 't':
1208                    $usedMonth = true;
1209                    $num = self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, $code );
1210                    break;
1211
1212                case 'L':
1213                    $usedIsLeapYear = true;
1214                    $num = self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, $code );
1215                    break;
1216
1217                case 'o':
1218                    $usedISOYear = true;
1219                    $num = self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, $code );
1220                    break;
1221
1222                case 'U':
1223                    $usedSecond = true;
1224                    // fall through
1225                case 'I':
1226                case 'Z':
1227                    $num = self::dateTimeObjFormat( $dateTimeObj, $ts, $zone, $code );
1228                    break;
1229
1230                case '\\':
1231                    # Backslash escaping
1232                    if ( $p < $formatLength - 1 ) {
1233                        $s .= $format[++$p];
1234                    } else {
1235                        $s .= '\\';
1236                    }
1237                    break;
1238
1239                case '"':
1240                    # Quoted literal
1241                    if ( $p < $formatLength - 1 ) {
1242                        $endQuote = strpos( $format, '"', $p + 1 );
1243                        if ( $endQuote === false ) {
1244                            # No terminating quote, assume literal "
1245                            $s .= '"';
1246                        } else {
1247                            $s .= substr( $format, $p + 1, $endQuote - $p - 1 );
1248                            $p = $endQuote;
1249                        }
1250                    } else {
1251                        # Quote at the end of the string, assume literal "
1252                        $s .= '"';
1253                    }
1254                    break;
1255
1256                default:
1257                    $s .= $format[$p];
1258            }
1259            if ( $num !== false ) {
1260                if ( $rawToggle || $raw ) {
1261                    $s .= $num;
1262                    $raw = false;
1263                } elseif ( $roman ) {
1264                    $s .= self::romanNumeral( $num );
1265                    $roman = false;
1266                } elseif ( $hebrewNum ) {
1267                    $s .= self::hebrewNumeral( $num );
1268                    $hebrewNum = false;
1269                } elseif ( preg_match( '/^[\d.]+$/', $num ) ) {
1270                    $s .= $this->formatNumNoSeparators( $num );
1271                } else {
1272                    $s .= $num;
1273                }
1274            }
1275        }
1276
1277        if ( $ttl !== 'unused' ) {
1278            // Ensure $dateTimeObj is set.
1279            self::dateTimeObj( $dateTimeObj, $ts, $zone );
1280        }
1281        '@phan-var DateTime $dateTimeObj';
1282        if ( $ttl === 'unused' ) {
1283            // No need to calculate the TTL, the caller won't use it anyway.
1284        } elseif ( $usedSecond ) {
1285            $ttl = self::computeUnitTimestampDeadline( $dateTimeObj, 's' );
1286        } elseif ( $usedMinute ) {
1287            $ttl = self::computeUnitTimestampDeadline( $dateTimeObj, 'm' );
1288        } elseif ( $usedHour ) {
1289            $ttl = self::computeUnitTimestampDeadline( $dateTimeObj, 'H' );
1290        } elseif ( $usedAMPM ) {
1291            $ttl = self::computeUnitTimestampDeadline( $dateTimeObj, 'AM' );
1292        } elseif (
1293            $usedDay ||
1294            $usedHebrewMonth ||
1295            $usedIranianMonth ||
1296            $usedHijriMonth ||
1297            $usedHebrewYear ||
1298            $usedIranianYear ||
1299            $usedHijriYear ||
1300            $usedTennoYear
1301        ) {
1302            // @todo Someone who understands the non-Gregorian calendars
1303            // should write proper logic for them so that they don't need purged every day.
1304            $ttl = self::computeUnitTimestampDeadline( $dateTimeObj, 'D' );
1305        } else {
1306            $possibleTtls = [];
1307            if ( $usedWeek ) {
1308                $possibleTtls[] =
1309                    self::computeUnitTimestampDeadline( $dateTimeObj, 'W' );
1310            } elseif ( $usedISOYear ) {
1311                // December 28th falls on the last ISO week of the year, every year.
1312                // The last ISO week of a year can be 52 or 53.
1313                $possibleTtls[] =
1314                    self::computeUnitTimestampDeadline( $dateTimeObj, 'ISOY' );
1315            }
1316
1317            if ( $usedMonth ) {
1318                $possibleTtls[] =
1319                    self::computeUnitTimestampDeadline( $dateTimeObj, 'M' );
1320            } elseif ( $usedYear ) {
1321                $possibleTtls[] =
1322                    self::computeUnitTimestampDeadline( $dateTimeObj, 'Y' );
1323            } elseif ( $usedIsLeapYear ) {
1324                $year = (int)substr( $ts, 0, 4 );
1325                $mod = $year % 4;
1326                $timeRemainingInYear =
1327                    self::computeUnitTimestampDeadline( $dateTimeObj, 'Y' );
1328                if ( $mod || ( !( $year % 100 ) && $year % 400 ) ) {
1329                    // this isn't a leap year. see when the next one starts
1330                    $nextCandidate = $year - $mod + 4;
1331                    if ( $nextCandidate % 100 || !( $nextCandidate % 400 ) ) {
1332                        $possibleTtls[] = ( $nextCandidate - $year - 1 ) * 365 * 86400 +
1333                            $timeRemainingInYear;
1334                    } else {
1335                        $possibleTtls[] = ( $nextCandidate - $year + 3 ) * 365 * 86400 +
1336                            $timeRemainingInYear;
1337                    }
1338                } else {
1339                    // this is a leap year, so the next year isn't
1340                    $possibleTtls[] = $timeRemainingInYear;
1341                }
1342            }
1343
1344            if ( $possibleTtls ) {
1345                $ttl = min( $possibleTtls );
1346            }
1347        }
1348
1349        return $s;
1350    }
1351
1352    /**
1353     * Compute a cache expiry to account for a dynamic timestamp displayed in output
1354     *
1355     * @param DateTimeInterface $date Current timestamp with the display timezone
1356     * @param string $unit The unit the timestamp is expressed in; one of ("Y", "M", "W", "D", "H", "ISOY" or "AM")
1357     * @return int TTL in seconds, staggered and limited to prevent expiry
1358     *  stampedes.
1359     * @since 1.46
1360     */
1361    public static function computeUnitTimestampDeadline(
1362        DateTimeInterface $date,
1363        string $unit
1364    ): int {
1365        // Ensure we don't mutate our argument
1366        $date = DateTimeImmutable::createFromInterface( $date );
1367        $tsUnix = $date->getTimestamp();
1368
1369        $date = $date->modify( self::DEADLINE_DATE_SPEC_BY_UNIT[$unit] );
1370        if ( $unit === 'AM' ) {
1371            if ( $date->getTimestamp() <= $tsUnix ) {
1372                $date = $date->modify( self::DEADLINE_DATE_SPEC_BY_UNIT['D'] );
1373            }
1374        } elseif ( $unit === 'ISOY' ) {
1375            // December 28th falls on the last ISO week of the year, every year.
1376            // but we could be in the same, next, or previous ISO year as
1377            // December 28 of this calendar year.
1378            // Compute all three and use the first which is in the future.
1379            $lastYear = $date->modify(
1380                'december 28 midnight last year'
1381            )->modify( 'monday next week midnight' );
1382            $nextYear = $date->modify(
1383                'december 28 midnight next year'
1384            )->modify( 'monday next week midnight' );
1385            // Advance to first week of next ISO year
1386            $date = $date->modify( 'monday next week midnight' );
1387            if ( $lastYear->getTimestamp() > $tsUnix ) {
1388                $date = $lastYear;
1389            } elseif ( $date->getTimestamp() <= $tsUnix ) {
1390                $date = $nextYear;
1391            }
1392        } elseif ( $unit === 'H' ) {
1393            // Zero out the minutes/seconds
1394            $date = $date->setTime( intval( $date->format( 'H' ), 10 ), 0, 0 );
1395        } elseif ( $unit === 'm' ) {
1396            // Zero out the seconds
1397            $date = $date->setTime( intval( $date->format( 'H' ), 10 ),
1398                                    intval( $date->format( 'i' ), 10 ), 0 );
1399        } elseif ( $unit === 's' ) {
1400            // Zero out the fractional seconds
1401            $date = $date->setTime( intval( $date->format( 'H' ), 10 ),
1402                                    intval( $date->format( 'i' ), 10 ),
1403                                    intval( $date->format( 's' ), 10 ) );
1404        } else {
1405            $date = $date->setTime( 0, 0, 0 );
1406        }
1407        $deadlineUnix = (int)$date->format( 'U' );