MediaWiki  master
CentralIdLookup.php
Go to the documentation of this file.
1 <?php
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 }
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given (local) user names, return the central IDs.
isAttached(User $user, $wikiId=null)
Check that a User is attached on the specified wiki.
static CentralIdLookup [] $instances
lookupCentralIds(array $idToName, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given central user IDs, return the (local) user names.
string $wgCentralIdLookupProvider
Central ID lookup provider to use by default.
centralIdFromName( $name, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a (local) user name, return the central ID.
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2283
namesFromCentralIds(array $ids, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a an array of central user IDs, return the (local) user names.
nameFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return the (local) user name.
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
$wgCentralIdLookupProviders
Central ID lookup providers Key is the provider ID, value is a specification for ObjectFactory.
checkAudience( $audience)
Check that the "audience" parameter is valid.
centralIdFromLocalUser(User $user, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a local User object, return the central ID.
centralIdsFromNames(array $names, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given an array of (local) user names, return the central IDs.
static factory( $providerId=null)
Fetch a CentralIdLookup.
localUserFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return a local User object.
getId()
Get the user&#39;s ID.
Definition: User.php:2254
static resetCache()
Reset internal cache for unit testing.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:536