MediaWiki  1.34.4
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 
82  public static function factoryNonLocal(): ?self {
83  $centralIdLookup = self::factory();
84 
85  if ( $centralIdLookup instanceof LocalIdLookup ) {
86  /*
87  * A LocalIdLookup (which is the default) may actually be non-local,
88  * if shared user tables are used.
89  * However, we cannot know that here, so play it safe and refuse to return it.
90  * See also T163277 and T170996.
91  */
92  return null;
93  }
94 
95  return $centralIdLookup;
96  }
97 
102  public static function resetCache() {
103  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
104  throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
105  }
106  self::$instances = [];
107  }
108 
109  final public function getProviderId() {
110  return $this->providerId;
111  }
112 
119  protected function checkAudience( $audience ) {
120  if ( $audience instanceof User ) {
121  return $audience;
122  }
123  if ( $audience === self::AUDIENCE_PUBLIC ) {
124  return new User;
125  }
126  if ( $audience === self::AUDIENCE_RAW ) {
127  return null;
128  }
129  throw new InvalidArgumentException( 'Invalid audience' );
130  }
131 
143  abstract public function isAttached( User $user, $wikiId = null );
144 
156  abstract public function lookupCentralIds(
157  array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
158  );
159 
171  abstract public function lookupUserNames(
172  array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
173  );
174 
185  public function nameFromCentralId(
186  $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
187  ) {
188  $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
189  return $idToName[$id];
190  }
191 
200  public function namesFromCentralIds(
201  array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
202  ) {
203  $idToName = array_fill_keys( $ids, false );
204  $names = $this->lookupCentralIds( $idToName, $audience, $flags );
205  $names = array_unique( $names );
206  $names = array_filter( $names, function ( $name ) {
207  return $name !== false && $name !== '';
208  } );
209 
210  return array_values( $names );
211  }
212 
223  public function centralIdFromName(
224  $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
225  ) {
226  $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
227  return $nameToId[$name];
228  }
229 
238  public function centralIdsFromNames(
239  array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
240  ) {
241  $nameToId = array_fill_keys( $names, false );
242  $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
243  $ids = array_unique( $ids );
244  $ids = array_filter( $ids, function ( $id ) {
245  return $id !== false;
246  } );
247 
248  return array_values( $ids );
249  }
250 
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 
286  public function centralIdFromLocalUser(
287  User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
288  ) {
289  return $this->isAttached( $user )
290  ? $this->centralIdFromName( $user->getName(), $audience, $flags )
291  : 0;
292  }
293 
294 }
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:200
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:286
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:185
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:518
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:4432
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:238
CentralIdLookup\AUDIENCE_PUBLIC
const AUDIENCE_PUBLIC
Definition: CentralIdLookup.php:32
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:102
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:4424
CentralIdLookup\centralIdFromName
centralIdFromName( $name, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a (local) user name, return the central ID.
Definition: CentralIdLookup.php:223
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:109
CentralIdLookup\checkAudience
checkAudience( $audience)
Check that the "audience" parameter is valid.
Definition: CentralIdLookup.php:119
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:2364
CentralIdLookup\factoryNonLocal
static factoryNonLocal()
Returns a CentralIdLookup that is guaranteed to be non-local.
Definition: CentralIdLookup.php:82
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