Bug 35034 - moved autocomment-prefix between the prefix and the arrow. Follow up...
[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 * @return array
167 */
168 function getGroups() {
169 $res = $this->db->select( 'user_groups',
170 array( 'ug_group' ),
171 array( 'ug_user' => $this->id ),
172 __METHOD__ );
173 $groups = array();
174 foreach ( $res as $row ) {
175 $groups[] = $row->ug_group;
176 }
177 return $groups;
178 }
179
180 /**
181 * Replaces User::addUserGroup()
182 */
183 function addGroup( $group ) {
184 $this->db->insert( 'user_groups',
185 array(
186 'ug_user' => $this->id,
187 'ug_group' => $group,
188 ),
189 __METHOD__,
190 array( 'IGNORE' ) );
191 }
192
193 /**
194 * Replaces User::removeUserGroup()
195 */
196 function removeGroup( $group ) {
197 $this->db->delete( 'user_groups',
198 array(
199 'ug_user' => $this->id,
200 'ug_group' => $group,
201 ),
202 __METHOD__ );
203 }
204
205 /**
206 * Replaces User::setOption()
207 */
208 public function setOption( $option, $value ) {
209 $this->newOptions[$option] = $value;
210 }
211
212 public function saveSettings() {
213 $rows = array();
214 foreach ( $this->newOptions as $option => $value ) {
215 $rows[] = array(
216 'up_user' => $this->id,
217 'up_property' => $option,
218 'up_value' => $value,
219 );
220 }
221 $this->db->replace( 'user_properties',
222 array( array( 'up_user', 'up_property' ) ),
223 $rows, __METHOD__
224 );
225 $this->invalidateCache();
226 }
227
228 /**
229 * Replaces User::touchUser()
230 */
231 function invalidateCache() {
232 $this->db->update( 'user',
233 array( 'user_touched' => $this->db->timestamp() ),
234 array( 'user_id' => $this->id ),
235 __METHOD__ );
236
237 global $wgMemc;
238 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
239 $wgMemc->delete( $key );
240 }
241 }