rdbms: Introduce and use SQB::recency to phase out DBAccessObjectUtils

This can slowly simplify and clean up this class.

Bug: T354194
Change-Id: I925544ec3107334d40afa42da37be930c25358a8
This commit is contained in:
Amir Sarabadani 2024-01-10 22:48:50 +01:00
parent 0daee7697d
commit e3241d7fe3
10 changed files with 39 additions and 24 deletions

View file

@ -190,13 +190,13 @@ class LocalPasswordPrimaryAuthenticationProvider
return false;
}
[ $mode, $options ] = \DBAccessObjectUtils::getDBOptions( $flags );
[ $mode, ] = \DBAccessObjectUtils::getDBOptions( $flags );
$db = \DBAccessObjectUtils::getDBFromIndex( $this->dbProvider, $mode );
return (bool)$db->newSelectQueryBuilder()
->select( [ 'user_id' ] )
->from( 'user' )
->where( [ 'user_name' => $username ] )
->options( $options )
->recency( $flags )
->caller( __METHOD__ )->fetchField();
}

View file

@ -207,13 +207,13 @@ class TemporaryPasswordPrimaryAuthenticationProvider
return false;
}
[ $mode, $options ] = \DBAccessObjectUtils::getDBOptions( $flags );
[ $mode, ] = \DBAccessObjectUtils::getDBOptions( $flags );
$db = \DBAccessObjectUtils::getDBFromIndex( $this->dbProvider, $mode );
return (bool)$db->newSelectQueryBuilder()
->select( [ 'user_id' ] )
->from( 'user' )
->where( [ 'user_name' => $username ] )
->options( $options )
->recency( $flags )
->caller( __METHOD__ )->fetchField();
}

View file

@ -2,6 +2,8 @@
namespace Wikimedia\Rdbms;
use IDBAccessObject;
/**
* Build SELECT queries with a fluent interface.
*
@ -684,6 +686,19 @@ class SelectQueryBuilder extends JoinGroupBase {
return $this;
}
/**
* @param int $recency Bitfield of IDBAccessObject::READ_* constants
* @return $this
*/
public function recency( $recency ) {
if ( ( $recency & IDBAccessObject::READ_EXCLUSIVE ) == IDBAccessObject::READ_EXCLUSIVE ) {
$this->forUpdate();
} elseif ( ( $recency & IDBAccessObject::READ_LOCKING ) == IDBAccessObject::READ_LOCKING ) {
$this->lockInShareMode();
}
return $this;
}
/**
* Set the method name to be included in an SQL comment.
*

View file

@ -546,12 +546,12 @@ class Title implements LinkTarget, PageIdentity, IDBAccessObject {
public static function newFromID( $id, $flags = 0 ) {
$flags |= ( $flags & self::GAID_FOR_UPDATE ) ? self::READ_LATEST : 0; // b/c
$pageStore = MediaWikiServices::getInstance()->getPageStore();
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $flags );
$row = wfGetDB( $index )->newSelectQueryBuilder()
->select( $pageStore->getSelectFields() )
->from( 'page' )
->where( [ 'page_id' => $id ] )
->options( $options )
->recency( $flags )
->caller( __METHOD__ )->fetchRow();
if ( $row !== false ) {
$title = self::newFromRow( $row );

View file

@ -220,13 +220,13 @@ class BotPassword implements IDBAccessObject {
* @return Password
*/
private function getPassword() {
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $this->flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $this->flags );
$db = self::getDB( $index );
$password = $db->newSelectQueryBuilder()
->select( 'bp_password' )
->from( 'bot_passwords' )
->where( [ 'bp_user' => $this->centralId, 'bp_app_id' => $this->appId ] )
->options( $options )
->recency( $this->flags )
->caller( __METHOD__ )->fetchField();
if ( $password === false ) {
return PasswordFactory::newInvalidPassword();

View file

@ -134,13 +134,13 @@ class BotPasswordStore implements IDBAccessObject {
return null;
}
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $flags );
$db = $this->getDatabase( $index );
$row = $db->newSelectQueryBuilder()
->select( [ 'bp_user', 'bp_app_id', 'bp_token', 'bp_restrictions', 'bp_grants' ] )
->from( 'bot_passwords' )
->where( [ 'bp_user' => $centralId, 'bp_app_id' => $appId ] )
->options( $options )
->recency( $flags )
->caller( __METHOD__ )->fetchRow();
return $row ? new BotPassword( $row, true, $flags ) : null;
}

View file

@ -96,14 +96,14 @@ class LocalIdLookup extends CentralIdLookup {
return [];
}
$audience = $this->checkAudience( $audience );
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $flags );
$db = DBAccessObjectUtils::getDBFromIndex( $this->dbProvider, $index );
$queryBuilder = $db->newSelectQueryBuilder();
$queryBuilder
->select( [ 'user_id', 'user_name' ] )
->from( 'user' )
->where( [ 'user_id' => array_map( 'intval', array_keys( $idToName ) ) ] )
->options( $options );
->recency( $flags );
if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
$this->hideUserUtils->addFieldToBuilder( $queryBuilder );
@ -125,14 +125,14 @@ class LocalIdLookup extends CentralIdLookup {
}
$audience = $this->checkAudience( $audience );
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $flags );
$db = DBAccessObjectUtils::getDBFromIndex( $this->dbProvider, $index );
$queryBuilder = $db->newSelectQueryBuilder();
$queryBuilder
->select( [ 'user_id', 'user_name' ] )
->from( 'user' )
->where( [ 'user_name' => array_map( 'strval', array_keys( $nameToId ) ) ] )
->options( $options );
->recency( $flags );
if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
$queryBuilder->andWhere( $this->hideUserUtils->getExpression( $db ) );

View file

@ -543,13 +543,13 @@ class UserOptionsManager extends UserOptionsLookup {
): array {
if ( $prefetchedOptions === null ) {
$this->logger->debug( 'Loading options from database', [ 'user_id' => $user->getId() ] );
[ $mode, $options ] = DBAccessObjectUtils::getDBOptions( $queryFlags );
[ $mode, ] = DBAccessObjectUtils::getDBOptions( $queryFlags );
$dbr = DBAccessObjectUtils::getDBFromIndex( $this->dbProvider, $mode );
$res = $dbr->newSelectQueryBuilder()
->select( [ 'up_property', 'up_value' ] )
->from( 'user_properties' )
->where( [ 'up_user' => $user->getId() ] )
->options( $options )
->recency( $queryFlags )
->caller( __METHOD__ )->fetchResultSet();
} else {
$res = [];

View file

@ -401,11 +401,11 @@ class User implements Authority, UserIdentity, UserEmailContact {
$this->queryFlagsUsed = $flags;
}
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $flags );
$queryBuilder = wfGetDB( $index )->newSelectQueryBuilder()
->select( [ 'actor_id', 'actor_user', 'actor_name' ] )
->from( 'actor' )
->options( $options );
->recency( $flags );
if ( $this->mFrom === 'name' ) {
// make sure to use normalized form of IP for anonymous users
$queryBuilder->where( [ 'actor_name' => IPUtils::sanitizeIP( $this->mName ) ] );
@ -1095,12 +1095,12 @@ class User implements Authority, UserIdentity, UserEmailContact {
return false;
}
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $flags );
$db = wfGetDB( $index );
$row = self::newQueryBuilder( $db )
->where( [ 'user_id' => $this->mId ] )
->options( $options )
->recency( $flags )
->caller( __METHOD__ )
->fetchRow();
@ -2571,14 +2571,14 @@ class User implements Authority, UserIdentity, UserEmailContact {
return 0;
}
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $flags );
$db = wfGetDB( $index );
$id = $db->newSelectQueryBuilder()
->select( 'user_id' )
->from( 'user' )
->where( [ 'user_name' => $s ] )
->options( $options )
->recency( $flags )
->caller( __METHOD__ )->fetchField();
return (int)$id;

View file

@ -286,7 +286,7 @@ class UserFactory implements IDBAccessObject, UserRigorOptions {
string $confirmationCode,
int $flags = IDBAccessObject::READ_NORMAL
) {
[ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
[ $index, ] = DBAccessObjectUtils::getDBOptions( $flags );
$db = $this->loadBalancer->getConnectionRef( $index );
@ -295,7 +295,7 @@ class UserFactory implements IDBAccessObject, UserRigorOptions {
->from( 'user' )
->where( [ 'user_email_token' => md5( $confirmationCode ) ] )
->andWhere( $db->expr( 'user_email_token_expires', '>', $db->timestamp() ) )
->options( $options )
->recency( $flags )
->caller( __METHOD__ )->fetchField();
if ( !$id ) {