MediaWiki master
LocalIdLookup.php
Go to the documentation of this file.
1<?php
24
32
44
45 private IConnectionProvider $dbProvider;
46 private HideUserUtils $hideUserUtils;
47
49 private $sharedDB;
50
52 private $sharedTables;
53
55 private $localDatabases;
56
62 public function __construct(
63 Config $config,
64 IConnectionProvider $dbProvider,
65 HideUserUtils $hideUserUtils
66 ) {
67 $this->sharedDB = $config->get( MainConfigNames::SharedDB );
68 $this->sharedTables = $config->get( MainConfigNames::SharedTables );
69 $this->localDatabases = $config->get( MainConfigNames::LocalDatabases );
70 $this->dbProvider = $dbProvider;
71 $this->hideUserUtils = $hideUserUtils;
72 }
73
74 public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool {
75 // If the user has no ID, it can't be attached
76 if ( !$user->isRegistered() ) {
77 return false;
78 }
79
80 // Easy case, we're checking locally
81 if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) {
82 return true;
83 }
84
85 // Assume that shared user tables are set up as described above, if
86 // they're being used at all.
87 return $this->sharedDB !== null &&
88 in_array( 'user', $this->sharedTables, true ) &&
89 in_array( $wikiId, $this->localDatabases, true );
90 }
91
92 public function lookupCentralIds(
93 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
94 ): array {
95 if ( !$idToName ) {
96 return [];
97 }
98 $audience = $this->checkAudience( $audience );
99 [ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
100 $db = DBAccessObjectUtils::getDBFromIndex( $this->dbProvider, $index );
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 ->options( $options );
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 = self::READ_NORMAL
122 ): array {
123 if ( !$nameToId ) {
124 return [];
125 }
126
127 $audience = $this->checkAudience( $audience );
128 [ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
129 $db = DBAccessObjectUtils::getDBFromIndex( $this->dbProvider, $index );
130 $queryBuilder = $db->newSelectQueryBuilder();
131 $queryBuilder
132 ->select( [ 'user_id', 'user_name' ] )
133 ->from( 'user' )
134 ->where( [ 'user_name' => array_map( 'strval', array_keys( $nameToId ) ) ] )
135 ->options( $options );
136
137 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
138 $queryBuilder->andWhere( $this->hideUserUtils->getExpression( $db ) );
139 }
140
141 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
142 foreach ( $res as $row ) {
143 $nameToId[$row->user_name] = (int)$row->user_id;
144 }
145
146 return $nameToId;
147 }
148}
149
154class_alias( LocalIdLookup::class, 'LocalIdLookup' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:88
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=self::READ_NORMAL)
Given central user IDs, return the (local) user names.
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=self::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.