3efd4e0889a531083a503e2b0f94ddd36bdec933
[lhc/web/wiklou.git] / includes / api / ApiQueryUsers.php
1 <?php
2
3 /*
4 * Created on July 30, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to get information about a list of users
33 *
34 * @ingroup API
35 */
36
37 class ApiQueryUsers extends ApiQueryBase {
38
39 public function __construct($query, $moduleName) {
40 parent :: __construct($query, $moduleName, 'us');
41 }
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45 $result = $this->getResult();
46 $r = array();
47
48 if (!is_null($params['prop'])) {
49 $this->prop = array_flip($params['prop']);
50 } else {
51 $this->prop = array();
52 }
53
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 === '')
61 {
62 $vals = array('name' => $u, 'invalid' => '');
63 $fit = $result->addValue(array('query', $this->getModuleName()),
64 null, $vals);
65 if(!$fit)
66 {
67 $this->setContinueEnumParameter('users',
68 implode('|', array_diff($users, $done)));
69 $goodNames = array();
70 break;
71 }
72 $done[] = $u;
73 }
74 else
75 $goodNames[] = $n;
76 }
77 if(count($goodNames))
78 {
79 $db = $this->getDb();
80 $this->addTables('user', 'u1');
81 $this->addFields('u1.*');
82 $this->addWhereFld('u1.user_name', $goodNames);
83
84 if(isset($this->prop['groups'])) {
85 $this->addTables('user_groups');
86 $this->addJoinConds(array('user_groups' => array('LEFT JOIN', 'ug_user=u1.user_id')));
87 $this->addFields('ug_group');
88 }
89 if(isset($this->prop['blockinfo'])) {
90 $this->addTables('ipblocks');
91 $this->addTables('user', 'u2');
92 $u2 = $this->getAliasedName('user', 'u2');
93 $this->addJoinConds(array(
94 'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
95 $u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
96 $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
97 }
98
99 $data = array();
100 $res = $this->select(__METHOD__);
101 while(($r = $db->fetchObject($res))) {
102 $user = User::newFromRow($r);
103 $name = $user->getName();
104 $data[$name]['name'] = $name;
105 if(isset($this->prop['editcount']))
106 // No proper member function in the User class for this
107 $data[$name]['editcount'] = $r->user_editcount;
108 if(isset($this->prop['registration']))
109 // Nor for this one
110 $data[$name]['registration'] = wfTimestampOrNull(TS_ISO_8601, $r->user_registration);
111 if(isset($this->prop['groups']) && !is_null($r->ug_group))
112 // This row contains only one group, others will be added from other rows
113 $data[$name]['groups'][] = $r->ug_group;
114 if(isset($this->prop['blockinfo']) && !is_null($r->blocker_name)) {
115 $data[$name]['blockedby'] = $r->blocker_name;
116 $data[$name]['blockreason'] = $r->ipb_reason;
117 }
118 if(isset($this->prop['emailable']) && $user->canReceiveEmail())
119 $data[$name]['emailable'] = '';
120 }
121 }
122 // Second pass: add result data to $retval
123 foreach($goodNames as $u) {
124 if(!isset($data[$u]))
125 $data[$u] = array('name' => $u, 'missing' => '');
126 else {
127 if(isset($this->prop['groups']) && isset($data[$u]['groups']))
128 $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
129 }
130 $fit = $result->addValue(array('query', $this->getModuleName()),
131 null, $data[$u]);
132 if(!$fit)
133 {
134 $this->setContinueEnumParameter('users',
135 implode('|', array_diff($users, $done)));
136 break;
137 }
138 $done[] = $u;
139 }
140 return $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'user');
141 }
142
143 public function getAllowedParams() {
144 return array (
145 'prop' => array (
146 ApiBase :: PARAM_DFLT => NULL,
147 ApiBase :: PARAM_ISMULTI => true,
148 ApiBase :: PARAM_TYPE => array (
149 'blockinfo',
150 'groups',
151 'editcount',
152 'registration',
153 'emailable',
154 )
155 ),
156 'users' => array(
157 ApiBase :: PARAM_ISMULTI => true
158 )
159 );
160 }
161
162 public function getParamDescription() {
163 return array (
164 'prop' => array(
165 'What pieces of information to include',
166 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
167 ' groups - lists all the groups the user belongs to',
168 ' editcount - adds the user\'s edit count',
169 ' registration - adds the user\'s registration timestamp',
170 ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
171 ),
172 'users' => 'A list of users to obtain the same information for'
173 );
174 }
175
176 public function getDescription() {
177 return 'Get information about a list of users';
178 }
179
180 protected function getExamples() {
181 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount';
182 }
183
184 public function getVersion() {
185 return __CLASS__ . ': $Id$';
186 }
187 }