MediaWiki  master
CentralIdLookup.php
Go to the documentation of this file.
1 <?php
22 use Wikimedia\ObjectFactory;
23 
31 abstract class CentralIdLookup implements IDBAccessObject {
32  // Audience options for accessors
33  public const AUDIENCE_PUBLIC = 1;
34  public const AUDIENCE_RAW = 2;
35 
37  private static $instances = [];
38 
40  private $providerId;
41 
47  public static function factory( $providerId = null ) {
49 
50  if ( $providerId === null ) {
52  }
53 
54  if ( !array_key_exists( $providerId, self::$instances ) ) {
55  self::$instances[$providerId] = null;
56 
57  if ( isset( $wgCentralIdLookupProviders[$providerId] ) ) {
58  $provider = ObjectFactory::getObjectFromSpec( $wgCentralIdLookupProviders[$providerId] );
59  if ( $provider instanceof CentralIdLookup ) {
60  $provider->providerId = $providerId;
61  self::$instances[$providerId] = $provider;
62  }
63  }
64  }
65 
66  return self::$instances[$providerId];
67  }
68 
81  public static function factoryNonLocal(): ?self {
82  $centralIdLookup = self::factory();
83 
84  if ( $centralIdLookup instanceof LocalIdLookup ) {
85  /*
86  * A LocalIdLookup (which is the default) may actually be non-local,
87  * if shared user tables are used.
88  * However, we cannot know that here, so play it safe and refuse to return it.
89  * See also T163277 and T170996.
90  */
91  return null;
92  }
93 
94  return $centralIdLookup;
95  }
96 
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 
262  public function localUserFromCentralId(
263  $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
264  ) {
265  $name = $this->nameFromCentralId( $id, $audience, $flags );
266  if ( $name !== null && $name !== '' ) {
267  $user = User::newFromName( $name );
268  if ( $user && $user->getId() && $this->isAttached( $user ) ) {
269  return $user;
270  }
271  }
272  return null;
273  }
274 
287  public function centralIdFromLocalUser(
288  User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
289  ) {
290  return $this->isAttached( $user )
291  ? $this->centralIdFromName( $user->getName(), $audience, $flags )
292  : 0;
293  }
294 
295 }
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:199
CentralIdLookup\centralIdFromLocalUser
centralIdFromLocalUser(User $user, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a local User object, return the central ID Stable to override.
Definition: CentralIdLookup.php:287
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:184
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:538
IDBAccessObject
Interface for database access objects.
Definition: IDBAccessObject.php:57
MWException
MediaWiki exception.
Definition: MWException.php:29
$wgCentralIdLookupProvider
string $wgCentralIdLookupProvider
Central ID lookup provider to use by default.
Definition: DefaultSettings.php:4805
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:237
CentralIdLookup\AUDIENCE_PUBLIC
const AUDIENCE_PUBLIC
Definition: CentralIdLookup.php:33
LocalIdLookup
A CentralIdLookup provider that just uses local IDs.
Definition: LocalIdLookup.php:35
CentralIdLookup\resetCache
static resetCache()
Reset internal cache for unit testing.
Definition: CentralIdLookup.php:101
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:37
$wgCentralIdLookupProviders
$wgCentralIdLookupProviders
Central ID lookup providers Key is the provider ID, value is a specification for ObjectFactory.
Definition: DefaultSettings.php:4797
CentralIdLookup\centralIdFromName
centralIdFromName( $name, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a (local) user name, return the central ID.
Definition: CentralIdLookup.php:222
CentralIdLookup\$providerId
string $providerId
Definition: CentralIdLookup.php:40
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:31
CentralIdLookup\isAttached
isAttached(User $user, $wikiId=null)
Check that a User is attached on the specified wiki.
CentralIdLookup\getProviderId
getProviderId()
Definition: CentralIdLookup.php:108
CentralIdLookup\checkAudience
checkAudience( $audience)
Check that the "audience" parameter is valid.
Definition: CentralIdLookup.php:118
CentralIdLookup\AUDIENCE_RAW
const AUDIENCE_RAW
Definition: CentralIdLookup.php:34
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:47
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:1954
CentralIdLookup\factoryNonLocal
static factoryNonLocal()
Returns a CentralIdLookup that is guaranteed to be non-local.
Definition: CentralIdLookup.php:81
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:262