Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
70.00% |
21 / 30 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
TimeUnits | |
70.00% |
21 / 30 |
|
0.00% |
0 / 2 |
14.27 | |
0.00% |
0 / 1 |
getUnits | |
33.33% |
3 / 9 |
|
0.00% |
0 / 1 |
8.74 | |||
loadLanguage | |
85.71% |
18 / 21 |
|
0.00% |
0 / 1 |
7.14 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CLDR; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | |
7 | /** |
8 | * A class for querying translated time units from CLDR data. |
9 | * |
10 | * @author Niklas Laxström |
11 | * @author Ryan Kaldari |
12 | * @copyright Copyright © 2007-2013 |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class TimeUnits { |
16 | |
17 | /** @var array */ |
18 | private static $cache = []; |
19 | |
20 | /** |
21 | * Get localized time units for a particular language, using fallback languages for missing |
22 | * items. The time units are returned as an associative array. The keys are of the form: |
23 | * <unit>-<tense>-<ordinality> (for example, 'hour-future-two'). The values include a placeholder |
24 | * for the number (for example, '{0} months ago'). |
25 | * |
26 | * @param string $code The language to return the list in |
27 | * @return array an associative array of time unit codes and localized time units |
28 | */ |
29 | public static function getUnits( $code ) { |
30 | // Load time units localized for the requested language |
31 | $units = self::loadLanguage( $code ); |
32 | |
33 | if ( $units ) { |
34 | return $units; |
35 | } |
36 | // Load missing time units from fallback languages |
37 | $fallbacks = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code ); |
38 | foreach ( $fallbacks as $fallback ) { |
39 | if ( $units ) { |
40 | break; |
41 | } |
42 | // Get time units from a fallback language |
43 | $units = self::loadLanguage( $fallback ); |
44 | } |
45 | |
46 | return $units; |
47 | } |
48 | |
49 | /** |
50 | * Load time units localized for a particular language. Helper function for getUnits. |
51 | * |
52 | * @param string $code The language to return the list in |
53 | * @return array an associative array of time unit codes and localized time units |
54 | */ |
55 | private static function loadLanguage( $code ) { |
56 | if ( !isset( self::$cache[$code] ) ) { |
57 | self::$cache[$code] = []; |
58 | |
59 | $langNameUtils = MediaWikiServices::getInstance()->getLanguageNameUtils(); |
60 | |
61 | if ( !$langNameUtils->isValidBuiltInCode( $code ) ) { |
62 | return []; |
63 | } |
64 | |
65 | /* Load override for wrong or missing entries in cldr */ |
66 | $override = __DIR__ . '/../LocalNames/' . |
67 | $langNameUtils->getFileName( 'LocalNames', $code, '.php' ); |
68 | if ( file_exists( $override ) ) { |
69 | $timeUnits = false; |
70 | |
71 | require $override; |
72 | |
73 | // @phan-suppress-next-line PhanImpossibleCondition |
74 | if ( is_array( $timeUnits ) ) { |
75 | self::$cache[$code] = $timeUnits; |
76 | } |
77 | } |
78 | |
79 | $filename = __DIR__ . '/../CldrNames/' . |
80 | $langNameUtils->getFileName( 'CldrNames', $code, '.php' ); |
81 | if ( file_exists( $filename ) ) { |
82 | $timeUnits = false; |
83 | require $filename; |
84 | // @phan-suppress-next-line PhanImpossibleCondition |
85 | if ( is_array( $timeUnits ) ) { |
86 | self::$cache[$code] += $timeUnits; |
87 | } |
88 | } else { |
89 | wfDebug( __METHOD__ . ": Unable to load time units for $filename\n" ); |
90 | } |
91 | } |
92 | |
93 | return self::$cache[$code]; |
94 | } |
95 | } |