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