From f3e96a72fa30e33d75071c247dee1f09cf87b885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Fri, 26 Apr 2024 03:38:05 +0200 Subject: [PATCH] ApiDiscussionToolsCompare: Deduplicate 'from'/'to' param handling Change-Id: I0351228f50e9ea5a599d11b37f684b8e66553a83 --- includes/ApiDiscussionToolsCompare.php | 66 +++++++++++--------------- 1 file changed, 28 insertions(+), 38 deletions(-) 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 );