Merge "[JobQueue] Added support for approximate FIFO job queues."
[lhc/web/wiklou.git] / includes / UserRightsProxy.php
1 <?php
2 /**
3 * Representation of an user on a other locally-hosted wiki.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Cut-down copy of User interface for local-interwiki-database
25 * user rights manipulation.
26 */
27 class UserRightsProxy {
28
29 /**
30 * Constructor.
31 *
32 * @see newFromId()
33 * @see newFromName()
34 * @param $db DatabaseBase: db connection
35 * @param $database String: database name
36 * @param $name String: user name
37 * @param $id Integer: user ID
38 */
39 private function __construct( $db, $database, $name, $id ) {
40 $this->db = $db;
41 $this->database = $database;
42 $this->name = $name;
43 $this->id = intval( $id );
44 $this->newOptions = array();
45 }
46
47 /**
48 * Accessor for $this->database
49 *
50 * @return String: database name
51 */
52 public function getDBName() {
53 return $this->database;
54 }
55
56 /**
57 * Confirm the selected database name is a valid local interwiki database name.
58 *
59 * @param $database String: database name
60 * @return Boolean
61 */
62 public static function validDatabase( $database ) {
63 global $wgLocalDatabases;
64 return in_array( $database, $wgLocalDatabases );
65 }
66
67 /**
68 * Same as User::whoIs()
69 *
70 * @param $database String: database name
71 * @param $id Integer: user ID
72 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
73 * @return String: user name or false if the user doesn't exist
74 */
75 public static function whoIs( $database, $id, $ignoreInvalidDB = false ) {
76 $user = self::newFromId( $database, $id, $ignoreInvalidDB );
77 if( $user ) {
78 return $user->name;
79 } else {
80 return false;
81 }
82 }
83
84 /**
85 * Factory function; get a remote user entry by ID number.
86 *
87 * @param $database String: database name
88 * @param $id Integer: user ID
89 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
90 * @return UserRightsProxy or null if doesn't exist
91 */
92 public static function newFromId( $database, $id, $ignoreInvalidDB = false ) {
93 return self::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB );
94 }
95
96 /**
97 * Factory function; get a remote user entry by name.
98 *
99 * @param $database String: database name
100 * @param $name String: user name
101 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
102 * @return UserRightsProxy or null if doesn't exist
103 */
104 public static function newFromName( $database, $name, $ignoreInvalidDB = false ) {
105 return self::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB );
106 }
107
108 /**
109 * @param $database
110 * @param $field
111 * @param $value
112 * @param $ignoreInvalidDB bool
113 * @return null|UserRightsProxy
114 */
115 private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
116 $db = self::getDB( $database, $ignoreInvalidDB );
117 if( $db ) {
118 $row = $db->selectRow( 'user',
119 array( 'user_id', 'user_name' ),
120 array( $field => $value ),
121 __METHOD__ );
122 if( $row !== false ) {
123 return new UserRightsProxy( $db, $database,
124 $row->user_name,
125 intval( $row->user_id ) );
126 }
127 }
128 return null;
129 }
130
131 /**
132 * Open a database connection to work on for the requested user.
133 * This may be a new connection to another database for remote users.
134 *
135 * @param $database String
136 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
137 * @return DatabaseBase or null if invalid selection
138 */
139 public static function getDB( $database, $ignoreInvalidDB = false ) {
140 global $wgDBname;
141 if( self::validDatabase( $database ) ) {
142 if( $database == $wgDBname ) {
143 // Hmm... this shouldn't happen though. :)
144 return wfGetDB( DB_MASTER );
145 } else {
146 return wfGetDB( DB_MASTER, array(), $database );
147 }
148 }
149 return null;
150 }
151
152 /**
153 * @return int
154 */
155 public function getId() {
156 return $this->id;
157 }
158
159 /**
160 * @return bool
161 */
162 public function isAnon() {
163 return $this->getId() == 0;
164 }
165
166 /**
167 * Same as User::getName()
168 *
169 * @return String
170 */
171 public function getName() {
172 return $this->name . '@' . $this->database;
173 }
174
175 /**
176 * Same as User::getUserPage()
177 *
178 * @return Title object
179 */
180 public function getUserPage() {
181 return Title::makeTitle( NS_USER, $this->getName() );
182 }
183
184 /**
185 * Replaces User::getUserGroups()
186 * @return array
187 */
188 function getGroups() {
189 $res = $this->db->select( 'user_groups',
190 array( 'ug_group' ),
191 array( 'ug_user' => $this->id ),
192 __METHOD__ );
193 $groups = array();
194 foreach ( $res as $row ) {
195 $groups[] = $row->ug_group;
196 }
197 return $groups;
198 }
199
200 /**
201 * Replaces User::addUserGroup()
202 */
203 function addGroup( $group ) {
204 $this->db->insert( 'user_groups',
205 array(
206 'ug_user' => $this->id,
207 'ug_group' => $group,
208 ),
209 __METHOD__,
210 array( 'IGNORE' ) );
211 }
212
213 /**
214 * Replaces User::removeUserGroup()
215 */
216 function removeGroup( $group ) {
217 $this->db->delete( 'user_groups',
218 array(
219 'ug_user' => $this->id,
220 'ug_group' => $group,
221 ),
222 __METHOD__ );
223 }
224
225 /**
226 * Replaces User::setOption()
227 */
228 public function setOption( $option, $value ) {
229 $this->newOptions[$option] = $value;
230 }
231
232 public function saveSettings() {
233 $rows = array();
234 foreach ( $this->newOptions as $option => $value ) {
235 $rows[] = array(
236 'up_user' => $this->id,
237 'up_property' => $option,
238 'up_value' => $value,
239 );
240 }
241 $this->db->replace( 'user_properties',
242 array( array( 'up_user', 'up_property' ) ),
243 $rows, __METHOD__
244 );
245 $this->invalidateCache();
246 }
247
248 /**
249 * Replaces User::touchUser()
250 */
251 function invalidateCache() {
252 $this->db->update( 'user',
253 array( 'user_touched' => $this->db->timestamp() ),
254 array( 'user_id' => $this->id ),
255 __METHOD__ );
256
257 global $wgMemc;
258 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
259 $wgMemc->delete( $key );
260 }
261 }