From ef7213a3d3fcf36b21d89b9514f0a4ab4e3daa21 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Mon, 19 Dec 2022 19:07:39 +1100 Subject: [PATCH] Clean up ApiDiscussionToolsPageInfo hack Treat non-talk pages as empty ContentThreadItemSet objects, instead of returning early from execute(). Exclude non-talk pages even if only the rev_id is specified. If both rev_id and title are specified, use the title from the database. Bug: T325477 Bug: T325598 Change-Id: I3947ac94bb7c9a3d24b73c95f0df2cf847c955f2 --- includes/ApiDiscussionToolsPageInfo.php | 91 +++++++++++++++---------- 1 file changed, 54 insertions(+), 37 deletions(-) diff --git a/includes/ApiDiscussionToolsPageInfo.php b/includes/ApiDiscussionToolsPageInfo.php index aaeadcf79..a0064cc02 100644 --- a/includes/ApiDiscussionToolsPageInfo.php +++ b/includes/ApiDiscussionToolsPageInfo.php @@ -4,6 +4,7 @@ namespace MediaWiki\Extension\DiscussionTools; use ApiBase; use ApiMain; +use ApiUsageException; use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils; use MediaWiki\Extension\DiscussionTools\ThreadItem\CommentItem; use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem; @@ -41,43 +42,7 @@ class ApiDiscussionToolsPageInfo extends ApiBase { public function execute() { $params = $this->extractRequestParams(); $this->requireAtLeastOneParameter( $params, 'page', 'oldid' ); - - if ( isset( $params['page'] ) ) { - $title = Title::newFromText( $params['page'] ); - if ( !$title ) { - $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] ); - } - if ( !HookUtils::isAvailableForTitle( $title ) ) { - $this->getResult()->addValue( - null, - $this->getModuleName(), - [ 'transcludedfrom' => [] ] - ); - return; - } - } - - if ( isset( $params['oldid'] ) ) { - $revision = $this->revisionLookup->getRevisionById( $params['oldid'] ); - if ( !$revision ) { - $this->dieWithError( [ 'apierror-nosuchrevid', $params['oldid'] ] ); - } - - } else { - $title = Title::newFromText( $params['page'] ); - if ( !$title ) { - $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] ); - } - $revision = $this->revisionLookup->getRevisionByTitle( $title ); - if ( !$revision ) { - $this->dieWithError( - [ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ], - 'nosuchrevid' - ); - } - } - - $threadItemSet = HookUtils::parseRevisionParsoidHtml( $revision ); + $threadItemSet = $this->getThreadItemSet( $params ); $result = []; $prop = array_fill_keys( $params['prop'], true ); @@ -93,6 +58,58 @@ class ApiDiscussionToolsPageInfo extends ApiBase { $this->getResult()->addValue( null, $this->getModuleName(), $result ); } + /** + * Get the thread item set for the specified revision + * + * @param array $params + * @return ContentThreadItemSet + */ + private function getThreadItemSet( $params ) { + if ( isset( $params['page'] ) ) { + $title = Title::newFromText( $params['page'] ); + if ( !$title ) { + throw ApiUsageException::newWithMessage( + $this, + [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] + ); + } + } + + if ( isset( $params['oldid'] ) ) { + $revision = $this->revisionLookup->getRevisionById( $params['oldid'] ); + if ( !$revision ) { + throw ApiUsageException::newWithMessage( + $this, + [ 'apierror-nosuchrevid', $params['oldid'] ] + ); + } + } else { + $title = Title::newFromText( $params['page'] ); + if ( !$title ) { + throw ApiUsageException::newWithMessage( + $this, + [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] + ); + } + $revision = $this->revisionLookup->getRevisionByTitle( $title ); + if ( !$revision ) { + throw ApiUsageException::newWithMessage( + $this, + [ 'apierror-missingrev-title', wfEscapeWikiText( $title->getPrefixedText() ) ], + 'nosuchrevid' + ); + } + } + $title = Title::castFromPageIdentity( $revision->getPage() ); + + if ( !$title || !HookUtils::isAvailableForTitle( $title ) ) { + // T325477: don't parse non-discussion pages + return new ContentThreadItemSet; + } + + return HookUtils::parseRevisionParsoidHtml( $revision ); + } + /** * Get transcluded=from data for a ContentThreadItemSet *