MediaWiki REL1_35
CentralIdLookup.php
Go to the documentation of this file.
1<?php
22use Wikimedia\ObjectFactory;
23
31abstract class CentralIdLookup implements IDBAccessObject {
32 // Audience options for accessors
33 public const AUDIENCE_PUBLIC = 1;
34 public const AUDIENCE_RAW = 2;
35
37 private static $instances = [];
38
40 private $providerId;
41
47 public static function factory( $providerId = null ) {
49
50 if ( $providerId === null ) {
51 $providerId = $wgCentralIdLookupProvider;
52 }
53
54 if ( !array_key_exists( $providerId, self::$instances ) ) {
55 self::$instances[$providerId] = null;
56
57 if ( isset( $wgCentralIdLookupProviders[$providerId] ) ) {
58 $provider = ObjectFactory::getObjectFromSpec( $wgCentralIdLookupProviders[$providerId] );
59 if ( $provider instanceof CentralIdLookup ) {
60 $provider->providerId = $providerId;
61 self::$instances[$providerId] = $provider;
62 }
63 }
64 }
65
66 return self::$instances[$providerId];
67 }
68
83 public static function factoryNonLocal(): ?self {
84 $centralIdLookup = self::factory();
85
86 if ( $centralIdLookup instanceof LocalIdLookup ) {
87 /*
88 * A LocalIdLookup (which is the default) may actually be non-local,
89 * if shared user tables are used.
90 * However, we cannot know that here, so play it safe and refuse to return it.
91 * See also T163277 and T170996.
92 */
93 return null;
94 }
95
96 return $centralIdLookup;
97 }
98
103 public static function resetCache() {
104 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
105 throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
106 }
107 self::$instances = [];
108 }
109
110 final public function getProviderId() {
111 return $this->providerId;
112 }
113
120 protected function checkAudience( $audience ) {
121 if ( $audience instanceof User ) {
122 return $audience;
123 }
124 if ( $audience === self::AUDIENCE_PUBLIC ) {
125 return new User;
126 }
127 if ( $audience === self::AUDIENCE_RAW ) {
128 return null;
129 }
130 throw new InvalidArgumentException( 'Invalid audience' );
131 }
132
144 abstract public function isAttached( User $user, $wikiId = null );
145
157 abstract public function lookupCentralIds(
158 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
159 );
160
172 abstract public function lookupUserNames(
173 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
174 );
175
186 public function nameFromCentralId(
187 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
188 ) {
189 $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
190 return $idToName[$id];
191 }
192
201 public function namesFromCentralIds(
202 array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
203 ) {
204 $idToName = array_fill_keys( $ids, false );
205 $names = $this->lookupCentralIds( $idToName, $audience, $flags );
206 $names = array_unique( $names );
207 $names = array_filter( $names, function ( $name ) {
208 return $name !== false && $name !== '';
209 } );
210
211 return array_values( $names );
212 }
213
224 public function centralIdFromName(
225 $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
226 ) {
227 $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
228 return $nameToId[$name];
229 }
230
239 public function centralIdsFromNames(
240 array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
241 ) {
242 $nameToId = array_fill_keys( $names, false );
243 $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
244 $ids = array_unique( $ids );
245 $ids = array_filter( $ids, function ( $id ) {
246 return $id !== false;
247 } );
248
249 return array_values( $ids );
250 }
251
264 public function localUserFromCentralId(
265 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
266 ) {
267 $name = $this->nameFromCentralId( $id, $audience, $flags );
268 if ( $name !== null && $name !== '' ) {
269 $user = User::newFromName( $name );
270 if ( $user && $user->getId() && $this->isAttached( $user ) ) {
271 return $user;
272 }
273 }
274 return null;
275 }
276
289 public function centralIdFromLocalUser(
290 User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
291 ) {
292 return $this->isAttached( $user )
293 ? $this->centralIdFromName( $user->getName(), $audience, $flags )
294 : 0;
295 }
296
297}
string $wgCentralIdLookupProvider
Central ID lookup provider to use by default.
$wgCentralIdLookupProviders
Central ID lookup providers Key is the provider ID, value is a specification for ObjectFactory.
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
static resetCache()
Reset internal cache for unit testing.
static factory( $providerId=null)
Fetch a CentralIdLookup.
nameFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return the (local) user name.
static factoryNonLocal()
Returns a CentralIdLookup that is guaranteed to be non-local.
isAttached(User $user, $wikiId=null)
Check that a User is attached on the specified wiki.
centralIdFromLocalUser(User $user, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a local User object, return the central ID Stable to override.
static CentralIdLookup[] $instances
lookupCentralIds(array $idToName, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given central user IDs, return the (local) user names.
centralIdFromName( $name, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a (local) user name, return the central ID.
namesFromCentralIds(array $ids, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a an array of central user IDs, return the (local) user names.
centralIdsFromNames(array $names, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given an array of (local) user names, return the central IDs.
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given (local) user names, return the central IDs.
checkAudience( $audience)
Check that the "audience" parameter is valid.
localUserFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return a local User object.
A CentralIdLookup provider that just uses local IDs.
MediaWiki exception.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2150
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
Interface for database access objects.