MediaWiki master
LocalIdLookup.php
Go to the documentation of this file.
1<?php
24
33
45
46 private IConnectionProvider $dbProvider;
47 private HideUserUtils $hideUserUtils;
48
50 private $sharedDB;
51
53 private $sharedTables;
54
56 private $localDatabases;
57
63 public function __construct(
64 Config $config,
65 IConnectionProvider $dbProvider,
66 HideUserUtils $hideUserUtils
67 ) {
68 $this->sharedDB = $config->get( MainConfigNames::SharedDB );
69 $this->sharedTables = $config->get( MainConfigNames::SharedTables );
70 $this->localDatabases = $config->get( MainConfigNames::LocalDatabases );
71 $this->dbProvider = $dbProvider;
72 $this->hideUserUtils = $hideUserUtils;
73 }
74
75 public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool {
76 // If the user has no ID, it can't be attached
77 if ( !$user->isRegistered() ) {
78 return false;
79 }
80
81 // Easy case, we're checking locally
82 if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) {
83 return true;
84 }
85
86 // Assume that shared user tables are set up as described above, if
87 // they're being used at all.
88 return $this->sharedDB !== null &&
89 in_array( 'user', $this->sharedTables, true ) &&
90 in_array( $wikiId, $this->localDatabases, true );
91 }
92
93 public function lookupCentralIds(
94 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
95 ): array {
96 if ( !$idToName ) {
97 return [];
98 }
99 $audience = $this->checkAudience( $audience );
100 $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
101 $queryBuilder = $db->newSelectQueryBuilder();
102 $queryBuilder
103 ->select( [ 'user_id', 'user_name' ] )
104 ->from( 'user' )
105 ->where( [ 'user_id' => array_map( 'intval', array_keys( $idToName ) ) ] )
106 ->recency( $flags );
107
108 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
109 $this->hideUserUtils->addFieldToBuilder( $queryBuilder );
110 }
111
112 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
113 foreach ( $res as $row ) {
114 $idToName[$row->user_id] = empty( $row->hu_deleted ) ? $row->user_name : '';
115 }
116
117 return $idToName;
118 }
119
120 public function lookupUserNames(
121 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
122 ): array {
123 if ( !$nameToId ) {
124 return [];
125 }
126
127 $audience = $this->checkAudience( $audience );
128 $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
129 $queryBuilder = $db->newSelectQueryBuilder();
130 $queryBuilder
131 ->select( [ 'user_id', 'user_name' ] )
132 ->from( 'user' )
133 ->where( [ 'user_name' => array_map( 'strval', array_keys( $nameToId ) ) ] )
134 ->recency( $flags );
135
136 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
137 $queryBuilder->andWhere( $this->hideUserUtils->getExpression( $db ) );
138 }
139
140 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
141 foreach ( $res as $row ) {
142 $nameToId[$row->user_name] = (int)$row->user_id;
143 }
144
145 return $nameToId;
146 }
147}
148
150class_alias( LocalIdLookup::class, 'LocalIdLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Helper class for DAO classes.
Helpers for building queries that determine whether a user is hidden.
A class containing constants representing the names of configuration variables.
const SharedDB
Name constant for the SharedDB setting, for use with Config::get()
const LocalDatabases
Name constant for the LocalDatabases setting, for use with Config::get()
const SharedTables
Name constant for the SharedTables setting, for use with Config::get()
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
A CentralIdLookup provider that just uses local IDs.
isAttached(UserIdentity $user, $wikiId=UserIdentity::LOCAL)
Check that a user is attached on the specified wiki.
lookupCentralIds(array $idToName, $audience=self::AUDIENCE_PUBLIC, $flags=IDBAccessObject::READ_NORMAL)
Given central user IDs, return the (local) user names.
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=IDBAccessObject::READ_NORMAL)
Given (local) user names, return the central IDs.
__construct(Config $config, IConnectionProvider $dbProvider, HideUserUtils $hideUserUtils)
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Interface for database access objects.
Interface for configuration instances.
Definition Config.php:32
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
Interface for objects representing user identity.
Provide primary and replica IDatabase connections.