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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query module to enumerate all user blocks
37 class ApiQueryBlocks
extends ApiQueryBase
{
44 public function __construct( $query, $moduleName ) {
45 parent
::__construct( $query, $moduleName, 'bk' );
48 public function execute() {
49 global $wgUser, $wgContLang;
51 $params = $this->extractRequestParams();
52 $this->requireMaxOneParameter( $params, 'users', 'ip' );
54 $prop = array_flip( $params['prop'] );
55 $fld_id = isset( $prop['id'] );
56 $fld_user = isset( $prop['user'] );
57 $fld_userid = isset( $prop['userid'] );
58 $fld_by = isset( $prop['by'] );
59 $fld_byid = isset( $prop['byid'] );
60 $fld_timestamp = isset( $prop['timestamp'] );
61 $fld_expiry = isset( $prop['expiry'] );
62 $fld_reason = isset( $prop['reason'] );
63 $fld_range = isset( $prop['range'] );
64 $fld_flags = isset( $prop['flags'] );
66 $result = $this->getResult();
68 $this->addTables( 'ipblocks' );
69 $this->addFields( 'ipb_auto' );
71 $this->addFieldsIf ( 'ipb_id', $fld_id );
72 $this->addFieldsIf( array( 'ipb_address', 'ipb_user' ), $fld_user ||
$fld_userid );
73 $this->addFieldsIf( 'ipb_by_text', $fld_by );
74 $this->addFieldsIf( 'ipb_by', $fld_byid );
75 $this->addFieldsIf( 'ipb_timestamp', $fld_timestamp );
76 $this->addFieldsIf( 'ipb_expiry', $fld_expiry );
77 $this->addFieldsIf( 'ipb_reason', $fld_reason );
78 $this->addFieldsIf( array( 'ipb_range_start', 'ipb_range_end' ), $fld_range );
79 $this->addFieldsIf( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock',
80 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ),
83 $this->addOption( 'LIMIT', $params['limit'] +
1 );
84 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
85 if ( isset( $params['ids'] ) ) {
86 $this->addWhereFld( 'ipb_id', $params['ids'] );
88 if ( isset( $params['users'] ) ) {
89 foreach ( (array)$params['users'] as $u ) {
90 $this->prepareUsername( $u );
92 $this->addWhereFld( 'ipb_address', $this->usernames
);
93 $this->addWhereFld( 'ipb_auto', 0 );
95 if ( isset( $params['ip'] ) ) {
96 list( $ip, $range ) = IP
::parseCIDR( $params['ip'] );
97 if ( $ip && $range ) {
98 // We got a CIDR range
100 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
101 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
102 $upper = wfBaseConvert( $ip +
pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
104 $lower = $upper = IP
::toHex( $params['ip'] );
106 $prefix = substr( $lower, 0, 4 );
108 $db = $this->getDB();
109 $this->addWhere( array(
110 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
111 "ipb_range_start <= '$lower'",
112 "ipb_range_end >= '$upper'",
117 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
118 $this->addWhereFld( 'ipb_deleted', 0 );
121 // Purge expired entries on one in every 10 queries
122 if ( !mt_rand( 0, 10 ) ) {
123 Block
::purgeExpired();
126 $res = $this->select( __METHOD__
);
129 foreach ( $res as $row ) {
130 if ( ++
$count > $params['limit'] ) {
132 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
137 $block['id'] = $row->ipb_id
;
139 if ( $fld_user && !$row->ipb_auto
) {
140 $block['user'] = $row->ipb_address
;
142 if ( $fld_userid && !$row->ipb_auto
) {
143 $block['userid'] = $row->ipb_user
;
146 $block['by'] = $row->ipb_by_text
;
149 $block['byid'] = $row->ipb_by
;
151 if ( $fld_timestamp ) {
152 $block['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
155 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry
, TS_ISO_8601
);
158 $block['reason'] = $row->ipb_reason
;
160 if ( $fld_range && !$row->ipb_auto
) {
161 $block['rangestart'] = IP
::hexToQuad( $row->ipb_range_start
);
162 $block['rangeend'] = IP
::hexToQuad( $row->ipb_range_end
);
165 // For clarity, these flags use the same names as their action=block counterparts
166 if ( $row->ipb_auto
) {
167 $block['automatic'] = '';
169 if ( $row->ipb_anon_only
) {
170 $block['anononly'] = '';
172 if ( $row->ipb_create_account
) {
173 $block['nocreate'] = '';
175 if ( $row->ipb_enable_autoblock
) {
176 $block['autoblock'] = '';
178 if ( $row->ipb_block_email
) {
179 $block['noemail'] = '';
181 if ( $row->ipb_deleted
) {
182 $block['hidden'] = '';
184 if ( $row->ipb_allow_usertalk
) {
185 $block['allowusertalk'] = '';
188 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
190 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
194 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
197 protected function prepareUsername( $user ) {
199 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
201 $name = User
::isIP( $user )
203 : User
::getCanonicalName( $user, 'valid' );
204 if ( $name === false ) {
205 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
207 $this->usernames
[] = $name;
210 public function getAllowedParams() {
213 ApiBase
::PARAM_TYPE
=> 'timestamp'
216 ApiBase
::PARAM_TYPE
=> 'timestamp',
219 ApiBase
::PARAM_TYPE
=> array(
223 ApiBase
::PARAM_DFLT
=> 'older'
226 ApiBase
::PARAM_TYPE
=> 'integer',
227 ApiBase
::PARAM_ISMULTI
=> true
230 ApiBase
::PARAM_ISMULTI
=> true
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
241 ApiBase
::PARAM_DFLT
=> 'id|user|by|timestamp|expiry|reason|flags',
242 ApiBase
::PARAM_TYPE
=> array(
254 ApiBase
::PARAM_ISMULTI
=> true
259 public function getParamDescription() {
261 'start' => 'The timestamp to start enumerating from',
262 'end' => 'The timestamp to stop enumerating at',
263 'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
264 'ids' => 'Pipe-separated list of block IDs to list (optional)',
265 'users' => 'Pipe-separated list of users to search for (optional)',
266 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
267 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
268 'limit' => 'The maximum amount of blocks to list',
270 'Which properties to get',
271 ' id - Adds the ID of the block',
272 ' user - Adds the username of the blocked user',
273 ' userid - Adds the user ID of the blocked user',
274 ' by - Adds the username of the blocking user',
275 ' byid - Adds the user ID of the blocking user',
276 ' timestamp - Adds the timestamp of when the block was given',
277 ' expiry - Adds the timestamp of when the block expires',
278 ' reason - Adds the reason given for the block',
279 ' range - Adds the range of IPs affected by the block',
280 ' flags - Tags the ban with (autoblock, anononly, etc)',
285 public function getDescription() {
286 return 'List all blocked users and IP addresses';
289 public function getPossibleErrors() {
290 return array_merge( parent
::getPossibleErrors(), array(
291 $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
292 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
293 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
294 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
298 protected function getExamples() {
300 'api.php?action=query&list=blocks',
301 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
305 public function getVersion() {
306 return __CLASS__
. ': $Id$';