Use ParserCache for local file description renders

Currently for every File page view, if the file is local,
CommonsMetadata extension renders the file page twice -
once to extract the metadata, and once to show the page.
Metadata extraction parse was always uncached, so let's
at least use PoolCounter and ParserCache for this parse.

Bug: T292302
Change-Id: If6e1a1a72d794f4fb87105b7528ea0afe92a585f
This commit is contained in:
Petr Pchelko 2021-10-05 19:40:02 -07:00 committed by Ppchelko
parent 85c0575e9a
commit 47e8872398
2 changed files with 32 additions and 18 deletions

View file

@ -25,7 +25,6 @@ use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\Authority;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Storage\BlobStore;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserIdentityValue;
@ -2539,28 +2538,29 @@ class LocalFile extends File {
return false; // Avoid hard failure when the file does not exist. T221812
}
$store = MediaWikiServices::getInstance()->getRevisionStore();
$revision = $store->getRevisionByTitle( $this->title, 0, RevisionStore::READ_NORMAL );
if ( !$revision ) {
$services = MediaWikiServices::getInstance();
$page = $services->getPageStore()->getPageByReference( $this->getTitle() );
if ( !$page ) {
return false;
}
$renderer = MediaWikiServices::getInstance()->getRevisionRenderer();
$rendered = $renderer->getRenderedRevision(
$revision,
ParserOptions::newFromUserAndLang(
if ( $lang ) {
$parserOptions = ParserOptions::newFromUserAndLang(
RequestContext::getMain()->getUser(),
$lang
)
);
if ( !$rendered ) {
// audience check failed
return false;
);
} else {
$parserOptions = ParserOptions::newFromContext( RequestContext::getMain() );
}
$pout = $rendered->getRevisionParserOutput();
return $pout->getText();
$parseStatus = $services->getParserOutputAccess()
->getParserOutput( $page, $parserOptions );
if ( !$parseStatus->isGood() ) {
// Rendering failed.
return false;
}
return $parseStatus->getValue()->getText();
}
/**

View file

@ -498,13 +498,27 @@ class LocalFileTest extends MediaWikiIntegrationTestCase {
}
/**
* @covers File::getDescriptionText
* @covers LocalFile::getDescriptionText
*/
public function testDescriptionTextForNonExistingFile() {
public function testDescriptionText_NonExisting() {
$file = ( new LocalRepo( self::getDefaultInfo() ) )->newFile( 'test!' );
$this->assertFalse( $file->getDescriptionText() );
}
/**
* @covers LocalFile::getDescriptionText
*/
public function testDescriptionText_Existing() {
$this->assertTrue( $this->editPage(
__METHOD__,
'TEST CONTENT',
'',
NS_FILE
)->isOK() );
$file = ( new LocalRepo( self::getDefaultInfo() ) )->newFile( __METHOD__ );
$this->assertStringContainsString( 'TEST CONTENT', $file->getDescriptionText() );
}
public function provideLoadFromDBAndCache() {
return [
'legacy' => [