MediaWiki  master
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 ) ) {
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  );
241  }
242 
249  public function removeGroup( $group ) {
250  return $this->userGroupManager->removeUserFromGroup(
251  // TODO: Artificial UserIdentity just for passing the id and name.
252  // see comment in getGroupMemberships.
253  new UserIdentityValue( $this->getId(), $this->getName(), 0 ),
254  $group
255  );
256  }
257 
263  public function setOption( $option, $value ) {
264  $this->newOptions[$option] = $value;
265  }
266 
267  public function saveSettings() {
268  $rows = [];
269  foreach ( $this->newOptions as $option => $value ) {
270  $rows[] = [
271  'up_user' => $this->id,
272  'up_property' => $option,
273  'up_value' => $value,
274  ];
275  }
276  $this->db->replace(
277  'user_properties',
278  [ [ 'up_user', 'up_property' ] ],
279  $rows,
280  __METHOD__
281  );
282  $this->invalidateCache();
283  }
284 
288  public function invalidateCache() {
289  $this->db->update(
290  'user',
291  [ 'user_touched' => $this->db->timestamp() ],
292  [ 'user_id' => $this->id ],
293  __METHOD__
294  );
295 
296  $domainId = $this->db->getDomainID();
297  $userId = $this->id;
298  $this->db->onTransactionPreCommitOrIdle(
299  function () use ( $domainId, $userId ) {
300  User::purge( $domainId, $userId );
301  },
302  __METHOD__
303  );
304  }
305 }
MediaWiki\User\UserIdentityValue
Value object representing a user's identity.
Definition: UserIdentityValue.php:34
$wgLocalDatabases
string[] $wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
Definition: DefaultSettings.php:2283
$wgSharedTables
$wgSharedTables
Definition: DefaultSettings.php:2162
WikiMap\isCurrentWikiId
static isCurrentWikiId( $wikiId)
Definition: WikiMap.php:321
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
$wgSharedDB
$wgSharedDB
Shared database for multiple wikis.
Definition: DefaultSettings.php:2152
UserRightsProxy\saveSettings
saveSettings()
Definition: UserRightsProxy.php:267
UserRightsProxy\newFromId
static newFromId( $dbDomain, $id, $ignoreInvalidDB=false)
Factory function; get a remote user entry by ID number.
Definition: UserRightsProxy.php:101
UserRightsProxy\validDatabase
static validDatabase( $dbDomain)
Confirm the selected database name is a valid local interwiki database name.
Definition: UserRightsProxy.php:71
UserRightsProxy\getGroupMemberships
getGroupMemberships()
Replaces User::getGroupMemberships()
Definition: UserRightsProxy.php:217
UserRightsProxy\$name
string $name
Definition: UserRightsProxy.php:38
UserRightsProxy\getId
getId()
Definition: UserRightsProxy.php:174
UserRightsProxy
Cut-down copy of User interface for local-interwiki-database user rights manipulation.
Definition: UserRightsProxy.php:32
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
UserRightsProxy\newFromLookup
static newFromLookup( $dbDomain, $field, $value, $ignoreInvalidDB=false)
Definition: UserRightsProxy.php:124
MediaWiki\User\UserGroupManager
Managers user groups.
Definition: UserGroupManager.php:51
UserRightsProxy\setOption
setOption( $option, $value)
Replaces User::setOption()
Definition: UserRightsProxy.php:263
UserRightsProxy\$dbDomain
string $dbDomain
Definition: UserRightsProxy.php:36
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2467
UserRightsProxy\$newOptions
array $newOptions
Definition: UserRightsProxy.php:42
UserRightsProxy\invalidateCache
invalidateCache()
Replaces User::touchUser()
Definition: UserRightsProxy.php:288
UserRightsProxy\getUserPage
getUserPage()
Same as User::getUserPage()
Definition: UserRightsProxy.php:199
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
UserRightsProxy\__construct
__construct( $db, $dbDomain, $name, $id)
Definition: UserRightsProxy.php:54
DB_MASTER
const DB_MASTER
Definition: defines.php:26
UserRightsProxy\$id
int $id
Definition: UserRightsProxy.php:40
UserRightsProxy\newFromName
static newFromName( $dbDomain, $name, $ignoreInvalidDB=false)
Factory function; get a remote user entry by name.
Definition: UserRightsProxy.php:113
UserRightsProxy\removeGroup
removeGroup( $group)
Replaces User::removeGroup()
Definition: UserRightsProxy.php:249
UserRightsProxy\$userGroupManager
UserGroupManager $userGroupManager
Definition: UserRightsProxy.php:44
UserRightsProxy\whoIs
static whoIs( $dbDomain, $id, $ignoreInvalidDB=false)
Same as User::whoIs()
Definition: UserRightsProxy.php:84
UserRightsProxy\getName
getName()
Same as User::getName()
Definition: UserRightsProxy.php:190
User\purge
static purge( $dbDomain, $userId)
Definition: User.php:433
UserRightsProxy\$db
IDatabase $db
Definition: UserRightsProxy.php:34
NS_USER
const NS_USER
Definition: Defines.php:71
UserRightsProxy\getDB
static getDB( $dbDomain, $ignoreInvalidDB=false)
Open a database connection to work on for the requested user.
Definition: UserRightsProxy.php:159
UserRightsProxy\isAnon
isAnon()
Definition: UserRightsProxy.php:181
UserRightsProxy\addGroup
addGroup( $group, $expiry=null)
Replaces User::addGroup()
Definition: UserRightsProxy.php:233
UserRightsProxy\getGroups
getGroups()
Replaces User::getUserGroups()
Definition: UserRightsProxy.php:207