API: Added bkip parameter to list=blocks. This more than fixes bug 14405.
[lhc/web/wiklou.git] / includes / api / ApiQueryBlocks.php
1 <?php
2
3 /*
4 * Created on Sep 10, 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 enumerate all available pages.
33 *
34 * @ingroup API
35 */
36 class ApiQueryBlocks extends ApiQueryBase {
37
38 var $users;
39
40 public function __construct($query, $moduleName) {
41 parent :: __construct($query, $moduleName, 'bk');
42 }
43
44 public function execute() {
45 $this->run();
46 }
47
48 private function run() {
49 global $wgUser;
50
51 $params = $this->extractRequestParams();
52 if(isset($params['users']) && isset($params['ip']))
53 $this->dieUsage('bkusers and bkip cannot be used together', 'usersandip');
54
55 $prop = array_flip($params['prop']);
56 $fld_id = isset($prop['id']);
57 $fld_user = isset($prop['user']);
58 $fld_by = isset($prop['by']);
59 $fld_timestamp = isset($prop['timestamp']);
60 $fld_expiry = isset($prop['expiry']);
61 $fld_reason = isset($prop['reason']);
62 $fld_range = isset($prop['range']);
63 $fld_flags = isset($prop['flags']);
64
65 $result = $this->getResult();
66 $pageSet = $this->getPageSet();
67 $titles = $pageSet->getTitles();
68 $data = array();
69
70 $this->addTables('ipblocks');
71 if($fld_id)
72 $this->addFields('ipb_id');
73 if($fld_user)
74 $this->addFields(array('ipb_address', 'ipb_user'));
75 if($fld_by)
76 {
77 $this->addTables('user');
78 $this->addFields(array('ipb_by', 'user_name'));
79 $this->addWhere('user_id = ipb_by');
80 }
81 if($fld_timestamp)
82 $this->addFields('ipb_timestamp');
83 if($fld_expiry)
84 $this->addFields('ipb_expiry');
85 if($fld_reason)
86 $this->addFields('ipb_reason');
87 if($fld_range)
88 $this->addFields(array('ipb_range_start', 'ipb_range_end'));
89 if($fld_flags)
90 $this->addFields(array('ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted'));
91
92 $this->addOption('LIMIT', $params['limit'] + 1);
93 $this->addWhereRange('ipb_timestamp', $params['dir'], $params['start'], $params['end']);
94 if(isset($params['ids']))
95 $this->addWhere(array('ipb_id' => $params['ids']));
96 if(isset($params['users']))
97 {
98 foreach((array)$params['users'] as $u)
99 $this->prepareUsername($u);
100 $this->addWhere(array('ipb_address' => $this->usernames));
101 }
102 if(isset($params['ip']))
103 {
104 list($ip, $range) = IP::parseCIDR($params['ip']);
105 if($ip && $range)
106 {
107 # We got a CIDR range
108 if($range < 16)
109 $this->dieUsage('CIDR ranges broader than /16 are not accepted', 'cidrtoobroad');
110 $lower = wfBaseConvert($ip, 10, 16, 8, false);
111 $upper = wfBaseConvert($ip + pow(2, 32 - $range) - 1, 10, 16, 8, false);
112 }
113 else
114 $lower = $upper = IP::toHex($params['ip']);
115 $prefix = substr($lower, 0, 4);
116 $this->addWhere(array(
117 "ipb_range_start LIKE '$prefix%'",
118 "ipb_range_start <= '$lower'",
119 "ipb_range_end >= '$upper'"
120 ));
121 }
122 if(!$wgUser->isAllowed('suppress'))
123 $this->addWhere(array('ipb_deleted' => 0));
124
125 // Purge expired entries on one in every 10 queries
126 if(!mt_rand(0, 10))
127 Block::purgeExpired();
128
129 $res = $this->select(__METHOD__);
130 $db = wfGetDB();
131
132 $count = 0;
133 while($row = $db->fetchObject($res))
134 {
135 if($count++ == $params['limit'])
136 {
137 // We've had enough
138 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ipb_timestamp));
139 break;
140 }
141 $block = array();
142 if($fld_id)
143 $block['id'] = $row->ipb_id;
144 if($fld_user && !$row->ipb_auto)
145 {
146 $block['user'] = $row->ipb_address;
147 }
148 if($fld_by)
149 {
150 $block['by'] = $row->user_name;
151 }
152 if($fld_timestamp)
153 $block['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ipb_timestamp);
154 if($fld_expiry)
155 $block['expiry'] = Block::decodeExpiry($row->ipb_expiry, TS_ISO_8601);
156 if($fld_reason)
157 $block['reason'] = $row->ipb_reason;
158 if($fld_range)
159 {
160 $block['rangestart'] = $this->convertHexIP($row->ipb_range_start);
161 $block['rangeend'] = $this->convertHexIP($row->ipb_range_end);
162 }
163 if($fld_flags)
164 {
165 // For clarity, these flags use the same names as their action=block counterparts
166 if($row->ipb_auto)
167 $block['automatic'] = '';
168 if($row->ipb_anon_only)
169 $block['anononly'] = '';
170 if($row->ipb_create_account)
171 $block['nocreate'] = '';
172 if($row->ipb_enable_autoblock)
173 $block['autoblock'] = '';
174 if($row->ipb_block_email)
175 $block['noemail'] = '';
176 if($row->ipb_deleted)
177 $block['hidden'] = '';
178 }
179 $data[] = $block;
180 }
181 $result->setIndexedTagName($data, 'block');
182 $result->addValue('query', $this->getModuleName(), $data);
183 }
184
185 protected function prepareUsername($user)
186 {
187 if(!$user)
188 $this->dieUsage('User parameter may not be empty', 'param_user');
189 $name = User::isIP($user)
190 ? $user
191 : User::getCanonicalName($user, 'valid');
192 if($name === false)
193 $this->dieUsage("User name {$user} is not valid", 'param_user');
194 $this->usernames[] = $name;
195 }
196
197 protected function convertHexIP($ip)
198 {
199 // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format
200 $dec = wfBaseConvert($ip, 16, 10);
201 $parts[0] = (int)($dec / (256*256*256));
202 $dec %= 256*256*256;
203 $parts[1] = (int)($dec / (256*256));
204 $dec %= 256*256;
205 $parts[2] = (int)($dec / 256);
206 $parts[3] = $dec % 256;
207 return implode('.', $parts);
208 }
209
210 public function getAllowedParams() {
211 return array (
212 'start' => array(
213 ApiBase :: PARAM_TYPE => 'timestamp'
214 ),
215 'end' => array(
216 ApiBase :: PARAM_TYPE => 'timestamp',
217 ),
218 'dir' => array(
219 ApiBase :: PARAM_TYPE => array(
220 'newer',
221 'older'
222 ),
223 ApiBase :: PARAM_DFLT => 'older'
224 ),
225 'ids' => array(
226 ApiBase :: PARAM_TYPE => 'integer',
227 ApiBase :: PARAM_ISMULTI => true
228 ),
229 'users' => array(
230 ApiBase :: PARAM_ISMULTI => true
231 ),
232 'ip' => null,
233 'limit' => array(
234 ApiBase :: PARAM_DFLT => 10,
235 ApiBase :: PARAM_TYPE => 'limit',
236 ApiBase :: PARAM_MIN => 1,
237 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
238 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
239 ),
240 'prop' => array(
241 ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
242 ApiBase :: PARAM_TYPE => array(
243 'id',
244 'user',
245 'by',
246 'timestamp',
247 'expiry',
248 'reason',
249 'range',
250 'flags'
251 ),
252 ApiBase :: PARAM_ISMULTI => true
253 )
254 );
255 }
256
257 public function getParamDescription() {
258 return array (
259 'start' => 'The timestamp to start enumerating from',
260 'end' => 'The timestamp to stop enumerating at',
261 'dir' => 'The direction in which to enumerate',
262 'ids' => 'Pipe-separated list of block IDs to list (optional)',
263 'users' => 'Pipe-separated list of users to search for (optional)',
264 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
265 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted.'),
266 'limit' => 'The maximum amount of blocks to list',
267 'prop' => 'Which properties to get',
268 );
269 }
270
271 public function getDescription() {
272 return 'List all blocked users and IP addresses.';
273 }
274
275 protected function getExamples() {
276 return array ( 'api.php?action=query&list=blocks',
277 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
278 );
279 }
280
281 public function getVersion() {
282 return __CLASS__ . ': $Id$';
283 }
284 }