5 * Created on Sep 10, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
28 * Query module to enumerate all user blocks
32 class ApiQueryBlocks
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'bk' );
43 public function execute() {
46 $params = $this->extractRequestParams();
47 $this->requireMaxOneParameter( $params, 'users', 'ip' );
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'] );
61 $result = $this->getResult();
63 $this->addTables( 'ipblocks' );
64 $this->addFields( 'ipb_auto' );
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' ),
78 $this->addOption( 'LIMIT', $params['limit'] +
1 );
79 $this->addTimestampWhereRange(
88 if ( isset( $params['ids'] ) ) {
89 $this->addWhereFld( 'ipb_id', $params['ids'] );
91 if ( isset( $params['users'] ) ) {
92 foreach ( (array)$params['users'] as $u ) {
93 $this->prepareUsername( $u );
95 $this->addWhereFld( 'ipb_address', $this->usernames
);
96 $this->addWhereFld( 'ipb_auto', 0 );
98 if ( isset( $params['ip'] ) ) {
99 global $wgBlockCIDRLimit;
100 if ( IP
::isIPv4( $params['ip'] ) ) {
102 $cidrLimit = $wgBlockCIDRLimit['IPv4'];
104 } elseif ( IP
::isIPv6( $params['ip'] ) ) {
106 $cidrLimit = $wgBlockCIDRLimit['IPv6'];
107 $prefixLen = 3; // IP::toHex output is prefixed with "v6-"
109 $this->dieUsage( 'IP parameter is not valid', 'param_ip' );
112 # Check range validity, if it's a CIDR
113 list( $ip, $range ) = IP
::parseCIDR( $params['ip'] );
114 if ( $ip !== false && $range !== false && $range < $cidrLimit ) {
116 "$type CIDR ranges broader than /$cidrLimit are not accepted",
121 # Let IP::parseRange handle calculating $upper, instead of duplicating the logic here.
122 list( $lower, $upper ) = IP
::parseRange( $params['ip'] );
124 # Extract the common prefix to any rangeblock affecting this IP/CIDR
125 $prefix = substr( $lower, 0, $prefixLen +
floor( $cidrLimit / 4 ) );
127 # Fairly hard to make a malicious SQL statement out of hex characters,
128 # but it is good practice to add quotes
129 $lower = $db->addQuotes( $lower );
130 $upper = $db->addQuotes( $upper );
132 $this->addWhere( array(
133 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
134 'ipb_range_start <= ' . $lower,
135 'ipb_range_end >= ' . $upper,
140 if ( !is_null( $params['show'] ) ) {
141 $show = array_flip( $params['show'] );
143 /* Check for conflicting parameters. */
144 if ( ( isset( $show['account'] ) && isset( $show['!account'] ) )
145 ||
( isset( $show['ip'] ) && isset( $show['!ip'] ) )
146 ||
( isset( $show['range'] ) && isset( $show['!range'] ) )
147 ||
( isset( $show['temp'] ) && isset( $show['!temp'] ) )
149 $this->dieUsageMsg( 'show' );
152 $this->addWhereIf( 'ipb_user = 0', isset( $show['!account'] ) );
153 $this->addWhereIf( 'ipb_user != 0', isset( $show['account'] ) );
154 $this->addWhereIf( 'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show['!ip'] ) );
155 $this->addWhereIf( 'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show['ip'] ) );
156 $this->addWhereIf( 'ipb_expiry = ' .
157 $db->addQuotes( $db->getInfinity() ), isset( $show['!temp'] ) );
158 $this->addWhereIf( 'ipb_expiry != ' .
159 $db->addQuotes( $db->getInfinity() ), isset( $show['temp'] ) );
160 $this->addWhereIf( 'ipb_range_end = ipb_range_start', isset( $show['!range'] ) );
161 $this->addWhereIf( 'ipb_range_end > ipb_range_start', isset( $show['range'] ) );
164 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
165 $this->addWhereFld( 'ipb_deleted', 0 );
168 // Purge expired entries on one in every 10 queries
169 if ( !mt_rand( 0, 10 ) ) {
170 Block
::purgeExpired();
173 $res = $this->select( __METHOD__
);
176 foreach ( $res as $row ) {
177 if ( ++
$count > $params['limit'] ) {
179 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
184 $block['id'] = $row->ipb_id
;
186 if ( $fld_user && !$row->ipb_auto
) {
187 $block['user'] = $row->ipb_address
;
189 if ( $fld_userid && !$row->ipb_auto
) {
190 $block['userid'] = $row->ipb_user
;
193 $block['by'] = $row->ipb_by_text
;
196 $block['byid'] = $row->ipb_by
;
198 if ( $fld_timestamp ) {
199 $block['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
202 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry
, TS_ISO_8601
);
205 $block['reason'] = $row->ipb_reason
;
207 if ( $fld_range && !$row->ipb_auto
) {
208 $block['rangestart'] = IP
::formatHex( $row->ipb_range_start
);
209 $block['rangeend'] = IP
::formatHex( $row->ipb_range_end
);
212 // For clarity, these flags use the same names as their action=block counterparts
213 if ( $row->ipb_auto
) {
214 $block['automatic'] = '';
216 if ( $row->ipb_anon_only
) {
217 $block['anononly'] = '';
219 if ( $row->ipb_create_account
) {
220 $block['nocreate'] = '';
222 if ( $row->ipb_enable_autoblock
) {
223 $block['autoblock'] = '';
225 if ( $row->ipb_block_email
) {
226 $block['noemail'] = '';
228 if ( $row->ipb_deleted
) {
229 $block['hidden'] = '';
231 if ( $row->ipb_allow_usertalk
) {
232 $block['allowusertalk'] = '';
235 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
237 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
241 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
244 protected function prepareUsername( $user ) {
246 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
248 $name = User
::isIP( $user )
250 : User
::getCanonicalName( $user, 'valid' );
251 if ( $name === false ) {
252 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
254 $this->usernames
[] = $name;
257 public function getAllowedParams() {
260 ApiBase
::PARAM_TYPE
=> 'timestamp'
263 ApiBase
::PARAM_TYPE
=> 'timestamp',
266 ApiBase
::PARAM_TYPE
=> array(
270 ApiBase
::PARAM_DFLT
=> 'older'
273 ApiBase
::PARAM_TYPE
=> 'integer',
274 ApiBase
::PARAM_ISMULTI
=> true
277 ApiBase
::PARAM_ISMULTI
=> true
281 ApiBase
::PARAM_DFLT
=> 10,
282 ApiBase
::PARAM_TYPE
=> 'limit',
283 ApiBase
::PARAM_MIN
=> 1,
284 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
285 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
288 ApiBase
::PARAM_DFLT
=> 'id|user|by|timestamp|expiry|reason|flags',
289 ApiBase
::PARAM_TYPE
=> array(
301 ApiBase
::PARAM_ISMULTI
=> true
304 ApiBase
::PARAM_TYPE
=> array(
314 ApiBase
::PARAM_ISMULTI
=> true
319 public function getParamDescription() {
320 global $wgBlockCIDRLimit;
321 $p = $this->getModulePrefix();
324 'start' => 'The timestamp to start enumerating from',
325 'end' => 'The timestamp to stop enumerating at',
326 'dir' => $this->getDirectionDescription( $p ),
327 'ids' => 'List of block IDs to list (optional)',
328 'users' => 'List of users to search for (optional)',
330 'Get all blocks applying to this IP or CIDR range, including range blocks.',
331 "Cannot be used together with bkusers. CIDR ranges broader than " .
332 "IPv4/{$wgBlockCIDRLimit['IPv4']} or IPv6/{$wgBlockCIDRLimit['IPv6']} " .
335 'limit' => 'The maximum amount of blocks to list',
337 'Which properties to get',
338 ' id - Adds the ID of the block',
339 ' user - Adds the username of the blocked user',
340 ' userid - Adds the user ID of the blocked user',
341 ' by - Adds the username of the blocking user',
342 ' byid - Adds the user ID of the blocking user',
343 ' timestamp - Adds the timestamp of when the block was given',
344 ' expiry - Adds the timestamp of when the block expires',
345 ' reason - Adds the reason given for the block',
346 ' range - Adds the range of IPs affected by the block',
347 ' flags - Tags the ban with (autoblock, anononly, etc)',
350 'Show only items that meet this criteria.',
351 "For example, to see only indefinite blocks on IPs, set {$p}show=ip|!temp"
356 public function getResultProperties() {
363 ApiBase
::PROP_TYPE
=> 'string',
364 ApiBase
::PROP_NULLABLE
=> true
369 ApiBase
::PROP_TYPE
=> 'integer',
370 ApiBase
::PROP_NULLABLE
=> true
379 'timestamp' => array(
380 'timestamp' => 'timestamp'
383 'expiry' => 'timestamp'
389 'rangestart' => array(
390 ApiBase
::PROP_TYPE
=> 'string',
391 ApiBase
::PROP_NULLABLE
=> true
394 ApiBase
::PROP_TYPE
=> 'string',
395 ApiBase
::PROP_NULLABLE
=> true
399 'automatic' => 'boolean',
400 'anononly' => 'boolean',
401 'nocreate' => 'boolean',
402 'autoblock' => 'boolean',
403 'noemail' => 'boolean',
404 'hidden' => 'boolean',
405 'allowusertalk' => 'boolean'
410 public function getDescription() {
411 return 'List all blocked users and IP addresses';
414 public function getPossibleErrors() {
415 global $wgBlockCIDRLimit;
417 return array_merge( parent
::getPossibleErrors(),
418 $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
421 'code' => 'cidrtoobroad',
422 'info' => "IPv4 CIDR ranges broader than /{$wgBlockCIDRLimit['IPv4']} are not accepted"
425 'code' => 'cidrtoobroad',
426 'info' => "IPv6 CIDR ranges broader than /{$wgBlockCIDRLimit['IPv6']} are not accepted"
428 array( 'code' => 'param_ip', 'info' => 'IP parameter is not valid' ),
429 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
430 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
436 public function getExamples() {
438 'api.php?action=query&list=blocks',
439 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
443 public function getHelpUrls() {
444 return 'https://www.mediawiki.org/wiki/API:Blocks';