Followup r85884
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate all registered users.
34 *
35 * @ingroup API
36 */
37 class ApiQueryAllUsers extends ApiQueryBase {
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'au' );
40 }
41
42 public function execute() {
43 $db = $this->getDB();
44 $params = $this->extractRequestParams();
45
46 $prop = $params['prop'];
47 if ( !is_null( $prop ) ) {
48 $prop = array_flip( $prop );
49 $fld_blockinfo = isset( $prop['blockinfo'] );
50 $fld_editcount = isset( $prop['editcount'] );
51 $fld_groups = isset( $prop['groups'] );
52 $fld_rights = isset( $prop['rights'] );
53 $fld_registration = isset( $prop['registration'] );
54 } else {
55 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = false;
56 }
57
58 $limit = $params['limit'];
59
60 $this->addTables( 'user' );
61 $useIndex = true;
62
63 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
64 $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
65 $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
66
67 $this->addWhereRange( 'user_name', $dir, $from, $to );
68
69 if ( !is_null( $params['prefix'] ) ) {
70 $this->addWhere( 'user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
71 }
72
73 if ( !is_null( $params['rights'] ) ) {
74 $groups = array();
75 foreach( $params['rights'] as $r ) {
76 $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
77 }
78
79 $groups = array_diff( array_unique( $groups ), User::getImplicitGroups() );
80
81 if ( is_null( $params['group'] ) ) {
82 $params['group'] = $groups;
83 } else {
84 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
85 }
86 }
87
88 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
89 $useIndex = false;
90 // Filter only users that belong to a given group
91 $this->addTables( 'user_groups', 'ug1' );
92 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
93 'ug1.ug_group' => $params['group'] ) ) ) );
94 }
95
96 if ( $params['witheditsonly'] ) {
97 $this->addWhere( 'user_editcount > 0' );
98 }
99
100 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
101
102 if ( $fld_groups || $fld_rights ) {
103 // Show the groups the given users belong to
104 // request more than needed to avoid not getting all rows that belong to one user
105 $groupCount = count( User::getAllGroups() );
106 $sqlLimit = $limit + $groupCount + 1;
107
108 $this->addTables( 'user_groups', 'ug2' );
109 $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
110 $this->addFields( 'ug2.ug_group ug_group2' );
111 } else {
112 $sqlLimit = $limit + 1;
113 }
114
115 if ( $params['activeusers'] ) {
116 global $wgActiveUserDays;
117 $this->addTables( 'recentchanges' );
118
119 $this->addJoinConds( array( 'recentchanges' => array(
120 'INNER JOIN', 'rc_user_text=user_name'
121 ) ) );
122
123 $this->addFields( 'COUNT(*) AS recentedits' );
124
125 $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
126 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 );
127 $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
128
129 $this->addOption( 'GROUP BY', 'user_name' );
130 }
131
132 $this->addOption( 'LIMIT', $sqlLimit );
133
134 $this->addFields( array(
135 'user_name',
136 'user_id'
137 ) );
138 $this->addFieldsIf( 'user_editcount', $fld_editcount );
139 $this->addFieldsIf( 'user_registration', $fld_registration );
140
141 if ( $useIndex ) {
142 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
143 }
144
145 $res = $this->select( __METHOD__ );
146
147 $count = 0;
148 $lastUserData = false;
149 $lastUser = false;
150 $result = $this->getResult();
151
152 //
153 // This loop keeps track of the last entry.
154 // For each new row, if the new row is for different user then the last, the last entry is added to results.
155 // Otherwise, the group of the new row is appended to the last entry.
156 // The setContinue... is more complex because of this, and takes into account the higher sql limit
157 // to make sure all rows that belong to the same user are received.
158
159 foreach ( $res as $row ) {
160 $count++;
161
162 if ( $lastUser !== $row->user_name ) {
163 // Save the last pass's user data
164 if ( is_array( $lastUserData ) ) {
165 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
166 null, $lastUserData );
167
168 $lastUserData = null;
169
170 if ( !$fit ) {
171 $this->setContinueEnumParameter( 'from',
172 $this->keyToTitle( $lastUserData['name'] ) );
173 break;
174 }
175 }
176
177 if ( $count > $limit ) {
178 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
179 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
180 break;
181 }
182
183 // Record new user's data
184 $lastUser = $row->user_name;
185 $lastUserData = array(
186 'userid' => $row->user_id,
187 'name' => $lastUser,
188 );
189 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
190 $lastUserData['blockedby'] = $row->ipb_by_text;
191 $lastUserData['blockreason'] = $row->ipb_reason;
192 $lastUserData['blockexpiry'] = $row->ipb_expiry;
193 }
194 if ( $row->ipb_deleted ) {
195 $lastUserData['hidden'] = '';
196 }
197 if ( $fld_editcount ) {
198 $lastUserData['editcount'] = intval( $row->user_editcount );
199 }
200 if ( $params['activeusers'] ) {
201 $lastUserData['recenteditcount'] = intval( $row->recentedits );
202 }
203 if ( $fld_registration ) {
204 $lastUserData['registration'] = $row->user_registration ?
205 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
206 }
207 }
208
209 if ( $sqlLimit == $count ) {
210 // BUG! database contains group name that User::getAllGroups() does not return
211 // TODO: should handle this more gracefully
212 ApiBase::dieDebug( __METHOD__,
213 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
214 }
215
216 // Add user's group info
217 if ( $fld_groups ) {
218 if ( !isset( $lastUserData['groups'] ) ) {
219 $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( User::newFromName( $lastUser ) );
220 }
221
222 if ( !is_null( $row->ug_group2 ) ) {
223 $lastUserData['groups'][] = $row->ug_group2;
224 }
225 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
226 }
227
228 if ( $fld_rights ) {
229 if ( !isset( $lastUserData['rights'] ) ) {
230 $lastUserData['rights'] = User::getGroupPermissions( User::getImplicitGroups() );
231 }
232 if ( !is_null( $row->ug_group2 ) ) {
233 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
234 User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
235 }
236 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
237 }
238 }
239
240 if ( is_array( $lastUserData ) ) {
241 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
242 null, $lastUserData );
243 if ( !$fit ) {
244 $this->setContinueEnumParameter( 'from',
245 $this->keyToTitle( $lastUserData['name'] ) );
246 }
247 }
248
249 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
250 }
251
252 public function getCacheMode( $params ) {
253 return 'anon-public-user-private';
254 }
255
256 public function getAllowedParams() {
257 return array(
258 'from' => null,
259 'to' => null,
260 'prefix' => null,
261 'dir' => array(
262 ApiBase::PARAM_DFLT => 'ascending',
263 ApiBase::PARAM_TYPE => array(
264 'ascending',
265 'descending'
266 ),
267 ),
268 'group' => array(
269 ApiBase::PARAM_TYPE => User::getAllGroups(),
270 ApiBase::PARAM_ISMULTI => true,
271 ),
272 'rights' => array(
273 ApiBase::PARAM_TYPE => User::getAllRights(),
274 ApiBase::PARAM_ISMULTI => true,
275 ),
276 'prop' => array(
277 ApiBase::PARAM_ISMULTI => true,
278 ApiBase::PARAM_TYPE => array(
279 'blockinfo',
280 'groups',
281 'rights',
282 'editcount',
283 'registration'
284 )
285 ),
286 'limit' => array(
287 ApiBase::PARAM_DFLT => 10,
288 ApiBase::PARAM_TYPE => 'limit',
289 ApiBase::PARAM_MIN => 1,
290 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
291 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
292 ),
293 'witheditsonly' => false,
294 'activeusers' => false,
295 );
296 }
297
298 public function getParamDescription() {
299 global $wgActiveUserDays;
300 return array(
301 'from' => 'The user name to start enumerating from',
302 'to' => 'The user name to stop enumerating at',
303 'prefix' => 'Search for all users that begin with this value',
304 'dir' => 'Direction to sort in',
305 'group' => 'Limit users to given group name(s)',
306 'rights' => 'Limit users to given right(s)',
307 'prop' => array(
308 'What pieces of information to include.',
309 ' blockinfo - Adds the information about a current block on the user',
310 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
311 ' rights - Lists rights that the user has',
312 ' editcount - Adds the edit count of the user',
313 ' registration - Adds the timestamp of when the user registered',
314 ),
315 'limit' => 'How many total user names to return',
316 'witheditsonly' => 'Only list users who have made edits',
317 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
318 );
319 }
320
321 public function getDescription() {
322 return 'Enumerate all registered users';
323 }
324
325 protected function getExamples() {
326 return array(
327 'api.php?action=query&list=allusers&aufrom=Y',
328 );
329 }
330
331 public function getVersion() {
332 return __CLASS__ . ': $Id$';
333 }
334 }