SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / UserRightsProxy.php
blob3be58473207ca9e052807ad817d823a0267def6e
1 <?php
2 /**
3 * Representation of an user on a other locally-hosted wiki.
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.
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.
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
20 * @file
23 /**
24 * Cut-down copy of User interface for local-interwiki-database
25 * user rights manipulation.
27 class UserRightsProxy {
29 /**
30 * Constructor.
32 * @see newFromId()
33 * @see newFromName()
34 * @param DatabaseBase $db Db connection
35 * @param string $database Database name
36 * @param string $name User name
37 * @param int $id User ID
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();
47 /**
48 * Accessor for $this->database
50 * @return string Database name
52 public function getDBName() {
53 return $this->database;
56 /**
57 * Confirm the selected database name is a valid local interwiki database name.
59 * @param string $database Database name
60 * @return bool
62 public static function validDatabase( $database ) {
63 global $wgLocalDatabases;
64 return in_array( $database, $wgLocalDatabases );
67 /**
68 * Same as User::whoIs()
70 * @param string $database Database name
71 * @param int $id User ID
72 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
73 * @return string User name or false if the user doesn't exist
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;
84 /**
85 * Factory function; get a remote user entry by ID number.
87 * @param string $database Database name
88 * @param int $id User ID
89 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
90 * @return UserRightsProxy|null If doesn't exist
92 public static function newFromId( $database, $id, $ignoreInvalidDB = false ) {
93 return self::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB );
96 /**
97 * Factory function; get a remote user entry by name.
99 * @param string $database Database name
100 * @param string $name User name
101 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
102 * @return UserRightsProxy|null If doesn't exist
104 public static function newFromName( $database, $name, $ignoreInvalidDB = false ) {
105 return self::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB );
109 * @param string $database
110 * @param string $field
111 * @param string $value
112 * @param bool $ignoreInvalidDB
113 * @return null|UserRightsProxy
115 private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
116 global $wgSharedDB, $wgSharedTables;
117 // If the user table is shared, perform the user query on it, but don't pass it to the UserRightsProxy,
118 // as user rights are normally not shared.
119 if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
120 $userdb = self::getDB( $wgSharedDB, $ignoreInvalidDB );
121 } else {
122 $userdb = self::getDB( $database, $ignoreInvalidDB );
125 $db = self::getDB( $database, $ignoreInvalidDB );
127 if ( $db && $userdb ) {
128 $row = $userdb->selectRow( 'user',
129 array( 'user_id', 'user_name' ),
130 array( $field => $value ),
131 __METHOD__ );
133 if ( $row !== false ) {
134 return new UserRightsProxy( $db, $database,
135 $row->user_name,
136 intval( $row->user_id ) );
139 return null;
143 * Open a database connection to work on for the requested user.
144 * This may be a new connection to another database for remote users.
146 * @param string $database
147 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
148 * @return DatabaseBase|null If invalid selection
150 public static function getDB( $database, $ignoreInvalidDB = false ) {
151 global $wgDBname;
152 if ( $ignoreInvalidDB || self::validDatabase( $database ) ) {
153 if ( $database == $wgDBname ) {
154 // Hmm... this shouldn't happen though. :)
155 return wfGetDB( DB_MASTER );
156 } else {
157 return wfGetDB( DB_MASTER, array(), $database );
160 return null;
164 * @return int
166 public function getId() {
167 return $this->id;
171 * @return bool
173 public function isAnon() {
174 return $this->getId() == 0;
178 * Same as User::getName()
180 * @return string
182 public function getName() {
183 return $this->name . '@' . $this->database;
187 * Same as User::getUserPage()
189 * @return Title
191 public function getUserPage() {
192 return Title::makeTitle( NS_USER, $this->getName() );
196 * Replaces User::getUserGroups()
197 * @return array
199 function getGroups() {
200 $res = $this->db->select( 'user_groups',
201 array( 'ug_group' ),
202 array( 'ug_user' => $this->id ),
203 __METHOD__ );
204 $groups = array();
205 foreach ( $res as $row ) {
206 $groups[] = $row->ug_group;
208 return $groups;
212 * Replaces User::addUserGroup()
213 * @param string $group
215 * @return bool
217 function addGroup( $group ) {
218 $this->db->insert( 'user_groups',
219 array(
220 'ug_user' => $this->id,
221 'ug_group' => $group,
223 __METHOD__,
224 array( 'IGNORE' ) );
226 return true;
230 * Replaces User::removeUserGroup()
231 * @param string $group
233 * @return bool
235 function removeGroup( $group ) {
236 $this->db->delete( 'user_groups',
237 array(
238 'ug_user' => $this->id,
239 'ug_group' => $group,
241 __METHOD__ );
243 return true;
247 * Replaces User::setOption()
248 * @param string $option
249 * @param mixed $value
251 public function setOption( $option, $value ) {
252 $this->newOptions[$option] = $value;
255 public function saveSettings() {
256 $rows = array();
257 foreach ( $this->newOptions as $option => $value ) {
258 $rows[] = array(
259 'up_user' => $this->id,
260 'up_property' => $option,
261 'up_value' => $value,
264 $this->db->replace( 'user_properties',
265 array( array( 'up_user', 'up_property' ) ),
266 $rows, __METHOD__
268 $this->invalidateCache();
272 * Replaces User::touchUser()
274 function invalidateCache() {
275 $this->db->update( 'user',
276 array( 'user_touched' => $this->db->timestamp() ),
277 array( 'user_id' => $this->id ),
278 __METHOD__ );
280 global $wgMemc;
281 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
282 $wgMemc->delete( $key );