Reduce usage of the Language class

reduce/deprecate visibility of some members of the Language class

Bug: T243913
Change-Id: I6bad608455ceaa46f895f00dcc6380cec6d32680
This commit is contained in:
ArtBaltai 2020-02-06 11:47:10 +03:00
parent b53231efcf
commit 31283f34bf
9 changed files with 143 additions and 32 deletions

View file

@ -74,13 +74,26 @@ class Language {
throw new RuntimeException( "Cannot get '$name' property." );
}
public $mVariants, $mCode, $mLoaded = false;
public $mMagicExtensions = [];
private $mHtmlCode = null;
/** @var Language|false */
private $mParentLanguage = false;
public $mCode;
/**
* @deprecated since 1.35, use LocalisationCache with custom language config
*/
public $mMagicExtensions = [];
private $mHtmlCode = null;
/**
* memoize
* @deprecated since 1.35, must be private
*/
public $dateFormatStrings = [];
/**
* memoize
* @var array[]
* @deprecated since 1.35, must be protected
*/
public $mExtendedSpecialPageAliases;
/** @var array|null */
@ -88,7 +101,8 @@ class Language {
protected $mNamespaceIds, $namespaceAliases;
/**
* ReplacementArray object caches
* ReplacementArray object memoize
* @deprecated since 1.35, must be private
*/
public $transformData = [];
@ -463,7 +477,7 @@ class Language {
}
/**
* Calling this directly is deprecated. Use LanguageFactory instead.
* @internal Calling this directly is deprecated. Use LanguageFactory instead.
*
* @param string|null $code Which code to use. Passing null is deprecated in 1.35.
* @param LocalisationCache|null $localisationCache
@ -2967,11 +2981,12 @@ class Language {
* @return string
*/
public function normalize( $s ) {
global $wgAllUnicodeFixes;
global $wgAllUnicodeFixes, $IP;
$s = UtfNormal\Validator::cleanUp( $s );
if ( $wgAllUnicodeFixes ) {
$s = $this->transformUsingPairFile( 'normalize-ar.php', $s );
$s = $this->transformUsingPairFile( 'normalize-ml.php', $s );
$s = $this->transformUsingPairFile( 'normalize-ar.php', $s, $IP );
$s = $this->transformUsingPairFile( 'normalize-ml.php', $s, $IP );
}
return $s;
@ -2987,17 +3002,40 @@ class Language {
*
* @param string $file
* @param string $string
* @param string|null $basePath
*
* @throws MWException
* @return string
*/
protected function transformUsingPairFile( $file, $string ) {
if ( !isset( $this->transformData[$file] ) ) {
global $IP;
$data = require "$IP/languages/data/{$file}";
$this->transformData[$file] = new ReplacementArray( $data );
protected function transformUsingPairFile( $file, $string, $basePath = null ) {
if ( isset( $this->transformData[$file] ) ) {
wfDeprecated(
__METHOD__ . ' structure of $transformData is changed',
'1.35'
);
return $this->transformData[$file]->replace( $string );
}
return $this->transformData[$file]->replace( $string );
if ( $basePath === null ) {
wfDeprecated( __METHOD__ . ' $basePath is required', '1.35' );
global $IP;
$basePath = $IP;
}
if (
$basePath === null
|| $basePath === ''
|| !file_exists( "{$basePath}/languages/data/{$file}" )
) {
return $string;
}
if ( !isset( $this->transformData[$basePath][$file] ) ) {
$data = require "{$basePath}/languages/data/{$file}";
$this->transformData[$basePath][$file] = new ReplacementArray( $data );
}
return $this->transformData[$basePath][$file]->replace( $string );
}
/**
@ -4776,6 +4814,8 @@ class Language {
/**
* Helper function for viewPrevNext() that generates links
*
* @deprecated since 1.35, used with {@link viewPrevNext} only
*
* @param Title $title Title object to link
* @param int $offset
* @param int $limit
@ -4815,8 +4855,8 @@ class Language {
public function getCompiledPluralRules() {
$pluralRules =
$this->localisationCache->getItem( strtolower( $this->mCode ), 'compiledPluralRules' );
$fallbacks = $this->langFallback->getAll( $this->mCode );
if ( !$pluralRules ) {
$fallbacks = $this->getFallbackLanguages();
foreach ( $fallbacks as $fallbackCode ) {
$pluralRules = $this->localisationCache
->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
@ -4836,8 +4876,8 @@ class Language {
public function getPluralRules() {
$pluralRules =
$this->localisationCache->getItem( strtolower( $this->mCode ), 'pluralRules' );
$fallbacks = $this->langFallback->getAll( $this->mCode );
if ( !$pluralRules ) {
$fallbacks = $this->getFallbackLanguages();
foreach ( $fallbacks as $fallbackCode ) {
$pluralRules = $this->localisationCache
->getItem( strtolower( $fallbackCode ), 'pluralRules' );
@ -4857,8 +4897,8 @@ class Language {
public function getPluralRuleTypes() {
$pluralRuleTypes =
$this->localisationCache->getItem( strtolower( $this->mCode ), 'pluralRuleTypes' );
$fallbacks = $this->langFallback->getAll( $this->mCode );
if ( !$pluralRuleTypes ) {
$fallbacks = $this->getFallbackLanguages();
foreach ( $fallbacks as $fallbackCode ) {
$pluralRuleTypes = $this->localisationCache
->getItem( strtolower( $fallbackCode ), 'pluralRuleTypes' );

View file

@ -41,8 +41,9 @@ class LanguageAr extends Language {
* @return string
*/
public function normalize( $s ) {
global $IP;
$s = parent::normalize( $s );
$s = $this->transformUsingPairFile( 'normalize-ar.php', $s );
$s = $this->transformUsingPairFile( 'normalize-ar.php', $s, $IP );
return $s;
}
}

View file

@ -42,8 +42,9 @@ class LanguageMl extends Language {
* @return string
*/
public function normalize( $s ) {
global $IP;
$s = parent::normalize( $s );
$s = $this->transformUsingPairFile( 'normalize-ml.php', $s );
$s = $this->transformUsingPairFile( 'normalize-ml.php', $s, $IP );
return $s;
}
}

View file

@ -32,16 +32,19 @@ require_once __DIR__ . '/../Maintenance.php';
* @ingroup MaintenanceLanguage
*/
class AllTrans extends Maintenance {
/** @var LocalisationCache */
private $localisationCache;
public function __construct() {
parent::__construct();
$this->addDescription( 'Get all messages as defined by the English language file' );
$this->localisationCache = MediaWikiServices::getInstance()->getLocalisationCache();
}
public function execute() {
$englishMessages = array_keys( MediaWikiServices::getInstance()
->getLocalisationCache()
->getItem( 'en', 'messages' ) );
foreach ( $englishMessages as $key ) {
$englishMessages = $this->localisationCache->getItem( 'en', 'messages' );
foreach ( array_keys( $englishMessages ) as $key ) {
$this->output( "$key\n" );
}
}

View file

@ -33,17 +33,20 @@ require_once __DIR__ . '/../Maintenance.php';
* @ingroup MaintenanceLanguage
*/
class DumpMessages extends Maintenance {
/** @var LocalisationCache */
private $localisationCache;
public function __construct() {
parent::__construct();
$this->addDescription( 'Dump an entire language, using the keys from English' );
$this->localisationCache = MediaWikiServices::getInstance()->getLocalisationCache();
}
public function execute() {
$messages = [];
foreach ( array_keys( MediaWikiServices::getInstance()
->getLocalisationCache()
->getItem( 'en', 'messages' ) ) as $key
) {
$localisationMessagesEn = $this->localisationCache->getItem( 'en', 'messages' );
foreach ( array_keys( $localisationMessagesEn ) as $key ) {
$messages[$key] = wfMessage( $key )->text();
}
$this->output( "MediaWiki " . MW_VERSION . " language file\n" );

View file

@ -1,5 +1,7 @@
<?php
use Wikimedia\TestingAccessWrapper;
/**
* @group Media
*/
@ -141,4 +143,39 @@ class FormatMetadataTest extends MediaWikiMediaTestCase {
],
];
}
/**
* @covers FormatMetadata::getPriorityLanguages
* @dataProvider provideGetPriorityLanguagesData
* @param string $languageClass
* @param string[] $expected
*/
public function testGetPriorityLanguagesInternal_language_expect(
string $languageClass,
array $expected
): void {
$formatMetadata = TestingAccessWrapper::newFromObject( new FormatMetadata() );
$context = $formatMetadata->getContext();
$context->setLanguage( new $languageClass() );
$x = $formatMetadata->getPriorityLanguages();
$this->assertSame( $expected, $x );
}
public function provideGetPriorityLanguagesData() {
return [
'LanguageMl' => [
LanguageMl::class,
[ 'ml', 'en' ],
],
'LanguageEn' => [
LanguageEn::class,
[ 'en', 'en' ],
],
'LanguageQqx' => [
LanguageQqx::class,
[ 'qqx', 'en' ],
],
];
}
}

View file

@ -2,6 +2,7 @@
use MediaWiki\MediaWikiServices;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
/**
* Factory for handling the special page list and generating SpecialPage objects.
@ -139,7 +140,8 @@ class SpecialPageFactoryTest extends MediaWikiTestCase {
$this->hideDeprecated( 'SpecialPageFactory::resolveAlias' );
$this->hideDeprecated( 'SpecialPageFactory::getLocalNameFor' );
$lang = clone MediaWikiServices::getInstance()->getContentLanguage();
$lang->mExtendedSpecialPageAliases = $aliasesList;
$wrappedLang = TestingAccessWrapper::newFromObject( $lang );
$wrappedLang->mExtendedSpecialPageAliases = $aliasesList;
$this->setMwGlobals( 'wgSpecialPages',
array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) )
);

View file

@ -30,7 +30,11 @@ class LanguageArTest extends LanguageClassesTestCase {
throw new Exception( 'Expected output must differ.' );
}
$this->assertSame( $expected, $this->getLang()->normalize( $input ), 'ar-normalised form' );
$this->assertSame(
$expected,
$this->getLang()->normalize( $input ),
'ar-normalised form'
);
}
public static function provideNormalize() {
@ -39,6 +43,14 @@ class LanguageArTest extends LanguageClassesTestCase {
'ﷅ',
'صمم',
],
[
'ﻴ',
'ي',
],
[
'',
'ه',
],
];
}

View file

@ -49,7 +49,11 @@ class LanguageMlTest extends LanguageClassesTestCase {
throw new Exception( 'Expected output must differ.' );
}
$this->assertSame( $expected, $this->getLang()->normalize( $input ), 'ml-normalised form' );
$this->assertSame(
$expected,
$this->getLang()->normalize( $input ),
'ml-normalised form'
);
}
public static function provideNormalize() {
@ -58,6 +62,14 @@ class LanguageMlTest extends LanguageClassesTestCase {
'ല്‍',
'ൽ',
],
[
'ര്‍',
'ർ',
],
[
'ള്‍',
'ൾ',
],
];
}
}