ThreadItemStore: Add ->caller() in queries

It makes it easier to find the queries in debug logs in production.

Change-Id: I80f94d1fc8c49227218b07234e162445cc43ca00
This commit is contained in:
Bartosz Dziewoński 2024-02-09 19:08:37 +01:00
parent ddea94a867
commit fc4d70f9f5

View file

@ -87,6 +87,7 @@ class ThreadItemStore {
} }
$queryBuilder = $this->getIdsNamesBuilder() $queryBuilder = $this->getIdsNamesBuilder()
->caller( __METHOD__ )
->where( [ ->where( [
'it_itemname' => $itemName, 'it_itemname' => $itemName,
// Disallow querying for headings of sections that contain no comments. // Disallow querying for headings of sections that contain no comments.
@ -125,7 +126,8 @@ class ThreadItemStore {
return []; return [];
} }
$queryBuilder = $this->getIdsNamesBuilder(); $queryBuilder = $this->getIdsNamesBuilder()
->caller( __METHOD__ );
// First find the name associated with the ID; then find by name. Otherwise we wouldn't find the // First find the name associated with the ID; then find by name. Otherwise we wouldn't find the
// latest revision in case comment ID changed, e.g. the comment was moved elsewhere on the page. // latest revision in case comment ID changed, e.g. the comment was moved elsewhere on the page.
@ -189,6 +191,7 @@ class ThreadItemStore {
// 1. Try to find items which have appeared on the page at some point // 1. Try to find items which have appeared on the page at some point
// in its history. // in its history.
$itemIdInPageHistoryQueryBuilder = $this->getIdsNamesBuilder() $itemIdInPageHistoryQueryBuilder = $this->getIdsNamesBuilder()
->caller( __METHOD__ . ' case 1' )
->join( 'revision', null, [ 'rev_id = itr_revision_id' ] ) ->join( 'revision', null, [ 'rev_id = itr_revision_id' ] )
->where( $dbw->expr( 'itid_itemid', IExpression::LIKE, new LikeValue( ->where( $dbw->expr( 'itid_itemid', IExpression::LIKE, new LikeValue(
'h-' . $heading . '-', 'h-' . $heading . '-',
@ -198,7 +201,8 @@ class ThreadItemStore {
->where( [ 'rev_page' => $articleId ] ) ->where( [ 'rev_page' => $articleId ] )
->field( 'itid_itemid' ); ->field( 'itid_itemid' );
$threadItems = $this->findNewestRevisionsByQuery( $itemIdInPageHistoryQueryBuilder, $limit ); $threadItems = $this->findNewestRevisionsByQuery( __METHOD__ . ' case 1',
$itemIdInPageHistoryQueryBuilder, $limit );
if ( count( $threadItems ) ) { if ( count( $threadItems ) ) {
return $threadItems; return $threadItems;
@ -208,6 +212,7 @@ class ThreadItemStore {
// then approach (1) may not work, instead look for matching headings the currently // then approach (1) may not work, instead look for matching headings the currently
// appear on sub-pages, which matches the archiving convention on most wikis. // appear on sub-pages, which matches the archiving convention on most wikis.
$itemIdInSubPageQueryBuilder = $this->getIdsNamesBuilder() $itemIdInSubPageQueryBuilder = $this->getIdsNamesBuilder()
->caller( __METHOD__ . ' case 2' )
->join( 'page', null, [ 'page_id = itp_page_id' ] ) ->join( 'page', null, [ 'page_id = itp_page_id' ] )
->where( $dbw->expr( 'itid_itemid', IExpression::LIKE, new LikeValue( ->where( $dbw->expr( 'itid_itemid', IExpression::LIKE, new LikeValue(
'h-' . $heading . '-', 'h-' . $heading . '-',
@ -220,7 +225,8 @@ class ThreadItemStore {
->where( [ 'page_namespace' => $title->getNamespace() ] ) ->where( [ 'page_namespace' => $title->getNamespace() ] )
->field( 'itid_itemid' ); ->field( 'itid_itemid' );
$threadItems = $this->findNewestRevisionsByQuery( $itemIdInSubPageQueryBuilder, $limit ); $threadItems = $this->findNewestRevisionsByQuery( __METHOD__ . ' case 2',
$itemIdInSubPageQueryBuilder, $limit );
if ( count( $threadItems ) ) { if ( count( $threadItems ) ) {
return $threadItems; return $threadItems;
@ -229,6 +235,7 @@ class ThreadItemStore {
// 3. Look for an "exact" match of the heading on any page. Because we are searching // 3. Look for an "exact" match of the heading on any page. Because we are searching
// so broadly, only return if there is exactly one match to the heading name. // so broadly, only return if there is exactly one match to the heading name.
$itemIdInAnyPageQueryBuilder = $this->getIdsNamesBuilder() $itemIdInAnyPageQueryBuilder = $this->getIdsNamesBuilder()
->caller( __METHOD__ . ' case 3' )
->join( 'page', null, [ 'page_id = itp_page_id', 'page_latest = itr_revision_id' ] ) ->join( 'page', null, [ 'page_id = itp_page_id', 'page_latest = itr_revision_id' ] )
->where( $dbw->expr( 'itid_itemid', IExpression::LIKE, new LikeValue( ->where( $dbw->expr( 'itid_itemid', IExpression::LIKE, new LikeValue(
'h-' . $heading . '-', 'h-' . $heading . '-',
@ -241,19 +248,20 @@ class ThreadItemStore {
// Check there is only one result in the sub-query // Check there is only one result in the sub-query
$itemIds = $itemIdInAnyPageQueryBuilder->fetchFieldValues(); $itemIds = $itemIdInAnyPageQueryBuilder->fetchFieldValues();
if ( count( $itemIds ) === 1 ) { if ( count( $itemIds ) === 1 ) {
return $this->findNewestRevisionsByQuery( $itemIds[ 0 ] ); return $this->findNewestRevisionsByQuery( __METHOD__ . ' case 3', $itemIds[ 0 ] );
} }
return []; return [];
} }
/** /**
* @param string $fname
* @param SelectQueryBuilder|string $itemIdOrQueryBuilder Sub-query which returns item ID's, or an itemID * @param SelectQueryBuilder|string $itemIdOrQueryBuilder Sub-query which returns item ID's, or an itemID
* @param int|null $limit * @param int|null $limit
* @return DatabaseThreadItem[] * @return DatabaseThreadItem[]
*/ */
private function findNewestRevisionsByQuery( $itemIdOrQueryBuilder, ?int $limit = 50 ): array { private function findNewestRevisionsByQuery( $fname, $itemIdOrQueryBuilder, ?int $limit = 50 ): array {
$queryBuilder = $this->getIdsNamesBuilder(); $queryBuilder = $this->getIdsNamesBuilder()->caller( $fname . ' / ' . __METHOD__ );
if ( $itemIdOrQueryBuilder instanceof SelectQueryBuilder ) { if ( $itemIdOrQueryBuilder instanceof SelectQueryBuilder ) {
$queryBuilder $queryBuilder
->where( [ ->where( [
@ -307,6 +315,7 @@ class ThreadItemStore {
// Parent item ID (the string, not just the primary key) // Parent item ID (the string, not just the primary key)
->leftJoin( ->leftJoin(
$this->getIdsNamesBuilder() $this->getIdsNamesBuilder()
->caller( __METHOD__ )
->fields( [ ->fields( [
'itr_parent__itr_id' => 'itr_id', 'itr_parent__itr_id' => 'itr_id',
'itr_parent__itid_itemid' => 'itid_itemid', 'itr_parent__itid_itemid' => 'itid_itemid',
@ -332,9 +341,10 @@ class ThreadItemStore {
$revs[ $row->itr_revision_id ] = null; $revs[ $row->itr_revision_id ] = null;
} }
$revQueryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() $revQueryBuilder = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
->queryInfo( $this->revStore->getQueryInfo( [ 'page' ] ) ) ->caller( __METHOD__ )
->fields( $this->pageStore->getSelectFields() ) ->queryInfo( $this->revStore->getQueryInfo( [ 'page' ] ) )
->where( $revs ? [ 'rev_id' => array_keys( $revs ) ] : '0=1' ); ->fields( $this->pageStore->getSelectFields() )
->where( $revs ? [ 'rev_id' => array_keys( $revs ) ] : '0=1' );
$revResult = $revQueryBuilder->fetchResultSet(); $revResult = $revQueryBuilder->fetchResultSet();
foreach ( $revResult as $row ) { foreach ( $revResult as $row ) {
$revs[ $row->rev_id ] = $row; $revs[ $row->rev_id ] = $row;