MediaWiki REL1_37
LocalIdLookup.php
Go to the documentation of this file.
1<?php
25
37
40
42 private $sharedDB;
43
46
49
54 public function __construct(
55 Config $config,
56 ILoadBalancer $loadBalancer
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}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
A CentralIdLookup provider that just uses local IDs.
string null $sharedDB
string[] $sharedTables
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.
ILoadBalancer $loadBalancer
string[] $localDatabases
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.
Database cluster connection, tracking, load balancing, and transaction manager interface.