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 IDatabase $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
= [];
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 global $wgSharedDB, $wgSharedTables;
117 // If the user table is shared, perform the user query on it,
118 // but don't pass it to the UserRightsProxy,
119 // as user rights are normally not shared.
120 if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
121 $userdb = self
::getDB( $wgSharedDB, $ignoreInvalidDB );
123 $userdb = self
::getDB( $database, $ignoreInvalidDB );
126 $db = self
::getDB( $database, $ignoreInvalidDB );
128 if ( $db && $userdb ) {
129 $row = $userdb->selectRow( 'user',
130 [ 'user_id', 'user_name' ],
131 [ $field => $value ],
134 if ( $row !== false ) {
135 return new UserRightsProxy( $db, $database,
137 intval( $row->user_id
) );
144 * Open a database connection to work on for the requested user.
145 * This may be a new connection to another database for remote users.
147 * @param string $database
148 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
149 * @return IDatabase|null If invalid selection
151 public static function getDB( $database, $ignoreInvalidDB = false ) {
153 if ( $ignoreInvalidDB || self
::validDatabase( $database ) ) {
154 if ( $database == $wgDBname ) {
155 // Hmm... this shouldn't happen though. :)
156 return wfGetDB( DB_MASTER
);
158 return wfGetDB( DB_MASTER
, [], $database );
167 public function getId() {
174 public function isAnon() {
175 return $this->getId() == 0;
179 * Same as User::getName()
183 public function getName() {
184 return $this->name
. '@' . $this->database
;
188 * Same as User::getUserPage()
192 public function getUserPage() {
193 return Title
::makeTitle( NS_USER
, $this->getName() );
197 * Replaces User::getUserGroups()
200 function getGroups() {
201 return array_keys( self
::getGroupMemberships() );
205 * Replaces User::getGroupMemberships()
210 function getGroupMemberships() {
211 return UserGroupMembership
::getMembershipsForUser( $this->id
, $this->db
);
215 * Replaces User::addGroup()
217 * @param string $group
218 * @param string|null $expiry
221 function addGroup( $group, $expiry = null ) {
223 $expiry = wfTimestamp( TS_MW
, $expiry );
226 $ugm = new UserGroupMembership( $this->id
, $group, $expiry );
227 return $ugm->insert( true, $this->db
);
231 * Replaces User::removeGroup()
233 * @param string $group
236 function removeGroup( $group ) {
237 $ugm = UserGroupMembership
::getMembership( $this->id
, $group, $this->db
);
241 return $ugm->delete( $this->db
);
245 * Replaces User::setOption()
246 * @param string $option
247 * @param mixed $value
249 public function setOption( $option, $value ) {
250 $this->newOptions
[$option] = $value;
253 public function saveSettings() {
255 foreach ( $this->newOptions
as $option => $value ) {
257 'up_user' => $this->id
,
258 'up_property' => $option,
259 'up_value' => $value,
262 $this->db
->replace( 'user_properties',
263 [ [ 'up_user', 'up_property' ] ],
266 $this->invalidateCache();
270 * Replaces User::touchUser()
272 function invalidateCache() {
275 [ 'user_touched' => $this->db
->timestamp() ],
276 [ 'user_id' => $this->id
],
280 $wikiId = $this->db
->getWikiID();
282 $this->db
->onTransactionPreCommitOrIdle(
283 function () use ( $wikiId, $userId ) {
284 User
::purge( $wikiId, $userId );