ApiDiscussionToolsCompare: Deduplicate 'from'/'to' param handling

Change-Id: I0351228f50e9ea5a599d11b37f684b8e66553a83
This commit is contained in:
Bartosz Dziewoński 2024-04-26 03:38:05 +02:00
parent 1130ab2491
commit f3e96a72fa

View file

@ -32,6 +32,32 @@ class ApiDiscussionToolsCompare extends ApiBase {
$this->revisionLookup = $revisionLookup;
}
/**
* @throws ApiUsageException
*/
private function getRevision( array $params, string $prefix ): RevisionRecord {
if ( isset( $params["{$prefix}rev"] ) ) {
$rev = $this->revisionLookup->getRevisionById( $params["{$prefix}rev"] );
if ( !$rev ) {
$this->dieWithError( [ 'apierror-nosuchrevid', $params["{$prefix}rev"] ] );
}
} else {
$title = Title::newFromText( $params["{$prefix}title"] );
if ( !$title ) {
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params["{$prefix}title"] ) ] );
}
$rev = $this->revisionLookup->getRevisionByTitle( $title );
if ( !$rev ) {
$this->dieWithError(
[ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ],
'nosuchrevid'
);
}
}
return $rev;
}
/**
* @inheritDoc
* @throws ApiUsageException
@ -42,25 +68,7 @@ class ApiDiscussionToolsCompare extends ApiBase {
$this->requireOnlyOneParameter( $params, 'fromtitle', 'fromrev' );
$this->requireOnlyOneParameter( $params, 'totitle', 'torev' );
if ( isset( $params['torev'] ) ) {
$toRev = $this->revisionLookup->getRevisionById( $params['torev'] );
if ( !$toRev ) {
$this->dieWithError( [ 'apierror-nosuchrevid', $params['torev'] ] );
}
} else {
$toTitle = Title::newFromText( $params['totitle'] );
if ( !$toTitle ) {
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['totitle'] ) ] );
}
$toRev = $this->revisionLookup->getRevisionByTitle( $toTitle );
if ( !$toRev ) {
$this->dieWithError(
[ 'apierror-missingrev-title', wfEscapeWikiText( $toTitle->getPrefixedText() ) ],
'nosuchrevid'
);
}
}
$toRev = $this->getRevision( $params, 'to' );
// When polling for new comments this is an important optimisation,
// as usually there is no new revision.
@ -69,25 +77,7 @@ class ApiDiscussionToolsCompare extends ApiBase {
return;
}
if ( isset( $params['fromrev'] ) ) {
$fromRev = $this->revisionLookup->getRevisionById( $params['fromrev'] );
if ( !$fromRev ) {
$this->dieWithError( [ 'apierror-nosuchrevid', $params['fromrev'] ] );
}
} else {
$fromTitle = Title::newFromText( $params['fromtitle'] );
if ( !$fromTitle ) {
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['fromtitle'] ) ] );
}
$fromRev = $this->revisionLookup->getRevisionByTitle( $fromTitle );
if ( !$fromRev ) {
$this->dieWithError(
[ 'apierror-missingrev-title', wfEscapeWikiText( $fromTitle->getPrefixedText() ) ],
'nosuchrevid'
);
}
}
$fromRev = $this->getRevision( $params, 'from' );
if ( $fromRev->hasSameContent( $toRev ) ) {
$this->addResult( $fromRev, $toRev );