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 if(is_array($params['users'])) {
55 $r = $this->getOtherUsersInfo($params['users']);
56 $result->setIndexedTagName($r, 'user');
58 $result->addValue("query", $this->getModuleName(), $r);
61 protected function getOtherUsersInfo($users) {
62 $goodNames = $retval = array();
63 // Canonicalize user names
64 foreach($users as $u) {
65 $n = User
::getCanonicalName($u);
66 if($n === false ||
$n === '')
67 $retval[] = array('name' => $u, 'invalid' => '');
75 $userTable = $db->tableName('user');
76 $tables = "$userTable AS u1";
77 $this->addFields('u1.user_name');
78 $this->addWhereFld('u1.user_name', $goodNames);
79 $this->addFieldsIf('u1.user_editcount', isset($this->prop
['editcount']));
81 if(isset($this->prop
['groups'])) {
82 $ug = $db->tableName('user_groups');
83 $tables = "$tables LEFT JOIN $ug ON ug_user=u1.user_id";
84 $this->addFields('ug_group');
86 if(isset($this->prop
['blockinfo'])) {
87 $ipb = $db->tableName('ipblocks');
88 $tables = "$tables LEFT JOIN $ipb ON ipb_user=u1.user_id";
89 $tables = "$tables LEFT JOIN $userTable AS u2 ON ipb_by=u2.user_id";
90 $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
92 $this->addTables($tables);
95 $res = $this->select(__METHOD__
);
96 while(($r = $db->fetchObject($res))) {
97 $data[$r->user_name
]['name'] = $r->user_name
;
98 if(isset($this->prop
['editcount']))
99 $data[$r->user_name
]['editcount'] = $r->user_editcount
;
100 if(isset($this->prop
['groups']))
101 // This row contains only one group, others will be added from other rows
102 if(!is_null($r->ug_group
))
103 $data[$r->user_name
]['groups'][] = $r->ug_group
;
104 if(isset($this->prop
['blockinfo']))
105 if(!is_null($r->blocker_name
)) {
106 $data[$r->user_name
]['blockedby'] = $r->blocker_name
;
107 $data[$r->user_name
]['blockreason'] = $r->ipb_reason
;
111 // Second pass: add result data to $retval
112 foreach($goodNames as $u) {
113 if(!isset($data[$u]))
114 $retval[] = array('name' => $u, 'missing' => '');
116 if(isset($this->prop
['groups']) && isset($data[$u]['groups']))
117 $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
118 $retval[] = $data[$u];
124 public function getAllowedParams() {
127 ApiBase
:: PARAM_DFLT
=> NULL,
128 ApiBase
:: PARAM_ISMULTI
=> true,
129 ApiBase
:: PARAM_TYPE
=> array (
136 ApiBase
:: PARAM_ISMULTI
=> true
141 public function getParamDescription() {
144 'What pieces of information to include',
145 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
146 ' groups - lists all the groups the user belongs to',
147 ' editcount - adds the user\'s edit count'
149 'users' => 'A list of users to obtain the same information for'
153 public function getDescription() {
154 return 'Get information about a list of users';
157 protected function getExamples() {
158 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount';
161 public function getVersion() {
162 return __CLASS__
. ': $Id$';