mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-09-25 03:08:42 +00:00
Factor out revision fetching and validation logic to ApiParsoidTrait
Change-Id: I7707b419afac2c11d7d0ca91a597ffc68e5e3fe4
This commit is contained in:
parent
dfe642af10
commit
703fbaa6a2
|
@ -9,6 +9,7 @@
|
|||
*/
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Revision\RevisionRecord;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
|
@ -165,6 +166,65 @@ trait ApiParsoidTrait {
|
|||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest revision of a title
|
||||
*
|
||||
* @param Title $title Page title
|
||||
* @return RevisionRecord A revision record
|
||||
*/
|
||||
protected function getLatestRevision( Title $title ) {
|
||||
$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 $title Page title
|
||||
* @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, $oldid = null ) {
|
||||
$revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
|
||||
$revision = null;
|
||||
if ( $oldid === null || $oldid === 0 ) {
|
||||
return $this->getLatestRevision( $title );
|
||||
} else {
|
||||
$revisionRecord = $revisionLookup->getRevisionById( $oldid );
|
||||
if ( $revisionRecord ) {
|
||||
return $revisionRecord;
|
||||
}
|
||||
}
|
||||
$this->dieWithError( [ 'apierror-nosuchrevid', $oldid ], 'oldidnotfound' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Request page HTML from RESTBase
|
||||
*
|
||||
* @param RevisionRecord $revision Page revision
|
||||
* @return array The RESTBase server's response
|
||||
*/
|
||||
protected function requestRestbasePageHtml( RevisionRecord $revision ) {
|
||||
$title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
|
||||
return $this->requestRestbase(
|
||||
$title,
|
||||
'GET',
|
||||
'page/html/' . urlencode( $title->getPrefixedDBkey() ) .
|
||||
'/' . $revision->getId() .
|
||||
'?redirect=false&stash=true',
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform HTML to wikitext via Parsoid through RESTbase.
|
||||
*
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
use MediaWiki\Block\DatabaseBlock;
|
||||
use MediaWiki\Logger\LoggerFactory;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Revision\RevisionStoreRecord;
|
||||
|
||||
class ApiVisualEditor extends ApiBase {
|
||||
|
||||
|
@ -144,26 +143,12 @@ class ApiVisualEditor extends ApiBase {
|
|||
|
||||
// Get information about current revision
|
||||
if ( $title->exists() ) {
|
||||
$revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
|
||||
$latestRevision = $revisionLookup->getRevisionByTitle( $title );
|
||||
if ( $latestRevision === null ) {
|
||||
$this->dieWithError( 'apierror-visualeditor-latestnotfound', 'latestnotfound' );
|
||||
}
|
||||
$revision = null;
|
||||
if ( !isset( $parserParams['oldid'] ) || $parserParams['oldid'] === 0 ) {
|
||||
$parserParams['oldid'] = $latestRevision->getId();
|
||||
$revision = $latestRevision;
|
||||
} else {
|
||||
$revision = $revisionLookup->getRevisionById( $parserParams['oldid'] );
|
||||
if ( $revision === null ) {
|
||||
$this->dieWithError( [ 'apierror-nosuchrevid', $parserParams['oldid'] ], 'oldidnotfound' );
|
||||
}
|
||||
}
|
||||
$revision = $this->getValidRevision( $title, $parserParams['oldid'] ?? null );
|
||||
$latestRevision = $this->getLatestRevision( $title );
|
||||
|
||||
$restoring = $revision &&
|
||||
!( $revision instanceof RevisionStoreRecord && $revision->isCurrent() );
|
||||
$restoring = !$revision->isCurrent();
|
||||
$baseTimestamp = $latestRevision->getTimestamp();
|
||||
$oldid = intval( $parserParams['oldid'] );
|
||||
$oldid = $revision->getId();
|
||||
|
||||
// If requested, request HTML from Parsoid/RESTBase
|
||||
if ( $params['paction'] === 'parse' ) {
|
||||
|
@ -187,13 +172,7 @@ class ApiVisualEditor extends ApiBase {
|
|||
$title, $wikitext, false, $oldid, $stash
|
||||
);
|
||||
} else {
|
||||
$response = $this->requestRestbase(
|
||||
$title,
|
||||
'GET',
|
||||
'page/html/' . urlencode( $title->getPrefixedDBkey() ) . '/' . $oldid .
|
||||
'?redirect=false&stash=true',
|
||||
[]
|
||||
);
|
||||
$response = $this->requestRestbasePageHtml( $revision );
|
||||
}
|
||||
$content = $response['body'];
|
||||
$restbaseHeaders = $response['headers'];
|
||||
|
|
Loading…
Reference in a new issue