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 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
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
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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:2454
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:48
$wgCentralIdLookupProviders
Central ID lookup providers Key is the provider ID, value is a specification for ObjectFactory.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
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.
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
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:2427
static resetCache()
Reset internal cache for unit testing.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:587
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1473