4 * Cut-down copy of User interface for local-interwiki-database
5 * user rights manipulation.
7 class UserRightsProxy
{
14 * @param $db DatabaseBase: db connection
15 * @param $database String: database name
16 * @param $name String: user name
17 * @param $id Integer: user ID
19 private function __construct( $db, $database, $name, $id ) {
21 $this->database
= $database;
23 $this->id
= intval( $id );
24 $this->newOptions
= array();
28 * Accessor for $this->database
30 * @return String: database name
32 public function getDBName() {
33 return $this->database
;
37 * Confirm the selected database name is a valid local interwiki database name.
39 * @param $database String: database name
42 public static function validDatabase( $database ) {
43 global $wgLocalDatabases;
44 return in_array( $database, $wgLocalDatabases );
48 * Same as User::whoIs()
50 * @param $database String: database name
51 * @param $id Integer: user ID
52 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
53 * @return String: user name or false if the user doesn't exist
55 public static function whoIs( $database, $id, $ignoreInvalidDB = false ) {
56 $user = self
::newFromId( $database, $id, $ignoreInvalidDB );
65 * Factory function; get a remote user entry by ID number.
67 * @param $database String: database name
68 * @param $id Integer: user ID
69 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
70 * @return UserRightsProxy or null if doesn't exist
72 public static function newFromId( $database, $id, $ignoreInvalidDB = false ) {
73 return self
::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB );
77 * Factory function; get a remote user entry by name.
79 * @param $database String: database name
80 * @param $name String: user name
81 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
82 * @return UserRightsProxy or null if doesn't exist
84 public static function newFromName( $database, $name, $ignoreInvalidDB = false ) {
85 return self
::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB );
88 private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
89 $db = self
::getDB( $database, $ignoreInvalidDB );
91 $row = $db->selectRow( 'user',
92 array( 'user_id', 'user_name' ),
93 array( $field => $value ),
95 if( $row !== false ) {
96 return new UserRightsProxy( $db, $database,
98 intval( $row->user_id
) );
105 * Open a database connection to work on for the requested user.
106 * This may be a new connection to another database for remote users.
108 * @param $database String
109 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
110 * @return DatabaseBase or null if invalid selection
112 public static function getDB( $database, $ignoreInvalidDB = false ) {
114 if( self
::validDatabase( $database ) ) {
115 if( $database == $wgDBname ) {
116 // Hmm... this shouldn't happen though. :)
117 return wfGetDB( DB_MASTER
);
119 return wfGetDB( DB_MASTER
, array(), $database );
125 public function getId() {
129 public function isAnon() {
130 return $this->getId() == 0;
134 * Same as User::getName()
138 public function getName() {
139 return $this->name
. '@' . $this->database
;
143 * Same as User::getUserPage()
145 * @return Title object
147 public function getUserPage() {
148 return Title
::makeTitle( NS_USER
, $this->getName() );
152 * Replaces User::getUserGroups()
154 function getGroups() {
155 $res = $this->db
->select( 'user_groups',
157 array( 'ug_user' => $this->id
),
160 foreach ( $res as $row ) {
161 $groups[] = $row->ug_group
;
167 * Replaces User::addUserGroup()
169 function addGroup( $group ) {
170 $this->db
->insert( 'user_groups',
172 'ug_user' => $this->id
,
173 'ug_group' => $group,
180 * Replaces User::removeUserGroup()
182 function removeGroup( $group ) {
183 $this->db
->delete( 'user_groups',
185 'ug_user' => $this->id
,
186 'ug_group' => $group,
192 * Replaces User::setOption()
194 public function setOption( $option, $value ) {
195 $this->newOptions
[$option] = $value;
198 public function saveSettings() {
200 foreach ( $this->newOptions
as $option => $value ) {
202 'up_user' => $this->id
,
203 'up_property' => $option,
204 'up_value' => $value,
207 $this->db
->replace( 'user_properties',
208 array( array( 'up_user', 'up_property' ) ),
211 $this->invalidateCache();
215 * Replaces User::touchUser()
217 function invalidateCache() {
218 $this->db
->update( 'user',
219 array( 'user_touched' => $this->db
->timestamp() ),
220 array( 'user_id' => $this->id
),
224 $key = wfForeignMemcKey( $this->database
, false, 'user', 'id', $this->id
);
225 $wgMemc->delete( $key );