5 * Cut-down copy of User interface for local-interwiki-database
6 * user rights manipulation.
8 class UserRightsProxy
{
9 private function __construct( $db, $database, $name, $id ) {
11 $this->database
= $database;
13 $this->id
= intval( $id );
17 * Confirm the selected database name is a valid local interwiki database name.
20 public static function validDatabase( $database ) {
21 global $wgLocalDatabases;
22 return in_array( $database, $wgLocalDatabases );
25 public static function whoIs( $database, $id ) {
26 $user = self
::newFromId( $database, $id );
35 * Factory function; get a remote user entry by ID number.
36 * @return UserRightsProxy or null if doesn't exist
38 public static function newFromId( $database, $id ) {
39 return self
::newFromLookup( $database, 'user_id', intval( $id ) );
42 public static function newFromName( $database, $name ) {
43 return self
::newFromLookup( $database, 'user_name', $name );
46 private static function newFromLookup( $database, $field, $value ) {
47 $db = self
::getDB( $database );
49 $row = $db->selectRow( 'user',
50 array( 'user_id', 'user_name' ),
51 array( $field => $value ),
53 if( $row !== false ) {
54 return new UserRightsProxy( $db, $database,
56 intval( $row->user_id
) );
63 * Open a database connection to work on for the requested user.
64 * This may be a new connection to another database for remote users.
65 * @param $database string
66 * @return Database or null if invalid selection
68 public static function getDB( $database ) {
69 global $wgLocalDatabases, $wgDBname;
70 if( self
::validDatabase( $database ) ) {
71 if( $database == $wgDBname ) {
72 // Hmm... this shouldn't happen though. :)
73 return wfGetDB( DB_MASTER
);
75 return wfGetDB( DB_MASTER
, array(), $database );
81 public function getId() {
85 public function isAnon() {
86 return $this->getId() == 0;
89 public function getName() {
90 return $this->name
. '@' . $this->database
;
93 public function getUserPage() {
94 return Title
::makeTitle( NS_USER
, $this->getName() );
97 // Replaces getUserGroups()
98 function getGroups() {
99 $res = $this->db
->select( 'user_groups',
101 array( 'ug_user' => $this->id
),
104 while( $row = $this->db
->fetchObject( $res ) ) {
105 $groups[] = $row->ug_group
;
110 // replaces addUserGroup
111 function addGroup( $group ) {
112 $this->db
->insert( 'user_groups',
114 'ug_user' => $this->id
,
115 'ug_group' => $group,
121 // replaces removeUserGroup
122 function removeGroup( $group ) {
123 $this->db
->delete( 'user_groups',
125 'ug_user' => $this->id
,
126 'ug_group' => $group,
131 // replaces touchUser
132 function invalidateCache() {
133 $this->db
->update( 'user',
134 array( 'user_touched' => $this->db
->timestamp() ),
135 array( 'user_id' => $this->id
),
139 if ( function_exists( 'wfForeignMemcKey' ) ) {
140 $key = wfForeignMemcKey( $this->database
, false, 'user', 'id', $this->id
);
142 $key = "$this->database:user:id:" . $this->id
;
144 $wgMemc->delete( $key );