EventDispatcher: Ensure we fetch page content from the primary database

We used an internal API requests to fetch page content because it was
easy, but there's no way to guarantee that it returns data from the
primary database.

Use ParserOutputAccess::getParserOutput() to fetch from cache if
available. Also, use canonical output instead of user-specific,
not that it should matter.

Bug: T285895
Change-Id: I7dcd9659be77746dc2a0c4eeae2319887936b555
This commit is contained in:
Bartosz Dziewoński 2021-06-30 22:24:29 +02:00
parent 12f5a5365e
commit 37d6825c14

View file

@ -9,11 +9,9 @@
namespace MediaWiki\Extension\DiscussionTools\Notifications; namespace MediaWiki\Extension\DiscussionTools\Notifications;
use ApiMain;
use DOMElement; use DOMElement;
use EchoEvent; use EchoEvent;
use Error; use Error;
use FauxRequest;
use IDBAccessObject; use IDBAccessObject;
use Iterator; use Iterator;
use MediaWiki\Extension\DiscussionTools\CommentParser; use MediaWiki\Extension\DiscussionTools\CommentParser;
@ -22,30 +20,37 @@ use MediaWiki\Extension\DiscussionTools\SubscriptionItem;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionRecord; use MediaWiki\Revision\RevisionRecord;
use MediaWiki\User\UserIdentity; use MediaWiki\User\UserIdentity;
use ParserOptions;
use Title; use Title;
use Wikimedia\Assert\Assert;
use Wikimedia\Parsoid\Utils\DOMUtils; use Wikimedia\Parsoid\Utils\DOMUtils;
class EventDispatcher { class EventDispatcher {
/** /**
* @param RevisionRecord $revision * @param RevisionRecord $revRecord
* @return CommentParser * @return CommentParser
*/ */
private static function getParsedRevision( RevisionRecord $revision ) : CommentParser { private static function getParsedRevision( RevisionRecord $revRecord ) : CommentParser {
$api = new ApiMain( $services = MediaWikiServices::getInstance();
new FauxRequest(
[ $pageRecord = $services->getPageStore()->getPageByReference( $revRecord->getPage() );
'action' => 'parse', Assert::postcondition( $pageRecord !== null, 'Revision had no page' );
'oldid' => $revision->getId()
], // If the $revRecord was fetched from the primary database, this will also fetch the content
/* was posted? */ true // from the primary database (using the same query flags)
), $status = $services->getParserOutputAccess()->getParserOutput(
/* enable write? */ true $pageRecord,
ParserOptions::newCanonical( 'canonical' ),
$revRecord
); );
if ( !$status->isOK() ) {
throw new Error( 'Could not load revision for notifications' );
}
$api->execute(); $parserOutput = $status->getValue();
$data = $api->getResult()->getResultData(); $html = $parserOutput->getText();
$doc = DOMUtils::parseHTML( $data['parse']['text'] ); $doc = DOMUtils::parseHTML( $html );
$container = $doc->getElementsByTagName( 'body' )->item( 0 ); $container = $doc->getElementsByTagName( 'body' )->item( 0 );
if ( !( $container instanceof DOMElement ) ) { if ( !( $container instanceof DOMElement ) ) {
throw new Error( 'Could not load revision for notifications' ); throw new Error( 'Could not load revision for notifications' );