Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.71% |
18 / 21 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
Alphabets | |
85.71% |
18 / 21 |
|
0.00% |
0 / 2 |
7.14 | |
0.00% |
0 / 1 |
getIndexCharacters | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
loadLanguage | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
4.07 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CLDR; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | |
7 | /** |
8 | * Series of main alphabet symbols for a language, parsed from CLDR data. |
9 | * https://www.unicode.org/cldr/charts/45/by_type/core_data.alphabetic_information.html |
10 | * |
11 | * @license GPL-2.0-or-later |
12 | */ |
13 | class Alphabets { |
14 | |
15 | /** @var array<string,string[]|null> */ |
16 | private static array $cache = []; |
17 | |
18 | /** |
19 | * Get alphabet for a locale or its fallbacks |
20 | * |
21 | * The index or main alphabet, found in CLDR main `characters.exemplarCharacters`, |
22 | * and reduced to a simple form for creating a sequence. |
23 | * |
24 | * @param string $code Locale to query. If no entry exists, the fallback |
25 | * locales are iterated. |
26 | * @return string[] a sequence of symbols |
27 | */ |
28 | public static function getIndexCharacters( string $code ): array { |
29 | $fallbacks = [ |
30 | $code, |
31 | ...MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code ) |
32 | ]; |
33 | foreach ( $fallbacks as $languageCode ) { |
34 | $indexCharacters = self::loadLanguage( $languageCode ); |
35 | if ( $indexCharacters ) { |
36 | return $indexCharacters; |
37 | } |
38 | } |
39 | return []; |
40 | } |
41 | |
42 | /** |
43 | * Load alphabets for a particular language. |
44 | * |
45 | * @param string $code The language to return the list in |
46 | * @return string[]|null a list of characters in the language's index alphabet. |
47 | */ |
48 | private static function loadLanguage( string $code ): ?array { |
49 | if ( !array_key_exists( $code, self::$cache ) ) { |
50 | $langNameUtils = MediaWikiServices::getInstance()->getLanguageNameUtils(); |
51 | |
52 | if ( !$langNameUtils->isValidBuiltInCode( $code ) ) { |
53 | return null; |
54 | } |
55 | |
56 | $filename = __DIR__ . '/../CldrMain/' . |
57 | $langNameUtils->getFileName( 'CldrMain', $code ); |
58 | if ( file_exists( $filename ) ) { |
59 | $indexCharacters = null; |
60 | require $filename; |
61 | self::$cache[$code] = $indexCharacters; |
62 | } else { |
63 | wfDebug( __METHOD__ . ": Unable to load alphabet for $filename\n" ); |
64 | } |
65 | } |
66 | |
67 | return self::$cache[$code] ?? null; |
68 | } |
69 | } |