diff --git a/includes/ApiDiscussionToolsCompare.php b/includes/ApiDiscussionToolsCompare.php index b67acf590..0ac607fa7 100644 --- a/includes/ApiDiscussionToolsCompare.php +++ b/includes/ApiDiscussionToolsCompare.php @@ -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 );