(bug 14022) Added usprop=registration and auprop=blockinfo
[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 * @addtogroup 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 if(is_array($params['users'])) {
55 $r = $this->getOtherUsersInfo($params['users']);
56 $result->setIndexedTagName($r, 'user');
57 }
58 $result->addValue("query", $this->getModuleName(), $r);
59 }
60
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' => '');
68 else
69 $goodNames[] = $n;
70 }
71 if(empty($goodNames))
72 return $retval;
73
74 $db = $this->getDb();
75 $this->addFields('u1.user_name');
76 $this->addWhereFld('u1.user_name', $goodNames);
77 $this->addFieldsIf('u1.user_editcount', isset($this->prop['editcount']));
78 $this->addFieldsIf('u1.user_registration', isset($this->prop['registration']));
79
80 $join = false;
81 $tables = array('user');
82 $types = array();
83 $conds = array();
84 $aliases = array('u1');
85 if(isset($this->prop['groups'])) {
86 $join = true;
87 $tables[] = 'user_groups';
88 $types[] = ApiQueryBase::LEFT_JOIN;
89 $conds[] = 'ug_user=u1.user_id';
90 $aliases[] = null;
91 $this->addFields('ug_group');
92 }
93 if(isset($this->prop['blockinfo'])) {
94 $join = true;
95 $tables[] = 'ipblocks';
96 $types[] = ApiQueryBase::LEFT_JOIN;
97 $conds[] = 'ipb_user=u1.user_id';
98 $aliases[] = null;
99
100 $tables[] = 'user';
101 $types[] = ApiQueryBase::LEFT_JOIN;
102 $conds[] = 'ipb_by=u2.user_id';
103 $aliases[] = 'u2';
104 $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
105 }
106
107 if($join)
108 $this->addJoin($tables, $types, $conds, $aliases);
109 else
110 $this->addTables('user', 'u1');
111
112 $data = array();
113 $res = $this->select(__METHOD__);
114 while(($r = $db->fetchObject($res))) {
115 $data[$r->user_name]['name'] = $r->user_name;
116 if(isset($this->prop['editcount']))
117 $data[$r->user_name]['editcount'] = $r->user_editcount;
118 if(isset($this->prop['registration']))
119 $data[$r->user_name]['registration'] = wfTimestamp(TS_ISO_8601, $r->user_registration);
120 if(isset($this->prop['groups']))
121 // This row contains only one group, others will be added from other rows
122 if(!is_null($r->ug_group))
123 $data[$r->user_name]['groups'][] = $r->ug_group;
124 if(isset($this->prop['blockinfo']))
125 if(!is_null($r->blocker_name)) {
126 $data[$r->user_name]['blockedby'] = $r->blocker_name;
127 $data[$r->user_name]['blockreason'] = $r->ipb_reason;
128 }
129 }
130
131 // Second pass: add result data to $retval
132 foreach($goodNames as $u) {
133 if(!isset($data[$u]))
134 $retval[] = array('name' => $u, 'missing' => '');
135 else {
136 if(isset($this->prop['groups']) && isset($data[$u]['groups']))
137 $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
138 $retval[] = $data[$u];
139 }
140 }
141 return $retval;
142 }
143
144 public function getAllowedParams() {
145 return array (
146 'prop' => array (
147 ApiBase :: PARAM_DFLT => NULL,
148 ApiBase :: PARAM_ISMULTI => true,
149 ApiBase :: PARAM_TYPE => array (
150 'blockinfo',
151 'groups',
152 'editcount',
153 'registration'
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 ),
170 'users' => 'A list of users to obtain the same information for'
171 );
172 }
173
174 public function getDescription() {
175 return 'Get information about a list of users';
176 }
177
178 protected function getExamples() {
179 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount';
180 }
181
182 public function getVersion() {
183 return __CLASS__ . ': $Id$';
184 }
185 }