Use buildComparison() instead of raw SQL in WatchedItemQueryService

Bug: T321422
Change-Id: I1c330646cac85e396f95a64addcb0a19bdb6c8b9
This commit is contained in:
Bartosz Dziewoński 2022-09-16 14:44:18 +02:00
parent c86f4fa233
commit c1e7aa3009
2 changed files with 44 additions and 44 deletions

View file

@ -638,23 +638,14 @@ class WatchedItemQueryService {
}
private function getStartFromConds( IDatabase $db, array $options, array $startFrom ) {
$op = $options['dir'] === self::DIR_OLDER ? '<' : '>';
$op = $options['dir'] === self::DIR_OLDER ? '<=' : '>=';
[ $rcTimestamp, $rcId ] = $startFrom;
$rcTimestamp = $db->addQuotes( $db->timestamp( $rcTimestamp ) );
$rcTimestamp = $db->timestamp( $rcTimestamp );
$rcId = (int)$rcId;
return $db->makeList(
[
"rc_timestamp $op $rcTimestamp",
$db->makeList(
[
"rc_timestamp = $rcTimestamp",
"rc_id $op= $rcId"
],
LIST_AND
)
],
LIST_OR
);
return $db->buildComparison( $op, [
'rc_timestamp' => $rcTimestamp,
'rc_id' => $rcId,
] );
}
private function getWatchedItemsForUserQueryConds(
@ -674,15 +665,15 @@ class WatchedItemQueryService {
}
if ( isset( $options['from'] ) ) {
$op = $options['sort'] === self::SORT_ASC ? '>' : '<';
$op = $options['sort'] === self::SORT_ASC ? '>=' : '<=';
$conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
}
if ( isset( $options['until'] ) ) {
$op = $options['sort'] === self::SORT_ASC ? '<' : '>';
$op = $options['sort'] === self::SORT_ASC ? '<=' : '>=';
$conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
}
if ( isset( $options['startFrom'] ) ) {
$op = $options['sort'] === self::SORT_ASC ? '>' : '<';
$op = $options['sort'] === self::SORT_ASC ? '>=' : '<=';
$conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
}
@ -699,19 +690,10 @@ class WatchedItemQueryService {
* @return string
*/
private function getFromUntilTargetConds( IDatabase $db, LinkTarget $target, $op ) {
return $db->makeList(
[
"wl_namespace $op " . $target->getNamespace(),
$db->makeList(
[
'wl_namespace = ' . $target->getNamespace(),
"wl_title $op= " . $db->addQuotes( $target->getDBkey() )
],
LIST_AND
)
],
LIST_OR
);
return $db->buildComparison( $op, [
'wl_namespace' => $target->getNamespace(),
'wl_title' => $target->getDBkey(),
] );
}
private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {

View file

@ -75,6 +75,24 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
return implode( $sqlConj, $conds );
} );
$mock->method( 'buildComparison' )
->with(
$this->isType( 'string' ),
$this->isType( 'array' )
)
->willReturnCallback( static function ( string $op, array $conds ) {
$sql = '';
foreach ( array_reverse( $conds ) as $field => $value ) {
if ( $sql === '' ) {
$sql = "$field $op '$value'";
$op = rtrim( $op, '=' );
} else {
$sql = "$field $op '$value' OR ($field = '$value' AND ($sql))";
}
}
return $sql;
} );
$mock->method( 'addQuotes' )
->willReturnCallback( static function ( $value ) {
return "'$value'";
@ -737,7 +755,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
[],
[],
[
"(rc_timestamp < '20151212010101') OR ((rc_timestamp = '20151212010101') AND (rc_id <= 123))"
"rc_timestamp < '20151212010101' OR (rc_timestamp = '20151212010101' AND (rc_id <= '123'))"
],
[ 'ORDER BY' => [ 'rc_timestamp DESC', 'rc_id DESC' ] ],
[],
@ -748,7 +766,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
[],
[],
[
"(rc_timestamp > '20151212010101') OR ((rc_timestamp = '20151212010101') AND (rc_id >= 123))"
"rc_timestamp > '20151212010101' OR (rc_timestamp = '20151212010101' AND (rc_id >= '123'))"
],
[ 'ORDER BY' => [ 'rc_timestamp', 'rc_id' ] ],
[],
@ -759,7 +777,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
[],
[],
[
"(rc_timestamp < '20151212010101') OR ((rc_timestamp = '20151212010101') AND (rc_id <= 123))"
"rc_timestamp < '20151212010101' OR (rc_timestamp = '20151212010101' AND (rc_id <= '123'))"
],
[ 'ORDER BY' => [ 'rc_timestamp DESC', 'rc_id DESC' ] ],
[],
@ -1433,7 +1451,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
'from' => new TitleValue( 0, 'SomeDbKey' ),
'sort' => WatchedItemQueryService::SORT_ASC
],
[ "(wl_namespace > 0) OR ((wl_namespace = 0) AND (wl_title >= 'SomeDbKey'))", ],
[ "wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'SomeDbKey'))", ],
[ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
],
[
@ -1441,7 +1459,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
'from' => new TitleValue( 0, 'SomeDbKey' ),
'sort' => WatchedItemQueryService::SORT_DESC,
],
[ "(wl_namespace < 0) OR ((wl_namespace = 0) AND (wl_title <= 'SomeDbKey'))", ],
[ "wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeDbKey'))", ],
[ 'ORDER BY' => [ 'wl_namespace DESC', 'wl_title DESC' ] ]
],
[
@ -1449,7 +1467,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
'until' => new TitleValue( 0, 'SomeDbKey' ),
'sort' => WatchedItemQueryService::SORT_ASC
],
[ "(wl_namespace < 0) OR ((wl_namespace = 0) AND (wl_title <= 'SomeDbKey'))", ],
[ "wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeDbKey'))", ],
[ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
],
[
@ -1457,7 +1475,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
'until' => new TitleValue( 0, 'SomeDbKey' ),
'sort' => WatchedItemQueryService::SORT_DESC
],
[ "(wl_namespace > 0) OR ((wl_namespace = 0) AND (wl_title >= 'SomeDbKey'))", ],
[ "wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'SomeDbKey'))", ],
[ 'ORDER BY' => [ 'wl_namespace DESC', 'wl_title DESC' ] ]
],
[
@ -1468,9 +1486,9 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
'sort' => WatchedItemQueryService::SORT_ASC
],
[
"(wl_namespace > 0) OR ((wl_namespace = 0) AND (wl_title >= 'AnotherDbKey'))",
"(wl_namespace < 0) OR ((wl_namespace = 0) AND (wl_title <= 'SomeOtherDbKey'))",
"(wl_namespace > 0) OR ((wl_namespace = 0) AND (wl_title >= 'SomeDbKey'))",
"wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'AnotherDbKey'))",
"wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeOtherDbKey'))",
"wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'SomeDbKey'))",
],
[ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
],
@ -1482,9 +1500,9 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
'sort' => WatchedItemQueryService::SORT_DESC
],
[
"(wl_namespace < 0) OR ((wl_namespace = 0) AND (wl_title <= 'SomeOtherDbKey'))",
"(wl_namespace > 0) OR ((wl_namespace = 0) AND (wl_title >= 'AnotherDbKey'))",
"(wl_namespace < 0) OR ((wl_namespace = 0) AND (wl_title <= 'SomeDbKey'))",
"wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeOtherDbKey'))",
"wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'AnotherDbKey'))",
"wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeDbKey'))",
],
[ 'ORDER BY' => [ 'wl_namespace DESC', 'wl_title DESC' ] ]
],