| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Languages; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Config\ServiceOptions; |
| 25 |
use MediaWiki\HookContainer\HookContainer; |
| 26 |
use MediaWiki\HookContainer\HookRunner; |
| 27 |
use MediaWiki\Language\LanguageCode; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\Title\MediaWikiTitleCodec; |
| 30 |
use Wikimedia\ObjectCache\BagOStuff; |
| 31 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class LanguageNameUtils { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public const AUTONYMS = null; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public const ALL = 'all'; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public const DEFINED = 'mw'; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public const SUPPORTED = 'mwfile'; |
| 61 |
|
| 62 |
|
| 63 |
private $options; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private $languageNameCache; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private $validCodeCache = []; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public const CONSTRUCTOR_OPTIONS = [ |
| 81 |
MainConfigNames::ExtraLanguageNames, |
| 82 |
MainConfigNames::UsePigLatinVariant, |
| 83 |
MainConfigNames::UseXssLanguage, |
| 84 |
]; |
| 85 |
|
| 86 |
|
| 87 |
private $hookRunner; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function isSupportedLanguage( string $code ): bool { |
| 107 |
if ( !$this->isValidBuiltInCode( $code ) ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
if ( $code === 'qqq' ) { |
| 112 |
|
| 113 |
return false; |
| 114 |
} |
| 115 |
if ( |
| 116 |
$code === 'en-x-piglatin' && |
| 117 |
!$this->options->get( MainConfigNames::UsePigLatinVariant ) |
| 118 |
) { |
| 119 |
|
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
return is_readable( $this->getMessagesFileName( $code ) ) || |
| 124 |
is_readable( $this->getJsonMessagesFileName( $code ) ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function isValidCode( string $code ): bool { |
| 137 |
if ( !isset( $this->validCodeCache[$code] ) ) { |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
$this->validCodeCache[$code] = |
| 142 |
|
| 143 |
strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code ) && |
| 144 |
!preg_match( MediaWikiTitleCodec::getTitleInvalidRegex(), $code ) && |
| 145 |
|
| 146 |
strlen( $code ) <= 128; |
| 147 |
} |
| 148 |
return $this->validCodeCache[$code]; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function isValidBuiltInCode( string $code ): bool { |
| 159 |
return (bool)preg_match( '/^[a-z0-9-]{2,128}$/', $code ); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function isKnownLanguageTag( string $tag ): bool { |
| 170 |
|
| 171 |
|
| 172 |
if ( !$this->isValidBuiltInCode( $tag ) ) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
if ( isset( Data\Names::NAMES[$tag] ) || $this->getLanguageName( $tag, $tag ) !== '' ) { |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function getLanguageNames( $inLanguage = self::AUTONYMS, $include = self::DEFINED ) { |
| 196 |
if ( $inLanguage !== self::AUTONYMS ) { |
| 197 |
$inLanguage = LanguageCode::replaceDeprecatedCodes( LanguageCode::bcp47ToInternal( $inLanguage ) ); |
| 198 |
} |
| 199 |
$cacheKey = $inLanguage === self::AUTONYMS ? 'null' : $inLanguage; |
| 200 |
$cacheKey .= ":$include"; |
| 201 |
if ( !$this->languageNameCache ) { |
| 202 |
$this->languageNameCache = new HashBagOStuff( [ 'maxKeys' => 20 ] ); |
| 203 |
} |
| 204 |
|
| 205 |
return $this->languageNameCache->getWithSetCallback( |
| 206 |
$cacheKey, |
| 207 |
BagOStuff::TTL_INDEFINITE, |
| 208 |
function () use ( $inLanguage, $include ) { |
| 209 |
return $this->getLanguageNamesUncached( $inLanguage, $include ); |
| 210 |
} |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private function getLanguageNamesUncached( $inLanguage, $include ) { |
| 222 |
|
| 223 |
if ( $inLanguage !== self::AUTONYMS && !$this->isValidCode( $inLanguage ) ) { |
| 224 |
$inLanguage = 'en'; |
| 225 |
} |
| 226 |
|
| 227 |
$names = []; |
| 228 |
|
| 229 |
if ( $inLanguage !== self::AUTONYMS ) { |
| 230 |
|
| 231 |
|
| 232 |
$this->hookRunner->onLanguageGetTranslatedLanguageNames( $names, $inLanguage ); |
| 233 |
} |
| 234 |
|
| 235 |
$mwNames = $this->options->get( MainConfigNames::ExtraLanguageNames ) + Data\Names::NAMES; |
| 236 |
if ( !$this->options->get( MainConfigNames::UsePigLatinVariant ) ) { |
| 237 |
|
| 238 |
unset( $mwNames['en-x-piglatin'] ); |
| 239 |
} |
| 240 |
if ( $this->options->get( MainConfigNames::UseXssLanguage ) ) { |
| 241 |
$mwNames['x-xss'] = 'fake xss language (see $wgUseXssLanguage)'; |
| 242 |
} |
| 243 |
|
| 244 |
foreach ( $mwNames as $mwCode => $mwName ) { |
| 245 |
|
| 246 |
|
| 247 |
if ( $mwCode === $inLanguage || !isset( $names[$mwCode] ) ) { |
| 248 |
$names[$mwCode] = $mwName; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if ( $include === self::ALL ) { |
| 253 |
ksort( $names ); |
| 254 |
return $names; |
| 255 |
} |
| 256 |
|
| 257 |
$returnMw = []; |
| 258 |
$coreCodes = array_keys( $mwNames ); |
| 259 |
foreach ( $coreCodes as $coreCode ) { |
| 260 |
$returnMw[$coreCode] = $names[$coreCode]; |
| 261 |
} |
| 262 |
|
| 263 |
if ( $include === self::SUPPORTED ) { |
| 264 |
$namesMwFile = []; |
| 265 |
|
| 266 |
|
| 267 |
foreach ( $returnMw as $code => $value ) { |
| 268 |
if ( is_readable( $this->getMessagesFileName( $code ) ) || |
| 269 |
is_readable( $this->getJsonMessagesFileName( $code ) ) |
| 270 |
) { |
| 271 |
$namesMwFile[$code] = $names[$code]; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
ksort( $namesMwFile ); |
| 276 |
return $namesMwFile; |
| 277 |
} |
| 278 |
|
| 279 |
ksort( $returnMw ); |
| 280 |
|
| 281 |
|
| 282 |
return $returnMw; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public function getLanguageName( $code, $inLanguage = self::AUTONYMS, $include = self::ALL ) { |
| 294 |
$code = LanguageCode::replaceDeprecatedCodes( LanguageCode::bcp47ToInternal( $code ) ); |
| 295 |
$array = $this->getLanguageNames( $inLanguage, $include ); |
| 296 |
return $array[$code] ?? ''; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public function getFileName( $prefix, $code, $suffix = '.php' ) { |
| 308 |
if ( !$this->isValidBuiltInCode( $code ) ) { |
| 309 |
throw new InvalidArgumentException( "Invalid language code \"$code\"" ); |
| 310 |
} |
| 311 |
|
| 312 |
return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function getMessagesFileName( $code ) { |
| 320 |
global $IP; |
| 321 |
$file = $this->getFileName( "$IP/languages/messages/Messages", $code, '.php' ); |
| 322 |
$this->hookRunner->onLanguage__getMessagesFileName( $code, $file ); |
| 323 |
return $file; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function getJsonMessagesFileName( $code ) { |
| 331 |
global $IP; |
| 332 |
|
| 333 |
if ( !$this->isValidBuiltInCode( $code ) ) { |
| 334 |
throw new InvalidArgumentException( "Invalid language code \"$code\"" ); |
| 335 |
} |
| 336 |
|
| 337 |
return "$IP/languages/i18n/$code.json"; |
| 338 |
} |
| 339 |
} |