Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
82.14% |
23 / 28 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CountryNames | |
85.19% |
23 / 27 |
|
0.00% |
0 / 2 |
10.33 | |
0.00% |
0 / 1 |
getNames | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
loadLanguage | |
86.36% |
19 / 22 |
|
0.00% |
0 / 1 |
8.16 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CLDR; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | |
7 | /** |
8 | * A class for querying translated country names from CLDR data. |
9 | * |
10 | * @author Niklas Laxström |
11 | * @author Ryan Kaldari |
12 | * @copyright Copyright © 2007-2011 |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class CountryNames { |
16 | |
17 | private static $cache = []; |
18 | |
19 | /** |
20 | * Get localized country names for a particular language, using fallback languages for missing |
21 | * items. |
22 | * |
23 | * @param string $code The language to return the list in |
24 | * @return array an associative array of country codes and localized country names |
25 | */ |
26 | public static function getNames( $code ) { |
27 | // Load country names localized for the requested language |
28 | $names = self::loadLanguage( $code ); |
29 | |
30 | // Load missing country names from fallback languages |
31 | $fallbacks = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code ); |
32 | foreach ( $fallbacks as $fallback ) { |
33 | // Overwrite the things in fallback with what we have already |
34 | $names = array_merge( self::loadLanguage( $fallback ), $names ); |
35 | } |
36 | |
37 | return $names; |
38 | } |
39 | |
40 | /** |
41 | * Load country names localized for a particular language. Helper function for getNames. |
42 | * |
43 | * @param string $code The language to return the list in |
44 | * @return array an associative array of country codes and localized country names |
45 | */ |
46 | private static function loadLanguage( $code ) { |
47 | if ( !isset( self::$cache[$code] ) ) { |
48 | |
49 | $langNameUtils = MediaWikiServices::getInstance()->getLanguageNameUtils(); |
50 | |
51 | if ( !$langNameUtils->isValidBuiltInCode( $code ) ) { |
52 | return []; |
53 | } |
54 | |
55 | /* Load override for wrong or missing entries in cldr */ |
56 | $override = __DIR__ . '/../LocalNames/' . |
57 | $langNameUtils->getFileName( 'LocalNames', $code, '.php' ); |
58 | if ( file_exists( $override ) ) { |
59 | $countryNames = false; |
60 | require $override; |
61 | // @phan-suppress-next-line PhanImpossibleCondition |
62 | if ( is_array( $countryNames ) ) { |
63 | self::$cache[$code] = $countryNames; |
64 | } |
65 | } |
66 | |
67 | $filename = __DIR__ . '/../CldrNames/' . |
68 | $langNameUtils->getFileName( 'CldrNames', $code, '.php' ); |
69 | if ( file_exists( $filename ) ) { |
70 | $countryNames = false; |
71 | require $filename; |
72 | // @phan-suppress-next-line PhanImpossibleCondition |
73 | if ( is_array( $countryNames ) ) { |
74 | if ( isset( self::$cache[$code] ) ) { |
75 | // Add to existing list of localized country names |
76 | self::$cache[$code] += $countryNames; |
77 | } else { |
78 | // No list exists, so create it |
79 | self::$cache[$code] = $countryNames; |
80 | } |
81 | } |
82 | } else { |
83 | wfDebug( __METHOD__ . ": Unable to load country names for $filename\n" ); |
84 | } |
85 | } |
86 | |
87 | return self::$cache[$code] ?? []; |
88 | } |
89 | } |
90 | |
91 | class_alias( CountryNames::class, 'CountryNames' ); |