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