2007-07-08 07:50:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Created on July 7, 2007
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
2007-07-30 08:09:15 +00:00
|
|
|
* Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
2007-07-08 07:50:56 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (!defined('MEDIAWIKI')) {
|
|
|
|
|
// Eclipse helper - will be ignored in production
|
|
|
|
|
require_once ('ApiQueryBase.php');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Query module to enumerate all registered users.
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-07-08 07:50:56 +00:00
|
|
|
*/
|
|
|
|
|
class ApiQueryAllUsers extends ApiQueryBase {
|
|
|
|
|
|
|
|
|
|
public function __construct($query, $moduleName) {
|
|
|
|
|
parent :: __construct($query, $moduleName, 'au');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$db = $this->getDB();
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
|
|
|
|
$prop = $params['prop'];
|
|
|
|
|
if (!is_null($prop)) {
|
|
|
|
|
$prop = array_flip($prop);
|
2008-05-10 10:49:26 +00:00
|
|
|
$fld_blockinfo = isset($prop['blockinfo']);
|
2007-07-08 07:50:56 +00:00
|
|
|
$fld_editcount = isset($prop['editcount']);
|
2007-07-30 08:09:15 +00:00
|
|
|
$fld_groups = isset($prop['groups']);
|
2007-10-26 03:48:58 +00:00
|
|
|
$fld_registration = isset($prop['registration']);
|
2008-05-10 10:49:26 +00:00
|
|
|
} else {
|
|
|
|
|
$fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = false;
|
2007-07-08 07:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
$limit = $params['limit'];
|
2008-05-10 10:49:26 +00:00
|
|
|
$this->addTables('user', 'u1');
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-08-17 13:01:35 +00:00
|
|
|
if( !is_null( $params['from'] ) )
|
2008-06-29 22:26:23 +00:00
|
|
|
$this->addWhere( 'u1.user_name >= ' . $db->addQuotes( $this->keyToTitle( $params['from'] ) ) );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-08-17 13:01:35 +00:00
|
|
|
if( isset( $params['prefix'] ) )
|
2008-06-29 22:26:23 +00:00
|
|
|
$this->addWhere( 'u1.user_name LIKE "' . $db->escapeLike( $this->keyToTitle( $params['prefix'] ) ) . '%"' );
|
2007-07-30 08:09:15 +00:00
|
|
|
|
|
|
|
|
if (!is_null($params['group'])) {
|
|
|
|
|
// Filter only users that belong to a given group
|
2008-05-10 10:49:26 +00:00
|
|
|
$this->addTables('user_groups', 'ug1');
|
|
|
|
|
$this->addWhere('ug1.ug_user=u1.user_id');
|
2007-07-30 08:09:15 +00:00
|
|
|
$this->addWhereFld('ug1.ug_group', $params['group']);
|
|
|
|
|
}
|
2008-05-09 18:00:15 +00:00
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
if ($fld_groups) {
|
|
|
|
|
// Show the groups the given users belong to
|
|
|
|
|
// request more than needed to avoid not getting all rows that belong to one user
|
|
|
|
|
$groupCount = count(User::getAllGroups());
|
|
|
|
|
$sqlLimit = $limit+$groupCount+1;
|
2008-05-09 18:00:15 +00:00
|
|
|
|
2008-05-10 10:49:26 +00:00
|
|
|
$this->addTables('user_groups', 'ug2');
|
|
|
|
|
$tname = $this->getAliasedName('user_groups', 'ug2');
|
|
|
|
|
$this->addJoinConds(array($tname => array('LEFT JOIN', 'ug2.ug_user=u1.user_id')));
|
2007-07-30 08:09:15 +00:00
|
|
|
$this->addFields('ug2.ug_group ug_group2');
|
|
|
|
|
} else {
|
|
|
|
|
$sqlLimit = $limit+1;
|
|
|
|
|
}
|
2008-05-10 10:49:26 +00:00
|
|
|
if ($fld_blockinfo) {
|
|
|
|
|
$this->addTables('ipblocks');
|
|
|
|
|
$this->addTables('user', 'u2');
|
|
|
|
|
$u2 = $this->getAliasedName('user', 'u2');
|
|
|
|
|
$this->addJoinConds(array(
|
|
|
|
|
'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
|
|
|
|
|
$u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
|
|
|
|
|
$this->addFields(array('ipb_reason', 'u2.user_name blocker_name'));
|
|
|
|
|
}
|
2007-07-30 08:09:15 +00:00
|
|
|
|
|
|
|
|
$this->addOption('LIMIT', $sqlLimit);
|
|
|
|
|
|
2008-05-10 10:49:26 +00:00
|
|
|
$this->addFields('u1.user_name');
|
|
|
|
|
$this->addFieldsIf('u1.user_editcount', $fld_editcount);
|
|
|
|
|
$this->addFieldsIf('u1.user_registration', $fld_registration);
|
2007-07-08 07:50:56 +00:00
|
|
|
|
2008-05-10 10:49:26 +00:00
|
|
|
$this->addOption('ORDER BY', 'u1.user_name');
|
2007-07-08 07:50:56 +00:00
|
|
|
|
|
|
|
|
$res = $this->select(__METHOD__);
|
|
|
|
|
|
|
|
|
|
$data = array ();
|
|
|
|
|
$count = 0;
|
2007-07-30 08:09:15 +00:00
|
|
|
$lastUserData = false;
|
|
|
|
|
$lastUser = false;
|
|
|
|
|
$result = $this->getResult();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
//
|
|
|
|
|
// This loop keeps track of the last entry.
|
|
|
|
|
// For each new row, if the new row is for different user then the last, the last entry is added to results.
|
|
|
|
|
// Otherwise, the group of the new row is appended to the last entry.
|
|
|
|
|
// The setContinue... is more complex because of this, and takes into account the higher sql limit
|
|
|
|
|
// to make sure all rows that belong to the same user are received.
|
|
|
|
|
//
|
|
|
|
|
while (true) {
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
$row = $db->fetchObject($res);
|
|
|
|
|
$count++;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-12-11 21:51:01 +00:00
|
|
|
if (!$row || $lastUser !== $row->user_name) {
|
2007-07-30 08:09:15 +00:00
|
|
|
// Save the last pass's user data
|
|
|
|
|
if (is_array($lastUserData))
|
|
|
|
|
$data[] = $lastUserData;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
// No more rows left
|
|
|
|
|
if (!$row)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if ($count > $limit) {
|
|
|
|
|
// We've reached the one extra which shows that there are additional pages to be had. Stop here...
|
2008-06-29 22:26:23 +00:00
|
|
|
$this->setContinueEnumParameter('from', $this->keyToTitle($row->user_name));
|
2007-07-30 08:09:15 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Record new user's data
|
|
|
|
|
$lastUser = $row->user_name;
|
|
|
|
|
$lastUserData = array( 'name' => $lastUser );
|
2008-05-10 10:49:26 +00:00
|
|
|
if ($fld_blockinfo) {
|
|
|
|
|
$lastUserData['blockedby'] = $row->blocker_name;
|
|
|
|
|
$lastUserData['blockreason'] = $row->ipb_reason;
|
|
|
|
|
}
|
2007-07-30 08:09:15 +00:00
|
|
|
if ($fld_editcount)
|
|
|
|
|
$lastUserData['editcount'] = intval($row->user_editcount);
|
2007-10-26 03:48:58 +00:00
|
|
|
if ($fld_registration)
|
|
|
|
|
$lastUserData['registration'] = wfTimestamp(TS_ISO_8601, $row->user_registration);
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-08 07:50:56 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
if ($sqlLimit == $count) {
|
|
|
|
|
// BUG! database contains group name that User::getAllGroups() does not return
|
|
|
|
|
// TODO: should handle this more gracefully
|
2008-04-14 07:45:50 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__,
|
2007-07-30 08:09:15 +00:00
|
|
|
'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function');
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
// Add user's group info
|
|
|
|
|
if ($fld_groups && !is_null($row->ug_group2)) {
|
|
|
|
|
$lastUserData['groups'][] = $row->ug_group2;
|
|
|
|
|
$result->setIndexedTagName($lastUserData['groups'], 'g');
|
2007-07-08 07:50:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-08 07:50:56 +00:00
|
|
|
$db->freeResult($res);
|
|
|
|
|
|
|
|
|
|
$result->setIndexedTagName($data, 'u');
|
|
|
|
|
$result->addValue('query', $this->getModuleName(), $data);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2007-07-08 07:50:56 +00:00
|
|
|
return array (
|
|
|
|
|
'from' => null,
|
2007-07-30 08:09:15 +00:00
|
|
|
'prefix' => null,
|
|
|
|
|
'group' => array(
|
|
|
|
|
ApiBase :: PARAM_TYPE => User::getAllGroups()
|
|
|
|
|
),
|
2007-07-08 07:50:56 +00:00
|
|
|
'prop' => array (
|
2008-04-14 07:45:50 +00:00
|
|
|
ApiBase :: PARAM_ISMULTI => true,
|
2007-07-08 07:50:56 +00:00
|
|
|
ApiBase :: PARAM_TYPE => array (
|
2008-05-10 10:49:26 +00:00
|
|
|
'blockinfo',
|
2007-07-30 08:09:15 +00:00
|
|
|
'groups',
|
2008-05-10 10:49:26 +00:00
|
|
|
'editcount',
|
|
|
|
|
'registration'
|
2007-07-08 07:50:56 +00:00
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
'limit' => array (
|
|
|
|
|
ApiBase :: PARAM_DFLT => 10,
|
|
|
|
|
ApiBase :: PARAM_TYPE => 'limit',
|
|
|
|
|
ApiBase :: PARAM_MIN => 1,
|
|
|
|
|
ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
|
|
|
|
|
ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2007-07-08 07:50:56 +00:00
|
|
|
return array (
|
|
|
|
|
'from' => 'The user name to start enumerating from.',
|
2007-07-30 08:09:15 +00:00
|
|
|
'prefix' => 'Search for all page titles that begin with this value.',
|
|
|
|
|
'group' => 'Limit users to a given group name',
|
|
|
|
|
'prop' => array(
|
|
|
|
|
'What pieces of information to include.',
|
|
|
|
|
'`groups` property uses more server resources and may return fewer results than the limit.'),
|
|
|
|
|
'limit' => 'How many total user names to return.',
|
2007-07-08 07:50:56 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2007-07-08 07:50:56 +00:00
|
|
|
return 'Enumerate all registered users';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getExamples() {
|
|
|
|
|
return array (
|
|
|
|
|
'api.php?action=query&list=allusers&aufrom=Y',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVersion() {
|
2007-12-06 18:33:18 +00:00
|
|
|
return __CLASS__ . ': $Id$';
|
2007-07-08 07:50:56 +00:00
|
|
|
}
|
|
|
|
|
}
|