Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.69% covered (warning)
84.69%
83 / 98
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
IcuCollation
84.69% covered (warning)
84.69%
83 / 98
28.57% covered (danger)
28.57%
2 / 7
36.91
0.00% covered (danger)
0.00%
0 / 1
 __construct
64.29% covered (warning)
64.29%
9 / 14
0.00% covered (danger)
0.00%
0 / 1
4.73
 getSortKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFirstLetter
79.17% covered (warning)
79.17%
19 / 24
0.00% covered (danger)
0.00%
0 / 1
8.58
 getPrimarySortKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFirstLetterData
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 fetchFirstLetterData
92.31% covered (success)
92.31%
36 / 39
0.00% covered (danger)
0.00%
0 / 1
13.08
 isCjk
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
4.25
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21use MediaWiki\Language\Language;
22use MediaWiki\Languages\LanguageFactory;
23use MediaWiki\MediaWikiServices;
24use Wikimedia\ArrayUtils\ArrayUtils;
25
26/**
27 * @since 1.16.3
28 */
29class IcuCollation extends Collation {
30    private const FIRST_LETTER_VERSION = 4;
31
32    /** @var Collator */
33    private $primaryCollator;
34
35    /** @var Collator */
36    private $mainCollator;
37
38    /** @var string */
39    private $locale;
40
41    /** @var Language */
42    protected $digitTransformLanguage;
43
44    /** @var bool */
45    private $useNumericCollation = false;
46
47    /** @var array */
48    private $firstLetterData;
49
50    /**
51     * Unified CJK blocks.
52     *
53     * The same definition of a CJK block must be used for both Collation and
54     * generateCollationData.php. These blocks are omitted from the first
55     * letter data, as an optimisation measure and because the default UCA table
56     * is pretty useless for sorting Chinese text anyway. Japanese and Korean
57     * blocks are not included here, because they are smaller and more useful.
58     */
59    private const CJK_BLOCKS = [
60        [ 0x2E80, 0x2EFF ], // CJK Radicals Supplement
61        [ 0x2F00, 0x2FDF ], // Kangxi Radicals
62        [ 0x2FF0, 0x2FFF ], // Ideographic Description Characters
63        [ 0x3000, 0x303F ], // CJK Symbols and Punctuation
64        [ 0x31C0, 0x31EF ], // CJK Strokes
65        [ 0x3200, 0x32FF ], // Enclosed CJK Letters and Months
66        [ 0x3300, 0x33FF ], // CJK Compatibility
67        [ 0x3400, 0x4DBF ], // CJK Unified Ideographs Extension A
68        [ 0x4E00, 0x9FFF ], // CJK Unified Ideographs
69        [ 0xF900, 0xFAFF ], // CJK Compatibility Ideographs
70        [ 0xFE30, 0xFE4F ], // CJK Compatibility Forms
71        [ 0x20000, 0x2A6DF ], // CJK Unified Ideographs Extension B
72        [ 0x2A700, 0x2B73F ], // CJK Unified Ideographs Extension C
73        [ 0x2B740, 0x2B81F ], // CJK Unified Ideographs Extension D
74        [ 0x2F800, 0x2FA1F ], // CJK Compatibility Ideographs Supplement
75    ];
76
77    /**
78     * Additional characters (or character groups) to be considered separate
79     * letters for given languages, or to be removed from the list of such
80     * letters (denoted by keys starting with '-').
81     *
82     * These are additions to (or subtractions from) the data stored in the
83     * first-letters-root.php data file (which among others includes full basic
84     * Latin, Cyrillic and Greek alphabets).
85     *
86     * "Separate letter" is a letter that would have a separate heading/section
87     * for it in a dictionary or a phone book in this language. This data isn't
88     * used for sorting (the ICU library handles that), only for deciding which
89     * characters (or character groups) to use as headings.
90     *
91     * Initially generated based on the primary level of Unicode collation
92     * tailorings available at http://developer.mimer.com/charts/tailorings.htm ,
93     * later modified.
94     *
95     * Empty arrays are intended; this signifies that the data for the language is
96     * available and that there are, in fact, no additional letters to consider.
97     */
98    private const TAILORING_FIRST_LETTERS = [
99        'af' => [],
100        'am' => [],
101        'ar' => [],
102        'as' => [ "\u{0982}", "\u{0981}", "\u{0983}", "\u{09CE}", "ক্ষ " ],
103        'ast' => [ "Ch", "Ll", "Ñ" ], // not in libicu
104        'az' => [ "Ç", "Ə", "Ğ", "İ", "Ö", "Ş", "Ü" ],
105        'be' => [ "Ё" ],
106        'be-tarask' => [ "Ё" ],
107        'bg' => [],
108        'bn' => [ 'ং', 'ঃ', 'ঁ' ],
109        'bn@collation=traditional' => [
110            'ং', 'ঃ', 'ঁ', 'ক্', 'খ্', 'গ্', 'ঘ্', 'ঙ্', 'চ্', 'ছ্', 'জ্', 'ঝ্',
111            'ঞ্', 'ট্', 'ঠ্', 'ড্', 'ঢ্', 'ণ্', 'ৎ', 'থ্', 'দ্', 'ধ্', 'ন্', 'প্',
112            'ফ্', 'ব্', 'ভ্', 'ম্', 'য্', 'র্', 'ৰ্', 'ল্', 'ৱ্', 'শ্', 'ষ্', 'স্', 'হ্'
113        ],
114        'bo' => [],
115        'br' => [ "Ch", "C'h" ],
116        'bs' => [ "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ],
117        'bs-Cyrl' => [],
118        'ca' => [],
119        'chr' => [],
120        'co' => [], // not in libicu
121        'cs' => [ "Č", "Ch", "Ř", "Š", "Ž" ],
122        'cy' => [ "Ch", "Dd", "Ff", "Ng", "Ll", "Ph", "Rh", "Th" ],
123        'da' => [ "Æ", "Ø", "Å" ],
124        'de' => [],
125        'de-AT@collation=phonebook' => [ 'ä', 'ö', 'ü', 'ß' ],
126        'dsb' => [ "Č", "Ć", "Dź", "Ě", "Ch", "Ł", "Ń", "Ŕ", "Š", "Ś", "Ž", "Ź" ],
127        'ee' => [ "Dz", "Ɖ", "Ɛ", "Ƒ", "Gb", "Ɣ", "Kp", "Ny", "Ŋ", "Ɔ", "Ts", "Ʋ" ],
128        'el' => [],
129        'en' => [],
130        'eo' => [ "Ĉ", "Ĝ", "Ĥ", "Ĵ", "Ŝ", "Ŭ" ],
131        'es' => [ "Ñ" ],
132        'et' => [ "Š", "Ž", "Õ", "Ä", "Ö", "Ü" ],
133        'eu' => [ "Ñ" ], // not in libicu
134        'fa' => [
135            // RTL, let's put each letter on a new line
136            "آ",
137            "ء",
138            "ه",
139            "ا",
140            "و"
141        ],
142        'fi' => [ "Å", "Ä", "Ö" ],
143        'fil' => [ "Ñ", "Ng" ],
144        'fo' => [ "Á", "Ð", "Í", "Ó", "Ú", "Ý", "Æ", "Ø", "Å" ],
145        'fr' => [],
146        'fr-CA' => [], // fr-CA sorts accents slightly different from fr.
147        'fur' => [ "À", "Á", "Â", "È", "Ì", "Ò", "Ù" ], // not in libicu
148        'fy' => [], // not in libicu
149        'ga' => [],
150        'gd' => [], // not in libicu
151        'gl' => [ "Ch", "Ll", "Ñ" ],
152        'gu' => [ "\u{0A82}", "\u{0A83}", "\u{0A81}", "\u{0AB3}" ],
153        'ha' => [ 'Ɓ', 'Ɗ', 'Ƙ', 'Sh', 'Ts', 'Ƴ' ],
154        'haw' => [ 'ʻ' ],
155        'he' => [],
156        'hi' => [ "\u{0902}", "\u{0903}" ],
157        'hr' => [ "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ],
158        'hsb' => [ "Č", "Dź", "Ě", "Ch", "Ł", "Ń", "Ř", "Š", "Ć", "Ž" ],
159        'hu' => [ "Cs", "Dz", "Dzs", "Gy", "Ly", "Ny", "Ö", "Sz", "Ty", "Ü", "Zs" ],
160        'hy' => [ "և" ],
161        'id' => [],
162        'ig' => [ "Ch", "Gb", "Gh", "Gw", "Ị", "Kp", "Kw", "Ṅ", "Nw", "Ny", "Ọ", "Sh", "Ụ" ],
163        'is' => [ "Á", "Ð", "É", "Í", "Ó", "Ú", "Ý", "Þ", "Æ", "Ö", "Å" ],
164        'it' => [],
165        'ka' => [],
166        'kk' => [ "Ё", "Ү", "І" ],
167        'kl' => [ "Æ", "Ø", "Å" ],
168        'km' => [
169            "រ", "ឫ", "ឬ", "ល", "ឭ", "ឮ", "\u{17BB}\u{17C6}",
170            "\u{17C6}", "\u{17B6}\u{17C6}", "\u{17C7}",
171            "\u{17B7}\u{17C7}", "\u{17BB}\u{17C7}",
172            "\u{17C1}\u{17C7}", "\u{17C4}\u{17C7}",
173        ],
174        'kn' => [ "\u{0C81}", "\u{0C83}", "\u{0CF1}", "\u{0CF2}" ],
175        'kok' => [ "\u{0902}", "\u{0903}", "ळ", "क्ष" ],
176        'ku' => [ "Ç", "Ê", "Î", "Ş", "Û" ], // not in libicu
177        'ky' => [ "Ё" ],
178        'la' => [], // not in libicu
179        'lb' => [],
180        'lkt' => [ 'Č', 'Ǧ', 'Ȟ', 'Š', 'Ž' ],
181        'ln' => [ 'Ɛ' ],
182        'lo' => [],
183        'lt' => [ "Č", "Š", "Ž" ],
184        'lv' => [ "Č", "Ģ", "Ķ", "Ļ", "Ņ", "Š", "Ž" ],
185        'mk' => [ "Ѓ", "Ќ" ],
186        'ml' => [],
187        'mn' => [],
188        'mo' => [ "Ă", "Â", "Î", "Ș", "Ț" ], // not in libicu
189        'mr' => [ "\u{0902}", "\u{0903}", "ळ", "क्ष", "ज्ञ" ],
190        'ms' => [],
191        'mt' => [ "Ċ", "Ġ", "Għ", "Ħ", "Ż" ],
192        'nb' => [ "Æ", "Ø", "Å" ],
193        'ne' => [],
194        'nl' => [],
195        'nn' => [ "Æ", "Ø", "Å" ],
196        'no' => [ "Æ", "Ø", "Å" ], // not in libicu. You should probably use nb or nn instead.
197        'oc' => [], // not in libicu
198        'om' => [ 'Ch', 'Dh', 'Kh', 'Ny', 'Ph', 'Sh' ],
199        'or' => [ "\u{0B01}", "\u{0B02}", "\u{0B03}", "କ୍ଷ" ],
200        'pa' => [ "\u{0A4D}" ],
201        'pl' => [ "Ą", "Ć", "Ę", "Ł", "Ń", "Ó", "Ś", "Ź", "Ż" ],
202        'pt' => [],
203        'rm' => [], // not in libicu
204        'ro' => [ "Ă", "Â", "Î", "Ș", "Ț" ],
205        'ru' => [],
206        'rup' => [ "Ă", "Â", "Î", "Ľ", "Ń", "Ș", "Ț" ], // not in libicu
207        'sco' => [],
208        'se' => [
209            'Á', 'Č', 'Ʒ', 'Ǯ', 'Đ', 'Ǧ', 'Ǥ', 'Ǩ', 'Ŋ',
210            'Š', 'Ŧ', 'Ž', 'Ø', 'Æ', 'Ȧ', 'Ä', 'Ö'
211        ],
212        'si' => [ "\u{0D82}", "\u{0D83}", "\u{0DA4}" ],
213        'sk' => [ "Ä", "Č", "Ch", "Ô", "Š", "Ž" ],
214        'sl' => [ "Č", "Š", "Ž" ],
215        'smn' => [ "Â", "Á", "Č", "Đ", "Ŋ", "Š", "Ŧ", "Ž", "Æ", "Ø", "Å", "Ä", "Ö" ],
216        'sq' => [ "Ç", "Dh", "Ë", "Gj", "Ll", "Nj", "Rr", "Sh", "Th", "Xh", "Zh" ],
217        'sr' => [],
218        'sr-Latn' => [ "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ],
219        'sv' => [ "Å", "Ä", "Ö" ],
220        'sv@collation=standard' => [ "Å", "Ä", "Ö" ],
221        'sw' => [],
222        'ta' => [
223            "\u{0B82}", "ஃ", "க்ஷ", "க்", "ங்", "ச்", "ஞ்", "ட்", "ண்", "த்", "ந்",
224            "ப்", "ம்", "ய்", "ர்", "ல்", "வ்", "ழ்", "ள்", "ற்", "ன்", "ஜ்", "ஶ்", "ஷ்",
225            "ஸ்", "ஹ்", "க்ஷ்"
226        ],
227        'te' => [ "\u{0C01}", "\u{0C02}", "\u{0C03}" ],
228        'th' => [ "ฯ", "\u{0E46}", "\u{0E4D}", "\u{0E3A}" ],
229        'tk' => [ "Ç", "Ä", "Ž", "Ň", "Ö", "Ş", "Ü", "Ý" ],
230        'tl' => [ "Ñ", "Ng" ], // not in libicu
231        'to' => [ "Ng", "ʻ" ],
232        'tr' => [ "Ç", "Ğ", "İ", "Ö", "Ş", "Ü" ],
233        '-tr' => [ "ı" ],
234        'tt' => [ "Ә", "Ө", "Ү", "Җ", "Ң", "Һ" ], // not in libicu
235        'uk' => [ "Ґ", "Ь" ],
236        'uz' => [ "Ch", "G'", "Ng", "O'", "Sh" ], // not in libicu
237        'vi' => [ "Ă", "Â", "Đ", "Ê", "Ô", "Ơ", "Ư" ],
238        'vo' => [ "Ä", "Ö", "Ü" ],
239        'yi' => [
240            "\u{05D1}\u{05BF}", "\u{05DB}\u{05BC}", "\u{05E4}\u{05BC}",
241            "\u{05E9}\u{05C2}", "\u{05EA}\u{05BC}"
242        ],
243        'yo' => [ "Ẹ", "Gb", "Ọ", "Ṣ" ],
244        'zu' => [],
245    ];
246
247    /**
248     * @param LanguageFactory $languageFactory
249     * @param string $locale
250     */
251    public function __construct(
252        LanguageFactory $languageFactory,
253        $locale
254    ) {
255        $this->locale = $locale;
256        // Drop everything after the '@' in locale's name
257        $localeParts = explode( '@', $locale );
258        $this->digitTransformLanguage = $languageFactory->getLanguage( $locale === 'root' ? 'en' : $localeParts[0] );
259
260        $mainCollator = Collator::create( $locale );
261        if ( !$mainCollator ) {
262            throw new InvalidArgumentException( "Invalid ICU locale specified for collation: $locale" );
263        }
264        $this->mainCollator = $mainCollator;
265
266        // @phan-suppress-next-line PhanPossiblyNullTypeMismatchProperty successed before, no null check needed
267        $this->primaryCollator = Collator::create( $locale );
268        $this->primaryCollator->setStrength( Collator::PRIMARY );
269
270        // If the special suffix for numeric collation is present, turn on numeric collation.
271        if ( str_ends_with( $locale, '-u-kn' ) ) {
272            $this->useNumericCollation = true;
273            // Strip off the special suffix so it doesn't trip up fetchFirstLetterData().
274            $this->locale = substr( $this->locale, 0, -5 );
275            $this->mainCollator->setAttribute( Collator::NUMERIC_COLLATION, Collator::ON );
276            $this->primaryCollator->setAttribute( Collator::NUMERIC_COLLATION, Collator::ON );
277        }
278    }
279
280    /** @inheritDoc */
281    public function getSortKey( $string ) {
282        return $this->mainCollator->getSortKey( $string );
283    }
284
285    /** @inheritDoc */
286    public function getFirstLetter( $string ) {
287        $string = strval( $string );
288        if ( $string === '' ) {
289            return '';
290        }
291
292        $firstChar = mb_substr( $string, 0, 1, 'UTF-8' );
293
294        // If the first character is a CJK character, just return that character.
295        if ( ord( $firstChar ) > 0x7f && self::isCjk( mb_ord( $firstChar ) ) ) {
296            return $firstChar;
297        }
298
299        $sortKey = $this->getPrimarySortKey( $string );
300        $data = $this->getFirstLetterData();
301        $keys = $data['keys'];
302        $letters = $data['chars'];
303
304        // Do a binary search to find the correct letter to sort under
305        $min = ArrayUtils::findLowerBound(
306            static function ( $index ) use ( $keys ) {
307                return $keys[$index];
308            },
309            count( $keys ),
310            'strcmp',
311            $sortKey );
312
313        if ( $min === false ) {
314            // Before the first letter
315            return '';
316        }
317
318        $sortLetter = $letters[$min];
319
320        if ( $this->useNumericCollation ) {
321            // If the sort letter is a number, return '0–9' (or localized equivalent).
322            // ASCII value of 0 is 48. ASCII value of 9 is 57.
323            // Note that this also applies to non-Arabic numerals since they are
324            // mapped to Arabic numeral sort letters. For example, ২ sorts as 2.
325            if ( ord( $sortLetter ) >= 48 && ord( $sortLetter ) <= 57 ) {
326                $sortLetter = wfMessage( 'category-header-numerals' )->numParams( 0, 9 )->text();
327            }
328        }
329        return $sortLetter;
330    }
331
332    private function getPrimarySortKey( string $string ): string {
333        return $this->primaryCollator->getSortKey( $string );
334    }
335
336    /**
337     * @since 1.16.3
338     * @return array
339     */
340    private function getFirstLetterData() {
341        if ( $this->firstLetterData === null ) {
342            $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()
343                ->getLocalServerInstance( CACHE_ANYTHING );
344            $cacheKey = $cache->makeKey(
345                'first-letters',
346                static::class,
347                $this->locale,
348                $this->digitTransformLanguage->getCode(),
349                INTL_ICU_VERSION,
350                self::FIRST_LETTER_VERSION
351            );
352            $this->firstLetterData = $cache->getWithSetCallback( $cacheKey, $cache::TTL_WEEK, function () {
353                return $this->fetchFirstLetterData();
354            } );
355        }
356        return $this->firstLetterData;
357    }
358
359    /**
360     * @return array
361     */
362    private function fetchFirstLetterData() {
363        // Generate data from serialized data file
364        if ( isset( self::TAILORING_FIRST_LETTERS[$this->locale] ) ) {
365            $letters = require __DIR__ . "/data/first-letters-root.php";
366            // Append additional characters
367            $letters = array_merge( $letters, self::TAILORING_FIRST_LETTERS[$this->locale] );
368            // Remove unnecessary ones, if any
369            if ( isset( self::TAILORING_FIRST_LETTERS['-' . $this->locale] ) ) {
370                $letters = array_diff( $letters, self::TAILORING_FIRST_LETTERS['-' . $this->locale] );
371            }
372            // Apply digit transforms
373            $digits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
374            $letters = array_diff( $letters, $digits );
375            foreach ( $digits as $digit ) {
376                $letters[] = $this->digitTransformLanguage->formatNumNoSeparators( $digit );
377            }
378        } elseif ( $this->locale === 'root' ) {
379            $letters = require __DIR__ . "/data/first-letters-root.php";
380        } else {
381            throw new RuntimeException( "MediaWiki does not support ICU locale " .
382                "\"{$this->locale}\"" );
383        }
384
385        /* Sort the letters.
386         *
387         * It's impossible to have the precompiled data file properly sorted,
388         * because the sort order changes depending on ICU version. If the
389         * array is not properly sorted, the binary search will return random
390         * results.
391         *
392         * We also take this opportunity to remove primary collisions.
393         */
394        $letterMap = [];
395        foreach ( $letters as $letter ) {
396            $key = $this->getPrimarySortKey( $letter );
397            if ( isset( $letterMap[$key] ) ) {
398                // Primary collision (two characters with the same sort position).
399                // Keep whichever one sorts first in the main collator.
400                $comp = $this->mainCollator->compare( $letter, $letterMap[$key] );
401                wfDebug( "Primary collision '$letter' '{$letterMap[$key]}' (comparison: $comp)" );
402                // If that also has a collision, use codepoint as a tiebreaker.
403                if ( $comp === 0 ) {
404                    $comp = mb_ord( $letter ) <=> mb_ord( $letterMap[$key] );
405                }
406                if ( $comp < 0 ) {
407                    $letterMap[$key] = $letter;
408                }
409            } else {
410                $letterMap[$key] = $letter;
411            }
412        }
413        ksort( $letterMap, SORT_STRING );
414
415        /* Remove duplicate prefixes. Basically, if something has a sortkey
416         * which is a prefix of some other sortkey, then it is an
417         * expansion and probably should not be considered a section
418         * header.
419         *
420         * For example, 'þ' is sometimes sorted as if it is the letters
421         * 'th'. Other times it is its own primary element. Another
422         * example is '₨'. Sometimes it's a currency symbol. Sometimes it
423         * is an 'R' followed by an 's'.
424         *
425         * Additionally, an expanded element should always sort directly
426         * after its first element due to the way sortkeys work.
427         *
428         * UCA sortkey elements are of variable length, but no collation
429         * element should be a prefix of some other element, so I think
430         * this is safe. See:
431         * - https://unicode-org.github.io/icu-docs/design/collation/ICU_collation_design.htm
432         * - https://icu.unicode.org/design/collation/uca-weight-allocation
433         *
434         * Additionally, there is something called primary compression to
435         * worry about. Basically, if you have two primary elements that
436         * are more than one byte and both start with the same byte, then
437         * the first byte is dropped on the second primary. Additionally,
438         * either \x03 or \xFF may be added to mean that the next primary
439         * does not start with the first byte of the first primary.
440         *
441         * This shouldn't matter much, as the first primary is not
442         * changed, and that is what we are comparing against.
443         *
444         * tl;dr: This makes some assumptions about how ICU implements
445         * collations. It seems incredibly unlikely these assumptions
446         * will change, but nonetheless they are assumptions.
447         */
448
449        $prev = '';
450        $duplicatePrefixes = [];
451        foreach ( $letterMap as $key => $value ) {
452            // Due to the fact the array is sorted, we only have
453            // to compare with the element directly previous
454            // to the current element (skipping expansions).
455            // An element "X" will always sort directly
456            // before "XZ" (Unless we have "XY", but we
457            // do not update $prev in that case).
458            if ( $prev !== '' && str_starts_with( $key, $prev ) ) {
459                $duplicatePrefixes[] = $key;
460                // If this is an expansion, we don't want to
461                // compare the next element to this element,
462                // but to what is currently $prev
463                continue;
464            }
465            $prev = $key;
466        }
467        foreach ( $duplicatePrefixes as $badKey ) {
468            wfDebug( "Removing '{$letterMap[$badKey]}' from first letters." );
469            unset( $letterMap[$badKey] );
470            // This code assumes that unsetting does not change sort order.
471        }
472        return [
473            'chars' => array_values( $letterMap ),
474            'keys' => array_keys( $letterMap ),
475        ];
476    }
477
478    /**
479     * Test if a code point is a CJK (Chinese, Japanese, Korean) character
480     * @param int $codepoint
481     * @return bool
482     * @since 1.16.3
483     */
484    public static function isCjk( $codepoint ) {
485        foreach ( self::CJK_BLOCKS as $block ) {
486            if ( $codepoint >= $block[0] && $codepoint <= $block[1] ) {
487                return true;
488            }
489        }
490        return false;
491    }
492}