Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.73% covered (success)
98.73%
78 / 79
90.91% covered (success)
90.91%
10 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
LanguageNameUtils
98.73% covered (success)
98.73%
78 / 79
90.91% covered (success)
90.91%
10 / 11
41
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isSupportedLanguage
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
6.05
 isValidCode
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 isValidBuiltInCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isKnownLanguageTag
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 getLanguageNames
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 getLanguageNamesUncached
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
15
 getLanguageName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFileName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getMessagesFileName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getJsonMessagesFileName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
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
21namespace MediaWiki\Languages;
22
23use InvalidArgumentException;
24use MediaWiki\Config\ServiceOptions;
25use MediaWiki\HookContainer\HookContainer;
26use MediaWiki\HookContainer\HookRunner;
27use MediaWiki\Language\LanguageCode;
28use MediaWiki\MainConfigNames;
29use MediaWiki\Title\MediaWikiTitleCodec;
30use Wikimedia\ObjectCache\BagOStuff;
31use Wikimedia\ObjectCache\HashBagOStuff;
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 */
41class 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    public function __construct( ServiceOptions $options, HookContainer $hookContainer ) {
90        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
91        $this->options = $options;
92        $this->hookRunner = new HookRunner( $hookContainer );
93    }
94
95    /**
96     * Checks whether any localisation is available for that language tag in MediaWiki
97     * (MessagesXx.php or xx.json exists).
98     *
99     * @param string $code Language tag (in lower case)
100     * @return bool Whether language is supported
101     */
102    public function isSupportedLanguage( string $code ): bool {
103        if ( !$this->isValidBuiltInCode( $code ) ) {
104            return false;
105        }
106
107        if ( $code === 'qqq' ) {
108            // Special code for internal use, not supported even though there is a qqq.json
109            return false;
110        }
111        if (
112            $code === 'en-x-piglatin' &&
113            !$this->options->get( MainConfigNames::UsePigLatinVariant )
114        ) {
115            // Suppress Pig Latin unless explicitly enabled.
116            return false;
117        }
118
119        return is_readable( $this->getMessagesFileName( $code ) ) ||
120            is_readable( $this->getJsonMessagesFileName( $code ) );
121    }
122
123    /**
124     * Returns true if a language code string is of a valid form, whether it exists.
125     * This includes codes which are used solely for customisation via the MediaWiki namespace.
126     *
127     * @param string $code
128     *
129     * @return bool False if the language code contains dangerous characters, e.g, HTML special
130     *  characters or characters that are illegal in MediaWiki titles.
131     */
132    public function isValidCode( string $code ): bool {
133        if ( !isset( $this->validCodeCache[$code] ) ) {
134            // People think language codes are HTML-safe, so enforce it. Ideally, we should only
135            // allow a-zA-Z0-9- but .+ and other chars are often used for {{int:}} hacks.  See bugs
136            // T39564, T39587, T38938.