MediaWiki REL1_30
CentralIdLookup.php
Go to the documentation of this file.
1<?php
29abstract class CentralIdLookup implements IDBAccessObject {
30 // Audience options for accessors
31 const AUDIENCE_PUBLIC = 1;
32 const AUDIENCE_RAW = 2;
33
35 private static $instances = [];
36
38 private $providerId;
39
45 public static function factory( $providerId = null ) {
47
48 if ( $providerId === null ) {
50 }
51
52 if ( !array_key_exists( $providerId, self::$instances ) ) {
53 self::$instances[$providerId] = null;
54
56 $provider = ObjectFactory::getObjectFromSpec( $wgCentralIdLookupProviders[$providerId] );
57 if ( $provider instanceof CentralIdLookup ) {
58 $provider->providerId = $providerId;
59 self::$instances[$providerId] = $provider;
60 }
61 }
62 }
63
64 return self::$instances[$providerId];
65 }
66
70 public static function resetCache() {
71 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
72 throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
73 }
74 self::$instances = [];
75 }
76
77 final public function getProviderId() {
78 return $this->providerId;
79 }
80
87 protected function checkAudience( $audience ) {
88 if ( $audience instanceof User ) {
89 return $audience;
90 }
91 if ( $audience === self::AUDIENCE_PUBLIC ) {
92 return new User;
93 }
94 if ( $audience === self::AUDIENCE_RAW ) {
95 return null;
96 }
97 throw new InvalidArgumentException( 'Invalid audience' );
98 }
99
111 abstract public function isAttached( User $user, $wikiId = null );
112
124 abstract public function lookupCentralIds(
125 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
126 );
127
139 abstract public function lookupUserNames(
140 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
141 );
142
153 public function nameFromCentralId(
154 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
155 ) {
156 $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
157 return $idToName[$id];
158 }
159
168 public function namesFromCentralIds(
169 array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
170 ) {
171 $idToName = array_fill_keys( $ids, false );
172 $names = $this->lookupCentralIds( $idToName, $audience, $flags );
173 $names = array_unique( $names );
174 $names = array_filter( $names, function ( $name ) {
175 return $name !== false && $name !== '';
176 } );
177
178 return array_values( $names );
179 }
180
191 public function centralIdFromName(
192 $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
193 ) {
194 $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
195 return $nameToId[$name];
196 }
197
206 public function centralIdsFromNames(
207 array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
208 ) {
209 $nameToId = array_fill_keys( $names, false );
210 $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
211 $ids = array_unique( $ids );
212 $ids = array_filter( $ids, function ( $id ) {
213 return $id !== false;
214 } );
215
216 return array_values( $ids );
217 }
218
230 public function localUserFromCentralId(
231 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
232 ) {
233 $name = $this->nameFromCentralId( $id, $audience, $flags );
234 if ( $name !== null && $name !== '' ) {
235 $user = User::newFromName( $name );
236 if ( $user && $user->getId() && $this->isAttached( $user ) ) {
237 return $user;
238 }
239 }
240 return null;
241 }
242
254 public function centralIdFromLocalUser(
255 User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
256 ) {
257 return $this->isAttached( $user )
258 ? $this->centralIdFromName( $user->getName(), $audience, $flags )
259 : 0;
260 }
261
262}
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:51
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2249
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition hooks.txt:2805
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
Interface for database access objects.