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 );
92 * @param $ignoreInvalidDB bool
93 * @return null|UserRightsProxy
95 private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
96 $db = self
::getDB( $database, $ignoreInvalidDB );
98 $row = $db->selectRow( 'user',
99 array( 'user_id', 'user_name' ),
100 array( $field => $value ),
102 if( $row !== false ) {
103 return new UserRightsProxy( $db, $database,
105 intval( $row->user_id
) );
112 * Open a database connection to work on for the requested user.
113 * This may be a new connection to another database for remote users.
115 * @param $database String
116 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
117 * @return DatabaseBase or null if invalid selection
119 public static function getDB( $database, $ignoreInvalidDB = false ) {
121 if( self
::validDatabase( $database ) ) {
122 if( $database == $wgDBname ) {
123 // Hmm... this shouldn't happen though. :)
124 return wfGetDB( DB_MASTER
);
126 return wfGetDB( DB_MASTER
, array(), $database );
135 public function getId() {
142 public function isAnon() {
143 return $this->getId() == 0;
147 * Same as User::getName()
151 public function getName() {
152 return $this->name
. '@' . $this->database
;
156 * Same as User::getUserPage()
158 * @return Title object
160 public function getUserPage() {
161 return Title
::makeTitle( NS_USER
, $this->getName() );
165 * Replaces User::getUserGroups()
168 function getGroups() {
169 $res = $this->db
->select( 'user_groups',
171 array( 'ug_user' => $this->id
),
174 foreach ( $res as $row ) {
175 $groups[] = $row->ug_group
;
181 * Replaces User::addUserGroup()
183 function addGroup( $group ) {
184 $this->db
->insert( 'user_groups',
186 'ug_user' => $this->id
,
187 'ug_group' => $group,
194 * Replaces User::removeUserGroup()
196 function removeGroup( $group ) {
197 $this->db
->delete( 'user_groups',
199 'ug_user' => $this->id
,
200 'ug_group' => $group,
206 * Replaces User::setOption()
208 public function setOption( $option, $value ) {
209 $this->newOptions
[$option] = $value;
212 public function saveSettings() {
214 foreach ( $this->newOptions
as $option => $value ) {
216 'up_user' => $this->id
,
217 'up_property' => $option,
218 'up_value' => $value,
221 $this->db
->replace( 'user_properties',
222 array( array( 'up_user', 'up_property' ) ),
225 $this->invalidateCache();
229 * Replaces User::touchUser()
231 function invalidateCache() {
232 $this->db
->update( 'user',
233 array( 'user_touched' => $this->db
->timestamp() ),
234 array( 'user_id' => $this->id
),
238 $key = wfForeignMemcKey( $this->database
, false, 'user', 'id', $this->id
);
239 $wgMemc->delete( $key );