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( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
83 if ( isset( $params['ids'] ) ) {
84 $this->addWhereFld( 'ipb_id', $params['ids'] );
86 if ( isset( $params['users'] ) ) {
87 foreach ( (array)$params['users'] as $u ) {
88 $this->prepareUsername( $u );
90 $this->addWhereFld( 'ipb_address', $this->usernames
);
91 $this->addWhereFld( 'ipb_auto', 0 );
93 if ( isset( $params['ip'] ) ) {
94 list( $ip, $range ) = IP
::parseCIDR( $params['ip'] );
95 if ( $ip && $range ) {
96 // We got a CIDR range
98 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
99 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
100 $upper = wfBaseConvert( $ip +
pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
102 $lower = $upper = IP
::toHex( $params['ip'] );
104 $prefix = substr( $lower, 0, 4 );
106 $this->addWhere( array(
107 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
108 "ipb_range_start <= '$lower'",
109 "ipb_range_end >= '$upper'",
114 if ( !is_null( $params['show'] ) ) {
115 $show = array_flip( $params['show'] );
117 /* Check for conflicting parameters. */
118 if ( ( isset ( $show['account'] ) && isset ( $show['!account'] ) )
119 ||
( isset ( $show['ip'] ) && isset ( $show['!ip'] ) )
120 ||
( isset ( $show['range'] ) && isset ( $show['!range'] ) )
121 ||
( isset ( $show['temp'] ) && isset ( $show['!temp'] ) )
123 $this->dieUsageMsg( 'show' );
126 $this->addWhereIf( 'ipb_user = 0', isset( $show['!account'] ) );
127 $this->addWhereIf( 'ipb_user != 0', isset( $show['account'] ) );
128 $this->addWhereIf( 'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show['!ip'] ) );
129 $this->addWhereIf( 'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show['ip'] ) );
130 $this->addWhereIf( 'ipb_expiry = '.$db->addQuotes($db->getInfinity()), isset( $show['!temp'] ) );
131 $this->addWhereIf( 'ipb_expiry != '.$db->addQuotes($db->getInfinity()), isset( $show['temp'] ) );
132 $this->addWhereIf( "ipb_range_end = ipb_range_start", isset( $show['!range'] ) );
133 $this->addWhereIf( "ipb_range_end > ipb_range_start", isset( $show['range'] ) );
136 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
137 $this->addWhereFld( 'ipb_deleted', 0 );
140 // Purge expired entries on one in every 10 queries
141 if ( !mt_rand( 0, 10 ) ) {
142 Block
::purgeExpired();
145 $res = $this->select( __METHOD__
);
148 foreach ( $res as $row ) {
149 if ( ++
$count > $params['limit'] ) {
151 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
156 $block['id'] = $row->ipb_id
;
158 if ( $fld_user && !$row->ipb_auto
) {
159 $block['user'] = $row->ipb_address
;
161 if ( $fld_userid && !$row->ipb_auto
) {
162 $block['userid'] = $row->ipb_user
;
165 $block['by'] = $row->ipb_by_text
;
168 $block['byid'] = $row->ipb_by
;
170 if ( $fld_timestamp ) {
171 $block['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
174 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry
, TS_ISO_8601
);
177 $block['reason'] = $row->ipb_reason
;
179 if ( $fld_range && !$row->ipb_auto
) {
180 $block['rangestart'] = IP
::hexToQuad( $row->ipb_range_start
);
181 $block['rangeend'] = IP
::hexToQuad( $row->ipb_range_end
);
184 // For clarity, these flags use the same names as their action=block counterparts
185 if ( $row->ipb_auto
) {
186 $block['automatic'] = '';
188 if ( $row->ipb_anon_only
) {
189 $block['anononly'] = '';
191 if ( $row->ipb_create_account
) {
192 $block['nocreate'] = '';
194 if ( $row->ipb_enable_autoblock
) {
195 $block['autoblock'] = '';
197 if ( $row->ipb_block_email
) {
198 $block['noemail'] = '';
200 if ( $row->ipb_deleted
) {
201 $block['hidden'] = '';
203 if ( $row->ipb_allow_usertalk
) {
204 $block['allowusertalk'] = '';
207 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
209 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
213 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
216 protected function prepareUsername( $user ) {
218 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
220 $name = User
::isIP( $user )
222 : User
::getCanonicalName( $user, 'valid' );
223 if ( $name === false ) {
224 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
226 $this->usernames
[] = $name;
229 public function getAllowedParams() {
232 ApiBase
::PARAM_TYPE
=> 'timestamp'
235 ApiBase
::PARAM_TYPE
=> 'timestamp',
238 ApiBase
::PARAM_TYPE
=> array(
242 ApiBase
::PARAM_DFLT
=> 'older'
245 ApiBase
::PARAM_TYPE
=> 'integer',
246 ApiBase
::PARAM_ISMULTI
=> true
249 ApiBase
::PARAM_ISMULTI
=> true
253 ApiBase
::PARAM_DFLT
=> 10,
254 ApiBase
::PARAM_TYPE
=> 'limit',
255 ApiBase
::PARAM_MIN
=> 1,
256 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
257 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
260 ApiBase
::PARAM_DFLT
=> 'id|user|by|timestamp|expiry|reason|flags',
261 ApiBase
::PARAM_TYPE
=> array(
273 ApiBase
::PARAM_ISMULTI
=> true
276 ApiBase
::PARAM_TYPE
=> array(
286 ApiBase
::PARAM_ISMULTI
=> true
291 public function getParamDescription() {
292 $p = $this->getModulePrefix();
294 'start' => 'The timestamp to start enumerating from',
295 'end' => 'The timestamp to stop enumerating at',
296 'dir' => $this->getDirectionDescription( $p ),
297 'ids' => 'Pipe-separated list of block IDs to list (optional)',
298 'users' => 'Pipe-separated list of users to search for (optional)',
299 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
300 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
301 'limit' => 'The maximum amount of blocks to list',
303 'Which properties to get',
304 ' id - Adds the ID of the block',
305 ' user - Adds the username of the blocked user',
306 ' userid - Adds the user ID of the blocked user',
307 ' by - Adds the username of the blocking user',
308 ' byid - Adds the user ID of the blocking user',
309 ' timestamp - Adds the timestamp of when the block was given',
310 ' expiry - Adds the timestamp of when the block expires',
311 ' reason - Adds the reason given for the block',
312 ' range - Adds the range of IPs affected by the block',
313 ' flags - Tags the ban with (autoblock, anononly, etc)',
316 'Show only items that meet this criteria.',
317 "For example, to see only indefinite blocks on IPs, set {$p}show=ip|!temp"
322 public function getDescription() {
323 return 'List all blocked users and IP addresses';
326 public function getPossibleErrors() {
327 return array_merge( parent
::getPossibleErrors(), array(
328 $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
329 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
330 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
331 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
336 public function getExamples() {
338 'api.php?action=query&list=blocks',
339 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
343 public function getHelpUrls() {
344 return 'https://www.mediawiki.org/wiki/API:Blocks';
347 public function getVersion() {
348 return __CLASS__
. ': $Id$';