Use SQL expression builder in the first couple of places

* Change expr() return type to Expression, otherwise Phan and other
  tools think that we can't call ->or() / ->and() on it
* Add IExpression as allowed type in some query builder methods

Change-Id: I73f7c3a43b6960b14ecd733870e91397f5acca6e
This commit is contained in:
Bartosz Dziewoński 2023-10-20 20:23:52 +02:00 committed by Bartosz Dziewoński
parent 3ef6662f31
commit 5ade528f7a
10 changed files with 35 additions and 40 deletions

View file

@ -31,6 +31,7 @@ use MediaWiki\Title\Title;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
use Wikimedia\Rdbms\IReadableDatabase;
use Wikimedia\Rdbms\OrExpressionGroup;
/**
* Query module to enumerate all images.
@ -261,17 +262,12 @@ class ApiQueryAllImages extends ApiQueryGeneratorBase {
$mimeConds = [];
foreach ( $params['mime'] as $mime ) {
[ $major, $minor ] = File::splitMime( $mime );
$mimeConds[] = $db->makeList(
[
'img_major_mime' => $major,
'img_minor_mime' => $minor,
],
LIST_AND
);
$mimeConds[] =
$db->expr( 'img_major_mime', '=', $major )
->and( 'img_minor_mime', '=', $minor );
}
// safeguard against internal_api_error_DBQueryError
if ( count( $mimeConds ) > 0 ) {
$this->addWhere( $db->makeList( $mimeConds, LIST_OR ) );
$this->addWhere( new OrExpressionGroup( ...$mimeConds ) );
} else {
// no MIME types, no files
$this->getResult()->addValue( 'query', $this->getModuleName(), [] );

View file

@ -25,6 +25,7 @@ use MediaWiki\Title\MalformedTitleException;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleValue;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\IExpression;
use Wikimedia\Rdbms\IReadableDatabase;
use Wikimedia\Rdbms\IResultWrapper;
use Wikimedia\Rdbms\SelectQueryBuilder;
@ -227,7 +228,7 @@ abstract class ApiQueryBase extends ApiBase {
* input, consider using addWhereIDsFld() instead.
*
* @see IDatabase::select()
* @param string|array $value
* @param string|array|IExpression $value
*/
protected function addWhere( $value ) {
if ( is_array( $value ) ) {
@ -243,7 +244,7 @@ abstract class ApiQueryBase extends ApiBase {
/**
* Same as addWhere(), but add the WHERE clauses only if a condition is met
* @param string|array $value
* @param string|array|IExpression $value
* @param bool $condition If false, do nothing
* @return bool
*/

View file

@ -184,12 +184,12 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate {
->from( 'recentchanges' )
->join( 'actor', 'actor', 'actor_id=rc_actor' )
->where( [
'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Exclude external (Wikidata)
'actor_user IS NOT NULL',
'rc_bot' => 0,
'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
'rc_timestamp >= ' . $dbr->addQuotes(
$dbr->timestamp( time() - $config->get( MainConfigNames::ActiveUserDays ) * 24 * 3600 ) ),
$dbr->expr( 'rc_type', '!=', RC_EXTERNAL ), // Exclude external (Wikidata)
$dbr->expr( 'actor_user', '!=', null ),
$dbr->expr( 'rc_bot', '=', 0 ),
$dbr->expr( 'rc_log_type', '!=', 'newusers' )->or( 'rc_log_type', '=', null ),
$dbr->expr( 'rc_timestamp', '>=',
$dbr->timestamp( time() - $config->get( MainConfigNames::ActiveUserDays ) * 24 * 3600 ) )
] )
->caller( __METHOD__ )
->fetchField();

View file

@ -53,19 +53,17 @@ class ClearWatchlistNotificationsJob extends Job implements GenericParameterJob
$dbw = $lbFactory->getPrimaryDatabase();
$ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
$timestamp = $this->params['timestamp'] ?? null;
if ( $timestamp === null ) {
$timestampCond = 'wl_notificationtimestamp IS NOT NULL';
if ( !isset( $this->params['timestamp'] ) ) {
$timestamp = null;
$timestampCond = $dbw->expr( 'wl_notificationtimestamp', '!=', null );
} else {
$timestamp = $dbw->timestamp( $timestamp );
$timestampCond = 'wl_notificationtimestamp != ' . $dbw->addQuotes( $timestamp ) .
' OR wl_notificationtimestamp IS NULL';
$timestamp = $dbw->timestamp( $this->params['timestamp'] );
$timestampCond = $dbw->expr( 'wl_notificationtimestamp', '!=', $timestamp )
->or( 'wl_notificationtimestamp', '=', null );
}
// New notifications since the reset should not be cleared
$casTimeCond = $dbw->buildComparison(
'<',
[ 'wl_notificationtimestamp' => $dbw->timestamp( $this->params['casTime'] ) ] ) .
' OR wl_notificationtimestamp IS NULL';
$casTimeCond = $dbw->expr( 'wl_notificationtimestamp', '<', $dbw->timestamp( $this->params['casTime'] ) )
->or( 'wl_notificationtimestamp', '=', null );
$firstBatch = true;
do {

View file

@ -522,7 +522,7 @@ class DBConnRef implements IMaintainableDatabase {
return $this->__call( __FUNCTION__, func_get_args() );
}
public function expr( string $field, string $op, $value ): IExpression {
public function expr( string $field, string $op, $value ): Expression {
return new Expression( $field, $op, $value );
}

View file

@ -1595,7 +1595,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
}
}
public function expr( string $field, string $op, $value ): IExpression {
public function expr( string $field, string $op, $value ): Expression {
return new Expression( $field, $op, $value );
}

View file

@ -672,9 +672,9 @@ interface IReadableDatabase extends ISQLPlatform, DbQuoter, IDatabaseFlags {
* @param string $field
* @param string $op one of [ '>', '<', '!=', '=', '>=', '<=' ]
* @param string|int|float|null|bool|Blob|array $value
* @return IExpression
* @return Expression
*/
public function expr( string $field, string $op, $value ): IExpression;
public function expr( string $field, string $op, $value ): Expression;
/**
* Get a debugging string that mentions the database type, the ID of this instance,

View file

@ -127,7 +127,7 @@ class DeleteQueryBuilder {
* Add conditions to the query. The supplied conditions will be appended
* to the existing conditions, separated by AND.
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
*
* May be either a string containing a single condition, or an array of
@ -183,7 +183,7 @@ class DeleteQueryBuilder {
/**
* Add conditions to the query. Alias for where().
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
* @return $this
*/
@ -194,7 +194,7 @@ class DeleteQueryBuilder {
/**
* Add conditions to the query. Alias for where().
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
* @return $this
*/

View file

@ -281,7 +281,7 @@ class SelectQueryBuilder extends JoinGroupBase {
* Add conditions to the query. The supplied conditions will be appended
* to the existing conditions, separated by AND.
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
*
* May be either a string containing a single condition, or an array of
@ -337,7 +337,7 @@ class SelectQueryBuilder extends JoinGroupBase {
/**
* Add conditions to the query. Alias for where().
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
* @return $this
*/
@ -348,7 +348,7 @@ class SelectQueryBuilder extends JoinGroupBase {
/**
* Add conditions to the query. Alias for where().
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
* @return $this
*/

View file

@ -165,7 +165,7 @@ class UpdateQueryBuilder {
* Add conditions to the query. The supplied conditions will be appended
* to the existing conditions, separated by AND.
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
*
* May be either a string containing a single condition, or an array of
@ -221,7 +221,7 @@ class UpdateQueryBuilder {
/**
* Add conditions to the query. Alias for where().
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
* @return $this
*/
@ -232,7 +232,7 @@ class UpdateQueryBuilder {
/**
* Add conditions to the query. Alias for where().
*
* @param string|array $conds
* @param string|array|IExpression $conds
* @param-taint $conds exec_sql_numkey
* @return $this
*/