MediaWiki  master
LocalIdLookup.php
Go to the documentation of this file.
1 <?php
25 
37 
39  private $loadBalancer;
40 
42  private $sharedDB;
43 
45  private $sharedTables;
46 
48  private $localDatabases;
49 
54  public function __construct(
55  Config $config,
57  ) {
58  $this->sharedDB = $config->get( 'SharedDB' );
59  $this->sharedTables = $config->get( 'SharedTables' );
60  $this->localDatabases = $config->get( 'LocalDatabases' );
61  $this->loadBalancer = $loadBalancer;
62  }
63 
64  public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool {
65  // If the user has no ID, it can't be attached
66  if ( !$user->getId() ) {
67  return false;
68  }
69 
70  // Easy case, we're checking locally
71  if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) {
72  return true;
73  }
74 
75  // Assume that shared user tables are set up as described above, if
76  // they're being used at all.
77  return $this->sharedDB !== null &&
78  in_array( 'user', $this->sharedTables, true ) &&
79  in_array( $wikiId, $this->localDatabases, true );
80  }
81 
82  public function lookupCentralIds(
83  array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
84  ): array {
85  if ( !$idToName ) {
86  return [];
87  }
88  $audience = $this->checkAudience( $audience );
89  list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
90  $db = $this->loadBalancer->getConnectionRef( $index );
91 
92  $tables = [ 'user' ];
93  $fields = [ 'user_id', 'user_name' ];
94  $where = [
95  'user_id' => array_map( 'intval', array_keys( $idToName ) ),
96  ];
97  $join = [];
98  if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
99  $tables[] = 'ipblocks';
100  $join['ipblocks'] = [ 'LEFT JOIN', 'ipb_user=user_id' ];
101  $fields[] = 'ipb_deleted';
102  }
103 
104  $res = $db->select( $tables, $fields, $where, __METHOD__, $options, $join );
105  foreach ( $res as $row ) {
106  $idToName[$row->user_id] = empty( $row->ipb_deleted ) ? $row->user_name : '';
107  }
108 
109  return $idToName;
110  }
111 
112  public function lookupUserNames(
113  array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
114  ): array {
115  if ( !$nameToId ) {
116  return [];
117  }
118 
119  $audience = $this->checkAudience( $audience );
120  list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
121  $db = $this->loadBalancer->getConnectionRef( $index );
122 
123  $tables = [ 'user' ];
124  $fields = [ 'user_id', 'user_name' ];
125  $where = [
126  'user_name' => array_map( 'strval', array_keys( $nameToId ) ),
127  ];
128  $join = [];
129  if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
130  $tables[] = 'ipblocks';
131  $join['ipblocks'] = [ 'LEFT JOIN', 'ipb_user=user_id' ];
132  $where[] = 'ipb_deleted = 0 OR ipb_deleted IS NULL';
133  }
134 
135  $res = $db->select( $tables, $fields, $where, __METHOD__, $options, $join );
136  foreach ( $res as $row ) {
137  $nameToId[$row->user_name] = (int)$row->user_id;
138  }
139 
140  return $nameToId;
141  }
142 }
LocalIdLookup\$localDatabases
string[] $localDatabases
Definition: LocalIdLookup.php:48
WikiMap\isCurrentWikiId
static isCurrentWikiId( $wikiId)
Definition: WikiMap.php:321
LocalIdLookup\lookupUserNames
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given (local) user names, return the central IDs.
Definition: LocalIdLookup.php:112
MediaWiki\User\UserIdentity\getId
getId( $wikiId=self::LOCAL)
DBAccessObjectUtils\getDBOptions
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
Definition: DBAccessObjectUtils.php:52
LocalIdLookup\__construct
__construct(Config $config, ILoadBalancer $loadBalancer)
Definition: LocalIdLookup.php:54
$res
$res
Definition: testCompression.php:57
LocalIdLookup\$loadBalancer
ILoadBalancer $loadBalancer
Definition: LocalIdLookup.php:39
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
Config
Interface for configuration instances.
Definition: Config.php:30
LocalIdLookup\$sharedTables
string[] $sharedTables
Definition: LocalIdLookup.php:45
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
LocalIdLookup\lookupCentralIds
lookupCentralIds(array $idToName, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given central user IDs, return the (local) user names.
Definition: LocalIdLookup.php:82
LocalIdLookup
A CentralIdLookup provider that just uses local IDs.
Definition: LocalIdLookup.php:36
LocalIdLookup\$sharedDB
string null $sharedDB
Definition: LocalIdLookup.php:42
LocalIdLookup\isAttached
isAttached(UserIdentity $user, $wikiId=UserIdentity::LOCAL)
Check that a user is attached on the specified wiki.
Definition: LocalIdLookup.php:64
CentralIdLookup
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Definition: CentralIdLookup.php:35
CentralIdLookup\checkAudience
checkAudience( $audience)
Check that the "audience" parameter is valid.
Definition: CentralIdLookup.php:117
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81