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  $localDatabases = MediaWikiServices::getInstance()->getMainConfig()->get( 'LocalDatabases' );
76  return in_array( $dbDomain, $localDatabases );
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 ) {
128  $sharedDB = MediaWikiServices::getInstance()->getMainConfig()->get( 'SharedDB' );
129  $sharedTables = MediaWikiServices::getInstance()->getMainConfig()->get( 'SharedTables' );
130  // If the user table is shared, perform the user query on it,
131  // but don't pass it to the UserRightsProxy,
132  // as user rights are normally not shared.
133  if ( $sharedDB && in_array( 'user', $sharedTables ) ) {
134  $userdb = self::getDB( $sharedDB, $ignoreInvalidDB );
135  } else {
136  $userdb = self::getDB( $dbDomain, $ignoreInvalidDB );
137  }
138 
139  $db = self::getDB( $dbDomain, $ignoreInvalidDB );
140 
141  if ( $db && $userdb ) {
142  $row = $userdb->selectRow( 'user',
143  [ 'user_id', 'user_name' ],
144  [ $field => $value ],
145  __METHOD__ );
146 
147  if ( $row !== false ) {
148  return new UserRightsProxy(
149  $db, $dbDomain, $row->user_name, intval( $row->user_id ) );
150  }
151  }
152  return null;
153  }
154 
163  public static function getDB( $dbDomain, $ignoreInvalidDB = false ) {
164  if ( $ignoreInvalidDB || self::validDatabase( $dbDomain ) ) {
166  // Hmm... this shouldn't happen though. :)
167  return wfGetDB( DB_PRIMARY );
168  } else {
169  return wfGetDB( DB_PRIMARY, [], $dbDomain );
170  }
171  }
172  return null;
173  }
174 
179  public function getId( $wikiId = self::LOCAL ): int {
180  return $this->id;
181  }
182 
186  public function isAnon(): bool {
187  return !$this->isRegistered();
188  }
189 
195  public function getName(): string {
196  return $this->name . '@' . $this->dbDomain;
197  }
198 
204  public function getUserPage() {
205  return Title::makeTitle( NS_USER, $this->getName() );
206  }
207 
212  public function getGroups() {
213  return array_keys( self::getGroupMemberships() );
214  }
215 
222  public function getGroupMemberships() {
223  return $this->userGroupManager->getUserGroupMemberships( $this, IDBAccessObject::READ_LATEST );
224  }
225 
233  public function addGroup( $group, $expiry = null ) {
234  return $this->userGroupManager->addUserToGroup(
235  $this,
236  $group,
237  $expiry,
238  true
239  );
240  }
241 
248  public function removeGroup( $group ) {
249  return $this->userGroupManager->removeUserFromGroup(
250  $this,
251  $group
252  );
253  }
254 
260  public function setOption( $option, $value ) {
261  $this->newOptions[$option] = $value;
262  }
263 
264  public function saveSettings() {
265  $rows = [];
266  foreach ( $this->newOptions as $option => $value ) {
267  $rows[] = [
268  'up_user' => $this->id,
269  'up_property' => $option,
270  'up_value' => $value,
271  ];
272  }
273  $this->db->replace(
274  'user_properties',
275  [ [ 'up_user', 'up_property' ] ],
276  $rows,
277  __METHOD__
278  );
279  $this->invalidateCache();
280  }
281 
285  public function invalidateCache() {
286  $this->db->update(
287  'user',
288  [ 'user_touched' => $this->db->timestamp() ],
289  [ 'user_id' => $this->id ],
290  __METHOD__
291  );
292 
293  $domainId = $this->db->getDomainID();
294  $userId = $this->id;
295  $this->db->onTransactionPreCommitOrIdle(
296  static function () use ( $domainId, $userId ) {
297  User::purge( $domainId, $userId );
298  },
299  __METHOD__
300  );
301  }
302 
306  public function equals( ?UserIdentity $user ): bool {
307  if ( !$user ) {
308  return false;
309  }
310  return $this->getName() === $user->getName();
311  }
312 
316  public function isRegistered(): bool {
317  return $this->getId( $this->getWikiId() ) != 0;
318  }
319 
326  public function getWikiId() {
327  return $this->dbDomain;
328  }
329 }
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:316
UserRightsProxy\equals
equals(?UserIdentity $user)
1.32bool
Definition: UserRightsProxy.php:306
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:203
UserRightsProxy\saveSettings
saveSettings()
Definition: UserRightsProxy.php:264
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:222
UserRightsProxy\getWikiId
getWikiId()
Returns the db Domain of the wiki the UserRightsProxy is associated with.
Definition: UserRightsProxy.php:326
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:260
UserRightsProxy\getId
getId( $wikiId=self::LOCAL)
Definition: UserRightsProxy.php:179
UserRightsProxy\$dbDomain
string $dbDomain
Definition: UserRightsProxy.php:39
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2186
UserRightsProxy\$newOptions
array $newOptions
Definition: UserRightsProxy.php:45
UserRightsProxy\invalidateCache
invalidateCache()
Replaces User::touchUser()
Definition: UserRightsProxy.php:285
MediaWiki\User\UserIdentity\getName
getName()
UserRightsProxy\getUserPage
getUserPage()
Same as User::getUserPage()
Definition: UserRightsProxy.php:204
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:648
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:248
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:195
User\purge
static purge( $dbDomain, $userId)
Definition: User.php:482
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:163
UserRightsProxy\isAnon
isAnon()
Definition: UserRightsProxy.php:186
UserRightsProxy\addGroup
addGroup( $group, $expiry=null)
Replaces User::addGroup()
Definition: UserRightsProxy.php:233
UserRightsProxy\getGroups
getGroups()
Replaces User::getUserGroups()
Definition: UserRightsProxy.php:212