MediaWiki  1.34.0
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 !== '' ) {
237  $user = User::newFromName( $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 }
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:515
IDBAccessObject
Interface for database access objects.
Definition: IDBAccessObject.php:55
MWException
MediaWiki exception.
Definition: MWException.php:26
$wgCentralIdLookupProvider
string $wgCentralIdLookupProvider
Central ID lookup provider to use by default.
Definition: DefaultSettings.php:4408
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
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:4400
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:51
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2232
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