Missing commit for r56798
[lhc/web/wiklou.git] / includes / UserRightsProxy.php
1 <?php
2
3
4 /**
5 * Cut-down copy of User interface for local-interwiki-database
6 * user rights manipulation.
7 */
8 class UserRightsProxy {
9 private function __construct( $db, $database, $name, $id ) {
10 $this->db = $db;
11 $this->database = $database;
12 $this->name = $name;
13 $this->id = intval( $id );
14 }
15
16 public function getDBName() {
17 return $this->database;
18 }
19
20 /**
21 * Confirm the selected database name is a valid local interwiki database name.
22 * @return bool
23 */
24 public static function validDatabase( $database ) {
25 global $wgLocalDatabases;
26 return in_array( $database, $wgLocalDatabases );
27 }
28
29 public static function whoIs( $database, $id ) {
30 $user = self::newFromId( $database, $id );
31 if( $user ) {
32 return $user->name;
33 } else {
34 return false;
35 }
36 }
37
38 /**
39 * Factory function; get a remote user entry by ID number.
40 * @return UserRightsProxy or null if doesn't exist
41 */
42 public static function newFromId( $database, $id ) {
43 return self::newFromLookup( $database, 'user_id', intval( $id ) );
44 }
45
46 public static function newFromName( $database, $name ) {
47 return self::newFromLookup( $database, 'user_name', $name );
48 }
49
50 private static function newFromLookup( $database, $field, $value ) {
51 $db = self::getDB( $database );
52 if( $db ) {
53 $row = $db->selectRow( 'user',
54 array( 'user_id', 'user_name' ),
55 array( $field => $value ),
56 __METHOD__ );
57 if( $row !== false ) {
58 return new UserRightsProxy( $db, $database,
59 $row->user_name,
60 intval( $row->user_id ) );
61 }
62 }
63 return null;
64 }
65
66 /**
67 * Open a database connection to work on for the requested user.
68 * This may be a new connection to another database for remote users.
69 * @param $database string
70 * @return Database or null if invalid selection
71 */
72 public static function getDB( $database ) {
73 global $wgLocalDatabases, $wgDBname;
74 if( self::validDatabase( $database ) ) {
75 if( $database == $wgDBname ) {
76 // Hmm... this shouldn't happen though. :)
77 return wfGetDB( DB_MASTER );
78 } else {
79 return wfGetDB( DB_MASTER, array(), $database );
80 }
81 }
82 return null;
83 }
84
85 public function getId() {
86 return $this->id;
87 }
88
89 public function isAnon() {
90 return $this->getId() == 0;
91 }
92
93 public function getName() {
94 return $this->name . '@' . $this->database;
95 }
96
97 public function getUserPage() {
98 return Title::makeTitle( NS_USER, $this->getName() );
99 }
100
101 // Replaces getUserGroups()
102 function getGroups() {
103 $res = $this->db->select( 'user_groups',
104 array( 'ug_group' ),
105 array( 'ug_user' => $this->id ),
106 __METHOD__ );
107 $groups = array();
108 while( $row = $this->db->fetchObject( $res ) ) {
109 $groups[] = $row->ug_group;
110 }
111 return $groups;
112 }
113
114 // replaces addUserGroup
115 function addGroup( $group ) {
116 $this->db->insert( 'user_groups',
117 array(
118 'ug_user' => $this->id,
119 'ug_group' => $group,
120 ),
121 __METHOD__,
122 array( 'IGNORE' ) );
123 }
124
125 // replaces removeUserGroup
126 function removeGroup( $group ) {
127 $this->db->delete( 'user_groups',
128 array(
129 'ug_user' => $this->id,
130 'ug_group' => $group,
131 ),
132 __METHOD__ );
133 }
134
135 // replaces touchUser
136 function invalidateCache() {
137 $this->db->update( 'user',
138 array( 'user_touched' => $this->db->timestamp() ),
139 array( 'user_id' => $this->id ),
140 __METHOD__ );
141
142 global $wgMemc;
143 if ( function_exists( 'wfForeignMemcKey' ) ) {
144 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
145 } else {
146 $key = "$this->database:user:id:" . $this->id;
147 }
148 $wgMemc->delete( $key );
149 }
150 }