Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
98.73% |
78 / 79 |
|
90.91% |
10 / 11 |
CRAP | |
0.00% |
0 / 1 |
LanguageNameUtils | |
98.73% |
78 / 79 |
|
90.91% |
10 / 11 |
41 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
isSupportedLanguage | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
6.05 | |||
isValidCode | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
isValidBuiltInCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isKnownLanguageTag | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
getLanguageNames | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
getLanguageNamesUncached | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
15 | |||
getLanguageName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getFileName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getMessagesFileName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getJsonMessagesFileName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Languages; |
22 | |
23 | use BagOStuff; |
24 | use HashBagOStuff; |
25 | use InvalidArgumentException; |
26 | use LanguageCode; |
27 | use MediaWiki\Config\ServiceOptions; |
28 | use MediaWiki\HookContainer\HookContainer; |
29 | use MediaWiki\HookContainer\HookRunner; |
30 | use MediaWiki\MainConfigNames; |
31 | use MediaWiki\Title\MediaWikiTitleCodec; |
32 | |
33 | /** |
34 | * A service that provides utilities to do with language names and codes. |
35 | * |
36 | * See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more information. |
37 | * |
38 | * @since 1.34 |
39 | * @ingroup Language |
40 | */ |
41 | class LanguageNameUtils { |
42 | /** |
43 | * Return autonyms in getLanguageName(s). |
44 | */ |
45 | public const AUTONYMS = null; |
46 | |
47 | /** |
48 | * Return all known languages in getLanguageName(s). |
49 | */ |
50 | public const ALL = 'all'; |
51 | |
52 | /** |
53 | * Return in getLanguageName(s) only the languages that are defined by MediaWiki. |
54 | */ |
55 | public const DEFINED = 'mw'; |
56 | |
57 | /** |
58 | * Return in getLanguageName(s) only the languages for which we have at least some localisation. |
59 | */ |
60 | public const SUPPORTED = 'mwfile'; |
61 | |
62 | /** @var ServiceOptions */ |
63 | private $options; |
64 | |
65 | /** |
66 | * Cache for language names |
67 | * @var HashBagOStuff|null |
68 | */ |
69 | private $languageNameCache; |
70 | |
71 | /** |
72 | * Cache for validity of language codes |
73 | * @var array |
74 | */ |
75 | private $validCodeCache = []; |
76 | |
77 | /** |
78 | * @internal For use by ServiceWiring |
79 | */ |
80 | public const CONSTRUCTOR_OPTIONS = [ |
81 | MainConfigNames::ExtraLanguageNames, |
82 | MainConfigNames::UsePigLatinVariant, |
83 | MainConfigNames::UseXssLanguage, |
84 | ]; |
85 | |
86 | /** @var HookRunner */ |
87 | private $hookRunner; |
88 | |
89 | /** |
90 | * @param ServiceOptions $options |
91 | * @param HookContainer $hookContainer |
92 | */ |
93 | public function __construct( ServiceOptions $options, HookContainer $hookContainer ) { |
94 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
95 | $this->options = $options; |
96 | $this->hookRunner = new HookRunner( $hookContainer ); |
97 | } |
98 | |
99 | /** |
100 | * Checks whether any localisation is available for that language tag in MediaWiki |
101 | * (MessagesXx.php or xx.json exists). |
102 | * |
103 | * @param string $code Language tag (in lower case) |
104 | * @return bool Whether language is supported |
105 | */ |
106 | public function isSupportedLanguage( string $code ): bool { |
107 | if ( !$this->isValidBuiltInCode( $code ) ) { |
108 | return false; |
109 | } |
110 | |
111 | if ( $code === 'qqq' ) { |
112 | // Special code for internal use, not supported even though there is a qqq.json |
113 | return false; |
114 | } |
115 | if ( |
116 | $code === 'en-x-piglatin' && |
117 | !$this->options->get( MainConfigNames::UsePigLatinVariant ) |
118 | ) { |
119 | // Suppress Pig Latin unless explicitly enabled. |
120 | return false; |
121 | } |
122 | |
123 | return is_readable( $this->getMessagesFileName( $code ) ) || |
124 | is_readable( $this->getJsonMessagesFileName( $code ) ); |
125 | } |
126 | |
127 | /** |
128 | * Returns true if a language code string is of a valid form, whether it exists. |
129 | * This includes codes which are used solely for customisation via the MediaWiki namespace. |
130 | * |
131 | * @param string $code |
132 | * |
133 | * @return bool False if the language code contains dangerous characters, e.g, HTML special |
134 | * characters or characters that are illegal in MediaWiki titles. |
135 | */ |
136 | public function isValidCode( string $code ): bool { |