Merge "Use RevisionLookup methods instead of ApiParsoidTrait methods"

This commit is contained in:
jenkins-bot 2022-10-14 14:16:46 +00:00 committed by Gerrit Code Review
commit 669cbcea37
5 changed files with 24 additions and 50 deletions

View file

@ -179,6 +179,7 @@
"visualeditor": {
"class": "MediaWiki\\Extension\\VisualEditor\\ApiVisualEditor",
"services": [
"RevisionLookup",
"UserNameUtils",
"Parser",
"LinkRenderer",

View file

@ -26,7 +26,6 @@
"apierror-visualeditor-docserver-http-error": "Error contacting the Parsoid/RESTBase server: $1",
"apierror-visualeditor-docserver-http": "Error contacting the Parsoid/RESTBase server (HTTP $1): $2",
"apierror-visualeditor-docserver": "Error contacting the Parsoid/RESTBase server (no response)",
"apierror-visualeditor-latestnotfound": "Could not find latest revision for title",
"apihelp-visualeditor-description": "Returns HTML5 for a page from the Parsoid service.",
"apihelp-visualeditor-param-basetimestamp": "When saving, set this to the timestamp of the revision that was edited. Used to detect edit conflicts.",
"apihelp-visualeditor-param-badetag": "If RESTBase query returned a seemingly invalid ETag, pass it here for logging purposes.",

View file

@ -35,7 +35,6 @@
"apierror-visualeditor-docserver-http-error": "{{Related|apierror-visualeditor-docserver}}\n{{doc-apierror}}\n\nParameters:\n* $1 - Error message in English",
"apierror-visualeditor-docserver-http": "{{Related|apierror-visualeditor-docserver}}\n{{doc-apierror}}\n\nParameters:\n* $1 - HTTP status code (numeric)\n* $2 - Error message in English",
"apierror-visualeditor-docserver": "{{Related|apierror-visualeditor-docserver}}\n{{doc-apierror}}",
"apierror-visualeditor-latestnotfound": "{{doc-apierror}}",
"apihelp-visualeditor-description": "{{doc-apihelp-description|visualeditor}}",
"apihelp-visualeditor-param-basetimestamp": "{{doc-apihelp-param|visualeditor|basetimestamp}}",
"apihelp-visualeditor-param-badetag": "{{doc-apihelp-param|visualeditor|badetag}}",

View file

@ -40,46 +40,6 @@ trait ApiParsoidTrait {
$this->logger = $logger;
}
/**
* Get the latest revision of a title
*
* @param Title $title Page title
* @return RevisionRecord A revision record
*/
protected function getLatestRevision( Title $title ): RevisionRecord {
$revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
$latestRevision = $revisionLookup->getRevisionByTitle( $title );
if ( $latestRevision !== null ) {
return $latestRevision;
}
$this->dieWithError( 'apierror-visualeditor-latestnotfound', 'latestnotfound' );
}
/**
* Get a specific revision of a title
*
* If the oldid is ommitted or is 0, the latest revision will be fetched.
*
* If the oldid is invalid, an API error will be reported.
*
* @param Title|null $title Page title, not required if $oldid is used
* @param int|string|null $oldid Optional revision ID.
* Should be an integer but will validate and convert user input strings.
* @return RevisionRecord A revision record
*/
protected function getValidRevision( Title $title = null, $oldid = null ): RevisionRecord {
$revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
if ( $oldid === null || $oldid === 0 ) {
return $this->getLatestRevision( $title );
} else {
$revisionRecord = $revisionLookup->getRevisionById( $oldid );
if ( $revisionRecord ) {
return $revisionRecord;
}
}
$this->dieWithError( [ 'apierror-nosuchrevid', $oldid ], 'oldidnotfound' );
}
/**
* @param array $response
*/

View file

@ -31,6 +31,7 @@ use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Permissions\RestrictionStore;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\SpecialPage\SpecialPageFactory;
use MediaWiki\User\UserFactory;
@ -52,6 +53,9 @@ class ApiVisualEditor extends ApiBase {
use ApiBlockInfoTrait;
use ApiParsoidTrait;
/** @var RevisionLookup */
private $revisionLookup;
/** @var UserNameUtils */
private $userNameUtils;
@ -61,7 +65,7 @@ class ApiVisualEditor extends ApiBase {
/** @var LinkRenderer */
private $linkRenderer;
/** @var userOptionsLookup */
/** @var UserOptionsLookup */
private $userOptionsLookup;
/** @var WatchlistManager */
@ -94,6 +98,7 @@ class ApiVisualEditor extends ApiBase {
/**
* @param ApiMain $main
* @param string $name
* @param RevisionLookup $revisionLookup
* @param UserNameUtils $userNameUtils
* @param Parser $parser
* @param LinkRenderer $linkRenderer
@ -111,6 +116,7 @@ class ApiVisualEditor extends ApiBase {
public function __construct(
ApiMain $main,
$name,
RevisionLookup $revisionLookup,
UserNameUtils $userNameUtils,
Parser $parser,
LinkRenderer $linkRenderer,
@ -127,6 +133,7 @@ class ApiVisualEditor extends ApiBase {
) {
parent::__construct( $main, $name );
$this->setLogger( LoggerFactory::getInstance( 'VisualEditor' ) );
$this->revisionLookup = $revisionLookup;
$this->userNameUtils = $userNameUtils;
$this->parser = $parser;
$this->linkRenderer = $linkRenderer;
@ -241,11 +248,6 @@ class ApiVisualEditor extends ApiBase {
$this->dieWithError( 'apierror-pagecannotexist' );
}
$parserParams = [];
if ( isset( $params['oldid'] ) ) {
$parserParams['oldid'] = $params['oldid'];
}
wfDebugLog( 'visualeditor', "called on '$title' with paction: '{$params['paction']}'" );
switch ( $params['paction'] ) {
case 'parse':
@ -263,8 +265,21 @@ class ApiVisualEditor extends ApiBase {
// Get information about current revision
if ( $title->exists() ) {
$revision = $this->getValidRevision( $title, $parserParams['oldid'] ?? null );
$latestRevision = $this->getLatestRevision( $title );
$latestRevision = $this->revisionLookup->getRevisionByTitle( $title );
if ( !$latestRevision ) {
$this->dieWithError(
[ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ],
'nosuchrevid'
);
}
if ( isset( $params['oldid'] ) ) {
$revision = $this->revisionLookup->getRevisionById( $params['oldid'] );
if ( !$revision ) {
$this->dieWithError( [ 'apierror-nosuchrevid', $params['oldid'] ] );
}
} else {
$revision = $latestRevision;
}
$restoring = !$revision->isCurrent();
$baseTimestamp = $latestRevision->getTimestamp();