00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00036 class ApiQueryAllUsers extends ApiQueryBase {
00037
00038 public function __construct($query, $moduleName) {
00039 parent :: __construct($query, $moduleName, 'au');
00040 }
00041
00042 public function execute() {
00043 $db = $this->getDB();
00044 $params = $this->extractRequestParams();
00045
00046 $prop = $params['prop'];
00047 if (!is_null($prop)) {
00048 $prop = array_flip($prop);
00049 $fld_blockinfo = isset($prop['blockinfo']);
00050 $fld_editcount = isset($prop['editcount']);
00051 $fld_groups = isset($prop['groups']);
00052 $fld_registration = isset($prop['registration']);
00053 } else {
00054 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = false;
00055 }
00056
00057 $limit = $params['limit'];
00058 $this->addTables('user', 'u1');
00059
00060 if (!is_null($params['from']))
00061 $this->addWhere('u1.user_name >= ' . $db->addQuotes($this->keyToTitle($params['from'])));
00062
00063 if (!is_null($params['prefix']))
00064 $this->addWhere('u1.user_name LIKE "' . $db->escapeLike($this->keyToTitle( $params['prefix'])) . '%"');
00065
00066 if (!is_null($params['group'])) {
00067
00068 $this->addTables('user_groups', 'ug1');
00069 $this->addWhere('ug1.ug_user=u1.user_id');
00070 $this->addWhereFld('ug1.ug_group', $params['group']);
00071 }
00072
00073 if ($params['witheditsonly'])
00074 $this->addWhere('user_editcount > 0');
00075
00076 if ($fld_groups) {
00077
00078
00079 $groupCount = count(User::getAllGroups());
00080 $sqlLimit = $limit+$groupCount+1;
00081
00082 $this->addTables('user_groups', 'ug2');
00083 $tname = $this->getAliasedName('user_groups', 'ug2');
00084 $this->addJoinConds(array($tname => array('LEFT JOIN', 'ug2.ug_user=u1.user_id')));
00085 $this->addFields('ug2.ug_group ug_group2');
00086 } else {
00087 $sqlLimit = $limit+1;
00088 }
00089 if ($fld_blockinfo) {
00090 $this->addTables('ipblocks');
00091 $this->addTables('user', 'u2');
00092 $u2 = $this->getAliasedName('user', 'u2');
00093 $this->addJoinConds(array(
00094 'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
00095 $u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
00096 $this->addFields(array('ipb_reason', 'u2.user_name blocker_name'));
00097 }
00098
00099 $this->addOption('LIMIT', $sqlLimit);
00100
00101 $this->addFields('u1.user_name');
00102 $this->addFieldsIf('u1.user_editcount', $fld_editcount);
00103 $this->addFieldsIf('u1.user_registration', $fld_registration);
00104
00105 $this->addOption('ORDER BY', 'u1.user_name');
00106
00107 $res = $this->select(__METHOD__);
00108
00109 $data = array ();
00110 $count = 0;
00111 $lastUserData = false;
00112 $lastUser = false;
00113 $result = $this->getResult();
00114
00115
00116
00117
00118
00119
00120
00121
00122 while (true) {
00123
00124 $row = $db->fetchObject($res);
00125 $count++;
00126
00127 if (!$row || $lastUser !== $row->user_name) {
00128
00129 if (is_array($lastUserData))
00130 {
00131 $fit = $result->addValue(array('query', $this->getModuleName()),
00132 null, $lastUserData);
00133 if(!$fit)
00134 {
00135 $this->setContinueEnumParameter('from',
00136 $this->keyToTitle($lastUserData['name']));
00137 break;
00138 }
00139 }
00140
00141
00142 if (!$row)
00143 break;
00144
00145 if ($count > $limit) {
00146
00147 $this->setContinueEnumParameter('from', $this->keyToTitle($row->user_name));
00148 break;
00149 }
00150
00151
00152 $lastUser = $row->user_name;
00153 $lastUserData = array( 'name' => $lastUser );
00154 if ($fld_blockinfo) {
00155 $lastUserData['blockedby'] = $row->blocker_name;
00156 $lastUserData['blockreason'] = $row->ipb_reason;
00157 }
00158 if ($fld_editcount)
00159 $lastUserData['editcount'] = intval($row->user_editcount);
00160 if ($fld_registration)
00161 $lastUserData['registration'] = wfTimestamp(TS_ISO_8601, $row->user_registration);
00162
00163 }
00164
00165 if ($sqlLimit == $count) {
00166
00167
00168 ApiBase :: dieDebug(__METHOD__,
00169 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function');
00170 }
00171
00172
00173 if ($fld_groups && !is_null($row->ug_group2)) {
00174 $lastUserData['groups'][] = $row->ug_group2;
00175 $result->setIndexedTagName($lastUserData['groups'], 'g');
00176 }
00177 }
00178
00179 $db->freeResult($res);
00180
00181 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'u');
00182 }
00183
00184 public function getAllowedParams() {
00185 return array (
00186 'from' => null,
00187 'prefix' => null,
00188 'group' => array(
00189 ApiBase :: PARAM_TYPE => User::getAllGroups()
00190 ),
00191 'prop' => array (
00192 ApiBase :: PARAM_ISMULTI => true,
00193 ApiBase :: PARAM_TYPE => array (
00194 'blockinfo',
00195 'groups',
00196 'editcount',
00197 'registration'
00198 )
00199 ),
00200 'limit' => array (
00201 ApiBase :: PARAM_DFLT => 10,
00202 ApiBase :: PARAM_TYPE => 'limit',
00203 ApiBase :: PARAM_MIN => 1,
00204 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00205 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00206 ),
00207 'witheditsonly' => false,
00208 );
00209 }
00210
00211 public function getParamDescription() {
00212 return array (
00213 'from' => 'The user name to start enumerating from.',
00214 'prefix' => 'Search for all page titles that begin with this value.',
00215 'group' => 'Limit users to a given group name',
00216 'prop' => array(
00217 'What pieces of information to include.',
00218 '`groups` property uses more server resources and may return fewer results than the limit.'),
00219 'limit' => 'How many total user names to return.',
00220 'witheditsonly' => 'Only list users who have made edits',
00221 );
00222 }
00223
00224 public function getDescription() {
00225 return 'Enumerate all registered users';
00226 }
00227
00228 protected function getExamples() {
00229 return array (
00230 'api.php?action=query&list=allusers&aufrom=Y',
00231 );
00232 }
00233
00234 public function getVersion() {
00235 return __CLASS__ . ': $Id: ApiQueryAllUsers.php 46845 2009-02-05 14:30:59Z catrope $';
00236 }
00237 }