Cache nonexistence of grammar transformations
Previously every call to Language::getGrammarTransformations() for a language without grammar transformations (i.e., almost all) would look for a transformations file using is_readable(). This is not going to be especially expensive, but it's presumably still more expensive than caching the result. The change is not actually testable right now without resorting to TestingAccessWrapper or somehow mocking the file access, but I threw in some basic tests for the method anyway. If later we decide how best to mock the file access, they can be updated to check that the cache is working properly. Change-Id: I76e0fb7dbb1602df4f353722aacbd5adfff3c053
This commit is contained in:
parent
0de9c47b50
commit
251f4811be
3 changed files with 54 additions and 9 deletions
|
|
@ -3765,14 +3765,9 @@ class Language {
|
|||
|
||||
$grammarDataFile = __DIR__ . "/data/grammarTransformations/{$this->getCode()}.json";
|
||||
|
||||
if ( !is_readable( $grammarDataFile ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$this->grammarTransformCache = FormatJson::decode(
|
||||
file_get_contents( $grammarDataFile ),
|
||||
true
|
||||
);
|
||||
$this->grammarTransformCache = is_readable( $grammarDataFile )
|
||||
? FormatJson::decode( file_get_contents( $grammarDataFile ), true )
|
||||
: [];
|
||||
|
||||
if ( $this->grammarTransformCache === null ) {
|
||||
throw new MWException( "Invalid grammar data for \"{$this->getCode()}\"." );
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use MediaWiki\Languages\LanguageFallback;
|
|||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use Wikimedia\TestingAccessWrapper;
|
||||
|
||||
class LanguageTest extends LanguageClassesTestCase {
|
||||
class LanguageIntegrationTest extends LanguageClassesTestCase {
|
||||
use LanguageNameUtilsTestTrait;
|
||||
|
||||
/** @var array Copy of $wgHooks from before we unset LanguageGetTranslatedLanguageNames */
|
||||
50
tests/phpunit/unit/languages/LanguageTest.php
Normal file
50
tests/phpunit/unit/languages/LanguageTest.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Languages\LanguageNameUtils;
|
||||
use MediaWiki\Languages\LanguageFallback;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass Language
|
||||
*/
|
||||
class LanguageTest extends MediaWikiUnitTestCase {
|
||||
/**
|
||||
* @param array $options Valid keys:
|
||||
* 'code'
|
||||
* 'grammarTransformCache'
|
||||
*/
|
||||
private function getObj( array $options = [] ) {
|
||||
return new Language(
|
||||
$options['code'] ?? 'en',
|
||||
$this->createNoOpMock( LocalisationCache::class ),
|
||||
$this->createNoOpMock( LanguageNameUtils::class ),
|
||||
$this->createNoOpMock( LanguageFallback::class )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getGrammarTransformations
|
||||
* @todo Test the exception case
|
||||
*/
|
||||
public function testGetGrammarTransformations() {
|
||||
global $IP;
|
||||
|
||||
// XXX Inject a value here instead of reading the filesystem (T231159)
|
||||
$expected = FormatJson::decode(
|
||||
file_get_contents( "$IP/languages/data/grammarTransformations/he.json" ), true );
|
||||
|
||||
$lang = $this->getObj( [ 'code' => 'he' ] );
|
||||
|
||||
$this->assertSame( $expected, $lang->getGrammarTransformations() );
|
||||
$this->assertSame( $expected, $lang->getGrammarTransformations() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getGrammarTransformations
|
||||
*/
|
||||
public function testGetGrammarTransformations_empty() {
|
||||
$lang = $this->getObj();
|
||||
|
||||
$this->assertSame( [], $lang->getGrammarTransformations() );
|
||||
$this->assertSame( [], $lang->getGrammarTransformations() );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue