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
23 use Wikimedia\Rdbms\IDatabase
;
26 * Cut-down copy of User interface for local-interwiki-database
27 * user rights manipulation.
29 class UserRightsProxy
{
36 * @param IDatabase $db Db connection
37 * @param string $database Database name
38 * @param string $name User name
39 * @param int $id User ID
41 private function __construct( $db, $database, $name, $id ) {
43 $this->database
= $database;
45 $this->id
= intval( $id );
46 $this->newOptions
= [];
50 * Accessor for $this->database
52 * @return string Database name
54 public function getDBName() {
55 return $this->database
;
59 * Confirm the selected database name is a valid local interwiki database name.
61 * @param string $database Database name
64 public static function validDatabase( $database ) {
65 global $wgLocalDatabases;
66 return in_array( $database, $wgLocalDatabases );
70 * Same as User::whoIs()
72 * @param string $database Database name
73 * @param int $id User ID
74 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
75 * @return string User name or false if the user doesn't exist
77 public static function whoIs( $database, $id, $ignoreInvalidDB = false ) {
78 $user = self
::newFromId( $database, $id, $ignoreInvalidDB );
87 * Factory function; get a remote user entry by ID number.
89 * @param string $database Database name
90 * @param int $id User ID
91 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
92 * @return UserRightsProxy|null If doesn't exist
94 public static function newFromId( $database, $id, $ignoreInvalidDB = false ) {
95 return self
::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB );
99 * Factory function; get a remote user entry by name.
101 * @param string $database Database name
102 * @param string $name User name
103 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
104 * @return UserRightsProxy|null If doesn't exist
106 public static function newFromName( $database, $name, $ignoreInvalidDB = false ) {
107 return self
::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB );
111 * @param string $database
112 * @param string $field
113 * @param string $value
114 * @param bool $ignoreInvalidDB
115 * @return null|UserRightsProxy
117 private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
118 global $wgSharedDB, $wgSharedTables;
119 // If the user table is shared, perform the user query on it,
120 // but don't pass it to the UserRightsProxy,
121 // as user rights are normally not shared.
122 if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
123 $userdb = self
::getDB( $wgSharedDB, $ignoreInvalidDB );
125 $userdb = self
::getDB( $database, $ignoreInvalidDB );
128 $db = self
::getDB( $database, $ignoreInvalidDB );
130 if ( $db && $userdb ) {
131 $row = $userdb->selectRow( 'user',
132 [ 'user_id', 'user_name' ],
133 [ $field => $value ],
136 if ( $row !== false ) {
137 return new UserRightsProxy( $db, $database,
139 intval( $row->user_id
) );
146 * Open a database connection to work on for the requested user.
147 * This may be a new connection to another database for remote users.
149 * @param string $database
150 * @param bool $ignoreInvalidDB If true, don't check if $database is in $wgLocalDatabases
151 * @return IDatabase|null If invalid selection
153 public static function getDB( $database, $ignoreInvalidDB = false ) {
155 if ( $ignoreInvalidDB || self
::validDatabase( $database ) ) {
156 if ( $database == $wgDBname ) {
157 // Hmm... this shouldn't happen though. :)
158 return wfGetDB( DB_MASTER
);
160 return wfGetDB( DB_MASTER
, [], $database );
169 public function getId() {
176 public function isAnon() {
177 return $this->getId() == 0;
181 * Same as User::getName()
185 public function getName() {
186 return $this->name
. '@' . $this->database
;
190 * Same as User::getUserPage()
194 public function getUserPage() {
195 return Title
::makeTitle( NS_USER
, $this->getName() );
199 * Replaces User::getUserGroups()
202 function getGroups() {
203 return array_keys( self
::getGroupMemberships() );
207 * Replaces User::getGroupMemberships()
212 function getGroupMemberships() {
213 return UserGroupMembership
::getMembershipsForUser( $this->id
, $this->db
);
217 * Replaces User::addGroup()
219 * @param string $group
220 * @param string|null $expiry
223 function addGroup( $group, $expiry = null ) {
225 $expiry = wfTimestamp( TS_MW
, $expiry );
228 $ugm = new UserGroupMembership( $this->id
, $group, $expiry );
229 return $ugm->insert( true, $this->db
);
233 * Replaces User::removeGroup()
235 * @param string $group
238 function removeGroup( $group ) {
239 $ugm = UserGroupMembership
::getMembership( $this->id
, $group, $this->db
);
243 return $ugm->delete( $this->db
);
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() {
257 foreach ( $this->newOptions
as $option => $value ) {
259 'up_user' => $this->id
,
260 'up_property' => $option,
261 'up_value' => $value,
264 $this->db
->replace( 'user_properties',
265 [ [ 'up_user', 'up_property' ] ],
268 $this->invalidateCache();
272 * Replaces User::touchUser()
274 function invalidateCache() {
277 [ 'user_touched' => $this->db
->timestamp() ],
278 [ 'user_id' => $this->id
],
282 $wikiId = $this->db
->getWikiID();
284 $this->db
->onTransactionPreCommitOrIdle(
285 function () use ( $wikiId, $userId ) {
286 User
::purge( $wikiId, $userId );