MediaWiki  1.33.0
UserRightsProxy.php
Go to the documentation of this file.
1 <?php
24 
31  private $db;
33  private $wikiId;
35  private $name;
37  private $id;
39  private $newOptions;
40 
49  private function __construct( $db, $wikiId, $name, $id ) {
50  $this->db = $db;
51  $this->wikiId = $wikiId;
52  $this->name = $name;
53  $this->id = intval( $id );
54  $this->newOptions = [];
55  }
56 
63  public static function validDatabase( $wikiId ) {
64  global $wgLocalDatabases;
65  return in_array( $wikiId, $wgLocalDatabases );
66  }
67 
76  public static function whoIs( $wikiId, $id, $ignoreInvalidDB = false ) {
77  $user = self::newFromId( $wikiId, $id, $ignoreInvalidDB );
78  if ( $user ) {
79  return $user->name;
80  } else {
81  return false;
82  }
83  }
84 
93  public static function newFromId( $wikiId, $id, $ignoreInvalidDB = false ) {
94  return self::newFromLookup( $wikiId, 'user_id', intval( $id ), $ignoreInvalidDB );
95  }
96 
105  public static function newFromName( $wikiId, $name, $ignoreInvalidDB = false ) {
106  return self::newFromLookup( $wikiId, 'user_name', $name, $ignoreInvalidDB );
107  }
108 
116  private static function newFromLookup( $wikiId, $field, $value, $ignoreInvalidDB = false ) {
118  // If the user table is shared, perform the user query on it,
119  // but don't pass it to the UserRightsProxy,
120  // as user rights are normally not shared.
121  if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
122  $userdb = self::getDB( $wgSharedDB, $ignoreInvalidDB );
123  } else {
124  $userdb = self::getDB( $wikiId, $ignoreInvalidDB );
125  }
126 
127  $db = self::getDB( $wikiId, $ignoreInvalidDB );
128 
129  if ( $db && $userdb ) {
130  $row = $userdb->selectRow( 'user',
131  [ 'user_id', 'user_name' ],
132  [ $field => $value ],
133  __METHOD__ );
134 
135  if ( $row !== false ) {
136  return new UserRightsProxy(
137  $db, $wikiId, $row->user_name, intval( $row->user_id ) );
138  }
139  }
140  return null;
141  }
142 
151  public static function getDB( $wikiId, $ignoreInvalidDB = false ) {
152  if ( $ignoreInvalidDB || self::validDatabase( $wikiId ) ) {
154  // Hmm... this shouldn't happen though. :)
155  return wfGetDB( DB_MASTER );
156  } else {
157  return wfGetDB( DB_MASTER, [], $wikiId );
158  }
159  }
160  return null;
161  }
162 
166  public function getId() {
167  return $this->id;
168  }
169 
173  public function isAnon() {
174  return $this->getId() == 0;
175  }
176 
182  public function getName() {
183  return $this->name . '@' . $this->wikiId;
184  }
185 
191  public function getUserPage() {
192  return Title::makeTitle( NS_USER, $this->getName() );
193  }
194 
199  function getGroups() {
200  return array_keys( self::getGroupMemberships() );
201  }
202 
209  function getGroupMemberships() {
210  return UserGroupMembership::getMembershipsForUser( $this->id, $this->db );
211  }
212 
220  function addGroup( $group, $expiry = null ) {
221  if ( $expiry ) {
222  $expiry = wfTimestamp( TS_MW, $expiry );
223  }
224 
225  $ugm = new UserGroupMembership( $this->id, $group, $expiry );
226  return $ugm->insert( true, $this->db );
227  }
228 
235  function removeGroup( $group ) {
236  $ugm = UserGroupMembership::getMembership( $this->id, $group, $this->db );
237  if ( !$ugm ) {
238  return false;
239  }
240  return $ugm->delete( $this->db );
241  }
242 
248  public function setOption( $option, $value ) {
249  $this->newOptions[$option] = $value;
250  }
251 
252  public function saveSettings() {
253  $rows = [];
254  foreach ( $this->newOptions as $option => $value ) {
255  $rows[] = [
256  'up_user' => $this->id,
257  'up_property' => $option,
258  'up_value' => $value,
259  ];
260  }
261  $this->db->replace( 'user_properties',
262  [ [ 'up_user', 'up_property' ] ],
263  $rows, __METHOD__
264  );
265  $this->invalidateCache();
266  }
267 
271  function invalidateCache() {
272  $this->db->update(
273  'user',
274  [ 'user_touched' => $this->db->timestamp() ],
275  [ 'user_id' => $this->id ],
276  __METHOD__
277  );
278 
279  $domainId = $this->db->getDomainID();
280  $userId = $this->id;
281  $this->db->onTransactionPreCommitOrIdle(
282  function () use ( $domainId, $userId ) {
283  User::purge( $domainId, $userId );
284  },
285  __METHOD__
286  );
287  }
288 }
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
$wgLocalDatabases
string[] $wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
Definition: DefaultSettings.php:2159
$wgSharedTables
$wgSharedTables
Definition: DefaultSettings.php:2008
WikiMap\isCurrentWikiId
static isCurrentWikiId( $wikiId)
Definition: WikiMap.php:312
$wgSharedDB
$wgSharedDB
Shared database for multiple wikis.
Definition: DefaultSettings.php:1998
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1912
UserRightsProxy\newFromId
static newFromId( $wikiId, $id, $ignoreInvalidDB=false)
Factory function; get a remote user entry by ID number.
Definition: UserRightsProxy.php:93
UserRightsProxy\saveSettings
saveSettings()
Definition: UserRightsProxy.php:252
UserRightsProxy\newFromLookup
static newFromLookup( $wikiId, $field, $value, $ignoreInvalidDB=false)
Definition: UserRightsProxy.php:116
UserRightsProxy\getGroupMemberships
getGroupMemberships()
Replaces User::getGroupMemberships()
Definition: UserRightsProxy.php:209
UserRightsProxy\validDatabase
static validDatabase( $wikiId)
Confirm the selected database name is a valid local interwiki database name.
Definition: UserRightsProxy.php:63
UserRightsProxy\$name
string $name
Definition: UserRightsProxy.php:35
UserRightsProxy\getId
getId()
Definition: UserRightsProxy.php:166
UserRightsProxy
Cut-down copy of User interface for local-interwiki-database user rights manipulation.
Definition: UserRightsProxy.php:29
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
UserGroupMembership\getMembershipsForUser
static getMembershipsForUser( $userId, IDatabase $db=null)
Returns UserGroupMembership objects for all the groups a user currently belongs to.
Definition: UserGroupMembership.php:309
name
and how to run hooks for an and one after Each event has a name
Definition: hooks.txt:6
UserRightsProxy\getDB
static getDB( $wikiId, $ignoreInvalidDB=false)
Open a database connection to work on for the requested user.
Definition: UserRightsProxy.php:151
UserRightsProxy\setOption
setOption( $option, $value)
Replaces User::setOption()
Definition: UserRightsProxy.php:248
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
UserRightsProxy\$wikiId
string $wikiId
Definition: UserRightsProxy.php:33
UserRightsProxy\$newOptions
array $newOptions
Definition: UserRightsProxy.php:39
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
UserRightsProxy\invalidateCache
invalidateCache()
Replaces User::touchUser()
Definition: UserRightsProxy.php:271
UserRightsProxy\getUserPage
getUserPage()
Same as User::getUserPage()
Definition: UserRightsProxy.php:191
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:576
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
UserRightsProxy\whoIs
static whoIs( $wikiId, $id, $ignoreInvalidDB=false)
Same as User::whoIs()
Definition: UserRightsProxy.php:76
$value
$value
Definition: styleTest.css.php:49
UserRightsProxy\$id
int $id
Definition: UserRightsProxy.php:37
UserRightsProxy\newFromName
static newFromName( $wikiId, $name, $ignoreInvalidDB=false)
Factory function; get a remote user entry by name.
Definition: UserRightsProxy.php:105
UserRightsProxy\__construct
__construct( $db, $wikiId, $name, $id)
Definition: UserRightsProxy.php:49
UserRightsProxy\removeGroup
removeGroup( $group)
Replaces User::removeGroup()
Definition: UserRightsProxy.php:235
UserRightsProxy\getName
getName()
Same as User::getName()
Definition: UserRightsProxy.php:182
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2636
UserRightsProxy\$db
IDatabase $db
Definition: UserRightsProxy.php:31
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
NS_USER
const NS_USER
Definition: Defines.php:66
UserRightsProxy\isAnon
isAnon()
Definition: UserRightsProxy.php:173
UserGroupMembership\getMembership
static getMembership( $userId, $group, IDatabase $db=null)
Returns a UserGroupMembership object that pertains to the given user and group, or false if the user ...
Definition: UserGroupMembership.php:341
User\purge
static purge( $wikiId, $userId)
Definition: User.php:488
UserRightsProxy\addGroup
addGroup( $group, $expiry=null)
Replaces User::addGroup()
Definition: UserRightsProxy.php:220
UserGroupMembership
Represents a "user group membership" – a specific instance of a user belonging to a group.
Definition: UserGroupMembership.php:37
UserRightsProxy\getGroups
getGroups()
Replaces User::getUserGroups()
Definition: UserRightsProxy.php:199