MediaWiki master
LocalIdLookup.php
Go to the documentation of this file.
1<?php
22
31
43
44 private IConnectionProvider $dbProvider;
45 private HideUserUtils $hideUserUtils;
46
48 private $sharedDB;
49
51 private $sharedTables;
52
54 private $localDatabases;
55
61 public function __construct(
62 Config $config,
63 IConnectionProvider $dbProvider,
64 HideUserUtils $hideUserUtils
65 ) {
66 $this->sharedDB = $config->get( MainConfigNames::SharedDB );
67 $this->sharedTables = $config->get( MainConfigNames::SharedTables );
68 $this->localDatabases = $config->get( MainConfigNames::LocalDatabases );
69 $this->dbProvider = $dbProvider;
70 $this->hideUserUtils = $hideUserUtils;
71 }
72
73 public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool {
74 // If the user has no ID, it can't be attached
75 if ( !$user->isRegistered() ) {
76 return false;
77 }
78
79 return $this->isWikiSharingOurUsers( $wikiId );
80 }
81
88 private function isWikiSharingOurUsers( $wikiId ) {
89 // Easy case, the local wiki
90 if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) {
91 return true;
92 }
93
94 // Assume that shared user tables are set up as described above, if
95 // they're being used at all.
96 return $this->sharedDB !== null &&
97 in_array( 'user', $this->sharedTables, true ) &&
98 in_array( $wikiId, $this->localDatabases, true );
99 }
100
101 public function lookupCentralIds(
102 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
103 ): array {
104 if ( !$idToName ) {
105 return [];
106 }
107 $audience = $this->checkAudience( $audience );
108 $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
109 $queryBuilder = $db->newSelectQueryBuilder();
110 $queryBuilder
111 ->select( [ 'user_id', 'user_name' ] )
112 ->from( 'user' )
113 ->where( [ 'user_id' => array_map( 'intval', array_keys( $idToName ) ) ] )
114 ->recency( $flags );
115
116 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
117 $this->hideUserUtils->addFieldToBuilder( $queryBuilder );
118 }
119
120 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
121 foreach ( $res as $row ) {
122 $idToName[$row->user_id] = empty( $row->hu_deleted ) ? $row->user_name : '';
123 }
124
125 return $idToName;
126 }
127
129 array $nameToId, $filter, $audience = self::AUDIENCE_PUBLIC,
130 $flags = IDBAccessObject::READ_NORMAL,
131 $wikiId = UserIdentity::LOCAL
132 ): array {
133 if ( !$nameToId ) {
134 return [];
135 }
136 if ( ( $filter === self::FILTER_ATTACHED || $filter === self::FILTER_OWNED )
137 && !$this->isWikiSharingOurUsers( $wikiId )
138 ) {
139 // No users pass the filter
140 return $nameToId;
141 }
142
143 $audience = $this->checkAudience( $audience );
144 $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
145 $queryBuilder = $db->newSelectQueryBuilder();
146 $queryBuilder
147 ->select( [ 'user_id', 'user_name' ] )
148 ->from( 'user' )
149 ->where( [ 'user_name' => array_map( 'strval', array_keys( $nameToId ) ) ] )
150 ->recency( $flags );
151
152 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
153 $queryBuilder->andWhere( $this->hideUserUtils->getExpression( $db ) );
154 }
155
156 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
157 foreach ( $res as $row ) {
158 $nameToId[$row->user_name] = (int)$row->user_id;
159 }
160
161 return $nameToId;
162 }
163}
164
166class_alias( LocalIdLookup::class, 'LocalIdLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
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()
Find central user IDs associated with local user IDs, e.g.
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.
lookupUserNamesWithFilter(array $nameToId, $filter, $audience=self::AUDIENCE_PUBLIC, $flags=IDBAccessObject::READ_NORMAL, $wikiId=UserIdentity::LOCAL)
Given user names on the wiki specified by $wikiId, 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 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.
Interface for database access objects.