Merge "Don't trust RESTBase to give us the correct revision"

This commit is contained in:
jenkins-bot 2020-08-26 20:27:22 +00:00 committed by Gerrit Code Review
commit f167bfc5b1

View file

@ -7,6 +7,7 @@ use ApiMain;
use ApiParsoidTrait; use ApiParsoidTrait;
use DerivativeRequest; use DerivativeRequest;
use DOMElement; use DOMElement;
use MediaWiki\Logger\LoggerFactory;
use Title; use Title;
use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\Parsoid\Utils\DOMCompat; use Wikimedia\Parsoid\Utils\DOMCompat;
@ -21,6 +22,7 @@ class ApiDiscussionToolsEdit extends ApiBase {
*/ */
public function __construct( ApiMain $main, string $name ) { public function __construct( ApiMain $main, string $name ) {
parent::__construct( $main, $name ); parent::__construct( $main, $name );
$this->setLogger( LoggerFactory::getInstance( 'DiscussionTools' ) );
} }
/** /**
@ -39,12 +41,37 @@ class ApiDiscussionToolsEdit extends ApiBase {
switch ( $params['paction'] ) { switch ( $params['paction'] ) {
case 'addcomment': case 'addcomment':
// Fetch the latest revision // Fetch the latest revision
$revision = $this->getLatestRevision( $title ); $requestedRevision = $this->getLatestRevision( $title );
$oldid = $revision->getId(); $response = $this->requestRestbasePageHtml( $requestedRevision );
$response = $this->requestRestbasePageHtml( $revision );
$headers = $response['headers'];
$headers = $response['headers'];
$doc = DOMUtils::parseHTML( $response['body'] ); $doc = DOMUtils::parseHTML( $response['body'] );
// Don't trust RESTBase to always give us the revision we requested,
// instead get the revision ID from the document and use that.
// Ported from ve.init.mw.ArticleTarget.prototype.parseMetadata
$docRevId = null;
$aboutDoc = $doc->documentElement->getAttribute( 'about' );
if ( $aboutDoc ) {
preg_match( '/revision\\/([0-9]+)$/', $aboutDoc, $docRevIdMatches );
if ( $docRevIdMatches ) {
$docRevId = (int)$docRevIdMatches[ 1 ];
}
}
if ( !$docRevId ) {
$this->dieWithError( 'apierror-visualeditor-docserver', 'docserver' );
}
if ( $docRevId !== $requestedRevision->getId() ) {
// TODO: If this never triggers, consider removing the check.
$this->getLogger()->warning(
"Requested revision {$requestedRevision->getId()} " .
"but received {$docRevId}."
);
}
$container = $doc->getElementsByTagName( 'body' )->item( 0 ); $container = $doc->getElementsByTagName( 'body' )->item( 0 );
'@phan-var DOMElement $container'; '@phan-var DOMElement $container';
@ -87,10 +114,10 @@ class ApiDiscussionToolsEdit extends ApiBase {
'paction' => 'save', 'paction' => 'save',
'page' => $params['page'], 'page' => $params['page'],
'token' => $params['token'], 'token' => $params['token'],
'oldid' => $oldid, 'oldid' => $docRevId,
'html' => DOMCompat::getOuterHTML( $doc->documentElement ), 'html' => DOMCompat::getOuterHTML( $doc->documentElement ),
'summary' => $summary, 'summary' => $summary,
'baserevid' => $revision->getId(), 'baserevid' => $docRevId,
'starttimestamp' => wfTimestampNow(), 'starttimestamp' => wfTimestampNow(),
'etag' => $headers['etag'], 'etag' => $headers['etag'],
'watchlist' => $params['watchlist'], 'watchlist' => $params['watchlist'],