Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiQueryAllUsers.php
blob2e2ac320fd589483e146a1160791f2881bd51f70
1 <?php
2 /**
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
24 * @file
27 /**
28 * Query module to enumerate all registered users.
30 * @ingroup API
32 class ApiQueryAllUsers extends ApiQueryBase {
33 public function __construct( ApiQuery $query, $moduleName ) {
34 parent::__construct( $query, $moduleName, 'au' );
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
43 private function getCanonicalUserName( $name ) {
44 return strtr( $name, '_', ' ' );
47 public function execute() {
48 $params = $this->extractRequestParams();
49 $activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );
51 $db = $this->getDB();
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'] );
63 } else {
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'] ) ) {
92 $groups = [];
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( [ 'query', $this->getModuleName() ], '' );
101 return;
104 $groups = array_unique( $groups );
106 if ( is_null( $params['group'] ) ) {
107 $params['group'] = $groups;
108 } else {
109 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
113 $this->requireMaxOneParameter( $params, 'group', 'excludegroup' );
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( [ 'ug1' => [ 'INNER JOIN', [ 'ug1.ug_user=user_id',
120 'ug1.ug_group' => $params['group'] ] ] ] );
121 $maxDuplicateRows *= count( $params['group'] );
124 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
125 // Filter only users don't belong to a given group. This can only
126 // produce one row-per-user, because we only keep on "no match".
127 $this->addTables( 'user_groups', 'ug1' );
129 if ( count( $params['excludegroup'] ) == 1 ) {
130 $exclude = [ 'ug1.ug_group' => $params['excludegroup'][0] ];
131 } else {
132 $exclude = [ $db->makeList(
133 [ 'ug1.ug_group' => $params['excludegroup'] ],
134 LIST_OR
135 ) ];
137 $this->addJoinConds( [ 'ug1' => [ 'LEFT OUTER JOIN',
138 array_merge( [ 'ug1.ug_user=user_id' ], $exclude )
139 ] ] );
140 $this->addWhere( 'ug1.ug_user IS NULL' );
143 if ( $params['witheditsonly'] ) {
144 $this->addWhere( 'user_editcount > 0' );
147 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
149 if ( $fld_groups || $fld_rights ) {
150 $this->addFields( [ 'groups' =>
151 $db->buildGroupConcatField( '|', 'user_groups', 'ug_group', 'ug_user=user_id' )
152 ] );
155 if ( $params['activeusers'] ) {
156 $activeUserSeconds = $activeUserDays * 86400;
158 // Filter query to only include users in the active users cache.
159 // There shouldn't be any duplicate rows in querycachetwo here.
160 $this->addTables( 'querycachetwo' );
161 $this->addJoinConds( [ 'querycachetwo' => [
162 'INNER JOIN', [
163 'qcc_type' => 'activeusers',
164 'qcc_namespace' => NS_USER,
165 'qcc_title=user_name',
167 ] ] );
169 // Actually count the actions using a subquery (bug 64505 and bug 64507)
170 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
171 $this->addFields( [
172 'recentactions' => '(' . $db->selectSQLText(
173 'recentchanges',
174 'COUNT(*)',
176 'rc_user_text = user_name',
177 'rc_type != ' . $db->addQuotes( RC_EXTERNAL ), // no wikidata
178 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ),
179 'rc_timestamp >= ' . $db->addQuotes( $timestamp ),
181 ) . ')'
182 ] );
185 $sqlLimit = $limit + $maxDuplicateRows;
186 $this->addOption( 'LIMIT', $sqlLimit );
188 $this->addFields( [
189 'user_name',
190 'user_id'
191 ] );
192 $this->addFieldsIf( 'user_editcount', $fld_editcount );
193 $this->addFieldsIf( 'user_registration', $fld_registration );
195 $res = $this->select( __METHOD__ );
196 $count = 0;
197 $countDuplicates = 0;
198 $lastUser = false;
199 $result = $this->getResult();
200 foreach ( $res as $row ) {
201 $count++;
203 if ( $lastUser === $row->user_name ) {
204 // Duplicate row due to one of the needed subtable joins.
205 // Ignore it, but count the number of them to sanely handle
206 // miscalculation of $maxDuplicateRows.
207 $countDuplicates++;
208 if ( $countDuplicates == $maxDuplicateRows ) {
209 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
211 continue;
214 $countDuplicates = 0;
215 $lastUser = $row->user_name;
217 if ( $count > $limit ) {
218 // We've reached the one extra which shows that there are
219 // additional pages to be had. Stop here...
220 $this->setContinueEnumParameter( 'from', $row->user_name );
221 break;
224 if ( $count == $sqlLimit ) {
225 // Should never hit this (either the $countDuplicates check or
226 // the $count > $limit check should hit first), but check it
227 // anyway just in case.
228 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
231 if ( $params['activeusers'] && $row->recentactions === 0 ) {
232 // activeusers cache was out of date
233 continue;
236 $data = [
237 'userid' => (int)$row->user_id,
238 'name' => $row->user_name,
241 if ( $fld_centralids ) {
242 $data += ApiQueryUserInfo::getCentralUserInfo(
243 $this->getConfig(), User::newFromId( $row->user_id ), $params['attachedwiki']
247 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
248 $data['blockid'] = (int)$row->ipb_id;
249 $data['blockedby'] = $row->ipb_by_text;
250 $data['blockedbyid'] = (int)$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;
255 if ( $row->ipb_deleted ) {
256 $data['hidden'] = true;
258 if ( $fld_editcount ) {
259 $data['editcount'] = intval( $row->user_editcount );
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'];
266 if ( $fld_registration ) {
267 $data['registration'] = $row->user_registration ?
268 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
271 if ( $fld_implicitgroups || $fld_groups || $fld_rights ) {
272 $implicitGroups = User::newFromId( $row->user_id )->getAutomaticGroups();
273 if ( isset( $row->groups ) && $row->groups !== '' ) {
274 $groups = array_merge( $implicitGroups, explode( '|', $row->groups ) );
275 } else {
276 $groups = $implicitGroups;
279 if ( $fld_groups ) {
280 $data['groups'] = $groups;
281 ApiResult::setIndexedTagName( $data['groups'], 'g' );
282 ApiResult::setArrayType( $data['groups'], 'array' );
285 if ( $fld_implicitgroups ) {
286 $data['implicitgroups'] = $implicitGroups;
287 ApiResult::setIndexedTagName( $data['implicitgroups'], 'g' );
288 ApiResult::setArrayType( $data['implicitgroups'], 'array' );
291 if ( $fld_rights ) {
292 $data['rights'] = User::getGroupPermissions( $groups );
293 ApiResult::setIndexedTagName( $data['rights'], 'r' );
294 ApiResult::setArrayType( $data['rights'], 'array' );
298 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
299 if ( !$fit ) {
300 $this->setContinueEnumParameter( 'from', $data['name'] );
301 break;
305 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'u' );
308 public function getCacheMode( $params ) {
309 return 'anon-public-user-private';
312 public function getAllowedParams() {
313 $userGroups = User::getAllGroups();
315 return [
316 'from' => null,
317 'to' => null,
318 'prefix' => null,
319 'dir' => [
320 ApiBase::PARAM_DFLT => 'ascending',
321 ApiBase::PARAM_TYPE => [
322 'ascending',
323 'descending'
326 'group' => [
327 ApiBase::PARAM_TYPE => $userGroups,
328 ApiBase::PARAM_ISMULTI => true,
330 'excludegroup' => [
331 ApiBase::PARAM_TYPE => $userGroups,
332 ApiBase::PARAM_ISMULTI => true,
334 'rights' => [
335 ApiBase::PARAM_TYPE => User::getAllRights(),
336 ApiBase::PARAM_ISMULTI => true,
338 'prop' => [
339 ApiBase::PARAM_ISMULTI => true,
340 ApiBase::PARAM_TYPE => [
341 'blockinfo',
342 'groups',
343 'implicitgroups',
344 'rights',
345 'editcount',
346 'registration',
347 'centralids',
349 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
351 'limit' => [
352 ApiBase::PARAM_DFLT => 10,
353 ApiBase::PARAM_TYPE => 'limit',
354 ApiBase::PARAM_MIN => 1,
355 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
356 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
358 'witheditsonly' => false,
359 'activeusers' => [
360 ApiBase::PARAM_DFLT => false,
361 ApiBase::PARAM_HELP_MSG => [
362 'apihelp-query+allusers-param-activeusers',
363 $this->getConfig()->get( 'ActiveUserDays' )
366 'attachedwiki' => null,
370 protected function getExamplesMessages() {
371 return [
372 'action=query&list=allusers&aufrom=Y'
373 => 'apihelp-query+allusers-example-Y',
377 public function getHelpUrls() {
378 return 'https://www.mediawiki.org/wiki/API:Allusers';