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