Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.96% covered (warning)
84.96%
113 / 133
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CLDRParser
84.96% covered (warning)
84.96%
113 / 133
0.00% covered (danger)
0.00%
0 / 3
66.66
0.00% covered (danger)
0.00%
0 / 1
 parseMain
78.69% covered (warning)
78.69%
48 / 61
0.00% covered (danger)
0.00%
0 / 1
26.68
 parseSupplemental
92.31% covered (success)
92.31%
24 / 26
0.00% covered (danger)
0.00%
0 / 1
11.06
 parseCurrencySymbols
89.13% covered (warning)
89.13%
41 / 46
0.00% covered (danger)
0.00%
0 / 1
23.68
1<?php
2
3namespace MediaWiki\Extension\CLDR;
4
5use RuntimeException;
6use SimpleXMLElement;
7
8/**
9 * Extract data from cldr XML.
10 *
11 * @author Niklas Laxström
12 * @author Ryan Kaldari
13 * @author Santhosh Thottingal
14 * @author Sam Reed
15 * @copyright Copyright © 2007-2015
16 * @license GPL-2.0-or-later
17 */
18class CLDRParser {
19
20    public const LOCALITY_DEFAULT = '!DEFAULT';
21    public const LANGUAGE_DEFAULT = '!root';
22    public const CURRENCY_DEFAULT = '!DEFAULT';
23
24    /**
25     * Read the main/<locale>.xml file from CLDR core and convert to PHP
26     *
27     * @param string $inputFile filename
28     */
29    public function parseMain( $inputFile ): array {
30        $contents = file_get_contents( $inputFile );
31        $doc = new SimpleXMLElement( $contents );
32
33        $data = [
34            'indexCharacters' => [],
35            'languageNames' => [],
36            'currencyNames' => [],
37            'currencySymbols' => [],
38            'countryNames' => [],
39            'timeUnits' => [],
40        ];
41
42        // Take a Unicode Set for an alphabet and extract simple example characters.
43        // For example, "[aàâ b {ch}]" is extracted as `["a", "b", "ch"]`.
44        // TODO: Unicode Set allows for more complex syntax, but we support only
45        // the subset currently used here. Should rely on a library instead.
46        $indexCharacters = $doc->xpath( '//characters/exemplarCharacters[@type="index"]' );
47        if ( $indexCharacters && count( $indexCharacters ) === 1 ) {
48            [ $characters ] = $indexCharacters;
49            $splitSequence = preg_split( '/\s/',
50                trim( (string)$characters, '[]' ) );
51            $data['indexCharacters'] = array_map(
52                static fn ( $letter ) => preg_replace_callback_array( [
53                    // Convert unicode literals to characters.
54                    '/^\\\\u([\da-f]{4})/i' => static fn ( $m ) => mb_chr( hexdec( $m[1] ) ),
55
56                    // Take only the first character from a set like "aàâ".
57                    // When the character is made up of multiple symbols, it
58                    // will be enclosed in curly braces like "{ch}", and in this
59                    // case we want the entire group.  It's possible that the
60                    // two cases are combined like "{ch}ç".
61                    '/^(?:([^{])|\{([^}]+)\}).*$/u' => static fn ( $m ) => $m[2] ?? $m[1],
62                ], $letter ),
63                $splitSequence
64            );
65        }
66
67        foreach ( $doc->xpath( '//languages/language' ) as $elem ) {
68            if ( (string)$elem['alt'] !== '' ) {
69                continue;
70            }
71
72            if ( (string)$elem['type'] === 'root' ) {
73                continue;
74            }
75
76            $key = str_replace( '_', '-', strtolower( $elem['type'] ) );
77
78            $data['languageNames'][$key] = (string)$elem;
79        }
80
81        foreach ( $doc->xpath( '//currencies/currency' ) as $elem ) {
82            if ( (string)$elem->displayName[0] === '' ) {
83                continue;
84            }
85
86            $data['currencyNames'][(string)$elem['type']] = (string)$elem->displayName[0];
87            if ( (string)$elem->symbol[0] !== '' ) {
88                $data['currencySymbols'][(string)$elem['type']] = (string)$elem->symbol[0];
89            }
90        }
91
92        foreach ( $doc->xpath( '//territories/territory' ) as $elem ) {
93            if ( (string)$elem['alt'] !== '' && (string)$elem['alt'] !== 'short' ) {
94                continue;
95            }
96
97            if ( (string)$elem['type'] === 'ZZ' ||
98                !preg_match( '/^[A-Z][A-Z]$/', $elem['type'] )
99            ) {
100                continue;
101            }
102
103            $data['countryNames'][(string)$elem['type']] = (string)$elem;
104        }
105        foreach ( $doc->xpath( '//units/unitLength' ) as $unitLength ) {
106            if ( (string)$unitLength['type'] !== 'long' ) {
107                continue;
108            }
109            foreach ( $unitLength->unit as $elem ) {
110                $type = (string)$elem['type'];
111                if ( !str_starts_with( $type, 'duration-' ) ) {
112                    continue;
113                }
114                $type = substr( $type, strlen( 'duration-' ) );
115                foreach ( $elem->unitPattern as $pattern ) {
116                    $data['timeUnits'][$type . '-' . (string)$pattern['count']] = (string)$pattern;
117                }
118            }
119        }
120        foreach ( $doc->xpath( '//fields/field' ) as $field ) {
121            $fieldType = (string)$field['type'];
122
123            foreach ( $field->relativeTime as $relative ) {
124                $type = (string)$relative['type'];
125                foreach ( $relative->relativeTimePattern as $pattern ) {
126                    $data['timeUnits'][$fieldType . '-' . $type
127                    . '-' . (string)$pattern['count']] = (string)$pattern;
128                }
129            }
130        }
131
132        ksort( $data['timeUnits'] );
133        return $data;
134    }
135
136    /**
137     * Parse method for the file structure found in common/supplemental/supplementalData.xml
138     * @param string $inputFile
139     */
140    public function parseSupplemental( $inputFile ): array {
141        // Open the input file for reading
142
143        $contents = file_get_contents( $inputFile );
144        $doc = new SimpleXMLElement( $contents );
145
146        $data = [
147            'currencyFractions' => [],
148            'localeCurrencies' => [],
149        ];
150
151        // Pull currency attributes - digits, rounding, and cashRounding.
152        // This will tell us how many decmal places make sense to use with any currency,
153        // or if the currency is totally non-fractional
154        foreach ( $doc->xpath( '//currencyData/fractions/info' ) as $elem ) {
155            $iso4217 = (string)$elem['iso4217'];
156            if ( $iso4217 === '' ) {
157                continue;
158            }
159            if ( $iso4217 === 'DEFAULT' ) {
160                $iso4217 = self::CURRENCY_DEFAULT;
161            }
162
163            $attributes = [ 'digits', 'rounding', 'cashDigits', 'cashRounding' ];
164            foreach ( $attributes as $att ) {
165                if ( (string)$elem[$att] !== '' ) {
166                    $data['currencyFractions'][$iso4217][$att] = (string)$elem[$att];
167                }
168            }
169        }
170
171        ksort( $data['currencyFractions'] );
172
173        // Pull a map of regions to currencies in order of preference.
174        foreach ( $doc->xpath( '//currencyData/region' ) as $elem ) {
175            if ( (string)$elem['iso3166'] === '' ) {
176                continue;
177            }
178
179            $region = (string)$elem['iso3166'];
180
181            foreach ( $elem->currency as $currencynode ) {
182                if ( (string)$currencynode['to'] === '' && (string)$currencynode['tender'] !== 'false' ) {
183                    $data['localeCurrencies'][$region][] = (string)$currencynode['iso4217'];
184                }
185            }
186        }
187
188        ksort( $data['localeCurrencies'] );
189        return $data;
190    }
191
192    /**
193     * Parse method for the currency section in the names files.
194     * This is separate from the regular parse function, because we need all of
195     * the currency locale information, even if mediawiki doesn't support the language.
196     * (For instance: en_AU uses '$' for AUD, not USD, but it's not a supported mediawiki locality)
197     * @param string $inputDir the directory, in which we will parse everything.
198     */
199    public function parseCurrencySymbols( $inputDir ): array {
200        if ( !file_exists( $inputDir ) ) {
201            throw new RuntimeException( 'Input directory not found.' );
202        }
203        $files = scandir( $inputDir );
204
205        $data = [
206            'currencySymbols' => [],
207        ];
208
209        // Foreach files!
210        foreach ( $files as $inputFile ) {
211            if ( !str_ends_with( $inputFile, '.xml' ) ) {
212                continue;
213            }
214
215            $contents = file_get_contents( $inputDir . '/' . $inputFile );
216            $doc = new SimpleXMLElement( $contents );
217
218            // Tags in the <identity> section are guaranteed to appear once
219            $languages = $doc->xpath( '//identity/language/@type' );
220            $language = $languages
221                ? (string)$languages[0]
222                : pathinfo( $inputFile, PATHINFO_FILENAME );
223
224            // The <script> element is optional
225            $scripts = $doc->xpath( '//identity/script/@type' );
226            $script = $scripts ? (string)$scripts[0] : '';
227            // expand the language
228            if ( $script !== '' ) {
229                $language .= '-' . strtolower( $script );
230            }
231
232            // The <territory> element is optional
233            $territories = $doc->xpath( '//identity/territory/@type' );
234            $territory = $territories ? (string)$territories[0] : self::LOCALITY_DEFAULT;
235
236            if ( $language === 'root' ) {
237                $language = self::LANGUAGE_DEFAULT;
238            }
239
240            foreach ( $doc->xpath( '//currencies/currency' ) as $elem ) {
241                if ( (string)$elem->symbol[0] !== '' ) {
242                    $data['currencySymbols'][(string)$elem['type']][$language][$territory] =
243                        (string)$elem->symbol[0];
244                }
245            }
246        }
247
248        // now massage the data somewhat. It's pretty blown up at this point.
249
250        /**
251         * Part 1: Stop blowing up on defaults.
252         * Defaults apparently come in many forms. Listed below in order of scope
253         * (widest to narrowest)
254         * 1) The ISO code itself, in the absence of any other defaults
255         * 2) The 'root' language file definition
256         * 3) Language with no locality - locality will come in as 'DEFAULT'
257         *
258         * Intended behavior:
259         * From narrowest scope to widest, collapse the defaults
260         */
261        foreach ( $data['currencySymbols'] as $currency => $language ) {
262            // get the currency default symbol. This will either be defined in the
263            // 'root' language file, or taken from the ISO code.
264            $default = $language[self::LANGUAGE_DEFAULT][self::LOCALITY_DEFAULT] ?? $currency;
265
266            foreach ( $language as $lang => $territories ) {
267                if ( is_array( $territories ) ) {
268                    // Collapse a language (no locality) array if it's just the default. One value will do fine.
269                    if ( count( $territories ) === 1 && array_key_exists( self::LOCALITY_DEFAULT, $territories ) ) {
270                        $data['currencySymbols'][$currency][$lang] = $territories[self::LOCALITY_DEFAULT];
271                        if ( $territories[self::LOCALITY_DEFAULT] === $default
272                            && $lang !== self::LANGUAGE_DEFAULT
273                        ) {
274                            unset( $data['currencySymbols'][$currency][$lang] );
275                        }
276                    } else {
277                        // Collapse a language (with locality) array if it's default is just the default
278                        if ( !array_key_exists( self::LOCALITY_DEFAULT, $territories )
279                            || ( $territories[self::LOCALITY_DEFAULT] === $default
280                                && $lang !== self::LANGUAGE_DEFAULT )
281                        ) {
282                            foreach ( $territories as $territory => $symbol ) {
283                                if ( $symbol === $default ) {
284                                    unset( $data['currencySymbols'][$currency][$lang][$territory] );
285                                }
286                            }
287                        }
288                        ksort( $data['currencySymbols'][$currency][$lang] );
289                    }
290                }
291            }
292
293            ksort( $data['currencySymbols'][$currency] );
294        }
295
296        ksort( $data['currencySymbols'] );
297        return $data;
298    }
299
300}