MediaWiki  1.29.1
CentralIdLookup.php
Go to the documentation of this file.
1 <?php
29 abstract class CentralIdLookup implements IDBAccessObject {
30  // Audience options for accessors
31  const AUDIENCE_PUBLIC = 1;
32  const AUDIENCE_RAW = 2;
33 
35  private static $instances = [];
36 
38  private $providerId;
39 
45  public static function factory( $providerId = null ) {
46  global $wgCentralIdLookupProviders, $wgCentralIdLookupProvider;
47 
48  if ( $providerId === null ) {
49  $providerId = $wgCentralIdLookupProvider;
50  }
51 
52  if ( !array_key_exists( $providerId, self::$instances ) ) {
53  self::$instances[$providerId] = null;
54 
55  if ( isset( $wgCentralIdLookupProviders[$providerId] ) ) {
56  $provider = ObjectFactory::getObjectFromSpec( $wgCentralIdLookupProviders[$providerId] );
57  if ( $provider instanceof CentralIdLookup ) {
58  $provider->providerId = $providerId;
59  self::$instances[$providerId] = $provider;
60  }
61  }
62  }
63 
64  return self::$instances[$providerId];
65  }
66 
70  public static function resetCache() {
71  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
72  throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
73  }
74  self::$instances = [];
75  }
76 
77  final public function getProviderId() {
78  return $this->providerId;
79  }
80 
87  protected function checkAudience( $audience ) {
88  if ( $audience instanceof User ) {
89  return $audience;
90  }
91  if ( $audience === self::AUDIENCE_PUBLIC ) {
92  return new User;
93  }
94  if ( $audience === self::AUDIENCE_RAW ) {
95  return null;
96  }
97  throw new InvalidArgumentException( 'Invalid audience' );
98  }
99 
111  abstract public function isAttached( User $user, $wikiId = null );
112 
124  abstract public function lookupCentralIds(
125  array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
126  );
127 
139  abstract public function lookupUserNames(
140  array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
141  );
142 
153  public function nameFromCentralId(
154  $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
155  ) {
156  $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
157  return $idToName[$id];
158  }
159 
170  public function centralIdFromName(
171  $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
172  ) {
173  $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
174  return $nameToId[$name];
175  }
176 
188  public function localUserFromCentralId(
189  $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
190  ) {
191  $name = $this->nameFromCentralId( $id, $audience, $flags );
192  if ( $name !== null && $name !== '' ) {
194  if ( $user && $user->getId() && $this->isAttached( $user ) ) {
195  return $user;
196  }
197  }
198  return null;
199  }
200 
212  public function centralIdFromLocalUser(
213  User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
214  ) {
215  return $this->isAttached( $user )
216  ? $this->centralIdFromName( $user->getName(), $audience, $flags )
217  : 0;
218  }
219 
220 }
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:212
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:153
$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:246
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
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
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
CentralIdLookup\AUDIENCE_PUBLIC
const AUDIENCE_PUBLIC
Definition: CentralIdLookup.php:31
CentralIdLookup\resetCache
static resetCache()
Reset internal cache for unit testing.
Definition: CentralIdLookup.php:70
ObjectFactory\getObjectFromSpec
static getObjectFromSpec( $spec)
Instantiate an object based on a specification array.
Definition: ObjectFactory.php:59
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:35
CentralIdLookup\centralIdFromName
centralIdFromName( $name, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a (local) user name, return the central ID.
Definition: CentralIdLookup.php:170
CentralIdLookup\$providerId
string $providerId
Definition: CentralIdLookup.php:38
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:29
CentralIdLookup\isAttached
isAttached(User $user, $wikiId=null)
Check that a User is attached on the specified wiki.
CentralIdLookup\getProviderId
getProviderId()
Definition: CentralIdLookup.php:77
CentralIdLookup\checkAudience
checkAudience( $audience)
Check that the "audience" parameter is valid.
Definition: CentralIdLookup.php:87
CentralIdLookup\AUDIENCE_RAW
const AUDIENCE_RAW
Definition: CentralIdLookup.php:32
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:45
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2225
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2749
array
the array() calling protocol came about after MediaWiki 1.4rc1.
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:188