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
24 * Cut-down copy of User interface for local-interwiki-database
25 * user rights manipulation.
27 class UserRightsProxy
{
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 ) {
41 $this->database
= $database;
43 $this->id
= intval( $id );
44 $this->newOptions
= array();
48 * Accessor for $this->database
50 * @return string Database name
52 public function getDBName() {
53 return $this->database
;
57 * Confirm the selected database name is a valid local interwiki database name.
59 * @param string $database Database name
62 public static function validDatabase( $database ) {
63 global $wgLocalDatabases;
64 return in_array( $database, $wgLocalDatabases );
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 );
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 );
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 $db = self
::getDB( $database, $ignoreInvalidDB );
118 $row = $db->selectRow( 'user',
119 array( 'user_id', 'user_name' ),
120 array( $field => $value ),
122 if ( $row !== false ) {
123 return new UserRightsProxy( $db, $database,
125 intval( $row->user_id
) );
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.
135 * @param string $database
136 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
137 * @return DatabaseBase|null If invalid selection
139 public static function getDB( $database, $ignoreInvalidDB = false ) {
141 if ( $ignoreInvalidDB || self
::validDatabase( $database ) ) {
142 if ( $database == $wgDBname ) {
143 // Hmm... this shouldn't happen though. :)
144 return wfGetDB( DB_MASTER
);
146 return wfGetDB( DB_MASTER
, array(), $database );
155 public function getId() {
162 public function isAnon() {
163 return $this->getId() == 0;
167 * Same as User::getName()
171 public function getName() {
172 return $this->name
. '@' . $this->database
;
176 * Same as User::getUserPage()
180 public function getUserPage() {
181 return Title
::makeTitle( NS_USER
, $this->getName() );
185 * Replaces User::getUserGroups()
188 function getGroups() {
189 $res = $this->db
->select( 'user_groups',
191 array( 'ug_user' => $this->id
),
194 foreach ( $res as $row ) {
195 $groups[] = $row->ug_group
;
201 * Replaces User::addUserGroup()
202 * @param string $group
204 function addGroup( $group ) {
205 $this->db
->insert( 'user_groups',
207 'ug_user' => $this->id
,
208 'ug_group' => $group,
215 * Replaces User::removeUserGroup()
216 * @param string $group
218 function removeGroup( $group ) {
219 $this->db
->delete( 'user_groups',
221 'ug_user' => $this->id
,
222 'ug_group' => $group,
228 * Replaces User::setOption()
229 * @param string $option
230 * @param mixed $value
232 public function setOption( $option, $value ) {
233 $this->newOptions
[$option] = $value;
236 public function saveSettings() {
238 foreach ( $this->newOptions
as $option => $value ) {
240 'up_user' => $this->id
,
241 'up_property' => $option,
242 'up_value' => $value,
245 $this->db
->replace( 'user_properties',
246 array( array( 'up_user', 'up_property' ) ),
249 $this->invalidateCache();
253 * Replaces User::touchUser()
255 function invalidateCache() {
256 $this->db
->update( 'user',
257 array( 'user_touched' => $this->db
->timestamp() ),
258 array( 'user_id' => $this->id
),
262 $key = wfForeignMemcKey( $this->database
, false, 'user', 'id', $this->id
);
263 $wgMemc->delete( $key );