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
{
44 * @param IDatabase $db Db connection
45 * @param string $wikiId Database name
46 * @param string $name User name
47 * @param int $id User ID
49 private function __construct( $db, $wikiId, $name, $id ) {
51 $this->wikiId
= $wikiId;
53 $this->id
= intval( $id );
54 $this->newOptions
= [];
58 * Confirm the selected database name is a valid local interwiki database name.
60 * @param string $wikiId Database name
63 public static function validDatabase( $wikiId ) {
64 global $wgLocalDatabases;
65 return in_array( $wikiId, $wgLocalDatabases );
69 * Same as User::whoIs()
71 * @param string $wikiId Database name
72 * @param int $id User ID
73 * @param bool $ignoreInvalidDB If true, don't check if $wikiId is in $wgLocalDatabases
74 * @return string User name or false if the user doesn't exist
76 public static function whoIs( $wikiId, $id, $ignoreInvalidDB = false ) {
77 $user = self
::newFromId( $wikiId, $id, $ignoreInvalidDB );
86 * Factory function; get a remote user entry by ID number.
88 * @param string $wikiId Database name
89 * @param int $id User ID
90 * @param bool $ignoreInvalidDB If true, don't check if $wikiId is in $wgLocalDatabases
91 * @return UserRightsProxy|null If doesn't exist
93 public static function newFromId( $wikiId, $id, $ignoreInvalidDB = false ) {
94 return self
::newFromLookup( $wikiId, 'user_id', intval( $id ), $ignoreInvalidDB );
98 * Factory function; get a remote user entry by name.
100 * @param string $wikiId Database name
101 * @param string $name User name
102 * @param bool $ignoreInvalidDB If true, don't check if $wikiId is in $wgLocalDatabases
103 * @return UserRightsProxy|null If doesn't exist
105 public static function newFromName( $wikiId, $name, $ignoreInvalidDB = false ) {
106 return self
::newFromLookup( $wikiId, 'user_name', $name, $ignoreInvalidDB );
110 * @param string $wikiId
111 * @param string $field
112 * @param string $value
113 * @param bool $ignoreInvalidDB
114 * @return null|UserRightsProxy
116 private static function newFromLookup( $wikiId, $field, $value, $ignoreInvalidDB = false ) {
117 global $wgSharedDB, $wgSharedTables;
118 // If the user table is shared, perform the user query on it,
119 // but don't pass it to the UserRightsProxy,
120 // as user rights are normally not shared.
121 if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
122 $userdb = self
::getDB( $wgSharedDB, $ignoreInvalidDB );
124 $userdb = self
::getDB( $wikiId, $ignoreInvalidDB );
127 $db = self
::getDB( $wikiId, $ignoreInvalidDB );
129 if ( $db && $userdb ) {
130 $row = $userdb->selectRow( 'user',
131 [ 'user_id', 'user_name' ],
132 [ $field => $value ],
135 if ( $row !== false ) {
136 return new UserRightsProxy(
137 $db, $wikiId, $row->user_name
, 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 $wikiId
148 * @param bool $ignoreInvalidDB If true, don't check if $wikiId is in $wgLocalDatabases
149 * @return IDatabase|null If invalid selection
151 public static function getDB( $wikiId, $ignoreInvalidDB = false ) {
152 if ( $ignoreInvalidDB || self
::validDatabase( $wikiId ) ) {
153 if ( WikiMap
::isCurrentWikiId( $wikiId ) ) {
154 // Hmm... this shouldn't happen though. :)
155 return wfGetDB( DB_MASTER
);
157 return wfGetDB( DB_MASTER
, [], $wikiId );
166 public function getId() {
173 public function isAnon() {
174 return $this->getId() == 0;
178 * Same as User::getName()
182 public function getName() {
183 return $this->name
. '@' . $this->wikiId
;
187 * Same as User::getUserPage()
191 public function getUserPage() {
192 return Title
::makeTitle( NS_USER
, $this->getName() );
196 * Replaces User::getUserGroups()
199 function getGroups() {
200 return array_keys( self
::getGroupMemberships() );
204 * Replaces User::getGroupMemberships()
209 function getGroupMemberships() {
210 return UserGroupMembership
::getMembershipsForUser( $this->id
, $this->db
);
214 * Replaces User::addGroup()
216 * @param string $group
217 * @param string|null $expiry
220 function addGroup( $group, $expiry = null ) {
222 $expiry = wfTimestamp( TS_MW
, $expiry );
225 $ugm = new UserGroupMembership( $this->id
, $group, $expiry );
226 return $ugm->insert( true, $this->db
);
230 * Replaces User::removeGroup()
232 * @param string $group
235 function removeGroup( $group ) {
236 $ugm = UserGroupMembership
::getMembership( $this->id
, $group, $this->db
);
240 return $ugm->delete( $this->db
);
244 * Replaces User::setOption()
245 * @param string $option
246 * @param mixed $value
248 public function setOption( $option, $value ) {
249 $this->newOptions
[$option] = $value;
252 public function saveSettings() {
254 foreach ( $this->newOptions
as $option => $value ) {
256 'up_user' => $this->id
,
257 'up_property' => $option,
258 'up_value' => $value,
261 $this->db
->replace( 'user_properties',
262 [ [ 'up_user', 'up_property' ] ],
265 $this->invalidateCache();
269 * Replaces User::touchUser()
271 function invalidateCache() {
274 [ 'user_touched' => $this->db
->timestamp() ],
275 [ 'user_id' => $this->id
],
279 $domainId = $this->db
->getDomainID();
281 $this->db
->onTransactionPreCommitOrIdle(
282 function () use ( $domainId, $userId ) {
283 User
::purge( $domainId, $userId );