MediaWiki REL1_33
CentralIdLookup.php
Go to the documentation of this file.
1<?php
22use Wikimedia\ObjectFactory;
23
30abstract 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
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}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
string $wgCentralIdLookupProvider
Central ID lookup provider to use by default.
$wgCentralIdLookupProviders
Central ID lookup providers Key is the provider ID, value is a specification for ObjectFactory.
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
static resetCache()
Reset internal cache for unit testing.
static factory( $providerId=null)
Fetch a CentralIdLookup.
nameFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return the (local) user name.
isAttached(User $user, $wikiId=null)
Check that a User is attached on the specified wiki.
centralIdFromLocalUser(User $user, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a local User object, return the central ID.
static CentralIdLookup[] $instances
lookupCentralIds(array $idToName, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given central user IDs, return the (local) user names.
centralIdFromName( $name, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a (local) user name, return the central ID.
namesFromCentralIds(array $ids, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a an array of central user IDs, return the (local) user names.
centralIdsFromNames(array $names, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given an array of (local) user names, return the central IDs.
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given (local) user names, return the central IDs.
checkAudience( $audience)
Check that the "audience" parameter is valid.
localUserFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return a local User object.
MediaWiki exception.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
Interface for database access objects.
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))