5 * Created on July 7, 2007
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
28 * Query module to enumerate all registered users.
32 class ApiQueryAllUsers
extends ApiQueryBase
{
33 public function __construct( ApiQuery
$query, $moduleName ) {
34 parent
::__construct( $query, $moduleName, 'au' );
38 * This function converts the user name to a canonical form
39 * which is stored in the database.
43 private function getCanonicalUserName( $name ) {
44 return strtr( $name, '_', ' ' );
47 public function execute() {
48 $params = $this->extractRequestParams();
49 $activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );
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 $fld_centralids = isset( $prop['centralids'] );
64 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
65 $fld_rights = $fld_implicitgroups = $fld_centralids = false;
68 $limit = $params['limit'];
70 $this->addTables( 'user' );
72 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
73 $from = is_null( $params['from'] ) ?
null : $this->getCanonicalUserName( $params['from'] );
74 $to = is_null( $params['to'] ) ?
null : $this->getCanonicalUserName( $params['to'] );
76 # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
77 # despite the JOIN condition, so manually sort on the correct one.
78 $userFieldToSort = $params['activeusers'] ?
'qcc_title' : 'user_name';
80 # Some of these subtable joins are going to give us duplicate rows, so
81 # calculate the maximum number of duplicates we might see.
82 $maxDuplicateRows = 1;
84 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
86 if ( !is_null( $params['prefix'] ) ) {
87 $this->addWhere( $userFieldToSort .
88 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
91 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
93 foreach ( $params['rights'] as $r ) {
94 $groups = array_merge( $groups, User
::getGroupsWithPermission( $r ) );
97 // no group with the given right(s) exists, no need for a query
98 if ( !count( $groups ) ) {
99 $this->getResult()->addIndexedTagName( array( 'query', $this->getModuleName() ), '' );
104 $groups = array_unique( $groups );
106 if ( is_null( $params['group'] ) ) {
107 $params['group'] = $groups;
109 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
113 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
114 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
117 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
118 // Filter only users that belong to a given group. This might
119 // produce as many rows-per-user as there are groups being checked.
120 $this->addTables( 'user_groups', 'ug1' );
121 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
122 'ug1.ug_group' => $params['group'] ) ) ) );
123 $maxDuplicateRows *= count( $params['group'] );
126 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
127 // Filter only users don't belong to a given group. This can only
128 // produce one row-per-user, because we only keep on "no match".
129 $this->addTables( 'user_groups', 'ug1' );
131 if ( count( $params['excludegroup'] ) == 1 ) {
132 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
134 $exclude = array( $db->makeList(
135 array( 'ug1.ug_group' => $params['excludegroup'] ),
139 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
140 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
142 $this->addWhere( 'ug1.ug_user IS NULL' );
145 if ( $params['witheditsonly'] ) {
146 $this->addWhere( 'user_editcount > 0' );
149 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
151 if ( $fld_groups ||
$fld_rights ) {
152 $this->addFields( array( 'groups' =>
153 $db->buildGroupConcatField( '|', 'user_groups', 'ug_group', 'ug_user=user_id' )
157 if ( $params['activeusers'] ) {
158 $activeUserSeconds = $activeUserDays * 86400;
160 // Filter query to only include users in the active users cache.
161 // There shouldn't be any duplicate rows in querycachetwo here.
162 $this->addTables( 'querycachetwo' );
163 $this->addJoinConds( array( 'querycachetwo' => array(
165 'qcc_type' => 'activeusers',
166 'qcc_namespace' => NS_USER
,
167 'qcc_title=user_name',
171 // Actually count the actions using a subquery (bug 64505 and bug 64507)
172 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX
) - $activeUserSeconds );
173 $this->addFields( array(
174 'recentactions' => '(' . $db->selectSQLText(
178 'rc_user_text = user_name',
179 'rc_type != ' . $db->addQuotes( RC_EXTERNAL
), // no wikidata
180 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ),
181 'rc_timestamp >= ' . $db->addQuotes( $timestamp ),
187 $sqlLimit = $limit +
$maxDuplicateRows;
188 $this->addOption( 'LIMIT', $sqlLimit );
190 $this->addFields( array(
194 $this->addFieldsIf( 'user_editcount', $fld_editcount );
195 $this->addFieldsIf( 'user_registration', $fld_registration );
197 $res = $this->select( __METHOD__
);
199 $countDuplicates = 0;
201 $result = $this->getResult();
202 foreach ( $res as $row ) {
205 if ( $lastUser === $row->user_name
) {
206 // Duplicate row due to one of the needed subtable joins.
207 // Ignore it, but count the number of them to sanely handle
208 // miscalculation of $maxDuplicateRows.
210 if ( $countDuplicates == $maxDuplicateRows ) {
211 ApiBase
::dieDebug( __METHOD__
, 'Saw more duplicate rows than expected' );
216 $countDuplicates = 0;
217 $lastUser = $row->user_name
;
219 if ( $count > $limit ) {
220 // We've reached the one extra which shows that there are
221 // additional pages to be had. Stop here...
222 $this->setContinueEnumParameter( 'from', $row->user_name
);
226 if ( $count == $sqlLimit ) {
227 // Should never hit this (either the $countDuplicates check or
228 // the $count > $limit check should hit first), but check it
229 // anyway just in case.
230 ApiBase
::dieDebug( __METHOD__
, 'Saw more duplicate rows than expected' );
233 if ( $params['activeusers'] && $row->recentactions
=== 0 ) {
234 // activeusers cache was out of date
239 'userid' => (int)$row->user_id
,
240 'name' => $row->user_name
,
243 if ( $fld_centralids ) {
244 $data +
= ApiQueryUserInfo
::getCentralUserInfo(
245 $this->getConfig(), User
::newFromId( $row->user_id
), $params['attachedwiki']
249 if ( $fld_blockinfo && !is_null( $row->ipb_by_text
) ) {
250 $data['blockid'] = (int)$row->ipb_id
;
251 $data['blockedby'] = $row->ipb_by_text
;
252 $data['blockedbyid'] = (int)$row->ipb_by
;
253 $data['blockedtimestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
254 $data['blockreason'] = $row->ipb_reason
;
255 $data['blockexpiry'] = $row->ipb_expiry
;
257 if ( $row->ipb_deleted
) {
258 $data['hidden'] = true;
260 if ( $fld_editcount ) {
261 $data['editcount'] = intval( $row->user_editcount
);
263 if ( $params['activeusers'] ) {
264 $data['recentactions'] = intval( $row->recentactions
);
265 // @todo 'recenteditcount' is set for BC, remove in 1.25
266 $data['recenteditcount'] = $data['recentactions'];
268 if ( $fld_registration ) {
269 $data['registration'] = $row->user_registration ?
270 wfTimestamp( TS_ISO_8601
, $row->user_registration
) : '';
273 if ( $fld_implicitgroups ||
$fld_groups ||
$fld_rights ) {
274 $implicitGroups = User
::newFromId( $row->user_id
)->getAutomaticGroups();
275 if ( isset( $row->groups
) && $row->groups
!== '' ) {
276 $groups = array_merge( $implicitGroups, explode( '|', $row->groups
) );
278 $groups = $implicitGroups;
282 $data['groups'] = $groups;
283 ApiResult
::setIndexedTagName( $data['groups'], 'g' );
284 ApiResult
::setArrayType( $data['groups'], 'array' );
287 if ( $fld_implicitgroups ) {
288 $data['implicitgroups'] = $implicitGroups;
289 ApiResult
::setIndexedTagName( $data['implicitgroups'], 'g' );
290 ApiResult
::setArrayType( $data['implicitgroups'], 'array' );
294 $data['rights'] = User
::getGroupPermissions( $groups );
295 ApiResult
::setIndexedTagName( $data['rights'], 'r' );
296 ApiResult
::setArrayType( $data['rights'], 'array' );
300 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $data );
302 $this->setContinueEnumParameter( 'from', $data['name'] );
307 $result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'u' );
310 public function getCacheMode( $params ) {
311 return 'anon-public-user-private';
314 public function getAllowedParams() {
315 $userGroups = User
::getAllGroups();
322 ApiBase
::PARAM_DFLT
=> 'ascending',
323 ApiBase
::PARAM_TYPE
=> array(
329 ApiBase
::PARAM_TYPE
=> $userGroups,
330 ApiBase
::PARAM_ISMULTI
=> true,
332 'excludegroup' => array(
333 ApiBase
::PARAM_TYPE
=> $userGroups,
334 ApiBase
::PARAM_ISMULTI
=> true,
337 ApiBase
::PARAM_TYPE
=> User
::getAllRights(),
338 ApiBase
::PARAM_ISMULTI
=> true,
341 ApiBase
::PARAM_ISMULTI
=> true,
342 ApiBase
::PARAM_TYPE
=> array(
351 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> array(),
354 ApiBase
::PARAM_DFLT
=> 10,
355 ApiBase
::PARAM_TYPE
=> 'limit',
356 ApiBase
::PARAM_MIN
=> 1,
357 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
358 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
360 'witheditsonly' => false,
361 'activeusers' => array(
362 ApiBase
::PARAM_DFLT
=> false,
363 ApiBase
::PARAM_HELP_MSG
=> array(
364 'apihelp-query+allusers-param-activeusers',
365 $this->getConfig()->get( 'ActiveUserDays' )
368 'attachedwiki' => null,
372 protected function getExamplesMessages() {
374 'action=query&list=allusers&aufrom=Y'
375 => 'apihelp-query+allusers-example-Y',
379 public function getHelpUrls() {
380 return 'https://www.mediawiki.org/wiki/API:Allusers';