MediaWiki 1.40.4
LocalIdLookup.php
Go to the documentation of this file.
1<?php
27
39
41 private $loadBalancer;
42
44 private $sharedDB;
45
47 private $sharedTables;
48
50 private $localDatabases;
51
56 public function __construct(
57 Config $config,
58 ILoadBalancer $loadBalancer
59 ) {
60 $this->sharedDB = $config->get( MainConfigNames::SharedDB );
61 $this->sharedTables = $config->get( MainConfigNames::SharedTables );
62 $this->localDatabases = $config->get( MainConfigNames::LocalDatabases );
63 $this->loadBalancer = $loadBalancer;
64 }
65
66 public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool {
67 // If the user has no ID, it can't be attached
68 if ( !$user->isRegistered() ) {
69 return false;
70 }
71
72 // Easy case, we're checking locally
73 if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) {
74 return true;
75 }
76
77 // Assume that shared user tables are set up as described above, if
78 // they're being used at all.
79 return $this->sharedDB !== null &&
80 in_array( 'user', $this->sharedTables, true ) &&
81 in_array( $wikiId, $this->localDatabases, true );
82 }
83
84 public function lookupCentralIds(
85 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
86 ): array {
87 if ( !$idToName ) {
88 return [];
89 }
90 $audience = $this->checkAudience( $audience );
91 [ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
92 $db = $this->loadBalancer->getConnectionRef( $index );
93
94 $tables = [ 'user' ];
95 $fields = [ 'user_id', 'user_name' ];
96 $where = [
97 'user_id' => array_map( 'intval', array_keys( $idToName ) ),
98 ];
99 $join = [];
100 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
101 $tables[] = 'ipblocks';
102 $join['ipblocks'] = [ 'LEFT JOIN', 'ipb_user=user_id' ];
103 $fields[] = 'ipb_deleted';
104 }
105
106 $res = $db->select( $tables, $fields, $where, __METHOD__, $options, $join );
107 foreach ( $res as $row ) {
108 $idToName[$row->user_id] = empty( $row->ipb_deleted ) ? $row->user_name : '';
109 }
110
111 return $idToName;
112 }
113
114 public function lookupUserNames(
115 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
116 ): array {
117 if ( !$nameToId ) {
118 return [];
119 }
120
121 $audience = $this->checkAudience( $audience );
122 [ $index, $options ] = DBAccessObjectUtils::getDBOptions( $flags );
123 $db = $this->loadBalancer->getConnectionRef( $index );
124
125 $tables = [ 'user' ];
126 $fields = [ 'user_id', 'user_name' ];
127 $where = [
128 'user_name' => array_map( 'strval', array_keys( $nameToId ) ),
129 ];
130 $join = [];
131 if ( $audience && !$audience->isAllowed( 'hideuser' ) ) {
132 $tables[] = 'ipblocks';
133 $join['ipblocks'] = [ 'LEFT JOIN', 'ipb_user=user_id' ];
134 $where[] = 'ipb_deleted = 0 OR ipb_deleted IS NULL';
135 }
136
137 $res = $db->select( $tables, $fields, $where, __METHOD__, $options, $join );
138 foreach ( $res as $row ) {
139 $nameToId[$row->user_name] = (int)$row->user_id;
140 }
141
142 return $nameToId;
143 }
144}
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:88
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.
Helper tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:33
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.
This class is a delegate to ILBFactory for a given database cluster.