watchlist: Use expression builder to avoid IDatabase::addQuotes

Many unit tests needs adjust, because the ->with() call no longer
matched and using expression objects for the matching repeats the same
code between test and real implementation.

Bug: T361023
Change-Id: I6f8dbd12d7135ee28ad5b1eabb58d1354d1591d3
This commit is contained in:
Umherirrender 2024-07-18 22:34:40 +02:00
parent 98ebc56a12
commit 8d49affda6
3 changed files with 514 additions and 318 deletions

View file

@ -305,7 +305,7 @@ class WatchedItemStore implements WatchedItemStoreInterface {
IReadableDatabase $db
) {
if ( $this->expiryEnabled ) {
$queryBuilder->where( 'we_expiry IS NULL OR we_expiry > ' . $db->addQuotes( $db->timestamp() ) );
$queryBuilder->where( $db->expr( 'we_expiry', '=', null )->or( 'we_expiry', '>', $db->timestamp() ) );
$queryBuilder->leftJoin( 'watchlist_expiry', null, 'wl_id = we_item' );
}
}
@ -489,9 +489,8 @@ class WatchedItemStore implements WatchedItemStoreInterface {
->where( [
'wl_namespace' => $target->getNamespace(),
'wl_title' => $target->getDBkey(),
'wl_notificationtimestamp >= ' .
$dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
' OR wl_notificationtimestamp IS NULL'
$dbr->expr( 'wl_notificationtimestamp', '>=', $dbr->timestamp( $threshold ) )
->or( 'wl_notificationtimestamp', '=', null )
] )
->caller( __METHOD__ );
@ -673,13 +672,11 @@ class WatchedItemStore implements WatchedItemStoreInterface {
continue;
}
/** @var LinkTarget|PageIdentity $target */
$namespaceConds[$target->getNamespace()][] = $db->makeList( [
'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
$db->makeList( [
'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
'wl_notificationtimestamp IS NULL'
], LIST_OR )
], LIST_AND );
$namespaceConds[$target->getNamespace()][] = $db->expr( 'wl_title', '=', $target->getDBkey() )
->andExpr(
$db->expr( 'wl_notificationtimestamp', '>=', $db->timestamp( $threshold ) )
->or( 'wl_notificationtimestamp', '=', null )
);
}
$conds = [];
@ -1807,7 +1804,7 @@ class WatchedItemStore implements WatchedItemStoreInterface {
return $dbr->newSelectQueryBuilder()
->select( '*' )
->from( 'watchlist_expiry' )
->where( [ 'we_expiry <= ' . $dbr->addQuotes( $dbr->timestamp() ) ] )
->where( $dbr->expr( 'we_expiry', '<=', $dbr->timestamp() ) )
->caller( __METHOD__ )
->fetchRowCount();
}
@ -1824,7 +1821,7 @@ class WatchedItemStore implements WatchedItemStoreInterface {
$toDelete = $dbr->newSelectQueryBuilder()
->select( 'we_item' )
->from( 'watchlist_expiry' )
->where( [ 'we_expiry <= ' . $dbr->addQuotes( $dbr->timestamp() ) ] )
->where( $dbr->expr( 'we_expiry', '<=', $dbr->timestamp() ) )
->limit( $limit )
->caller( __METHOD__ )
->fetchFieldValues();

View file

@ -102,11 +102,6 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
return $sql;
} );
$mock->method( 'addQuotes' )
->willReturnCallback( static function ( $value ) {
return "'$value'";
} );
$mock->method( 'timestamp' )
->willReturnArgument( 0 );
@ -1064,10 +1059,6 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
$expectedConds = array_merge( [ 'wl_user' => 1 ], $expectedConds );
$mockDb = $this->getMockDb();
$mockDb->method( 'addQuotes' )
->willReturnCallback( static function ( $value ) {
return "'$value'";
} );
$mockDb->method( 'makeList' )
->with(
$this->isType( 'array' ),