(bug 17602) fix Monobook action tabs not quite touching the page body
[lhc/web/wiklou.git] / includes / api / ApiQueryBlocks.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 10, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate all user blocks
29 *
30 * @ingroup API
31 */
32 class ApiQueryBlocks extends ApiQueryBase {
33
34 /**
35 * @var Array
36 */
37 protected $usernames;
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'bk' );
41 }
42
43 public function execute() {
44 global $wgContLang;
45
46 $params = $this->extractRequestParams();
47 $this->requireMaxOneParameter( $params, 'users', 'ip' );
48
49 $prop = array_flip( $params['prop'] );
50 $fld_id = isset( $prop['id'] );
51 $fld_user = isset( $prop['user'] );
52 $fld_userid = isset( $prop['userid'] );
53 $fld_by = isset( $prop['by'] );
54 $fld_byid = isset( $prop['byid'] );
55 $fld_timestamp = isset( $prop['timestamp'] );
56 $fld_expiry = isset( $prop['expiry'] );
57 $fld_reason = isset( $prop['reason'] );
58 $fld_range = isset( $prop['range'] );
59 $fld_flags = isset( $prop['flags'] );
60
61 $result = $this->getResult();
62
63 $this->addTables( 'ipblocks' );
64 $this->addFields( 'ipb_auto' );
65
66 $this->addFieldsIf( 'ipb_id', $fld_id );
67 $this->addFieldsIf( array( 'ipb_address', 'ipb_user' ), $fld_user || $fld_userid );
68 $this->addFieldsIf( 'ipb_by_text', $fld_by );
69 $this->addFieldsIf( 'ipb_by', $fld_byid );
70 $this->addFieldsIf( 'ipb_timestamp', $fld_timestamp );
71 $this->addFieldsIf( 'ipb_expiry', $fld_expiry );
72 $this->addFieldsIf( 'ipb_reason', $fld_reason );
73 $this->addFieldsIf( array( 'ipb_range_start', 'ipb_range_end' ), $fld_range );
74 $this->addFieldsIf( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock',
75 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ),
76 $fld_flags );
77
78 $this->addOption( 'LIMIT', $params['limit'] + 1 );
79 $this->addTimestampWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
80
81 $db = $this->getDB();
82
83 if ( isset( $params['ids'] ) ) {
84 $this->addWhereFld( 'ipb_id', $params['ids'] );
85 }
86 if ( isset( $params['users'] ) ) {
87 foreach ( (array)$params['users'] as $u ) {
88 $this->prepareUsername( $u );
89 }
90 $this->addWhereFld( 'ipb_address', $this->usernames );
91 $this->addWhereFld( 'ipb_auto', 0 );
92 }
93 if ( isset( $params['ip'] ) ) {
94 list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
95 if ( $ip && $range ) {
96 // We got a CIDR range
97 if ( $range < 16 ) {
98 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
99 }
100 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
101 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
102 } else {
103 $lower = $upper = IP::toHex( $params['ip'] );
104 }
105 $prefix = substr( $lower, 0, 4 );
106
107 # Fairly hard to make a malicious SQL statement out of hex characters,
108 # but it is good practice to add quotes
109 $lower = $db->addQuotes( $lower );
110 $upper = $db->addQuotes( $upper );
111
112 $this->addWhere( array(
113 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
114 'ipb_range_start <= ' . $lower,
115 'ipb_range_end >= ' . $upper,
116 'ipb_auto' => 0
117 ) );
118 }
119
120 if ( !is_null( $params['show'] ) ) {
121 $show = array_flip( $params['show'] );
122
123 /* Check for conflicting parameters. */
124 if ( ( isset ( $show['account'] ) && isset ( $show['!account'] ) )
125 || ( isset ( $show['ip'] ) && isset ( $show['!ip'] ) )
126 || ( isset ( $show['range'] ) && isset ( $show['!range'] ) )
127 || ( isset ( $show['temp'] ) && isset ( $show['!temp'] ) )
128 ) {
129 $this->dieUsageMsg( 'show' );
130 }
131
132 $this->addWhereIf( 'ipb_user = 0', isset( $show['!account'] ) );
133 $this->addWhereIf( 'ipb_user != 0', isset( $show['account'] ) );
134 $this->addWhereIf( 'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show['!ip'] ) );
135 $this->addWhereIf( 'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show['ip'] ) );
136 $this->addWhereIf( 'ipb_expiry = ' . $db->addQuotes( $db->getInfinity() ), isset( $show['!temp'] ) );
137 $this->addWhereIf( 'ipb_expiry != ' . $db->addQuotes( $db->getInfinity() ), isset( $show['temp'] ) );
138 $this->addWhereIf( 'ipb_range_end = ipb_range_start', isset( $show['!range'] ) );
139 $this->addWhereIf( 'ipb_range_end > ipb_range_start', isset( $show['range'] ) );
140 }
141
142 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
143 $this->addWhereFld( 'ipb_deleted', 0 );
144 }
145
146 // Purge expired entries on one in every 10 queries
147 if ( !mt_rand( 0, 10 ) ) {
148 Block::purgeExpired();
149 }
150
151 $res = $this->select( __METHOD__ );
152
153 $count = 0;
154 foreach ( $res as $row ) {
155 if ( ++$count > $params['limit'] ) {
156 // We've had enough
157 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
158 break;
159 }
160 $block = array();
161 if ( $fld_id ) {
162 $block['id'] = $row->ipb_id;
163 }
164 if ( $fld_user && !$row->ipb_auto ) {
165 $block['user'] = $row->ipb_address;
166 }
167 if ( $fld_userid && !$row->ipb_auto ) {
168 $block['userid'] = $row->ipb_user;
169 }
170 if ( $fld_by ) {
171 $block['by'] = $row->ipb_by_text;
172 }
173 if ( $fld_byid ) {
174 $block['byid'] = $row->ipb_by;
175 }
176 if ( $fld_timestamp ) {
177 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
178 }
179 if ( $fld_expiry ) {
180 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
181 }
182 if ( $fld_reason ) {
183 $block['reason'] = $row->ipb_reason;
184 }
185 if ( $fld_range && !$row->ipb_auto ) {
186 $block['rangestart'] = IP::formatHex( $row->ipb_range_start );
187 $block['rangeend'] = IP::formatHex( $row->ipb_range_end );
188 }
189 if ( $fld_flags ) {
190 // For clarity, these flags use the same names as their action=block counterparts
191 if ( $row->ipb_auto ) {
192 $block['automatic'] = '';
193 }
194 if ( $row->ipb_anon_only ) {
195 $block['anononly'] = '';
196 }
197 if ( $row->ipb_create_account ) {
198 $block['nocreate'] = '';
199 }
200 if ( $row->ipb_enable_autoblock ) {
201 $block['autoblock'] = '';
202 }
203 if ( $row->ipb_block_email ) {
204 $block['noemail'] = '';
205 }
206 if ( $row->ipb_deleted ) {
207 $block['hidden'] = '';
208 }
209 if ( $row->ipb_allow_usertalk ) {
210 $block['allowusertalk'] = '';
211 }
212 }
213 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
214 if ( !$fit ) {
215 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
216 break;
217 }
218 }
219 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
220 }
221
222 protected function prepareUsername( $user ) {
223 if ( !$user ) {
224 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
225 }
226 $name = User::isIP( $user )
227 ? $user
228 : User::getCanonicalName( $user, 'valid' );
229 if ( $name === false ) {
230 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
231 }
232 $this->usernames[] = $name;
233 }
234
235 public function getAllowedParams() {
236 return array(
237 'start' => array(
238 ApiBase::PARAM_TYPE => 'timestamp'
239 ),
240 'end' => array(
241 ApiBase::PARAM_TYPE => 'timestamp',
242 ),
243 'dir' => array(
244 ApiBase::PARAM_TYPE => array(
245 'newer',
246 'older'
247 ),
248 ApiBase::PARAM_DFLT => 'older'
249 ),
250 'ids' => array(
251 ApiBase::PARAM_TYPE => 'integer',
252 ApiBase::PARAM_ISMULTI => true
253 ),
254 'users' => array(
255 ApiBase::PARAM_ISMULTI => true
256 ),
257 'ip' => null,
258 'limit' => array(
259 ApiBase::PARAM_DFLT => 10,
260 ApiBase::PARAM_TYPE => 'limit',
261 ApiBase::PARAM_MIN => 1,
262 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
263 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
264 ),
265 'prop' => array(
266 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
267 ApiBase::PARAM_TYPE => array(
268 'id',
269 'user',
270 'userid',
271 'by',
272 'byid',
273 'timestamp',
274 'expiry',
275 'reason',
276 'range',
277 'flags'
278 ),
279 ApiBase::PARAM_ISMULTI => true
280 ),
281 'show' => array(
282 ApiBase::PARAM_TYPE => array(
283 'account',
284 '!account',
285 'temp',
286 '!temp',
287 'ip',
288 '!ip',
289 'range',
290 '!range',
291 ),
292 ApiBase::PARAM_ISMULTI => true
293 ),
294 );
295 }
296
297 public function getParamDescription() {
298 $p = $this->getModulePrefix();
299 return array(
300 'start' => 'The timestamp to start enumerating from',
301 'end' => 'The timestamp to stop enumerating at',
302 'dir' => $this->getDirectionDescription( $p ),
303 'ids' => 'List of block IDs to list (optional)',
304 'users' => 'List of users to search for (optional)',
305 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
306 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
307 'limit' => 'The maximum amount of blocks to list',
308 'prop' => array(
309 'Which properties to get',
310 ' id - Adds the ID of the block',
311 ' user - Adds the username of the blocked user',
312 ' userid - Adds the user ID of the blocked user',
313 ' by - Adds the username of the blocking user',
314 ' byid - Adds the user ID of the blocking user',
315 ' timestamp - Adds the timestamp of when the block was given',
316 ' expiry - Adds the timestamp of when the block expires',
317 ' reason - Adds the reason given for the block',
318 ' range - Adds the range of IPs affected by the block',
319 ' flags - Tags the ban with (autoblock, anononly, etc)',
320 ),
321 'show' => array(
322 'Show only items that meet this criteria.',
323 "For example, to see only indefinite blocks on IPs, set {$p}show=ip|!temp"
324 ),
325 );
326 }
327
328 public function getResultProperties() {
329 return array(
330 'id' => array(
331 'id' => 'integer'
332 ),
333 'user' => array(
334 'user' => array(
335 ApiBase::PROP_TYPE => 'string',
336 ApiBase::PROP_NULLABLE => true
337 )
338 ),
339 'userid' => array(
340 'userid' => array(
341 ApiBase::PROP_TYPE => 'integer',
342 ApiBase::PROP_NULLABLE => true
343 )
344 ),
345 'by' => array(
346 'by' => 'string'
347 ),
348 'byid' => array(
349 'byid' => 'integer'
350 ),
351 'timestamp' => array(
352 'timestamp' => 'timestamp'
353 ),
354 'expiry' => array(
355 'expiry' => 'timestamp'
356 ),
357 'reason' => array(
358 'reason' => 'string'
359 ),
360 'range' => array(
361 'rangestart' => array(
362 ApiBase::PROP_TYPE => 'string',
363 ApiBase::PROP_NULLABLE => true
364 ),
365 'rangeend' => array(
366 ApiBase::PROP_TYPE => 'string',
367 ApiBase::PROP_NULLABLE => true
368 )
369 ),
370 'flags' => array(
371 'automatic' => 'boolean',
372 'anononly' => 'boolean',
373 'nocreate' => 'boolean',
374 'autoblock' => 'boolean',
375 'noemail' => 'boolean',
376 'hidden' => 'boolean',
377 'allowusertalk' => 'boolean'
378 )
379 );
380 }
381
382 public function getDescription() {
383 return 'List all blocked users and IP addresses';
384 }
385
386 public function getPossibleErrors() {
387 return array_merge( parent::getPossibleErrors(),
388 $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
389 array(
390 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
391 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
392 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
393 array( 'show' ),
394 )
395 );
396 }
397
398 public function getExamples() {
399 return array(
400 'api.php?action=query&list=blocks',
401 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
402 );
403 }
404
405 public function getHelpUrls() {
406 return 'https://www.mediawiki.org/wiki/API:Blocks';
407 }
408 }