Find comment: limit DB query for page comments, use a custom exception

Follow-up to b7af4e87f5

Bug: T374598
Change-Id: Ibc958f84991198ebb616cf41258e70968353f7fe
This commit is contained in:
David Lynch 2024-09-18 11:25:40 -05:00
parent b2e1dda6a7
commit 9492fcc1f8
3 changed files with 12 additions and 4 deletions

View file

@ -8,7 +8,6 @@ use ApiUsageException;
use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseThreadItem;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleFormatter;
use Wikimedia\NormalizedException\NormalizedException;
use Wikimedia\ParamValidator\ParamValidator;
class ApiDiscussionToolsFindComment extends ApiBase {
@ -66,7 +65,7 @@ class ApiDiscussionToolsFindComment extends ApiBase {
$byHeading = $this->threadItemStore->findNewestRevisionsByHeading(
$heading, $articleId, $title->getTitleValue()
);
} catch ( NormalizedException $e ) {
} catch ( PageNeverHadThreadsException $e ) {
$this->dieWithError( [ 'apierror-discussiontools-findcomment-pagenevertalk' ] );
}
foreach ( $byHeading as $item ) {

View file

@ -0,0 +1,8 @@
<?php
namespace MediaWiki\Extension\DiscussionTools;
use Wikimedia\NormalizedException\NormalizedException;
class PageNeverHadThreadsException extends NormalizedException {
}

View file

@ -268,12 +268,13 @@ class ThreadItemStore {
) ) )
// On the specified page ID
->where( [ 'rev_page' => $articleId ] )
->field( 'itid_itemid' );
->field( 'itid_itemid' )
->limit( 1 );
// Check there is only one result in the sub-query
$itemIds = $anyItemsInPageHistoryQueryBuilder->fetchFieldValues();
if ( count( $itemIds ) === 0 ) {
throw new NormalizedException(
throw new PageNeverHadThreadsException(
"Page {page} has never contained any discussions",
[ 'page' => $articleId ]
);