Reverted r42528. Links with href="#" make firefox scroll to the top of the page,...
[mediawiki.git] / includes / api / ApiQueryAllUsers.php
blob220327d9c6de569e4d459d2a562117de2300fd51
1 <?php
3 /*
4 * Created on July 7, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
31 /**
32 * Query module to enumerate all registered users.
34 * @ingroup API
36 class ApiQueryAllUsers extends ApiQueryBase {
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'au');
42 public function execute() {
43 $db = $this->getDB();
44 $params = $this->extractRequestParams();
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;
57 $limit = $params['limit'];
58 $this->addTables('user', 'u1');
60 if( !is_null( $params['from'] ) )
61 $this->addWhere( 'u1.user_name >= ' . $db->addQuotes( $this->keyToTitle( $params['from'] ) ) );
63 if( isset( $params['prefix'] ) )
64 $this->addWhere( 'u1.user_name LIKE "' . $db->escapeLike( $this->keyToTitle( $params['prefix'] ) ) . '%"' );
66 if (!is_null($params['group'])) {
67 // Filter only users that belong to a given group
68 $this->addTables('user_groups', 'ug1');
69 $this->addWhere('ug1.ug_user=u1.user_id');
70 $this->addWhereFld('ug1.ug_group', $params['group']);
73 if ($fld_groups) {
74 // Show the groups the given users belong to
75 // request more than needed to avoid not getting all rows that belong to one user
76 $groupCount = count(User::getAllGroups());
77 $sqlLimit = $limit+$groupCount+1;
79 $this->addTables('user_groups', 'ug2');
80 $tname = $this->getAliasedName('user_groups', 'ug2');
81 $this->addJoinConds(array($tname => array('LEFT JOIN', 'ug2.ug_user=u1.user_id')));
82 $this->addFields('ug2.ug_group ug_group2');
83 } else {
84 $sqlLimit = $limit+1;
86 if ($fld_blockinfo) {
87 $this->addTables('ipblocks');
88 $this->addTables('user', 'u2');
89 $u2 = $this->getAliasedName('user', 'u2');
90 $this->addJoinConds(array(
91 'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
92 $u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
93 $this->addFields(array('ipb_reason', 'u2.user_name blocker_name'));
96 $this->addOption('LIMIT', $sqlLimit);
98 $this->addFields('u1.user_name');
99 $this->addFieldsIf('u1.user_editcount', $fld_editcount);
100 $this->addFieldsIf('u1.user_registration', $fld_registration);
102 $this->addOption('ORDER BY', 'u1.user_name');
104 $res = $this->select(__METHOD__);
106 $data = array ();
107 $count = 0;
108 $lastUserData = false;
109 $lastUser = false;
110 $result = $this->getResult();
113 // This loop keeps track of the last entry.
114 // For each new row, if the new row is for different user then the last, the last entry is added to results.
115 // Otherwise, the group of the new row is appended to the last entry.
116 // The setContinue... is more complex because of this, and takes into account the higher sql limit
117 // to make sure all rows that belong to the same user are received.
119 while (true) {
121 $row = $db->fetchObject($res);
122 $count++;
124 if (!$row || $lastUser != $row->user_name) {
125 // Save the last pass's user data
126 if (is_array($lastUserData))
127 $data[] = $lastUserData;
129 // No more rows left
130 if (!$row)
131 break;
133 if ($count > $limit) {
134 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
135 $this->setContinueEnumParameter('from', $this->keyToTitle($row->user_name));
136 break;
139 // Record new user's data
140 $lastUser = $row->user_name;
141 $lastUserData = array( 'name' => $lastUser );
142 if ($fld_blockinfo) {
143 $lastUserData['blockedby'] = $row->blocker_name;
144 $lastUserData['blockreason'] = $row->ipb_reason;
146 if ($fld_editcount)
147 $lastUserData['editcount'] = intval($row->user_editcount);
148 if ($fld_registration)
149 $lastUserData['registration'] = wfTimestamp(TS_ISO_8601, $row->user_registration);
153 if ($sqlLimit == $count) {
154 // BUG! database contains group name that User::getAllGroups() does not return
155 // TODO: should handle this more gracefully
156 ApiBase :: dieDebug(__METHOD__,
157 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function');
160 // Add user's group info
161 if ($fld_groups && !is_null($row->ug_group2)) {
162 $lastUserData['groups'][] = $row->ug_group2;
163 $result->setIndexedTagName($lastUserData['groups'], 'g');
167 $db->freeResult($res);
169 $result->setIndexedTagName($data, 'u');
170 $result->addValue('query', $this->getModuleName(), $data);
173 public function getAllowedParams() {
174 return array (
175 'from' => null,
176 'prefix' => null,
177 'group' => array(
178 ApiBase :: PARAM_TYPE => User::getAllGroups()
180 'prop' => array (
181 ApiBase :: PARAM_ISMULTI => true,
182 ApiBase :: PARAM_TYPE => array (
183 'blockinfo',
184 'groups',
185 'editcount',
186 'registration'
189 'limit' => array (
190 ApiBase :: PARAM_DFLT => 10,
191 ApiBase :: PARAM_TYPE => 'limit',
192 ApiBase :: PARAM_MIN => 1,
193 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
194 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
199 public function getParamDescription() {
200 return array (
201 'from' => 'The user name to start enumerating from.',
202 'prefix' => 'Search for all page titles that begin with this value.',
203 'group' => 'Limit users to a given group name',
204 'prop' => array(
205 'What pieces of information to include.',
206 '`groups` property uses more server resources and may return fewer results than the limit.'),
207 'limit' => 'How many total user names to return.',
211 public function getDescription() {
212 return 'Enumerate all registered users';
215 protected function getExamples() {
216 return array (
217 'api.php?action=query&list=allusers&aufrom=Y',
221 public function getVersion() {
222 return __CLASS__ . ': $Id$';