Merge "Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment"
[lhc/web/wiklou.git] / includes / user / CentralIdLookup.php
1 <?php
2 /**
3 * A central user id lookup service
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22 use Wikimedia\ObjectFactory;
23
24 /**
25 * The CentralIdLookup service allows for connecting local users with
26 * cluster-wide IDs.
27 *
28 * @since 1.27
29 */
30 abstract class CentralIdLookup implements IDBAccessObject {
31 // Audience options for accessors
32 const AUDIENCE_PUBLIC = 1;
33 const AUDIENCE_RAW = 2;
34
35 /** @var CentralIdLookup[] */
36 private static $instances = [];
37
38 /** @var string */
39 private $providerId;
40
41 /**
42 * Fetch a CentralIdLookup
43 * @param string|null $providerId Provider ID from $wgCentralIdLookupProviders
44 * @return CentralIdLookup|null
45 */
46 public static function factory( $providerId = null ) {
47 global $wgCentralIdLookupProviders, $wgCentralIdLookupProvider;
48
49 if ( $providerId === null ) {
50 $providerId = $wgCentralIdLookupProvider;
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
68 /**
69 * Reset internal cache for unit testing
70 */
71 public static function resetCache() {
72 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
73 throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
74 }
75 self::$instances = [];
76 }
77
78 final public function getProviderId() {
79 return $this->providerId;
80 }
81
82 /**
83 * Check that the "audience" parameter is valid
84 * @param int|User $audience One of the audience constants, or a specific user
85 * @return User|null User to check against, or null if no checks are needed
86 * @throws InvalidArgumentException
87 */
88 protected function checkAudience( $audience ) {
89 if ( $audience instanceof User ) {
90 return $audience;
91 }
92 if ( $audience === self::AUDIENCE_PUBLIC ) {
93 return new User;
94 }
95 if ( $audience === self::AUDIENCE_RAW ) {
96 return null;
97 }
98 throw new InvalidArgumentException( 'Invalid audience' );
99 }
100
101 /**
102 * Check that a User is attached on the specified wiki.
103 *
104 * If unattached local accounts don't exist in your extension, this comes
105 * down to a check whether the central account exists at all and that
106 * $wikiId is using the same central database.
107 *
108 * @param User $user
109 * @param string|null $wikiId Wiki to check attachment status. If null, check the current wiki.
110 * @return bool
111 */
112 abstract public function isAttached( User $user, $wikiId = null );
113
114 /**
115 * Given central user IDs, return the (local) user names
116 * @note There's no requirement that the user names actually exist locally,
117 * or if they do that they're actually attached to the central account.
118 * @param array $idToName Array with keys being central user IDs
119 * @param int|User $audience One of the audience constants, or a specific user
120 * @param int $flags IDBAccessObject read flags
121 * @return array Copy of $idToName with values set to user names (or
122 * empty-string if the user exists but $audience lacks the rights needed
123 * to see it). IDs not corresponding to a user are unchanged.
124 */
125 abstract public function lookupCentralIds(
126 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
127 );
128
129 /**
130 * Given (local) user names, return the central IDs
131 * @note There's no requirement that the user names actually exist locally,
132 * or if they do that they're actually attached to the central account.
133 * @param array $nameToId Array with keys being canonicalized user names
134 * @param int|User $audience One of the audience constants, or a specific user
135 * @param int $flags IDBAccessObject read flags
136 * @return array Copy of $nameToId with values set to central IDs.
137 * Names not corresponding to a user (or $audience lacks the rights needed
138 * to see it) are unchanged.
139 */
140 abstract public function lookupUserNames(
141 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
142 );
143
144 /**
145 * Given a central user ID, return the (local) user name
146 * @note There's no requirement that the user name actually exists locally,
147 * or if it does that it's actually attached to the central account.
148 * @param int $id Central user ID
149 * @param int|User $audience One of the audience constants, or a specific user
150 * @param int $flags IDBAccessObject read flags
151 * @return string|null User name, or empty string if $audience lacks the
152 * rights needed to see it, or null if $id doesn't correspond to a user
153 */
154 public function nameFromCentralId(
155 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
156 ) {
157 $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
158 return $idToName[$id];
159 }
160
161 /**
162 * Given a an array of central user IDs, return the (local) user names.
163 * @param int[] $ids Central user IDs
164 * @param int|User $audience One of the audience constants, or a specific user
165 * @param int $flags IDBAccessObject read flags
166 * @return string[] User names
167 * @since 1.30
168 */
169 public function namesFromCentralIds(
170 array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
171 ) {
172 $idToName = array_fill_keys( $ids, false );
173 $names = $this->lookupCentralIds( $idToName, $audience, $flags );
174 $names = array_unique( $names );
175 $names = array_filter( $names, function ( $name ) {
176 return $name !== false && $name !== '';
177 } );
178
179 return array_values( $names );
180 }
181
182 /**
183 * Given a (local) user name, return the central ID
184 * @note There's no requirement that the user name actually exists locally,
185 * or if it does that it's actually attached to the central account.
186 * @param string $name Canonicalized user name
187 * @param int|User $audience One of the audience constants, or a specific user
188 * @param int $flags IDBAccessObject read flags
189 * @return int User ID; 0 if the name does not correspond to a user or
190 * $audience lacks the rights needed to see it.
191 */
192 public function centralIdFromName(
193 $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
194 ) {
195 $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
196 return $nameToId[$name];
197 }
198
199 /**
200 * Given an array of (local) user names, return the central IDs.
201 * @param string[] $names Canonicalized user names
202 * @param int|User $audience One of the audience constants, or a specific user
203 * @param int $flags IDBAccessObject read flags
204 * @return int[] User IDs
205 * @since 1.30
206 */
207 public function centralIdsFromNames(
208 array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
209 ) {
210 $nameToId = array_fill_keys( $names, false );
211 $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
212 $ids = array_unique( $ids );
213 $ids = array_filter( $ids, function ( $id ) {
214 return $id !== false;
215 } );
216
217 return array_values( $ids );
218 }
219
220 /**
221 * Given a central user ID, return a local User object
222 * @note Unlike nameFromCentralId(), this does guarantee that the local
223 * user exists and is attached to the central account.
224 * @param int $id Central user ID
225 * @param int|User $audience One of the audience constants, or a specific user
226 * @param int $flags IDBAccessObject read flags
227 * @return User|null Local user, or null if: $id doesn't correspond to a
228 * user, $audience lacks the rights needed to see the user, the user
229 * doesn't exist locally, or the user isn't locally attached.
230 */
231 public function localUserFromCentralId(
232 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
233 ) {
234 $name = $this->nameFromCentralId( $id, $audience, $flags );
235 if ( $name !== null && $name !== '' ) {
236 $user = User::newFromName( $name );
237 if ( $user && $user->getId() && $this->isAttached( $user ) ) {
238 return $user;
239 }
240 }
241 return null;
242 }
243
244 /**
245 * Given a local User object, return the central ID
246 * @note Unlike centralIdFromName(), this does guarantee that the local
247 * user is attached to the central account.
248 * @param User $user Local user
249 * @param int|User $audience One of the audience constants, or a specific user
250 * @param int $flags IDBAccessObject read flags
251 * @return int User ID; 0 if the local user does not correspond to a
252 * central user, $audience lacks the rights needed to see it, or the
253 * central user isn't locally attached.
254 */
255 public function centralIdFromLocalUser(
256 User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
257 ) {
258 return $this->isAttached( $user )
259 ? $this->centralIdFromName( $user->getName(), $audience, $flags )
260 : 0;
261 }
262
263 }