MediaWiki REL1_31
CentralIdLookup.php
Go to the documentation of this file.
1<?php
22use Wikimedia\ObjectFactory;
23
30abstract class CentralIdLookup implements IDBAccessObject {
31 // Audience options for accessors
32 const AUDIENCE_PUBLIC = 1;
33 const AUDIENCE_RAW = 2;
34
36 private static $instances = [];
37
39 private $providerId;
40
46 public static function factory( $providerId = null ) {
48
49 if ( $providerId === null ) {
51 }
52
53 if ( !array_key_exists( $providerId, self::$instances ) ) {
54 self::$instances[$providerId] = null;
55
57 $provider = ObjectFactory::getObjectFromSpec( $wgCentralIdLookupProviders[$providerId] );
58 if ( $provider instanceof CentralIdLookup ) {
59 $provider->providerId = $providerId;
60 self::$instances[$providerId] = $provider;
61 }
62 }
63 }
64
65 return self::$instances[$providerId];
66 }
67
82 public static function factoryNonLocal() {
83 $centralIdLookup = self::factory();
84
85 if ( $centralIdLookup instanceof LocalIdLookup ) {
86 /*
87 * A LocalIdLookup (which is the default) may actually be non-local,
88 * if shared user tables are used.
89 * However, we cannot know that here, so play it safe and refuse to return it.
90 * See also T163277 and T170996.
91 */
92 return null;
93 }
94
95 return $centralIdLookup;
96 }
97
101 public static function resetCache() {
102 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
103 throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
104 }
105 self::$instances = [];
106 }
107
108 final public function getProviderId() {
109 return $this->providerId;
110 }
111
118 protected function checkAudience( $audience ) {
119 if ( $audience instanceof User ) {
120 return $audience;
121 }
122 if ( $audience === self::AUDIENCE_PUBLIC ) {
123 return new User;
124 }
125 if ( $audience === self::AUDIENCE_RAW ) {
126 return null;
127 }
128 throw new InvalidArgumentException( 'Invalid audience' );
129 }
130
142 abstract public function isAttached( User $user, $wikiId = null );
143
155 abstract public function lookupCentralIds(
156 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
157 );
158
170 abstract public function lookupUserNames(
171 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
172 );
173
184 public function nameFromCentralId(
185 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
186 ) {
187 $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
188 return $idToName[$id];
189 }
190
199 public function namesFromCentralIds(
200 array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
201 ) {
202 $idToName = array_fill_keys( $ids, false );
203 $names = $this->lookupCentralIds( $idToName, $audience, $flags );
204 $names = array_unique( $names );
205 $names = array_filter( $names, function ( $name ) {
206 return $name !== false && $name !== '';
207 } );
208
209 return array_values( $names );
210 }
211
222 public function centralIdFromName(
223 $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
224 ) {
225 $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
226 return $nameToId[$name];
227 }
228
237 public function centralIdsFromNames(
238 array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
239 ) {
240 $nameToId = array_fill_keys( $names, false );
241 $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
242 $ids = array_unique( $ids );
243 $ids = array_filter( $ids, function ( $id ) {
244 return $id !== false;
245 } );
246
247 return array_values( $ids );
248 }
249
261 public function localUserFromCentralId(
262 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
263 ) {
264 $name = $this->nameFromCentralId( $id, $audience, $flags );
265 if ( $name !== null && $name !== '' ) {
267 if ( $user && $user->getId() && $this->isAttached( $user ) ) {
268 return $user;
269 }
270 }
271 return null;
272 }
273
285 public function centralIdFromLocalUser(
286 User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
287 ) {
288 return $this->isAttached( $user )
289 ? $this->centralIdFromName( $user->getName(), $audience, $flags )
290 : 0;
291 }
292
293}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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.
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:53
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:591
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
the array() calling protocol came about after MediaWiki 1.4rc1.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for database access objects.