aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmir Sarabadani <ladsgroup@gmail.com>2022-10-28 13:32:54 +0200
committerLadsgroup <Ladsgroup@gmail.com>2022-10-31 16:23:41 +0000
commitc9ea549714acd54d7ce2121090d7bb116531a6a5 (patch)
treeb1b26128e65588dc2666b1b4c95480323a4fada3
parentde584df7fe847f2eb2ddf363f06e36a74f33bade (diff)
downloadmediawikicore-c9ea549714acd54d7ce2121090d7bb116531a6a5.tar.gz
mediawikicore-c9ea549714acd54d7ce2121090d7bb116531a6a5.zip
user: Migrate selects in UserGroupManager to SelectQueryBuilder
Basically changing how adding query array works which made it simpler and less error prone. Bug: T311866 Change-Id: I2ca2a3d1d120f210cdabf7dacd5e858271908306
-rw-r--r--includes/api/ApiQueryUsers.php2
-rw-r--r--includes/specials/pagers/UsersPager.php15
-rw-r--r--includes/user/UserGroupManager.php68
3 files changed, 33 insertions, 52 deletions
diff --git a/includes/api/ApiQueryUsers.php b/includes/api/ApiQueryUsers.php
index 483f58b83ea2..3bf4b79478c5 100644
--- a/includes/api/ApiQueryUsers.php
+++ b/includes/api/ApiQueryUsers.php
@@ -173,7 +173,7 @@ class ApiQueryUsers extends ApiQueryBase {
$this->addTables( 'user_groups' );
$this->addJoinConds( [ 'user_groups' => [ 'JOIN', 'ug_user=user_id' ] ] );
$this->addFields( [ 'user_name' ] );
- $this->addFields( $this->userGroupManager->getQueryInfo()['fields'] );
+ $this->addFields( [ 'ug_user', 'ug_group', 'ug_expiry' ] );
$this->addWhere( 'ug_expiry IS NULL OR ug_expiry >= ' .
$db->addQuotes( $db->timestamp() ) );
$userGroupsRes = $this->select( __METHOD__ );
diff --git a/includes/specials/pagers/UsersPager.php b/includes/specials/pagers/UsersPager.php
index c3d1197d7e3f..23ef3f2b8891 100644
--- a/includes/specials/pagers/UsersPager.php
+++ b/includes/specials/pagers/UsersPager.php
@@ -290,15 +290,10 @@ class UsersPager extends AlphabeticPager {
}
// Lookup groups for all the users
- $dbr = $this->getDatabase();
- $groupsQueryInfo = $this->userGroupManager->getQueryInfo();
- $groupRes = $dbr->select(
- $groupsQueryInfo['tables'],
- $groupsQueryInfo['fields'],
- [ 'ug_user' => $userIds ],
- __METHOD__,
- $groupsQueryInfo['joins']
- );
+ $queryBuilder = $this->userGroupManager->newQueryBuilder( $this->getDatabase() );
+ $groupRes = $queryBuilder->where( [ 'ug_user' => $userIds ] )
+ ->caller( __METHOD__ )
+ ->fetchResultSet();
$cache = [];
$groups = [];
foreach ( $groupRes as $row ) {
@@ -311,7 +306,7 @@ class UsersPager extends AlphabeticPager {
// Give extensions a chance to add things like global user group data
// into the cache array to ensure proper output later on
- $this->hookRunner->onUsersPagerDoBatchLookups( $dbr, $userIds, $cache, $groups );
+ $this->hookRunner->onUsersPagerDoBatchLookups( $this->getDatabase(), $userIds, $cache, $groups );
$this->userGroupCache = $cache;
diff --git a/includes/user/UserGroupManager.php b/includes/user/UserGroupManager.php
index dc9c91d1f0bb..a50c57843cf0 100644
--- a/includes/user/UserGroupManager.php
+++ b/includes/user/UserGroupManager.php
@@ -44,8 +44,10 @@ use WikiMap;
use Wikimedia\Assert\Assert;
use Wikimedia\IPUtils;
use Wikimedia\Rdbms\DBConnRef;
+use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILBFactory;
use Wikimedia\Rdbms\ILoadBalancer;
+use Wikimedia\Rdbms\SelectQueryBuilder;
/**
* Managers user groups.
@@ -381,13 +383,12 @@ class UserGroupManager implements IDBAccessObject {
return [];
}
- $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
- $res = $db->select(
- 'user_former_groups',
- [ 'ufg_group' ],
- [ 'ufg_user' => $user->getId() ],
- __METHOD__
- );
+ $res = $this->getDBConnectionRefForQueryFlags( $queryFlags )->newSelectQueryBuilder()
+ ->select( 'ufg_group' )
+ ->from( 'user_former_groups' )
+ ->where( [ 'ufg_user' => $user->getId() ] )
+ ->caller( __METHOD__ )
+ ->fetchResultSet();
$formerGroups = [];
foreach ( $res as $row ) {
$formerGroups[] = $row->ufg_group;
@@ -711,16 +712,11 @@ class UserGroupManager implements IDBAccessObject {
return [];
}
- $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
- $queryInfo = $this->getQueryInfo();
- $res = $db->select(
- $queryInfo['tables'],
- $queryInfo['fields'],
- [ 'ug_user' => $user->getId() ],
- __METHOD__,
- [],
- $queryInfo['joins']
- );
+ $queryBuilder = $this->newQueryBuilder( $this->getDBConnectionRefForQueryFlags( $queryFlags ) );
+ $res = $queryBuilder
+ ->where( [ 'ug_user' => $user->getId() ] )
+ ->caller( __METHOD__ )
+ ->fetchResultSet();
$ugms = [];
foreach ( $res as $row ) {
@@ -943,26 +939,20 @@ class UserGroupManager implements IDBAccessObject {
}
/**
- * Return the tables and fields to be selected to construct new UserGroupMembership object
- * using newGroupMembershipFromRow method.
+ * Return the query builder to build upon and query
*
- * @return array[] With three keys:
- * - tables: (string[]) to include in the `$table` to `IDatabase->select()` or `SelectQueryBuilder::tables`
- * - fields: (string[]) to include in the `$vars` to `IDatabase->select()` or `SelectQueryBuilder::fields`
- * - joins: (array) to include in the `$join_conds` to `IDatabase->select()` or `SelectQueryBuilder::joinConds`
+ * @param IDatabase $db
+ * @return SelectQueryBuilder
* @internal
- * @phan-return array{tables:string[],fields:string[],joins:array}
*/
- public function getQueryInfo(): array {
- return [
- 'tables' => [ 'user_groups' ],
- 'fields' => [
+ public function newQueryBuilder( IDatabase $db ): SelectQueryBuilder {
+ return $db->newSelectQueryBuilder()
+ ->select( [
'ug_user',
'ug_group',
'ug_expiry',
- ],
- 'joins' => []
- ];
+ ] )
+ ->from( 'user_groups' );
}
/**
@@ -988,18 +978,14 @@ class UserGroupManager implements IDBAccessObject {
$now = time();
$purgedRows = 0;
- $queryInfo = $this->getQueryInfo();
do {
$dbw->startAtomic( __METHOD__ );
-
- $res = $dbw->select(
- $queryInfo['tables'],
- $queryInfo['fields'],
- [ 'ug_expiry < ' . $dbw->addQuotes( $dbw->timestamp( $now ) ) ],
- __METHOD__,
- [ 'FOR UPDATE', 'LIMIT' => 100 ],
- $queryInfo['joins']
- );
+ $res = $this->newQueryBuilder( $dbw )
+ ->where( [ 'ug_expiry < ' . $dbw->addQuotes( $dbw->timestamp( $now ) ) ] )
+ ->forUpdate()
+ ->limit( 100 )
+ ->caller( __METHOD__ )
+ ->fetchResultSet();
if ( $res->numRows() > 0 ) {
$insertData = []; // array of users/groups to insert to user_former_groups