Don't look for pipes in the root node.
[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 private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
89 $db = self::getDB( $database, $ignoreInvalidDB );
90 if( $db ) {
91 $row = $db->selectRow( 'user',
92 array( 'user_id', 'user_name' ),
93 array( $field => $value ),
94 __METHOD__ );
95 if( $row !== false ) {
96 return new UserRightsProxy( $db, $database,
97 $row->user_name,
98 intval( $row->user_id ) );
99 }
100 }
101 return null;
102 }
103
104 /**
105 * Open a database connection to work on for the requested user.
106 * This may be a new connection to another database for remote users.
107 *
108 * @param $database String
109 * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
110 * @return DatabaseBase or null if invalid selection
111 */
112 public static function getDB( $database, $ignoreInvalidDB = false ) {
113 global $wgDBname;
114 if( self::validDatabase( $database ) ) {
115 if( $database == $wgDBname ) {
116 // Hmm... this shouldn't happen though. :)
117 return wfGetDB( DB_MASTER );
118 } else {
119 return wfGetDB( DB_MASTER, array(), $database );
120 }
121 }
122 return null;
123 }
124
125 public function getId() {
126 return $this->id;
127 }
128
129 public function isAnon() {
130 return $this->getId() == 0;
131 }
132
133 /**
134 * Same as User::getName()
135 *
136 * @return String
137 */
138 public function getName() {
139 return $this->name . '@' . $this->database;
140 }
141
142 /**
143 * Same as User::getUserPage()
144 *
145 * @return Title object
146 */
147 public function getUserPage() {
148 return Title::makeTitle( NS_USER, $this->getName() );
149 }
150
151 /**
152 * Replaces User::getUserGroups()
153 */
154 function getGroups() {
155 $res = $this->db->select( 'user_groups',
156 array( 'ug_group' ),
157 array( 'ug_user' => $this->id ),
158 __METHOD__ );
159 $groups = array();
160 foreach ( $res as $row ) {
161 $groups[] = $row->ug_group;
162 }
163 return $groups;
164 }
165
166 /**
167 * Replaces User::addUserGroup()
168 */
169 function addGroup( $group ) {
170 $this->db->insert( 'user_groups',
171 array(
172 'ug_user' => $this->id,
173 'ug_group' => $group,
174 ),
175 __METHOD__,
176 array( 'IGNORE' ) );
177 }
178
179 /**
180 * Replaces User::removeUserGroup()
181 */
182 function removeGroup( $group ) {
183 $this->db->delete( 'user_groups',
184 array(
185 'ug_user' => $this->id,
186 'ug_group' => $group,
187 ),
188 __METHOD__ );
189 }
190
191 /**
192 * Replaces User::setOption()
193 */
194 public function setOption( $option, $value ) {
195 $this->newOptions[$option] = $value;
196 }
197
198 public function saveSettings() {
199 $rows = array();
200 foreach ( $this->newOptions as $option => $value ) {
201 $rows[] = array(
202 'up_user' => $this->id,
203 'up_property' => $option,
204 'up_value' => $value,
205 );
206 }
207 $this->db->replace( 'user_properties',
208 array( array( 'up_user', 'up_property' ) ),
209 $rows, __METHOD__
210 );
211 $this->invalidateCache();
212 }
213
214 /**
215 * Replaces User::touchUser()
216 */
217 function invalidateCache() {
218 $this->db->update( 'user',
219 array( 'user_touched' => $this->db->timestamp() ),
220 array( 'user_id' => $this->id ),
221 __METHOD__ );
222
223 global $wgMemc;
224 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
225 $wgMemc->delete( $key );
226 }
227 }