MediaWiki  master
UserRightsProxy.php
Go to the documentation of this file.
1 <?php
28 
33 class UserRightsProxy implements UserIdentity {
35 
37  private $db;
39  private $dbDomain;
41  private $name;
43  private $id;
45  private $newOptions;
48 
57  private function __construct( $db, $dbDomain, $name, $id ) {
58  $this->db = $db;
59  $this->dbDomain = $dbDomain;
60  $this->name = $name;
61  $this->id = intval( $id );
62  $this->newOptions = [];
63  $this->userGroupManager = MediaWikiServices::getInstance()
64  ->getUserGroupManagerFactory()
65  ->getUserGroupManager( $dbDomain );
66  }
67 
74  public static function validDatabase( $dbDomain ) {
75  global $wgLocalDatabases;
76  return in_array( $dbDomain, $wgLocalDatabases );
77  }
78 
87  public static function whoIs( $dbDomain, $id, $ignoreInvalidDB = false ) {
88  $user = self::newFromId( $dbDomain, $id, $ignoreInvalidDB );
89  if ( $user ) {
90  return $user->name;
91  } else {
92  return false;
93  }
94  }
95 
104  public static function newFromId( $dbDomain, $id, $ignoreInvalidDB = false ) {
105  return self::newFromLookup( $dbDomain, 'user_id', intval( $id ), $ignoreInvalidDB );
106  }
107 
116  public static function newFromName( $dbDomain, $name, $ignoreInvalidDB = false ) {
117  return self::newFromLookup( $dbDomain, 'user_name', $name, $ignoreInvalidDB );
118  }
119 
127  private static function newFromLookup( $dbDomain, $field, $value, $ignoreInvalidDB = false ) {
129  // If the user table is shared, perform the user query on it,
130  // but don't pass it to the UserRightsProxy,
131  // as user rights are normally not shared.
132  if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
133  $userdb = self::getDB( $wgSharedDB, $ignoreInvalidDB );
134  } else {
135  $userdb = self::getDB( $dbDomain, $ignoreInvalidDB );
136  }
137 
138  $db = self::getDB( $dbDomain, $ignoreInvalidDB );
139 
140  if ( $db && $userdb ) {
141  $row = $userdb->selectRow( 'user',
142  [ 'user_id', 'user_name' ],
143  [ $field => $value ],
144  __METHOD__ );
145 
146  if ( $row !== false ) {
147  return new UserRightsProxy(
148  $db, $dbDomain, $row->user_name, intval( $row->user_id ) );
149  }
150  }
151  return null;
152  }
153 
162  public static function getDB( $dbDomain, $ignoreInvalidDB = false ) {
163  if ( $ignoreInvalidDB || self::validDatabase( $dbDomain ) ) {
165  // Hmm... this shouldn't happen though. :)
166  return wfGetDB( DB_PRIMARY );
167  } else {
168  return wfGetDB( DB_PRIMARY, [], $dbDomain );
169  }
170  }
171  return null;
172  }
173 
178  public function getId( $wikiId = self::LOCAL ): int {
179  return $this->id;
180  }
181 
185  public function isAnon(): bool {
186  return !$this->isRegistered();
187  }
188 
194  public function getName(): string {
195  return $this->name . '@' . $this->dbDomain;
196  }
197 
203  public function getUserPage() {
204  return Title::makeTitle( NS_USER, $this->getName() );
205  }
206 
211  public function getGroups() {
212  return array_keys( self::getGroupMemberships() );
213  }
214 
221  public function getGroupMemberships() {
222  return $this->userGroupManager->getUserGroupMemberships( $this, IDBAccessObject::READ_LATEST );
223  }
224 
232  public function addGroup( $group, $expiry = null ) {
233  return $this->userGroupManager->addUserToGroup(
234  $this,
235  $group,
236  $expiry,
237  true
238  );
239  }
240 
247  public function removeGroup( $group ) {
248  return $this->userGroupManager->removeUserFromGroup(
249  $this,
250  $group
251  );
252  }
253 
259  public function setOption( $option, $value ) {
260  $this->newOptions[$option] = $value;
261  }
262 
263  public function saveSettings() {
264  $rows = [];
265  foreach ( $this->newOptions as $option => $value ) {
266  $rows[] = [
267  'up_user' => $this->id,
268  'up_property' => $option,
269  'up_value' => $value,
270  ];
271  }
272  $this->db->replace(
273  'user_properties',
274  [ [ 'up_user', 'up_property' ] ],
275  $rows,
276  __METHOD__
277  );
278  $this->invalidateCache();
279  }
280 
284  public function invalidateCache() {
285  $this->db->update(
286  'user',
287  [ 'user_touched' => $this->db->timestamp() ],
288  [ 'user_id' => $this->id ],
289  __METHOD__
290  );
291 
292  $domainId = $this->db->getDomainID();
293  $userId = $this->id;
294  $this->db->onTransactionPreCommitOrIdle(
295  static function () use ( $domainId, $userId ) {
296  User::purge( $domainId, $userId );
297  },
298  __METHOD__
299  );
300  }
301 
305  public function equals( ?UserIdentity $user ): bool {
306  if ( !$user ) {
307  return false;
308  }
309  return $this->getName() === $user->getName();
310  }
311 
315  public function isRegistered(): bool {
316  return $this->getId( $this->getWikiId() ) != 0;
317  }
318 
325  public function getWikiId() {
326  return $this->dbDomain;
327  }
328 }
MediaWiki\DAO\WikiAwareEntityTrait
trait WikiAwareEntityTrait
Definition: WikiAwareEntityTrait.php:32
UserRightsProxy\isRegistered
isRegistered()
1.34bool True if user is registered on this wiki, i.e., has a user ID. False if user is anonymous or ...
Definition: UserRightsProxy.php:315
$wgLocalDatabases
string[] $wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
Definition: DefaultSettings.php:2350
UserRightsProxy\equals
equals(?UserIdentity $user)
1.32bool
Definition: UserRightsProxy.php:305
$wgSharedTables
$wgSharedTables
Definition: DefaultSettings.php:2225
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:199
$wgSharedDB
$wgSharedDB
Shared database for multiple wikis.
Definition: DefaultSettings.php:2214
UserRightsProxy\saveSettings
saveSettings()
Definition: UserRightsProxy.php:263
UserRightsProxy\newFromId
static newFromId( $dbDomain, $id, $ignoreInvalidDB=false)
Factory function; get a remote user entry by ID number.
Definition: UserRightsProxy.php:104
UserRightsProxy\validDatabase
static validDatabase( $dbDomain)
Confirm the selected database name is a valid local interwiki database name.
Definition: UserRightsProxy.php:74
UserRightsProxy\getGroupMemberships
getGroupMemberships()
Replaces User::getGroupMemberships()
Definition: UserRightsProxy.php:221
UserRightsProxy\getWikiId
getWikiId()
Returns the db Domain of the wiki the UserRightsProxy is associated with.
Definition: UserRightsProxy.php:325
UserRightsProxy\$name
string $name
Definition: UserRightsProxy.php:41
UserRightsProxy
Cut-down copy of User interface for local-interwiki-database user rights manipulation.
Definition: UserRightsProxy.php:33
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
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:127
MediaWiki\User\UserGroupManager
Managers user groups.
Definition: UserGroupManager.php:52
UserRightsProxy\setOption
setOption( $option, $value)
Replaces User::setOption()
Definition: UserRightsProxy.php:259
UserRightsProxy\getId
getId( $wikiId=self::LOCAL)
Definition: UserRightsProxy.php:178
UserRightsProxy\$dbDomain
string $dbDomain
Definition: UserRightsProxy.php:39
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2200
UserRightsProxy\$newOptions
array $newOptions
Definition: UserRightsProxy.php:45
UserRightsProxy\invalidateCache
invalidateCache()
Replaces User::touchUser()
Definition: UserRightsProxy.php:284
MediaWiki\User\UserIdentity\getName
getName()
UserRightsProxy\getUserPage
getUserPage()
Same as User::getUserPage()
Definition: UserRightsProxy.php:203
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:650
UserRightsProxy\__construct
__construct( $db, $dbDomain, $name, $id)
Definition: UserRightsProxy.php:57
UserRightsProxy\$id
int $id
Definition: UserRightsProxy.php:43
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
UserRightsProxy\newFromName
static newFromName( $dbDomain, $name, $ignoreInvalidDB=false)
Factory function; get a remote user entry by name.
Definition: UserRightsProxy.php:116
UserRightsProxy\removeGroup
removeGroup( $group)
Replaces User::removeGroup()
Definition: UserRightsProxy.php:247
UserRightsProxy\$userGroupManager
UserGroupManager $userGroupManager
Definition: UserRightsProxy.php:47
NS_USER
const NS_USER
Definition: Defines.php:66
UserRightsProxy\whoIs
static whoIs( $dbDomain, $id, $ignoreInvalidDB=false)
Same as User::whoIs()
Definition: UserRightsProxy.php:87
UserRightsProxy\getName
getName()
Same as User::getName()
Definition: UserRightsProxy.php:194
User\purge
static purge( $dbDomain, $userId)
Definition: User.php:496
UserRightsProxy\$db
IDatabase $db
Definition: UserRightsProxy.php:37
UserRightsProxy\getDB
static getDB( $dbDomain, $ignoreInvalidDB=false)
Open a database connection to work on for the requested user.
Definition: UserRightsProxy.php:162
UserRightsProxy\isAnon
isAnon()
Definition: UserRightsProxy.php:185
UserRightsProxy\addGroup
addGroup( $group, $expiry=null)
Replaces User::addGroup()
Definition: UserRightsProxy.php:232
UserRightsProxy\getGroups
getGroups()
Replaces User::getUserGroups()
Definition: UserRightsProxy.php:211