(bug 14022) Added usprop=registration and auprop=blockinfo
[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 (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 * @addtogroup 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_editcount = isset($prop['editcount']);
50 $fld_groups = isset($prop['groups']);
51 $fld_registration = isset($prop['registration']);
52 $fld_blockinfo = isset($prop['blockinfo']);
53 } else {
54 $fld_editcount = $fld_groups = $fld_registration = $fld_blockinfo = false;
55 }
56
57 $limit = $params['limit'];
58
59 if( !is_null( $params['from'] ) )
60 $this->addWhere( 'u1.user_name >= ' . $db->addQuotes( self::keyToTitle( $params['from'] ) ) );
61
62 if( isset( $params['prefix'] ) )
63 $this->addWhere( 'u1.user_name LIKE "' . $db->escapeLike( self::keyToTitle( $params['prefix'] ) ) . '%"' );
64
65 $join = false;
66 $tables = array('user');
67 $types = array();
68 $conds = array();
69 $aliases = array('u1');
70 if (!is_null($params['group'])) {
71 // Filter only users that belong to a given group
72 $this->addTables('user_groups', 'ug1');
73 $this->addWhere('ug1.ug_user=u1.user_id');
74 $this->addWhereFld('ug1.ug_group', $params['group']);
75 }
76 if ($fld_groups) {
77 // Show the groups the given users belong to
78 // request more than needed to avoid not getting all rows that belong to one user
79 $groupCount = count(User::getAllGroups());
80 $sqlLimit = $limit+$groupCount+1;
81
82 $join = true;
83 $tables[] = 'user_groups';
84 $types[] = ApiQueryBase::LEFT_JOIN;
85 $conds[] = 'ug2.ug_user=u1.user_id';
86 $aliases[] = 'ug2';
87 $this->addFields('ug2.ug_group ug_group2');
88 } else {
89 $sqlLimit = $limit+1;
90 }
91 if ($fld_blockinfo) {
92 $join = true;
93 $tables[] = 'ipblocks';
94 $types[] = ApiQueryBase::LEFT_JOIN;
95 $conds[] = 'ipb_user=u1.user_id';
96 $aliases[] = null;
97
98 $tables[] = 'user';
99 $types[] = ApiQueryBase::LEFT_JOIN;
100 $conds[] = 'ipb_by=u2.user_id';
101 $aliases[] = 'u2';
102 $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
103 }
104
105 if ($join) {
106 $this->addJoin($tables, $types, $conds, $aliases);
107 } else {
108 $this->addTables('user', 'u1');
109 }
110
111 $this->addOption('LIMIT', $sqlLimit);
112
113 $this->addFields('u1.user_name');
114 $this->addFieldsIf('u1.user_editcount', $fld_editcount);
115 $this->addFieldsIf('u1.user_registration', $fld_registration);
116
117 $this->addOption('ORDER BY', 'u1.user_name');
118
119 $res = $this->select(__METHOD__);
120
121 $data = array ();
122 $count = 0;
123 $lastUserData = false;
124 $lastUser = false;
125 $result = $this->getResult();
126
127 //
128 // This loop keeps track of the last entry.
129 // For each new row, if the new row is for different user then the last, the last entry is added to results.
130 // Otherwise, the group of the new row is appended to the last entry.
131 // The setContinue... is more complex because of this, and takes into account the higher sql limit
132 // to make sure all rows that belong to the same user are received.
133 //
134 while (true) {
135
136 $row = $db->fetchObject($res);
137 $count++;
138
139 if (!$row || $lastUser != $row->user_name) {
140 // Save the last pass's user data
141 if (is_array($lastUserData))
142 $data[] = $lastUserData;
143
144 // No more rows left
145 if (!$row)
146 break;
147
148 if ($count > $limit) {
149 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
150 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->user_name));
151 break;
152 }
153
154 // Record new user's data
155 $lastUser = $row->user_name;
156 $lastUserData = array( 'name' => $lastUser );
157 if ($fld_editcount)
158 $lastUserData['editcount'] = intval($row->user_editcount);
159 if ($fld_registration)
160 $lastUserData['registration'] = wfTimestamp(TS_ISO_8601, $row->user_registration);
161 if ($fld_blockinfo && !is_null($row->blocker_name)) {
162 $lastUserData['blockedby'] = $row->blocker_name;
163 $lastUserData['blockreason'] = $row->ipb_reason;
164 }
165
166 }
167
168 if ($sqlLimit == $count) {
169 // BUG! database contains group name that User::getAllGroups() does not return
170 // TODO: should handle this more gracefully
171 ApiBase :: dieDebug(__METHOD__,
172 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function');
173 }
174
175 // Add user's group info
176 if ($fld_groups && !is_null($row->ug_group2)) {
177 $lastUserData['groups'][] = $row->ug_group2;
178 $result->setIndexedTagName($lastUserData['groups'], 'g');
179 }
180 }
181
182 $db->freeResult($res);
183
184 $result->setIndexedTagName($data, 'u');
185 $result->addValue('query', $this->getModuleName(), $data);
186 }
187
188 public function getAllowedParams() {
189 return array (
190 'from' => null,
191 'prefix' => null,
192 'group' => array(
193 ApiBase :: PARAM_TYPE => User::getAllGroups()
194 ),
195 'prop' => array (
196 ApiBase :: PARAM_ISMULTI => true,
197 ApiBase :: PARAM_TYPE => array (
198 'editcount',
199 'groups',
200 'registration',
201 'blockinfo'
202 )
203 ),
204 'limit' => array (
205 ApiBase :: PARAM_DFLT => 10,
206 ApiBase :: PARAM_TYPE => 'limit',
207 ApiBase :: PARAM_MIN => 1,
208 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
209 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
210 )
211 );
212 }
213
214 public function getParamDescription() {
215 return array (
216 'from' => 'The user name to start enumerating from.',
217 'prefix' => 'Search for all page titles that begin with this value.',
218 'group' => 'Limit users to a given group name',
219 'prop' => array(
220 'What pieces of information to include.',
221 '`groups` property uses more server resources and may return fewer results than the limit.'),
222 'limit' => 'How many total user names to return.',
223 );
224 }
225
226 public function getDescription() {
227 return 'Enumerate all registered users';
228 }
229
230 protected function getExamples() {
231 return array (
232 'api.php?action=query&list=allusers&aufrom=Y',
233 );
234 }
235
236 public function getVersion() {
237 return __CLASS__ . ': $Id$';
238 }
239 }