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
This commit is contained in:
C. Scott Ananian 2024-01-11 16:35:01 -05:00
parent 99bae67a4b
commit e2c9ca37b0
2 changed files with 6 additions and 33 deletions

View file

@ -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 ) {

View file

@ -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();