Checking permissions for $wgUser while doing an edit with another user is not a good...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
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_registration = isset( $prop['registration'] );
54 } else {
55 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = 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['group'] ) ) {
74 $useIndex = false;
75 // Filter only users that belong to a given group
76 $this->addTables( 'user_groups', 'ug1' );
77 $ug1 = $this->getAliasedName( 'user_groups', 'ug1' );
78 $this->addJoinConds( array( $ug1 => array( 'INNER JOIN', array( 'ug1.ug_user=u1.user_id',
79 'ug1.ug_group' => $params['group'] ) ) ) );
80 }
81
82 if ( $params['witheditsonly'] ) {
83 $this->addWhere( 'u1.user_editcount > 0' );
84 }
85
86 if ( $fld_groups ) {
87 // Show the groups the given users belong to
88 // request more than needed to avoid not getting all rows that belong to one user
89 $groupCount = count( User::getAllGroups() );
90 $sqlLimit = $limit + $groupCount + 1;
91
92 $this->addTables( 'user_groups', 'ug2' );
93 $tname = $this->getAliasedName( 'user_groups', 'ug2' );
94 $this->addJoinConds( array( $tname => array( 'LEFT JOIN', 'ug2.ug_user=u1.user_id' ) ) );
95 $this->addFields( 'ug2.ug_group ug_group2' );
96 } else {
97 $sqlLimit = $limit + 1;
98 }
99 if ( $fld_blockinfo ) {
100 $this->addTables( 'ipblocks' );
101 $this->addTables( 'user', 'u2' );
102 $u2 = $this->getAliasedName( 'user', 'u2' );
103 $this->addJoinConds( array(
104 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
105 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
106 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
107 }
108
109 $this->addOption( 'LIMIT', $sqlLimit );
110
111 $this->addFields( array(
112 'u1.user_name',
113 'u1.user_id'
114 ) );
115 $this->addFieldsIf( 'u1.user_editcount', $fld_editcount );
116 $this->addFieldsIf( 'u1.user_registration', $fld_registration );
117
118 $this->addOption( 'ORDER BY', 'u1.user_name' );
119 if ( $useIndex ) {
120 $u1 = $this->getAliasedName( 'user', 'u1' );
121 $this->addOption( 'USE INDEX', array( $u1 => 'user_name' ) );
122 }
123
124 $res = $this->select( __METHOD__ );
125
126 $count = 0;
127 $lastUserData = false;
128 $lastUser = false;
129 $result = $this->getResult();
130
131 //
132 // This loop keeps track of the last entry.
133 // For each new row, if the new row is for different user then the last, the last entry is added to results.
134 // Otherwise, the group of the new row is appended to the last entry.
135 // The setContinue... is more complex because of this, and takes into account the higher sql limit
136 // to make sure all rows that belong to the same user are received.
137
138 foreach ( $res as $row ) {
139 $count++;
140
141 if ( $lastUser !== $row->user_name ) {
142 // Save the last pass's user data
143 if ( is_array( $lastUserData ) ) {
144 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
145 null, $lastUserData );
146 if ( !$fit ) {
147 $this->setContinueEnumParameter( 'from',
148 $this->keyToTitle( $lastUserData['name'] ) );
149 break;
150 }
151 }
152
153 if ( $count > $limit ) {
154 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
155 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
156 break;
157 }
158
159 // Record new user's data
160 $lastUser = $row->user_name;
161 $lastUserData = array(
162 'name' => $lastUser,
163 'userid' => $row->user_id,
164 );
165 if ( $fld_blockinfo && !is_null( $row->blocker_name ) ) {
166 $lastUserData['blockedby'] = $row->blocker_name;
167 $lastUserData['blockreason'] = $row->ipb_reason;
168 }
169 if ( $fld_editcount ) {
170 $lastUserData['editcount'] = intval( $row->user_editcount );
171 }
172 if ( $fld_registration ) {
173 $lastUserData['registration'] = $row->user_registration ?
174 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
175 }
176
177 }
178
179 if ( $sqlLimit == $count ) {
180 // BUG! database contains group name that User::getAllGroups() does not return
181 // TODO: should handle this more gracefully
182 ApiBase::dieDebug( __METHOD__,
183 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
184 }
185
186 // Add user's group info
187 if ( $fld_groups && !is_null( $row->ug_group2 ) ) {
188 $lastUserData['groups'][] = $row->ug_group2;
189 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
190 }
191 }
192
193 if ( is_array( $lastUserData ) ) {
194 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
195 null, $lastUserData );
196 if ( !$fit ) {
197 $this->setContinueEnumParameter( 'from',
198 $this->keyToTitle( $lastUserData['name'] ) );
199 }
200 }
201
202 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
203 }
204
205 public function getCacheMode( $params ) {
206 return 'public';
207 }
208
209 public function getAllowedParams() {
210 return array(
211 'from' => null,
212 'to' => null,
213 'prefix' => null,
214 'group' => array(
215 ApiBase::PARAM_TYPE => User::getAllGroups()
216 ),
217 'prop' => array(
218 ApiBase::PARAM_ISMULTI => true,
219 ApiBase::PARAM_TYPE => array(
220 'blockinfo',
221 'groups',
222 'editcount',
223 'registration'
224 )
225 ),
226 'limit' => array(
227 ApiBase::PARAM_DFLT => 10,
228 ApiBase::PARAM_TYPE => 'limit',
229 ApiBase::PARAM_MIN => 1,
230 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
231 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
232 ),
233 'witheditsonly' => false,
234 );
235 }
236
237 public function getParamDescription() {
238 return array(
239 'from' => 'The user name to start enumerating from',
240 'to' => 'The user name to stop enumerating at',
241 'prefix' => 'Search for all users that begin with this value',
242 'group' => 'Limit users to a given group name',
243 'prop' => array(
244 'What pieces of information to include.',
245 ' blockinfo - Adds the information about a current block on the user',
246 ' groups - Lists groups that the user is in',
247 ' editcount - Adds the edit count of the user',
248 ' registration - Adds the timestamp of when the user registered',
249 '`groups` property uses more server resources and may return fewer results than the limit' ),
250 'limit' => 'How many total user names to return',
251 'witheditsonly' => 'Only list users who have made edits',
252 );
253 }
254
255 public function getDescription() {
256 return 'Enumerate all registered users';
257 }
258
259 protected function getExamples() {
260 return array(
261 'api.php?action=query&list=allusers&aufrom=Y',
262 );
263 }
264
265 public function getVersion() {
266 return __CLASS__ . ': $Id$';
267 }
268 }