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 | /** @var array */ |
| 18 | private static $cache = []; |
| 19 | |
| 20 | /** |
| 21 | * Get localized country names for a particular language, using fallback languages for missing |
| 22 | * items. |
| 23 | * |
| 24 | * @param string $code The language to return the list in |
| 25 | * @return array an associative array of country codes and localized country names |
| 26 | */ |
| 27 | public static function getNames( $code ) { |
| 28 | // Load country names localized for the requested language |
| 29 | $names = self::loadLanguage( $code ); |
| 30 | |
| 31 | // Load missing country names from fallback languages |
| 32 | $fallbacks = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code ); |
| 33 | foreach ( $fallbacks as $fallback ) { |
| 34 | // Overwrite the things in fallback with what we have already |
| 35 | $names = array_merge( self::loadLanguage( $fallback ), $names ); |
| 36 | } |
| 37 | |
| 38 | return $names; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Load country names localized for a particular language. Helper function for getNames. |
| 43 | * |
| 44 | * @param string $code The language to return the list in |
| 45 | * @return array an associative array of country codes and localized country names |
| 46 | */ |
| 47 | private static function loadLanguage( $code ) { |
| 48 | if ( !isset( self::$cache[$code] ) ) { |
| 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 ); |
| 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__ . '/../CldrMain/' . |
| 68 | $langNameUtils->getFileName( 'CldrMain', $code ); |
| 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' ); |