Wrap libxml_disable_entity_loader() calls in version constraint
[mediawiki.git] / includes / user / UserRightsProxy.php
blob4218b5f0158dab95177da8e3ec683df7ed31c825
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 use MediaWiki\MediaWikiServices;
24 use MediaWiki\User\UserGroupManager;
25 use MediaWiki\User\UserIdentityValue;
26 use Wikimedia\Rdbms\IDatabase;
28 /**
29 * Cut-down copy of User interface for local-interwiki-database
30 * user rights manipulation.
32 class UserRightsProxy {
33 /** @var IDatabase */
34 private $db;
35 /** @var string */
36 private $dbDomain;
37 /** @var string */
38 private $name;
39 /** @var int */
40 private $id;
41 /** @var array */
42 private $newOptions;
43 /** @var UserGroupManager */
44 private $userGroupManager;
46 /**
47 * @see newFromId()
48 * @see newFromName()
49 * @param IDatabase $db Db connection
50 * @param string $dbDomain Database name
51 * @param string $name User name
52 * @param int $id User ID
54 private function __construct( $db, $dbDomain, $name, $id ) {
55 $this->db = $db;
56 $this->dbDomain = $dbDomain;
57 $this->name = $name;
58 $this->id = intval( $id );
59 $this->newOptions = [];
60 $this->userGroupManager = MediaWikiServices::getInstance()
61 ->getUserGroupManagerFactory()
62 ->getUserGroupManager( $dbDomain );
65 /**
66 * Confirm the selected database name is a valid local interwiki database name.
68 * @param string $dbDomain Database name
69 * @return bool
71 public static function validDatabase( $dbDomain ) {
72 global $wgLocalDatabases;
73 return in_array( $dbDomain, $wgLocalDatabases );
76 /**
77 * Same as User::whoIs()
79 * @param string $dbDomain Database name
80 * @param int $id User ID
81 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
82 * @return string User name or false if the user doesn't exist
84 public static function whoIs( $dbDomain, $id, $ignoreInvalidDB = false ) {
85 $user = self::newFromId( $dbDomain, $id, $ignoreInvalidDB );
86 if ( $user ) {
87 return $user->name;
88 } else {
89 return false;
93 /**
94 * Factory function; get a remote user entry by ID number.
96 * @param string $dbDomain Database name
97 * @param int $id User ID
98 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
99 * @return UserRightsProxy|null If doesn't exist
101 public static function newFromId( $dbDomain, $id, $ignoreInvalidDB = false ) {
102 return self::newFromLookup( $dbDomain, 'user_id', intval( $id ), $ignoreInvalidDB );
106 * Factory function; get a remote user entry by name.
108 * @param string $dbDomain Database name
109 * @param string $name User name
110 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
111 * @return UserRightsProxy|null If doesn't exist
113 public static function newFromName( $dbDomain, $name, $ignoreInvalidDB = false ) {
114 return self::newFromLookup( $dbDomain, 'user_name', $name, $ignoreInvalidDB );
118 * @param string $dbDomain
119 * @param string $field
120 * @param string $value
121 * @param bool $ignoreInvalidDB
122 * @return null|UserRightsProxy
124 private static function newFromLookup( $dbDomain, $field, $value, $ignoreInvalidDB = false ) {
125 global $wgSharedDB, $wgSharedTables;
126 // If the user table is shared, perform the user query on it,
127 // but don't pass it to the UserRightsProxy,
128 // as user rights are normally not shared.
129 if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
130 $userdb = self::getDB( $wgSharedDB, $ignoreInvalidDB );
131 } else {
132 $userdb = self::getDB( $dbDomain, $ignoreInvalidDB );
135 $db = self::getDB( $dbDomain, $ignoreInvalidDB );
137 if ( $db && $userdb ) {
138 $row = $userdb->selectRow( 'user',
139 [ 'user_id', 'user_name' ],
140 [ $field => $value ],
141 __METHOD__ );
143 if ( $row !== false ) {
144 return new UserRightsProxy(
145 $db, $dbDomain, $row->user_name, intval( $row->user_id ) );
148 return null;
152 * Open a database connection to work on for the requested user.
153 * This may be a new connection to another database for remote users.
155 * @param string $dbDomain
156 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
157 * @return IDatabase|null If invalid selection
159 public static function getDB( $dbDomain, $ignoreInvalidDB = false ) {
160 if ( $ignoreInvalidDB || self::validDatabase( $dbDomain ) ) {
161 if ( WikiMap::isCurrentWikiId( $dbDomain ) ) {
162 // Hmm... this shouldn't happen though. :)
163 return wfGetDB( DB_MASTER );
164 } else {
165 return wfGetDB( DB_MASTER, [], $dbDomain );
168 return null;
172 * @return int
174 public function getId() {
175 return $this->id;
179 * @return bool
181 public function isAnon() {
182 return $this->getId() == 0;
186 * Same as User::getName()
188 * @return string
190 public function getName() {
191 return $this->name . '@' . $this->dbDomain;
195 * Same as User::getUserPage()
197 * @return Title
199 public function getUserPage() {
200 return Title::makeTitle( NS_USER, $this->getName() );
204 * Replaces User::getUserGroups()
205 * @return string[]
207 public function getGroups() {
208 return array_keys( self::getGroupMemberships() );
212 * Replaces User::getGroupMemberships()
214 * @return UserGroupMembership[]
215 * @since 1.29
217 public function getGroupMemberships() {
218 // TODO: We are creating an artificial UserIdentity to pass on to the user group manager.
219 // After all the relevant UserGroupMemberships methods are ported into UserGroupManager,
220 // the usages of this class will be changed into usages of the UserGroupManager,
221 // thus the need of this class and the need of this artificial UserIdentityValue will parish.
222 $user = new UserIdentityValue( $this->getId(), $this->getName(), 0 );
223 return $this->userGroupManager->getUserGroupMemberships( $user, IDBAccessObject::READ_LATEST );
227 * Replaces User::addGroup()
229 * @param string $group
230 * @param string|null $expiry
231 * @return bool
233 public function addGroup( $group, $expiry = null ) {
234 return $this->userGroupManager->addUserToGroup(
235 // TODO: Artificial UserIdentity just for passing the id and name.
236 // see comment in getGroupMemberships.
237 new UserIdentityValue( $this->getId(), $this->getName(), 0 ),
238 $group,
239 $expiry
244 * Replaces User::removeGroup()
246 * @param string $group
247 * @return bool
249 public function removeGroup( $group ) {
250 return $this->userGroupManager->removeUserFromGroup(
251 // TODO: Artificial UserIdentity just for passing the id and name.
252 // see comment in getGroupMemberships.
253 new UserIdentityValue( $this->getId(), $this->getName(), 0 ),
254 $group
259 * Replaces User::setOption()
260 * @param string $option
261 * @param mixed $value
263 public function setOption( $option, $value ) {
264 $this->newOptions[$option] = $value;
267 public function saveSettings() {
268 $rows = [];
269 foreach ( $this->newOptions as $option => $value ) {
270 $rows[] = [
271 'up_user' => $this->id,
272 'up_property' => $option,
273 'up_value' => $value,
276 $this->db->replace(
277 'user_properties',
278 [ [ 'up_user', 'up_property' ] ],
279 $rows,
280 __METHOD__
282 $this->invalidateCache();
286 * Replaces User::touchUser()
288 public function invalidateCache() {
289 $this->db->update(
290 'user',
291 [ 'user_touched' => $this->db->timestamp() ],
292 [ 'user_id' => $this->id ],
293 __METHOD__
296 $domainId = $this->db->getDomainID();
297 $userId = $this->id;
298 $this->db->onTransactionPreCommitOrIdle(
299 function () use ( $domainId, $userId ) {
300 User::purge( $domainId, $userId );
302 __METHOD__