Move a few braces noticed while doing CR
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
1 <?php
2
3 /**
4 * Created on July 7, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to enumerate all registered users.
33 *
34 * @ingroup API
35 */
36 class ApiQueryAllUsers extends ApiQueryBase {
37
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_registration = isset( $prop['registration'] );
53 } else {
54 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = false;
55 }
56
57 $limit = $params['limit'];
58 $this->addTables( 'user', 'u1' );
59 $useIndex = true;
60
61 if ( !is_null( $params['from'] ) ) {
62 $this->addWhere( 'u1.user_name >= ' . $db->addQuotes( $this->keyToTitle( $params['from'] ) ) );
63 }
64
65 if ( !is_null( $params['prefix'] ) ) {
66 $this->addWhere( 'u1.user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
67 }
68
69 if ( !is_null( $params['group'] ) ) {
70 $useIndex = false;
71 // Filter only users that belong to a given group
72 $this->addTables( 'user_groups', 'ug1' );
73 $ug1 = $this->getAliasedName( 'user_groups', 'ug1' );
74 $this->addJoinConds( array( $ug1 => array( 'INNER JOIN', array( 'ug1.ug_user=u1.user_id',
75 'ug1.ug_group' => $params['group'] ) ) ) );
76 }
77
78 if ( $params['witheditsonly'] ) {
79 $this->addWhere( 'u1.user_editcount > 0' );
80 }
81
82 if ( $fld_groups ) {
83 // Show the groups the given users belong to
84 // request more than needed to avoid not getting all rows that belong to one user
85 $groupCount = count( User::getAllGroups() );
86 $sqlLimit = $limit + $groupCount + 1;
87
88 $this->addTables( 'user_groups', 'ug2' );
89 $tname = $this->getAliasedName( 'user_groups', 'ug2' );
90 $this->addJoinConds( array( $tname => array( 'LEFT JOIN', 'ug2.ug_user=u1.user_id' ) ) );
91 $this->addFields( 'ug2.ug_group ug_group2' );
92 } else {
93 $sqlLimit = $limit + 1;
94 }
95 if ( $fld_blockinfo ) {
96 $this->addTables( 'ipblocks' );
97 $this->addTables( 'user', 'u2' );
98 $u2 = $this->getAliasedName( 'user', 'u2' );
99 $this->addJoinConds( array(
100 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
101 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
102 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
103 }
104
105 $this->addOption( 'LIMIT', $sqlLimit );
106
107 $this->addFields( 'u1.user_name' );
108 $this->addFieldsIf( 'u1.user_editcount', $fld_editcount );
109 $this->addFieldsIf( 'u1.user_registration', $fld_registration );
110
111 $this->addOption( 'ORDER BY', 'u1.user_name' );
112 if ( $useIndex ) {
113 $u1 = $this->getAliasedName( 'user', 'u1' );
114 $this->addOption( 'USE INDEX', array( $u1 => 'user_name' ) );
115 }
116
117 $res = $this->select( __METHOD__ );
118
119 $count = 0;
120 $lastUserData = false;
121 $lastUser = false;
122 $result = $this->getResult();
123
124 //
125 // This loop keeps track of the last entry.
126 // For each new row, if the new row is for different user then the last, the last entry is added to results.
127 // Otherwise, the group of the new row is appended to the last entry.
128 // The setContinue... is more complex because of this, and takes into account the higher sql limit
129 // to make sure all rows that belong to the same user are received.
130
131 $row = $db->fetchObject( $res );
132 foreach ( $res as $row ) {
133 $count++;
134
135 if ( $lastUser !== $row->user_name ) {
136 // Save the last pass's user data
137 if ( is_array( $lastUserData ) ) {
138 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
139 null, $lastUserData );
140 if ( !$fit ) {
141 $this->setContinueEnumParameter( 'from',
142 $this->keyToTitle( $lastUserData['name'] ) );
143 break;
144 }
145 }
146
147 if ( $count > $limit ) {
148 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
149 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
150 break;
151 }
152
153 // Record new user's data
154 $lastUser = $row->user_name;
155 $lastUserData = array( 'name' => $lastUser );
156 if ( $fld_blockinfo ) {
157 $lastUserData['blockedby'] = $row->blocker_name;
158 $lastUserData['blockreason'] = $row->ipb_reason;
159 }
160 if ( $fld_editcount ) {
161 $lastUserData['editcount'] = intval( $row->user_editcount );
162 }
163 if ( $fld_registration ) {
164 $lastUserData['registration'] = $row->user_registration ?
165 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
166 }
167
168 }
169
170 if ( $sqlLimit == $count ) {
171 // BUG! database contains group name that User::getAllGroups() does not return
172 // TODO: should handle this more gracefully
173 ApiBase::dieDebug( __METHOD__,
174 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
175 }
176
177 // Add user's group info
178 if ( $fld_groups && !is_null( $row->ug_group2 ) ) {
179 $lastUserData['groups'][] = $row->ug_group2;
180 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
181 }
182 }
183
184 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
185 }
186
187 public function getCacheMode( $params ) {
188 return 'public';
189 }
190
191 public function getAllowedParams() {
192 return array(
193 'from' => null,
194 'prefix' => null,
195 'group' => array(
196 ApiBase::PARAM_TYPE => User::getAllGroups()
197 ),
198 'prop' => array(
199 ApiBase::PARAM_ISMULTI => true,
200 ApiBase::PARAM_TYPE => array(
201 'blockinfo',
202 'groups',
203 'editcount',
204 'registration'
205 )
206 ),
207 'limit' => array(
208 ApiBase::PARAM_DFLT => 10,
209 ApiBase::PARAM_TYPE => 'limit',
210 ApiBase::PARAM_MIN => 1,
211 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
212 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
213 ),
214 'witheditsonly' => false,
215 );
216 }
217
218 public function getParamDescription() {
219 return array(
220 'from' => 'The user name to start enumerating from',
221 'prefix' => 'Search for all page titles that begin with this value',
222 'group' => 'Limit users to a given group name',
223 'prop' => array(
224 'What pieces of information to include.',
225 ' blockinfo - Adds the information about a current block on the user',
226 ' groups - Lists groups that the user is in',
227 ' editcount - Adds the edit count of the user',
228 ' registration - Adds the timestamp of when the user registered',
229 '`groups` property uses more server resources and may return fewer results than the limit' ),
230 'limit' => 'How many total user names to return',
231 'witheditsonly' => 'Only list users who have made edits',
232 );
233 }
234
235 public function getDescription() {
236 return 'Enumerate all registered users';
237 }
238
239 protected function getExamples() {
240 return array(
241 'api.php?action=query&list=allusers&aufrom=Y',
242 );
243 }
244
245 public function getVersion() {
246 return __CLASS__ . ': $Id$';
247 }
248 }