w/s
[lhc/web/wiklou.git] / includes / UserRightsProxy.php
1 <?php
2
3 /**
4 * Cut-down copy of User interface for local-interwiki-database
5 * user rights manipulation.
6 */
7 class UserRightsProxy {
8
9 /**
10 * Constructor.
11 *
12 * @see newFromId()
13 * @see newFromName()
14 * @param $db DatabaseBase: db connection
15 * @param $database String: database name
16 * @param $name String: user name
17 * @param $id Integer: user ID
18 */
19 private function __construct( $db, $database, $name, $id ) {
20 $this->db = $db;
21 $this->database = $database;
22 $this->name = $name;
23 $this->id = intval( $id );
24 $this->newOptions = array();
25 }
26
27 /**
28 * Accessor for $this->database
29 *
30 * @return String: database name
31 */
32 public function getDBName() {
33 return $this->database;
34 }
35
36 /**
37 * Confirm the selected database name is a valid local interwiki database name.
38 *
39 * @param $database String: database name
40 * @return Boolean
41 */
42 public static function validDatabase( $database ) {
43 global $wgLocalDatabases;
44 return in_array( $database, $wgLocalDatabases );
45 }
46
47 /**
48 * Same as User::whoIs()
49 *
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
54 */
55 public static function whoIs( $database, $id, $ignoreInvalidDB = false ) {
56 $user = self::newFromId( $database, $id, $ignoreInvalidDB );
57 if( $user ) {
58 return $user->name;
59 } else {
60 return false;
61 }
62 }
63
64 /**
65 * Factory function; get a remote user entry by ID number.
66 *
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
71 */
72 public static function newFromId( $database, $id, $ignoreInvalidDB = false ) {
73 return self::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB );
74 }
75
76 /**
77 * Factory function; get a remote user entry by name.
78 *
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
83 */
84 public static function newFromName( $database, $name, $ignoreInvalidDB = false ) {
85 return self::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB );
86 }
87
88 /**
89 * @param $database
90 * @param $field
91 * @param $value
92 * @param $ignoreInvalidDB bool
93 * @return null|UserRightsProxy
94 */
95 private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
96 $db = self::getDB( $database, $ignoreInvalidDB );
97 if( $db ) {
98 $row = $db->selectRow( 'user',
99 array( 'user_id', 'user_name' ),
100 array( $field => $value ),
101 __METHOD__ );
102 if( $row !== false ) {
103 return new UserRightsProxy( $db, $database,
104 $row->user_name,
105 intval( $row->user_id ) );
106 }
107 }
108 return null;
109 }
110
111 /**
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.
114 *
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
118 */
119 public static function getDB( $database, $ignoreInvalidDB = false ) {
120 global $wgDBname;
121 if( self::validDatabase( $database ) ) {
122 if( $database == $wgDBname ) {
123 // Hmm... this shouldn't happen though. :)
124 return wfGetDB( DB_MASTER );
125 } else {
126 return wfGetDB( DB_MASTER, array(), $database );
127 }
128 }
129 return null;
130 }
131
132 /**
133 * @return int
134 */
135 public function getId() {
136 return $this->id;
137 }
138
139 /**
140 * @return bool
141 */
142 public function isAnon() {
143 return $this->getId() == 0;
144 }
145
146 /**
147 * Same as User::getName()
148 *
149 * @return String
150 */
151 public function getName() {
152 return $this->name . '@' . $this->database;
153 }
154
155 /**
156 * Same as User::getUserPage()
157 *
158 * @return Title object
159 */
160 public function getUserPage() {
161 return Title::makeTitle( NS_USER, $this->getName() );
162 }
163
164 /**
165 * Replaces User::getUserGroups()
166 */
167 function getGroups() {
168 $res = $this->db->select( 'user_groups',
169 array( 'ug_group' ),
170 array( 'ug_user' => $this->id ),
171 __METHOD__ );
172 $groups = array();
173 foreach ( $res as $row ) {
174 $groups[] = $row->ug_group;
175 }
176 return $groups;
177 }
178
179 /**
180 * Replaces User::addUserGroup()
181 */
182 function addGroup( $group ) {
183 $this->db->insert( 'user_groups',
184 array(
185 'ug_user' => $this->id,
186 'ug_group' => $group,
187 ),
188 __METHOD__,
189 array( 'IGNORE' ) );
190 }
191
192 /**
193 * Replaces User::removeUserGroup()
194 */
195 function removeGroup( $group ) {
196 $this->db->delete( 'user_groups',
197 array(
198 'ug_user' => $this->id,
199 'ug_group' => $group,
200 ),
201 __METHOD__ );
202 }
203
204 /**
205 * Replaces User::setOption()
206 */
207 public function setOption( $option, $value ) {
208 $this->newOptions[$option] = $value;
209 }
210
211 public function saveSettings() {
212 $rows = array();
213 foreach ( $this->newOptions as $option => $value ) {
214 $rows[] = array(
215 'up_user' => $this->id,
216 'up_property' => $option,
217 'up_value' => $value,
218 );
219 }
220 $this->db->replace( 'user_properties',
221 array( array( 'up_user', 'up_property' ) ),
222 $rows, __METHOD__
223 );
224 $this->invalidateCache();
225 }
226
227 /**
228 * Replaces User::touchUser()
229 */
230 function invalidateCache() {
231 $this->db->update( 'user',
232 array( 'user_touched' => $this->db->timestamp() ),
233 array( 'user_id' => $this->id ),
234 __METHOD__ );
235
236 global $wgMemc;
237 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
238 $wgMemc->delete( $key );
239 }
240 }