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