Merge "LintUpdate: use content handler instead of directly invoking ParsoidParser"

This commit is contained in:
jenkins-bot 2024-09-27 19:01:51 +00:00 committed by Gerrit Code Review
commit 75cd94bc4f
4 changed files with 40 additions and 37 deletions

View file

@ -30,7 +30,6 @@
"services": [
"LinkRenderer",
"JobQueueGroup",
"ParsoidParserFactory",
"WikiPageFactory",
"Linter.CategoryManager",
"Linter.TotalsLookup",

View file

@ -38,7 +38,6 @@ use MediaWiki\Output\OutputPage;
use MediaWiki\Page\Hook\RevisionFromEditCompleteHook;
use MediaWiki\Page\Hook\WikiPageDeletionUpdatesHook;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
use MediaWiki\Revision\RenderedRevision;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\SpecialPage\SpecialPage;
@ -59,7 +58,6 @@ class Hooks implements
{
private LinkRenderer $linkRenderer;
private JobQueueGroup $jobQueueGroup;
private ParsoidParserFactory $parsoidParserFactory;
private WikiPageFactory $wikiPageFactory;
private CategoryManager $categoryManager;
private TotalsLookup $totalsLookup;
@ -69,7 +67,6 @@ class Hooks implements
/**
* @param LinkRenderer $linkRenderer
* @param JobQueueGroup $jobQueueGroup
* @param ParsoidParserFactory $parsoidParserFactory
* @param WikiPageFactory $wikiPageFactory
* @param CategoryManager $categoryManager
* @param TotalsLookup $totalsLookup
@ -78,7 +75,6 @@ class Hooks implements
public function __construct(
LinkRenderer $linkRenderer,
JobQueueGroup $jobQueueGroup,
ParsoidParserFactory $parsoidParserFactory,
WikiPageFactory $wikiPageFactory,
CategoryManager $categoryManager,
TotalsLookup $totalsLookup,
@ -87,7 +83,6 @@ class Hooks implements
) {
$this->linkRenderer = $linkRenderer;
$this->jobQueueGroup = $jobQueueGroup;
$this->parsoidParserFactory = $parsoidParserFactory;
$this->wikiPageFactory = $wikiPageFactory;
$this->categoryManager = $categoryManager;
$this->totalsLookup = $totalsLookup;
@ -363,7 +358,6 @@ class Hooks implements
}
$updates[] = new LintUpdate(
$this->parsoidParserFactory->create(),
$this->wikiPageFactory,
$renderedRevision
);

View file

@ -20,28 +20,25 @@
namespace MediaWiki\Linter;
use MediaWiki\Content\Renderer\ContentParseParams;
use MediaWiki\Content\TextContent;
use MediaWiki\Deferred\DataUpdate;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Parser\Parsoid\ParsoidParser;
use MediaWiki\Revision\RenderedRevision;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
class LintUpdate extends DataUpdate {
private ParsoidParser $parsoid;
private WikiPageFactory $wikiPageFactory;
private RenderedRevision $renderedRevision;
public function __construct(
ParsoidParser $parsoid,
WikiPageFactory $wikiPageFactory,
RenderedRevision $renderedRevision
) {
parent::__construct();
$this->parsoid = $parsoid;
$this->wikiPageFactory = $wikiPageFactory;
$this->renderedRevision = $renderedRevision;
}
@ -80,13 +77,15 @@ class LintUpdate extends DataUpdate {
// However, unlike RefreshLinksJob, we don't parse if we already
// have the output in the cache. This avoids duplicating the effort
// of ParsoidCachePrewarmJob.
$this->parsoid->parse(
$content->getText(),
$cpoParams = new ContentParseParams(
$rev->getPage(),
$rev->getId(),
$pOptions,
true,
true,
$rev->getId()
// no need to generate HTML
false,
// XXX no previous output available
null
);
$content->getContentHandler()->getParserOutput( $content, $cpoParams );
}
}

View file

@ -21,10 +21,11 @@
namespace MediaWiki\Linter\Test;
use MediaWiki\Content\JavaScriptContent;
use MediaWiki\Content\JavaScriptContentHandler;
use MediaWiki\Content\WikitextContentHandler;
use MediaWiki\Linter\LintUpdate;
use MediaWiki\MainConfigNames;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Parser\Parsoid\ParsoidParser;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\RenderedRevision;
use MediaWiki\Revision\RevisionRecord;
@ -54,8 +55,6 @@ class LintUpdateTest extends MediaWikiIntegrationTestCase {
* and in turn triggers the ParserLogLinterData hook via Parsoid.
*/
public function testUpdate() {
$parsoid = $this->getServiceContainer()->getParsoidParserFactory()->create();
// NOTE: This performs an edit, so do it before installing the temp hook below!
$rrev = $this->newRenderedRevision();
@ -64,7 +63,7 @@ class LintUpdateTest extends MediaWikiIntegrationTestCase {
$hookCalled = true;
}, false );
$update = $this->newLintUpdate( $parsoid, $rrev );
$update = $this->newLintUpdate( $rrev );
$update->doUpdate();
$this->assertTrue( $hookCalled );
@ -74,23 +73,28 @@ class LintUpdateTest extends MediaWikiIntegrationTestCase {
* Assert that we don't parse if the content model is not supported.
*/
public function testSkipModel() {
$parsoid = $this->createNoOpMock(
ParsoidParser::class,
[ 'parse' ]
$contentHandler = $this->createNoOpMock(
JavaScriptContentHandler::class,
[ 'getParserOutput' ]
);
$parsoid->expects( $this->never() )->method( 'parse' );
$contentHandler->expects( $this->never() )->method( 'getParserOutput' );
$contentHandlers = $this->getConfVar( MainConfigNames::ContentHandlers );
$this->overrideConfigValue( MainConfigNames::ContentHandlers, [
CONTENT_MODEL_JAVASCRIPT => [
'factory' => fn () => $contentHandler,
],
] + $contentHandlers );
$page = $this->getExistingTestPage();
$rev = new MutableRevisionRecord( $page );
$rev->setSlot(
SlotRecord::newUnsaved(
SlotRecord::MAIN,
new JavaScriptContent( '{}' )
new JavascriptContent( '{}' )
)
);
$update = $this->newLintUpdate( $parsoid, $this->newRenderedRevision( $page, $rev ) );
$update = $this->newLintUpdate( $this->newRenderedRevision( $page, $rev ) );
$update->doUpdate();
}
@ -99,13 +103,7 @@ class LintUpdateTest extends MediaWikiIntegrationTestCase {
* latest revision.
*/
public function testSkipOld() {
$parsoid = $this->createNoOpMock(
ParsoidParser::class,
[ 'parse' ]
);
$parsoid->expects( $this->never() )->method( 'parse' );
// This may use the "real" wikitext content handler
$page = $this->getExistingTestPage();
$rev = new MutableRevisionRecord( $page );
$rev->setSlot(
@ -117,8 +115,22 @@ class LintUpdateTest extends MediaWikiIntegrationTestCase {
// make it not the current revision
$rev->setId( $page->getLatest() - 1 );
$newRev = $this->newRenderedRevision( $page, $rev );
$update = $this->newLintUpdate( $parsoid, $this->newRenderedRevision( $page, $rev ) );
// Ok, now set up a mock content handler for the remainder
$contentHandler = $this->createNoOpMock(
WikitextContentHandler::class,
[ 'getParserOutput' ]
);
$contentHandler->expects( $this->never() )->method( 'getParserOutput' );
$contentHandlers = $this->getConfVar( MainConfigNames::ContentHandlers );
$this->overrideConfigValue( MainConfigNames::ContentHandlers, [
CONTENT_MODEL_WIKITEXT => [
'factory' => fn () => $contentHandler,
],
] + $contentHandlers );
$update = $this->newLintUpdate( $newRev );
$update->doUpdate();
}
@ -183,11 +195,10 @@ class LintUpdateTest extends MediaWikiIntegrationTestCase {
return $rrev;
}
private function newLintUpdate( ParsoidParser $parsoid, RenderedRevision $renderedRevision ) {
private function newLintUpdate( RenderedRevision $renderedRevision ) {
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
return new LintUpdate(
$parsoid,
$wikiPageFactory,
$renderedRevision
);