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