From ba41d323f9506b2caa362e6ce4b50d13252e7cbe Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Tue, 17 Sep 2024 13:29:51 -0400 Subject: [PATCH] LintUpdate: use content handler instead of directly invoking ParsoidParser We don't need to directly handle the ParsoidParserFactory in the LintUpdate job; use the existing ContentHandler pathways to reduce dependencies. Change-Id: I64a4556a74da4f735a5b562070c21310ecda36d1 --- extension.json | 1 - includes/Hooks.php | 6 ---- includes/LintUpdate.php | 17 +++++----- tests/phpunit/LintUpdateTest.php | 53 +++++++++++++++++++------------- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/extension.json b/extension.json index d0c659b5..f9ef37a0 100644 --- a/extension.json +++ b/extension.json @@ -30,7 +30,6 @@ "services": [ "LinkRenderer", "JobQueueGroup", - "ParsoidParserFactory", "WikiPageFactory", "Linter.CategoryManager", "Linter.TotalsLookup", diff --git a/includes/Hooks.php b/includes/Hooks.php index e2588059..eaa80c86 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -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 ); diff --git a/includes/LintUpdate.php b/includes/LintUpdate.php index a1941816..2fd71bef 100644 --- a/includes/LintUpdate.php +++ b/includes/LintUpdate.php @@ -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 ); } } diff --git a/tests/phpunit/LintUpdateTest.php b/tests/phpunit/LintUpdateTest.php index dae3cb0d..8ff50eea 100644 --- a/tests/phpunit/LintUpdateTest.php +++ b/tests/phpunit/LintUpdateTest.php @@ -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 );