From e2c9ca37b0c893c213790080a8333812a09a3b3f Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Thu, 11 Jan 2024 16:35:01 -0500 Subject: [PATCH] Use Parsoid HTML to drive notifications This reuses the existing comment parser code in HookUtils and makes the DataUpdatesHooks and EventDispatcher code consistent. Bug: T354792 Change-Id: I58a71b26b3f47898a68a29098a8105ee844403dd --- includes/Hooks/EchoHooks.php | 2 ++ includes/Notifications/EventDispatcher.php | 37 +++------------------- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/includes/Hooks/EchoHooks.php b/includes/Hooks/EchoHooks.php index cf14de17a..1dcf8f0b5 100644 --- a/includes/Hooks/EchoHooks.php +++ b/includes/Hooks/EchoHooks.php @@ -21,6 +21,7 @@ use MediaWiki\Extension\Notifications\Hooks\EchoGetBundleRulesHook; use MediaWiki\Extension\Notifications\Hooks\EchoGetEventsForRevisionHook; use MediaWiki\Extension\Notifications\Model\Event; use MediaWiki\Revision\RevisionRecord; +use Wikimedia\Parsoid\Core\ResourceLimitExceededException; class EchoHooks implements BeforeCreateEchoEventHook, @@ -130,6 +131,7 @@ class EchoHooks implements * @param array &$events * @param RevisionRecord $revision * @param bool $isRevert + * @throws ResourceLimitExceededException */ public function onEchoGetEventsForRevision( array &$events, RevisionRecord $revision, bool $isRevert ) { if ( $isRevert ) { diff --git a/includes/Notifications/EventDispatcher.php b/includes/Notifications/EventDispatcher.php index 0b08a0863..473236df9 100644 --- a/includes/Notifications/EventDispatcher.php +++ b/includes/Notifications/EventDispatcher.php @@ -16,7 +16,6 @@ use ExtensionRegistry; use IDBAccessObject; use Iterator; use MediaWiki\Deferred\DeferredUpdates; -use MediaWiki\Extension\DiscussionTools\CommentParser; use MediaWiki\Extension\DiscussionTools\CommentUtils; use MediaWiki\Extension\DiscussionTools\ContentThreadItemSet; use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils; @@ -33,12 +32,10 @@ use MediaWiki\MediaWikiServices; use MediaWiki\Page\PageIdentity; use MediaWiki\Revision\RevisionRecord; use MediaWiki\Title\Title; -use MediaWiki\Title\TitleValue; use MediaWiki\User\UserIdentity; -use ParserOptions; use RequestContext; -use RuntimeException; use Wikimedia\Assert\Assert; +use Wikimedia\Parsoid\Core\ResourceLimitExceededException; use Wikimedia\Parsoid\Utils\DOMCompat; use Wikimedia\Parsoid\Utils\DOMUtils; @@ -46,42 +43,16 @@ class EventDispatcher { /** * @param RevisionRecord $revRecord * @return ContentThreadItemSet + * @throws ResourceLimitExceededException */ private static function getParsedRevision( RevisionRecord $revRecord ): ContentThreadItemSet { - $services = MediaWikiServices::getInstance(); - - $pageRecord = $services->getPageStore()->getPageById( $revRecord->getPageId() ) ?: - $services->getPageStore()->getPageById( $revRecord->getPageId(), IDBAccessObject::READ_LATEST ); - - Assert::postcondition( $pageRecord !== null, 'Revision had no page' ); - - // If the $revRecord was fetched from the primary database, this will also fetch the content - // from the primary database (using the same query flags) - // TODO: Fetch HTML from Parsoid, as we do in onRevisionDataUpdates. - $status = $services->getParserOutputAccess()->getParserOutput( - $pageRecord, - ParserOptions::newFromAnon(), - $revRecord - ); - if ( !$status->isOK() ) { - throw new RuntimeException( 'Could not load revision for notifications' ); - } - - $title = TitleValue::newFromPage( $revRecord->getPage() ); - - $parserOutput = $status->getValue(); - $html = $parserOutput->getText(); - - $doc = DOMUtils::parseHTML( $html ); - $container = DOMCompat::getBody( $doc ); - /** @var CommentParser $parser */ - $parser = $services->getService( 'DiscussionTools.CommentParser' ); - return $parser->parse( $container, $title ); + return HookUtils::parseRevisionParsoidHtml( $revRecord, __METHOD__ ); } /** * @param array &$events * @param RevisionRecord $newRevRecord + * @throws ResourceLimitExceededException */ public static function generateEventsForRevision( array &$events, RevisionRecord $newRevRecord ): void { $services = MediaWikiServices::getInstance();