Merge "Make LocalisationCache a service"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
1 <?php
2 /**
3 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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 /**
24 * Query module to enumerate all registered users.
25 *
26 * @ingroup API
27 */
28 class ApiQueryAllUsers extends ApiQueryBase {
29 public function __construct( ApiQuery $query, $moduleName ) {
30 parent::__construct( $query, $moduleName, 'au' );
31 }
32
33 /**
34 * This function converts the user name to a canonical form
35 * which is stored in the database.
36 * @param string $name
37 * @return string
38 */
39 private function getCanonicalUserName( $name ) {
40 return strtr( $name, '_', ' ' );
41 }
42
43 public function execute() {
44 global $wgActorTableSchemaMigrationStage;
45
46 $params = $this->extractRequestParams();
47 $activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );
48
49 $db = $this->getDB();
50 $commentStore = CommentStore::getStore();
51
52 $prop = $params['prop'];
53 if ( !is_null( $prop ) ) {
54 $prop = array_flip( $prop );
55 $fld_blockinfo = isset( $prop['blockinfo'] );
56 $fld_editcount = isset( $prop['editcount'] );
57 $fld_groups = isset( $prop['groups'] );
58 $fld_rights = isset( $prop['rights'] );
59 $fld_registration = isset( $prop['registration'] );
60 $fld_implicitgroups = isset( $prop['implicitgroups'] );
61 $fld_centralids = isset( $prop['centralids'] );
62 } else {
63 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
64 $fld_rights = $fld_implicitgroups = $fld_centralids = false;
65 }
66
67 $limit = $params['limit'];
68
69 $this->addTables( 'user' );
70
71 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
72 $from = is_null( $params['from'] ) ? null : $this->getCanonicalUserName( $params['from'] );
73 $to = is_null( $params['to'] ) ? null : $this->getCanonicalUserName( $params['to'] );
74
75 # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
76 # despite the JOIN condition, so manually sort on the correct one.
77 $userFieldToSort = $params['activeusers'] ? 'qcc_title' : 'user_name';
78
79 # Some of these subtable joins are going to give us duplicate rows, so
80 # calculate the maximum number of duplicates we might see.
81 $maxDuplicateRows = 1;
82
83 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
84
85 if ( !is_null( $params['prefix'] ) ) {
86 $this->addWhere( $userFieldToSort .
87 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
88 }
89
90 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
91 $groups = [];
92 foreach ( $params['rights'] as $r ) {
93 $groups = array_merge( $groups, $this->getPermissionManager()
94 ->getGroupsWithPermission( $r ) );
95 }
96
97 // no group with the given right(s) exists, no need for a query
98 if ( $groups === [] ) {
99 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], '' );
100
101 return;
102 }
103
104 $groups = array_unique( $groups );
105
106 if ( is_null( $params['group'] ) ) {
107 $params['group'] = $groups;
108 } else {
109 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
110 }
111 }
112
113 $this->requireMaxOneParameter( $params, 'group', 'excludegroup' );
114
115 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
116 // Filter only users that belong to a given group. This might
117 // produce as many rows-per-user as there are groups being checked.
118 $this->addTables( 'user_groups', 'ug1' );
119 $this->addJoinConds( [
120 'ug1' => [
121 'JOIN',
122 [
123 'ug1.ug_user=user_id',
124 'ug1.ug_group' => $params['group'],
125 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
126 ]
127 ]
128 ] );
129 $maxDuplicateRows *= count( $params['group'] );
130 }
131
132 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
133 // Filter only users don't belong to a given group. This can only
134 // produce one row-per-user, because we only keep on "no match".
135 $this->addTables( 'user_groups', 'ug1' );
136
137 if ( count( $params['excludegroup'] ) == 1 ) {
138 $exclude = [ 'ug1.ug_group' => $params['excludegroup'][0] ];
139 } else {
140 $exclude = [ $db->makeList(
141 [ 'ug1.ug_group' => $params['excludegroup'] ],
142 LIST_OR
143 ) ];
144 }
145 $this->addJoinConds( [ 'ug1' => [ 'LEFT JOIN',
146 array_merge( [
147 'ug1.ug_user=user_id',
148 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
149 ], $exclude )
150 ] ] );
151 $this->addWhere( 'ug1.ug_user IS NULL' );
152 }
153
154 if ( $params['witheditsonly'] ) {
155 $this->addWhere( 'user_editcount > 0' );
156 }
157
158 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
159
160 if ( $fld_groups || $fld_rights ) {
161 $this->addFields( [ 'groups' =>
162 $db->buildGroupConcatField( '|', 'user_groups', 'ug_group', [
163 'ug_user=user_id',
164 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
165 ] )
166 ] );
167 }
168
169 if ( $params['activeusers'] ) {
170 $activeUserSeconds = $activeUserDays * 86400;
171
172 // Filter query to only include users in the active users cache.
173 // There shouldn't be any duplicate rows in querycachetwo here.
174 $this->addTables( 'querycachetwo' );
175 $this->addJoinConds( [ 'querycachetwo' => [
176 'JOIN', [
177 'qcc_type' => 'activeusers',
178 'qcc_namespace' => NS_USER,
179 'qcc_title=user_name',
180 ],
181 ] ] );
182
183 // Actually count the actions using a subquery (T66505 and T66507)
184 $tables = [ 'recentchanges' ];
185 $joins = [];
186 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_OLD ) {
187 $userCond = 'rc_user_text = user_name';
188 } else {
189 $tables[] = 'actor';
190 $joins['actor'] = [ 'JOIN', 'rc_actor = actor_id' ];
191 $userCond = 'actor_user = user_id';
192 }
193 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
194 $this->addFields( [
195 'recentactions' => '(' . $db->selectSQLText(
196 $tables,
197 'COUNT(*)',
198 [
199 $userCond,
200 'rc_type != ' . $db->addQuotes( RC_EXTERNAL ), // no wikidata
201 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ),
202 'rc_timestamp >= ' . $db->addQuotes( $timestamp ),
203 ],
204 __METHOD__,
205 [],
206 $joins
207 ) . ')'
208 ] );
209 }
210
211 $sqlLimit = $limit + $maxDuplicateRows;
212 $this->addOption( 'LIMIT', $sqlLimit );
213
214 $this->addFields( [
215 'user_name',
216 'user_id'
217 ] );
218 $this->addFieldsIf( 'user_editcount', $fld_editcount );
219 $this->addFieldsIf( 'user_registration', $fld_registration );
220
221 $res = $this->select( __METHOD__ );
222 $count = 0;
223 $countDuplicates = 0;
224 $lastUser = false;
225 $result = $this->getResult();
226 foreach ( $res as $row ) {
227 $count++;
228
229 if ( $lastUser === $row->user_name ) {
230 // Duplicate row due to one of the needed subtable joins.
231 // Ignore it, but count the number of them to sanely handle
232 // miscalculation of $maxDuplicateRows.
233 $countDuplicates++;
234 if ( $countDuplicates == $maxDuplicateRows ) {
235 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
236 }
237 continue;
238 }
239
240 $countDuplicates = 0;
241 $lastUser = $row->user_name;
242
243 if ( $count > $limit ) {
244 // We've reached the one extra which shows that there are
245 // additional pages to be had. Stop here...
246 $this->setContinueEnumParameter( 'from', $row->user_name );
247 break;
248 }
249
250 if ( $count == $sqlLimit ) {
251 // Should never hit this (either the $countDuplicates check or
252 // the $count > $limit check should hit first), but check it
253 // anyway just in case.
254 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
255 }
256
257 if ( $params['activeusers'] && $row->recentactions === 0 ) {
258 // activeusers cache was out of date
259 continue;
260 }
261
262 $data = [
263 'userid' => (int)$row->user_id,
264 'name' => $row->user_name,
265 ];
266
267 if ( $fld_centralids ) {
268 $data += ApiQueryUserInfo::getCentralUserInfo(
269 $this->getConfig(), User::newFromId( $row->user_id ), $params['attachedwiki']
270 );
271 }
272
273 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
274 $data['blockid'] = (int)$row->ipb_id;
275 $data['blockedby'] = $row->ipb_by_text;
276 $data['blockedbyid'] = (int)$row->ipb_by;
277 $data['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
278 $data['blockreason'] = $commentStore->getComment( 'ipb_reason', $row )->text;
279 $data['blockexpiry'] = $row->ipb_expiry;
280 }
281 if ( $row->ipb_deleted ) {
282 $data['hidden'] = true;
283 }
284 if ( $fld_editcount ) {
285 $data['editcount'] = (int)$row->user_editcount;
286 }
287 if ( $params['activeusers'] ) {
288 $data['recentactions'] = (int)$row->recentactions;
289 }
290 if ( $fld_registration ) {
291 $data['registration'] = $row->user_registration ?
292 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
293 }
294
295 if ( $fld_implicitgroups || $fld_groups || $fld_rights ) {
296 $implicitGroups = User::newFromId( $row->user_id )->getAutomaticGroups();
297 if ( isset( $row->groups ) && $row->groups !== '' ) {
298 $groups = array_merge( $implicitGroups, explode( '|', $row->groups ) );
299 } else {
300 $groups = $implicitGroups;
301 }
302
303 if ( $fld_groups ) {
304 $data['groups'] = $groups;
305 ApiResult::setIndexedTagName( $data['groups'], 'g' );
306 ApiResult::setArrayType( $data['groups'], 'array' );
307 }
308
309 if ( $fld_implicitgroups ) {
310 $data['implicitgroups'] = $implicitGroups;
311 ApiResult::setIndexedTagName( $data['implicitgroups'], 'g' );
312 ApiResult::setArrayType( $data['implicitgroups'], 'array' );
313 }
314
315 if ( $fld_rights ) {
316 $data['rights'] = $this->getPermissionManager()->getGroupPermissions( $groups );
317 ApiResult::setIndexedTagName( $data['rights'], 'r' );
318 ApiResult::setArrayType( $data['rights'], 'array' );
319 }
320 }
321
322 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
323 if ( !$fit ) {
324 $this->setContinueEnumParameter( 'from', $data['name'] );
325 break;
326 }
327 }
328
329 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'u' );
330 }
331
332 public function getCacheMode( $params ) {
333 return 'anon-public-user-private';
334 }
335
336 public function getAllowedParams() {
337 $userGroups = User::getAllGroups();
338
339 return [
340 'from' => null,
341 'to' => null,
342 'prefix' => null,
343 'dir' => [
344 ApiBase::PARAM_DFLT => 'ascending',
345 ApiBase::PARAM_TYPE => [
346 'ascending',
347 'descending'
348 ],
349 ],
350 'group' => [
351 ApiBase::PARAM_TYPE => $userGroups,
352 ApiBase::PARAM_ISMULTI => true,
353 ],
354 'excludegroup' => [
355 ApiBase::PARAM_TYPE => $userGroups,
356 ApiBase::PARAM_ISMULTI => true,
357 ],
358 'rights' => [
359 ApiBase::PARAM_TYPE => User::getAllRights(),
360 ApiBase::PARAM_ISMULTI => true,
361 ],
362 'prop' => [
363 ApiBase::PARAM_ISMULTI => true,
364 ApiBase::PARAM_TYPE => [
365 'blockinfo',
366 'groups',
367 'implicitgroups',
368 'rights',
369 'editcount',
370 'registration',
371 'centralids',
372 ],
373 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
374 ],
375 'limit' => [
376 ApiBase::PARAM_DFLT => 10,
377 ApiBase::PARAM_TYPE => 'limit',
378 ApiBase::PARAM_MIN => 1,
379 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
380 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
381 ],
382 'witheditsonly' => false,
383 'activeusers' => [
384 ApiBase::PARAM_DFLT => false,
385 ApiBase::PARAM_HELP_MSG => [
386 'apihelp-query+allusers-param-activeusers',
387 $this->getConfig()->get( 'ActiveUserDays' )
388 ],
389 ],
390 'attachedwiki' => null,
391 ];
392 }
393
394 protected function getExamplesMessages() {
395 return [
396 'action=query&list=allusers&aufrom=Y'
397 => 'apihelp-query+allusers-example-y',
398 ];
399 }
400
401 public function getHelpUrls() {
402 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allusers';
403 }
404 }