MediaWiki REL1_35
UserRightsProxy.php
Go to the documentation of this file.
1<?php
27
34 private $db;
36 private $dbDomain;
38 private $name;
40 private $id;
42 private $newOptions;
45
54 private function __construct( $db, $dbDomain, $name, $id ) {
55 $this->db = $db;
56 $this->dbDomain = $dbDomain;
57 $this->name = $name;
58 $this->id = intval( $id );
59 $this->newOptions = [];
60 $this->userGroupManager = MediaWikiServices::getInstance()
61 ->getUserGroupManagerFactory()
62 ->getUserGroupManager( $dbDomain );
63 }
64
71 public static function validDatabase( $dbDomain ) {
72 global $wgLocalDatabases;
73 return in_array( $dbDomain, $wgLocalDatabases );
74 }
75
84 public static function whoIs( $dbDomain, $id, $ignoreInvalidDB = false ) {
85 $user = self::newFromId( $dbDomain, $id, $ignoreInvalidDB );
86 if ( $user ) {
87 return $user->name;
88 } else {
89 return false;
90 }
91 }
92
101 public static function newFromId( $dbDomain, $id, $ignoreInvalidDB = false ) {
102 return self::newFromLookup( $dbDomain, 'user_id', intval( $id ), $ignoreInvalidDB );
103 }
104
113 public static function newFromName( $dbDomain, $name, $ignoreInvalidDB = false ) {
114 return self::newFromLookup( $dbDomain, 'user_name', $name, $ignoreInvalidDB );
115 }
116
124 private static function newFromLookup( $dbDomain, $field, $value, $ignoreInvalidDB = false ) {
126 // If the user table is shared, perform the user query on it,
127 // but don't pass it to the UserRightsProxy,
128 // as user rights are normally not shared.
129 if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
130 $userdb = self::getDB( $wgSharedDB, $ignoreInvalidDB );
131 } else {
132 $userdb = self::getDB( $dbDomain, $ignoreInvalidDB );
133 }
134
135 $db = self::getDB( $dbDomain, $ignoreInvalidDB );
136
137 if ( $db && $userdb ) {
138 $row = $userdb->selectRow( 'user',
139 [ 'user_id', 'user_name' ],
140 [ $field => $value ],
141 __METHOD__ );
142
143 if ( $row !== false ) {
144 return new UserRightsProxy(
145 $db, $dbDomain, $row->user_name, intval( $row->user_id ) );
146 }
147 }
148 return null;
149 }
150
159 public static function getDB( $dbDomain, $ignoreInvalidDB = false ) {
160 if ( $ignoreInvalidDB || self::validDatabase( $dbDomain ) ) {
161 if ( WikiMap::isCurrentWikiId( $dbDomain ) ) {
162 // Hmm... this shouldn't happen though. :)
163 return wfGetDB( DB_MASTER );
164 } else {
165 return wfGetDB( DB_MASTER, [], $dbDomain );
166 }
167 }
168 return null;
169 }
170
174 public function getId() {
175 return $this->id;
176 }
177
181 public function isAnon() {
182 return $this->getId() == 0;
183 }
184
190 public function getName() {
191 return $this->name . '@' . $this->dbDomain;
192 }
193
199 public function getUserPage() {
200 return Title::makeTitle( NS_USER, $this->getName() );
201 }
202
207 public function getGroups() {
208 return array_keys( self::getGroupMemberships() );
209 }
210
217 public function getGroupMemberships() {
218 // TODO: We are creating an artificial UserIdentity to pass on to the user group manager.
219 // After all the relevant UserGroupMemberships methods are ported into UserGroupManager,
220 // the usages of this class will be changed into usages of the UserGroupManager,
221 // thus the need of this class and the need of this artificial UserIdentityValue will parish.
222 $user = new UserIdentityValue( $this->getId(), $this->getName(), 0 );
223 return $this->userGroupManager->getUserGroupMemberships( $user, IDBAccessObject::READ_LATEST );
224 }
225
233 public function addGroup( $group, $expiry = null ) {
234 return $this->userGroupManager->addUserToGroup(
235 // TODO: Artificial UserIdentity just for passing the id and name.
236 // see comment in getGroupMemberships.
237 new UserIdentityValue( $this->getId(), $this->getName(), 0 ),
238 $group,
239 $expiry,
240 true
241 );
242 }
243
250 public function removeGroup( $group ) {
251 return $this->userGroupManager->removeUserFromGroup(
252 // TODO: Artificial UserIdentity just for passing the id and name.
253 // see comment in getGroupMemberships.
254 new UserIdentityValue( $this->getId(), $this->getName(), 0 ),
255 $group
256 );
257 }
258
264 public function setOption( $option, $value ) {
265 $this->newOptions[$option] = $value;
266 }
267
268 public function saveSettings() {
269 $rows = [];
270 foreach ( $this->newOptions as $option => $value ) {
271 $rows[] = [
272 'up_user' => $this->id,
273 'up_property' => $option,
274 'up_value' => $value,
275 ];
276 }
277 $this->db->replace(
278 'user_properties',
279 [ [ 'up_user', 'up_property' ] ],
280 $rows,
281 __METHOD__
282 );
283 $this->invalidateCache();
284 }
285
289 public function invalidateCache() {
290 $this->db->update(
291 'user',
292 [ 'user_touched' => $this->db->timestamp() ],
293 [ 'user_id' => $this->id ],
294 __METHOD__
295 );
296
297 $domainId = $this->db->getDomainID();
298 $userId = $this->id;
299 $this->db->onTransactionPreCommitOrIdle(
300 function () use ( $domainId, $userId ) {
301 User::purge( $domainId, $userId );
302 },
303 __METHOD__
304 );
305 }
306}
getDB()
$wgSharedTables
$wgSharedDB
Shared database for multiple wikis.
string[] $wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Value object representing a user's identity.
Cut-down copy of User interface for local-interwiki-database user rights manipulation.
static newFromId( $dbDomain, $id, $ignoreInvalidDB=false)
Factory function; get a remote user entry by ID number.
getName()
Same as User::getName()
getGroupMemberships()
Replaces User::getGroupMemberships()
static whoIs( $dbDomain, $id, $ignoreInvalidDB=false)
Same as User::whoIs()
removeGroup( $group)
Replaces User::removeGroup()
static newFromName( $dbDomain, $name, $ignoreInvalidDB=false)
Factory function; get a remote user entry by name.
static validDatabase( $dbDomain)
Confirm the selected database name is a valid local interwiki database name.
static newFromLookup( $dbDomain, $field, $value, $ignoreInvalidDB=false)
getGroups()
Replaces User::getUserGroups()
UserGroupManager $userGroupManager
setOption( $option, $value)
Replaces User::setOption()
__construct( $db, $dbDomain, $name, $id)
invalidateCache()
Replaces User::touchUser()
static getDB( $dbDomain, $ignoreInvalidDB=false)
Open a database connection to work on for the requested user.
addGroup( $group, $expiry=null)
Replaces User::addGroup()
getUserPage()
Same as User::getUserPage()
static purge( $dbDomain, $userId)
Definition User.php:436
const NS_USER
Definition Defines.php:72
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
const DB_MASTER
Definition defines.php:29