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