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