Localisation updates from https://translatewiki.net.
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 7, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate all registered users.
29 *
30 * @ingroup API
31 */
32 class ApiQueryAllUsers extends ApiQueryBase {
33 public function __construct( ApiQuery $query, $moduleName ) {
34 parent::__construct( $query, $moduleName, 'au' );
35 }
36
37 /**
38 * This function converts the user name to a canonical form
39 * which is stored in the database.
40 * @param string $name
41 * @return string
42 */
43 private function getCanonicalUserName( $name ) {
44 return str_replace( '_', ' ', $name );
45 }
46
47 public function execute() {
48 $params = $this->extractRequestParams();
49 $activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );
50
51 if ( $params['activeusers'] ) {
52 // Update active user cache
53 SpecialActiveUsers::mergeActiveUsers( 300, $activeUserDays );
54 }
55
56 $db = $this->getDB();
57
58 $prop = $params['prop'];
59 if ( !is_null( $prop ) ) {
60 $prop = array_flip( $prop );
61 $fld_blockinfo = isset( $prop['blockinfo'] );
62 $fld_editcount = isset( $prop['editcount'] );
63 $fld_groups = isset( $prop['groups'] );
64 $fld_rights = isset( $prop['rights'] );
65 $fld_registration = isset( $prop['registration'] );
66 $fld_implicitgroups = isset( $prop['implicitgroups'] );
67 } else {
68 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
69 $fld_rights = $fld_implicitgroups = false;
70 }
71
72 $limit = $params['limit'];
73
74 $this->addTables( 'user' );
75
76 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
77 $from = is_null( $params['from'] ) ? null : $this->getCanonicalUserName( $params['from'] );
78 $to = is_null( $params['to'] ) ? null : $this->getCanonicalUserName( $params['to'] );
79
80 # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
81 # despite the JOIN condition, so manually sort on the correct one.
82 $userFieldToSort = $params['activeusers'] ? 'qcc_title' : 'user_name';
83
84 # Some of these subtable joins are going to give us duplicate rows, so
85 # calculate the maximum number of duplicates we might see.
86 $maxDuplicateRows = 1;
87
88 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
89
90 if ( !is_null( $params['prefix'] ) ) {
91 $this->addWhere( $userFieldToSort .
92 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
93 }
94
95 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
96 $groups = array();
97 foreach ( $params['rights'] as $r ) {
98 $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
99 }
100
101 // no group with the given right(s) exists, no need for a query
102 if ( !count( $groups ) ) {
103 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), '' );
104
105 return;
106 }
107
108 $groups = array_unique( $groups );
109
110 if ( is_null( $params['group'] ) ) {
111 $params['group'] = $groups;
112 } else {
113 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
114 }
115 }
116
117 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
118 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
119 }
120
121 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
122 // Filter only users that belong to a given group. This might
123 // produce as many rows-per-user as there are groups being checked.
124 $this->addTables( 'user_groups', 'ug1' );
125 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
126 'ug1.ug_group' => $params['group'] ) ) ) );
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 = array( 'ug1.ug_group' => $params['excludegroup'][0] );
137 } else {
138 $exclude = array( $db->makeList(
139 array( 'ug1.ug_group' => $params['excludegroup'] ),
140 LIST_OR
141 ) );
142 }
143 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
144 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
145 ) ) );
146 $this->addWhere( 'ug1.ug_user IS NULL' );
147 }
148
149 if ( $params['witheditsonly'] ) {
150 $this->addWhere( 'user_editcount > 0' );
151 }
152
153 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
154
155 if ( $fld_groups || $fld_rights ) {
156 $this->addFields( array( 'groups' =>
157 $db->buildGroupConcatField( '|', 'user_groups', 'ug_group', 'ug_user=user_id' )
158 ) );
159 }
160
161 if ( $params['activeusers'] ) {
162 $activeUserSeconds = $activeUserDays * 86400;
163
164 // Filter query to only include users in the active users cache.
165 // There shouldn't be any duplicate rows in querycachetwo here.
166 $this->addTables( 'querycachetwo' );
167 $this->addJoinConds( array( 'querycachetwo' => array(
168 'INNER JOIN', array(
169 'qcc_type' => 'activeusers',
170 'qcc_namespace' => NS_USER,
171 'qcc_title=user_name',
172 ),
173 ) ) );
174
175 // Actually count the actions using a subquery (bug 64505 and bug 64507)
176 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
177 $this->addFields( array(
178 'recentactions' => '(' . $db->selectSQLText(
179 'recentchanges',
180 'COUNT(*)',
181 array(
182 'rc_user_text = user_name',
183 'rc_type != ' . $db->addQuotes( RC_EXTERNAL ), // no wikidata
184 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ),
185 'rc_timestamp >= ' . $db->addQuotes( $timestamp ),
186 )
187 ) . ')'
188 ) );
189 }
190
191 $sqlLimit = $limit + $maxDuplicateRows;
192 $this->addOption( 'LIMIT', $sqlLimit );
193
194 $this->addFields( array(
195 'user_name',
196 'user_id'
197 ) );
198 $this->addFieldsIf( 'user_editcount', $fld_editcount );
199 $this->addFieldsIf( 'user_registration', $fld_registration );
200
201 $res = $this->select( __METHOD__ );
202 $count = 0;
203 $countDuplicates = 0;
204 $lastUser = false;
205 $result = $this->getResult();
206 foreach ( $res as $row ) {
207 $count++;
208
209 if ( $lastUser === $row->user_name ) {
210 // Duplicate row due to one of the needed subtable joins.
211 // Ignore it, but count the number of them to sanely handle
212 // miscalculation of $maxDuplicateRows.
213 $countDuplicates++;
214 if ( $countDuplicates == $maxDuplicateRows ) {
215 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
216 }
217 continue;
218 }
219
220 $countDuplicates = 0;
221 $lastUser = $row->user_name;
222
223 if ( $count > $limit ) {
224 // We've reached the one extra which shows that there are
225 // additional pages to be had. Stop here...
226 $this->setContinueEnumParameter( 'from', $row->user_name );
227 break;
228 }
229
230 if ( $count == $sqlLimit ) {
231 // Should never hit this (either the $countDuplicates check or
232 // the $count > $limit check should hit first), but check it
233 // anyway just in case.
234 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
235 }
236
237 if ( $params['activeusers'] && $row->recentactions === 0 ) {
238 // activeusers cache was out of date
239 continue;
240 }
241
242 $data = array(
243 'userid' => $row->user_id,
244 'name' => $row->user_name,
245 );
246
247 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
248 $data['blockid'] = $row->ipb_id;
249 $data['blockedby'] = $row->ipb_by_text;
250 $data['blockedbyid'] = $row->ipb_by;
251 $data['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
252 $data['blockreason'] = $row->ipb_reason;
253 $data['blockexpiry'] = $row->ipb_expiry;
254 }
255 if ( $row->ipb_deleted ) {
256 $data['hidden'] = '';
257 }
258 if ( $fld_editcount ) {
259 $data['editcount'] = intval( $row->user_editcount );
260 }
261 if ( $params['activeusers'] ) {
262 $data['recentactions'] = intval( $row->recentactions );
263 // @todo 'recenteditcount' is set for BC, remove in 1.25
264 $data['recenteditcount'] = $data['recentactions'];
265 }
266 if ( $fld_registration ) {
267 $data['registration'] = $row->user_registration ?
268 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
269 }
270
271 if ( $fld_implicitgroups || $fld_groups || $fld_rights ) {
272 $user = User::newFromId( $row->user_id );
273 $implicitGroups = User::newFromId( $row->user_id )->getAutomaticGroups();
274 if ( isset( $row->groups ) && $row->groups !== '' ) {
275 $groups = array_merge( $implicitGroups, explode( '|', $row->groups ) );
276 } else {
277 $groups = $implicitGroups;
278 }
279
280 if ( $fld_groups ) {
281 $data['groups'] = $groups;
282 $result->setIndexedTagName( $data['groups'], 'g' );
283 }
284
285 if ( $fld_implicitgroups ) {
286 $data['implicitgroups'] = $implicitGroups;
287 $result->setIndexedTagName( $data['implicitgroups'], 'g' );
288 }
289
290 if ( $fld_rights ) {
291 $data['rights'] = User::getGroupPermissions( $groups );
292 $result->setIndexedTagName( $data['rights'], 'r' );
293 }
294 }
295
296 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $data );
297 if ( !$fit ) {
298 $this->setContinueEnumParameter( 'from', $data['name'] );
299 break;
300 }
301 }
302
303 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
304 }
305
306 public function getCacheMode( $params ) {
307 return 'anon-public-user-private';
308 }
309
310 public function getAllowedParams() {
311 $userGroups = User::getAllGroups();
312
313 return array(
314 'from' => null,
315 'to' => null,
316 'prefix' => null,
317 'dir' => array(
318 ApiBase::PARAM_DFLT => 'ascending',
319 ApiBase::PARAM_TYPE => array(
320 'ascending',
321 'descending'
322 ),
323 ),
324 'group' => array(
325 ApiBase::PARAM_TYPE => $userGroups,
326 ApiBase::PARAM_ISMULTI => true,
327 ),
328 'excludegroup' => array(
329 ApiBase::PARAM_TYPE => $userGroups,
330 ApiBase::PARAM_ISMULTI => true,
331 ),
332 'rights' => array(
333 ApiBase::PARAM_TYPE => User::getAllRights(),
334 ApiBase::PARAM_ISMULTI => true,
335 ),
336 'prop' => array(
337 ApiBase::PARAM_ISMULTI => true,
338 ApiBase::PARAM_TYPE => array(
339 'blockinfo',
340 'groups',
341 'implicitgroups',
342 'rights',
343 'editcount',
344 'registration'
345 )
346 ),
347 'limit' => array(
348 ApiBase::PARAM_DFLT => 10,
349 ApiBase::PARAM_TYPE => 'limit',
350 ApiBase::PARAM_MIN => 1,
351 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
352 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
353 ),
354 'witheditsonly' => false,
355 'activeusers' => array(
356 ApiBase::PARAM_DFLT => false,
357 ApiBase::PARAM_HELP_MSG => array(
358 'apihelp-query+allusers-param-activeusers',
359 $this->getConfig()->get( 'ActiveUserDays' )
360 ),
361 ),
362 );
363 }
364
365 protected function getExamplesMessages() {
366 return array(
367 'action=query&list=allusers&aufrom=Y'
368 => 'apihelp-query+allusers-example-Y',
369 );
370 }
371
372 public function getHelpUrls() {
373 return 'https://www.mediawiki.org/wiki/API:Allusers';
374 }
375 }