4 * Created on July 30, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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');
32 * Query module to get information about a list of users
37 class ApiQueryUsers
extends ApiQueryBase
{
39 public function __construct($query, $moduleName) {
40 parent
:: __construct($query, $moduleName, 'us');
43 public function execute() {
44 $params = $this->extractRequestParams();
45 $result = $this->getResult();
48 if (!is_null($params['prop'])) {
49 $this->prop
= array_flip($params['prop']);
51 $this->prop
= array();
54 $users = (array)$params['users'];
55 $goodNames = $done = array();
56 $result = $this->getResult();
57 // Canonicalize user names
58 foreach($users as $u) {
59 $n = User
::getCanonicalName($u);
60 if($n === false ||
$n === '')
62 $vals = array('name' => $u, 'invalid' => '');
63 $fit = $result->addValue(array('query', $this->getModuleName()),
67 $this->setContinueEnumParameter('users',
68 implode('|', array_diff($users, $done)));
80 $this->addTables('user', 'u1');
81 $this->addFields('u1.user_name');
82 $this->addWhereFld('u1.user_name', $goodNames);
83 $this->addFieldsIf('u1.user_editcount', isset($this->prop
['editcount']));
84 $this->addFieldsIf('u1.user_registration', isset($this->prop
['registration']));
86 if(isset($this->prop
['groups'])) {
87 $this->addTables('user_groups');
88 $this->addJoinConds(array('user_groups' => array('LEFT JOIN', 'ug_user=u1.user_id')));
89 $this->addFields('ug_group');
91 if(isset($this->prop
['blockinfo'])) {
92 $this->addTables('ipblocks');
93 $this->addTables('user', 'u2');
94 $u2 = $this->getAliasedName('user', 'u2');
95 $this->addJoinConds(array(
96 'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
97 $u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
98 $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
102 $res = $this->select(__METHOD__
);
103 while(($r = $db->fetchObject($res))) {
104 $data[$r->user_name
]['name'] = $r->user_name
;
105 if(isset($this->prop
['editcount']))
106 $data[$r->user_name
]['editcount'] = $r->user_editcount
;
107 if(isset($this->prop
['registration']))
108 $data[$r->user_name
]['registration'] = wfTimestampOrNull(TS_ISO_8601
, $r->user_registration
);
109 if(isset($this->prop
['groups']))
110 // This row contains only one group, others will be added from other rows
111 if(!is_null($r->ug_group
))
112 $data[$r->user_name
]['groups'][] = $r->ug_group
;
113 if(isset($this->prop
['blockinfo']))
114 if(!is_null($r->blocker_name
)) {
115 $data[$r->user_name
]['blockedby'] = $r->blocker_name
;
116 $data[$r->user_name
]['blockreason'] = $r->ipb_reason
;
120 // Second pass: add result data to $retval
121 foreach($goodNames as $u) {
122 if(!isset($data[$u]))
123 $data[$u] = array('name' => $u, 'missing' => '');
125 if(isset($this->prop
['groups']) && isset($data[$u]['groups']))
126 $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
128 $fit = $result->addValue(array('query', $this->getModuleName()),
132 $this->setContinueEnumParameter('users',
133 implode('|', array_diff($users, $done)));
138 return $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'user');
141 public function getAllowedParams() {
144 ApiBase
:: PARAM_DFLT
=> NULL,
145 ApiBase
:: PARAM_ISMULTI
=> true,
146 ApiBase
:: PARAM_TYPE
=> array (
155 ApiBase
:: PARAM_ISMULTI
=> true
160 public function getParamDescription() {
163 'What pieces of information to include',
164 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
165 ' groups - lists all the groups the user belongs to',
166 ' editcount - adds the user\'s edit count',
167 ' registration - adds the user\'s registration timestamp',
168 ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
170 'users' => 'A list of users to obtain the same information for'
174 public function getDescription() {
175 return 'Get information about a list of users';
178 protected function getExamples() {
179 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount';
182 public function getVersion() {
183 return __CLASS__
. ': $Id$';