Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / user / UserRightsProxy.php
1 <?php
2 /**
3 * Representation of an user on a other locally-hosted wiki.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use Wikimedia\Rdbms\IDatabase;
24
25 /**
26 * Cut-down copy of User interface for local-interwiki-database
27 * user rights manipulation.
28 */
29 class UserRightsProxy {
30 /** @var IDatabase */
31 private $db;
32 /** @var string */
33 private $dbDomain;
34 /** @var string */
35 private $name;
36 /** @var int */
37 private $id;
38 /** @var array */
39 private $newOptions;
40
41 /**
42 * @see newFromId()
43 * @see newFromName()
44 * @param IDatabase $db Db connection
45 * @param string $dbDomain Database name
46 * @param string $name User name
47 * @param int $id User ID
48 */
49 private function __construct( $db, $dbDomain, $name, $id ) {
50 $this->db = $db;
51 $this->dbDomain = $dbDomain;
52 $this->name = $name;
53 $this->id = intval( $id );
54 $this->newOptions = [];
55 }
56
57 /**
58 * Confirm the selected database name is a valid local interwiki database name.
59 *
60 * @param string $dbDomain Database name
61 * @return bool
62 */
63 public static function validDatabase( $dbDomain ) {
64 global $wgLocalDatabases;
65 return in_array( $dbDomain, $wgLocalDatabases );
66 }
67
68 /**
69 * Same as User::whoIs()
70 *
71 * @param string $dbDomain Database name
72 * @param int $id User ID
73 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
74 * @return string User name or false if the user doesn't exist
75 */
76 public static function whoIs( $dbDomain, $id, $ignoreInvalidDB = false ) {
77 $user = self::newFromId( $dbDomain, $id, $ignoreInvalidDB );
78 if ( $user ) {
79 return $user->name;
80 } else {
81 return false;
82 }
83 }
84
85 /**
86 * Factory function; get a remote user entry by ID number.
87 *
88 * @param string $dbDomain Database name
89 * @param int $id User ID
90 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
91 * @return UserRightsProxy|null If doesn't exist
92 */
93 public static function newFromId( $dbDomain, $id, $ignoreInvalidDB = false ) {
94 return self::newFromLookup( $dbDomain, 'user_id', intval( $id ), $ignoreInvalidDB );
95 }
96
97 /**
98 * Factory function; get a remote user entry by name.
99 *
100 * @param string $dbDomain Database name
101 * @param string $name User name
102 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
103 * @return UserRightsProxy|null If doesn't exist
104 */
105 public static function newFromName( $dbDomain, $name, $ignoreInvalidDB = false ) {
106 return self::newFromLookup( $dbDomain, 'user_name', $name, $ignoreInvalidDB );
107 }
108
109 /**
110 * @param string $dbDomain
111 * @param string $field
112 * @param string $value
113 * @param bool $ignoreInvalidDB
114 * @return null|UserRightsProxy
115 */
116 private static function newFromLookup( $dbDomain, $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 );
123 } else {
124 $userdb = self::getDB( $dbDomain, $ignoreInvalidDB );
125 }
126
127 $db = self::getDB( $dbDomain, $ignoreInvalidDB );
128
129 if ( $db && $userdb ) {
130 $row = $userdb->selectRow( 'user',
131 [ 'user_id', 'user_name' ],
132 [ $field => $value ],
133 __METHOD__ );
134
135 if ( $row !== false ) {
136 return new UserRightsProxy(
137 $db, $dbDomain, $row->user_name, intval( $row->user_id ) );
138 }
139 }
140 return null;
141 }
142
143 /**
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.
146 *
147 * @param string $dbDomain
148 * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
149 * @return IDatabase|null If invalid selection
150 */
151 public static function getDB( $dbDomain, $ignoreInvalidDB = false ) {
152 if ( $ignoreInvalidDB || self::validDatabase( $dbDomain ) ) {
153 if ( WikiMap::isCurrentWikiId( $dbDomain ) ) {
154 // Hmm... this shouldn't happen though. :)
155 return wfGetDB( DB_MASTER );
156 } else {
157 return wfGetDB( DB_MASTER, [], $dbDomain );
158 }
159 }
160 return null;
161 }
162
163 /**
164 * @return int
165 */
166 public function getId() {
167 return $this->id;
168 }
169
170 /**
171 * @return bool
172 */
173 public function isAnon() {
174 return $this->getId() == 0;
175 }
176
177 /**
178 * Same as User::getName()
179 *
180 * @return string
181 */
182 public function getName() {
183 return $this->name . '@' . $this->dbDomain;
184 }
185
186 /**
187 * Same as User::getUserPage()
188 *
189 * @return Title
190 */
191 public function getUserPage() {
192 return Title::makeTitle( NS_USER, $this->getName() );
193 }
194
195 /**
196 * Replaces User::getUserGroups()
197 * @return array
198 */
199 function getGroups() {
200 return array_keys( self::getGroupMemberships() );
201 }
202
203 /**
204 * Replaces User::getGroupMemberships()
205 *
206 * @return array
207 * @since 1.29
208 */
209 function getGroupMemberships() {
210 return UserGroupMembership::getMembershipsForUser( $this->id, $this->db );
211 }
212
213 /**
214 * Replaces User::addGroup()
215 *
216 * @param string $group
217 * @param string|null $expiry
218 * @return bool
219 */
220 function addGroup( $group, $expiry = null ) {
221 if ( $expiry ) {
222 $expiry = wfTimestamp( TS_MW, $expiry );
223 }
224
225 $ugm = new UserGroupMembership( $this->id, $group, $expiry );
226 return $ugm->insert( true, $this->db );
227 }
228
229 /**
230 * Replaces User::removeGroup()
231 *
232 * @param string $group
233 * @return bool
234 */
235 function removeGroup( $group ) {
236 $ugm = UserGroupMembership::getMembership( $this->id, $group, $this->db );
237 if ( !$ugm ) {
238 return false;
239 }
240 return $ugm->delete( $this->db );
241 }
242
243 /**
244 * Replaces User::setOption()
245 * @param string $option
246 * @param mixed $value
247 */
248 public function setOption( $option, $value ) {
249 $this->newOptions[$option] = $value;
250 }
251
252 public function saveSettings() {
253 $rows = [];
254 foreach ( $this->newOptions as $option => $value ) {
255 $rows[] = [
256 'up_user' => $this->id,
257 'up_property' => $option,
258 'up_value' => $value,
259 ];
260 }
261 $this->db->replace( 'user_properties',
262 [ [ 'up_user', 'up_property' ] ],
263 $rows, __METHOD__
264 );
265 $this->invalidateCache();
266 }
267
268 /**
269 * Replaces User::touchUser()
270 */
271 function invalidateCache() {
272 $this->db->update(
273 'user',
274 [ 'user_touched' => $this->db->timestamp() ],
275 [ 'user_id' => $this->id ],
276 __METHOD__
277 );
278
279 $domainId = $this->db->getDomainID();
280 $userId = $this->id;
281 $this->db->onTransactionPreCommitOrIdle(
282 function () use ( $domainId, $userId ) {
283 User::purge( $domainId, $userId );
284 },
285 __METHOD__
286 );
287 }
288 }