MediaWiki  1.32.5
CentralIdLookup.php
Go to the documentation of this file.
1 <?php
22 use Wikimedia\ObjectFactory;
23 
30 abstract 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 
56  if ( isset( $wgCentralIdLookupProviders[$providerId] ) ) {
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 
72  public static function resetCache() {
73  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
74  throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
75  }
76  self::$instances = [];
77  }
78 
79  final public function getProviderId() {
80  return $this->providerId;
81  }
82 
89  protected function checkAudience( $audience ) {
90  if ( $audience instanceof User ) {
91  return $audience;
92  }
93  if ( $audience === self::AUDIENCE_PUBLIC ) {
94  return new User;
95  }
96  if ( $audience === self::AUDIENCE_RAW ) {
97  return null;
98  }
99  throw new InvalidArgumentException( 'Invalid audience' );
100  }
101 
113  abstract public function isAttached( User $user, $wikiId = null );
114 
126  abstract public function lookupCentralIds(
127  array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
128  );
129 
141  abstract public function lookupUserNames(
142  array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
143  );
144 
155  public function nameFromCentralId(
156  $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
157  ) {
158  $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
159  return $idToName[$id];
160  }
161 
170  public function namesFromCentralIds(
171  array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
172  ) {
173  $idToName = array_fill_keys( $ids, false );
174  $names = $this->lookupCentralIds( $idToName, $audience, $flags );
175  $names = array_unique( $names );
176  $names = array_filter( $names, function ( $name ) {
177  return $name !== false && $name !== '';
178  } );
179 
180  return array_values( $names );
181  }
182 
193  public function centralIdFromName(
194  $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
195  ) {
196  $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
197  return $nameToId[$name];
198  }
199 
208  public function centralIdsFromNames(
209  array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
210  ) {
211  $nameToId = array_fill_keys( $names, false );
212  $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
213  $ids = array_unique( $ids );
214  $ids = array_filter( $ids, function ( $id ) {
215  return $id !== false;
216  } );
217 
218  return array_values( $ids );
219  }
220 
232  public function localUserFromCentralId(
233  $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
234  ) {
235  $name = $this->nameFromCentralId( $id, $audience, $flags );
236  if ( $name !== null && $name !== '' ) {
238  if ( $user && $user->getId() && $this->isAttached( $user ) ) {
239  return $user;
240  }
241  }
242  return null;
243  }
244 
256  public function centralIdFromLocalUser(
257  User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
258  ) {
259  return $this->isAttached( $user )
260  ? $this->centralIdFromName( $user->getName(), $audience, $flags )
261  : 0;
262  }
263 
264 }
$user
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 account $user
Definition: hooks.txt:244
CentralIdLookup\namesFromCentralIds
namesFromCentralIds(array $ids, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a an array of central user IDs, return the (local) user names.
Definition: CentralIdLookup.php:170
CentralIdLookup\centralIdFromLocalUser
centralIdFromLocalUser(User $user, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a local User object, return the central ID.
Definition: CentralIdLookup.php:256
CentralIdLookup\nameFromCentralId
nameFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return the (local) user name.
Definition: CentralIdLookup.php:155
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:592
User
User
Definition: All_system_messages.txt:425
IDBAccessObject
Interface for database access objects.
Definition: IDBAccessObject.php:55
php
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:35
MWException
MediaWiki exception.
Definition: MWException.php:26
$wgCentralIdLookupProvider
string $wgCentralIdLookupProvider
Central ID lookup provider to use by default.
Definition: DefaultSettings.php:4481
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
CentralIdLookup\centralIdsFromNames
centralIdsFromNames(array $names, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given an array of (local) user names, return the central IDs.
Definition: CentralIdLookup.php:208
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
CentralIdLookup\AUDIENCE_PUBLIC
const AUDIENCE_PUBLIC
Definition: CentralIdLookup.php:32
CentralIdLookup\resetCache
static resetCache()
Reset internal cache for unit testing.
Definition: CentralIdLookup.php:72
CentralIdLookup\lookupCentralIds
lookupCentralIds(array $idToName, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given central user IDs, return the (local) user names.
CentralIdLookup\$instances
static CentralIdLookup[] $instances
Definition: CentralIdLookup.php:36
$wgCentralIdLookupProviders
$wgCentralIdLookupProviders
Central ID lookup providers Key is the provider ID, value is a specification for ObjectFactory.
Definition: DefaultSettings.php:4473
CentralIdLookup\centralIdFromName
centralIdFromName( $name, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a (local) user name, return the central ID.
Definition: CentralIdLookup.php:193
CentralIdLookup\$providerId
string $providerId
Definition: CentralIdLookup.php:39
CentralIdLookup\lookupUserNames
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given (local) user names, return the central IDs.
CentralIdLookup
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Definition: CentralIdLookup.php:30
CentralIdLookup\isAttached
isAttached(User $user, $wikiId=null)
Check that a User is attached on the specified wiki.
CentralIdLookup\getProviderId
getProviderId()
Definition: CentralIdLookup.php:79
CentralIdLookup\checkAudience
checkAudience( $audience)
Check that the "audience" parameter is valid.
Definition: CentralIdLookup.php:89
CentralIdLookup\AUDIENCE_RAW
const AUDIENCE_RAW
Definition: CentralIdLookup.php:33
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:46
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:47
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2462
CentralIdLookup\localUserFromCentralId
localUserFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return a local User object.
Definition: CentralIdLookup.php:232