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 // Easy case, we're checking locally
80 if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) {
81 return true;
82 }
83
84 // Assume that shared user tables are set up as described above, if
85 // they're being used at all.
86 return $this->sharedDB !== null &&
87 in_array( 'user', $this->sharedTables, true ) &&
88 in_array( $wikiId, $this->localDatabases, true );
89 }
90
91 public function lookupCentralIds(
92 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
93 ): array {
94 if ( !$idToName ) {
95 return [];
96 }
97 $audience = $this->checkAudience( $audience );
98 $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
99 $queryBuilder = $db->newSelectQueryBuilder();
100 $queryBuilder
101 ->select( [ 'user_id', 'user_name' ] )
102 ->from( 'user' )
103 ->where( [ 'user_id' => array_map( 'intval', array_keys( $idToName ) ) ] )
104 ->recency( $flags );
105
106 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
107 $this->hideUserUtils->addFieldToBuilder( $queryBuilder );
108 }
109
110 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
111 foreach ( $res as $row ) {
112 $idToName[$row->user_id] = empty( $row->hu_deleted ) ? $row->user_name : '';
113 }
114
115 return $idToName;
116 }
117
118 public function lookupUserNames(
119 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
120 ): array {
121 if ( !$nameToId ) {
122 return [];
123 }
124
125 $audience = $this->checkAudience( $audience );
126 $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags );
127 $queryBuilder = $db->newSelectQueryBuilder();
128 $queryBuilder
129 ->select( [ 'user_id', 'user_name' ] )
130 ->from( 'user' )
131 ->where( [ 'user_name' => array_map( 'strval', array_keys( $nameToId ) ) ] )
132 ->recency( $flags );
133
134 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
135 $queryBuilder->andWhere( $this->hideUserUtils->getExpression( $db ) );
136 }
137
138 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
139 foreach ( $res as $row ) {
140 $nameToId[$row->user_name] = (int)$row->user_id;
141 }
142
143 return $nameToId;
144 }
145}
146
148class_alias( LocalIdLookup::class, 'LocalIdLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
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.
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 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.