1563f48bc9e8f38884bc4a3590efa9ee2030c5a0
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 available pages.
37 class ApiQueryBlocks
extends ApiQueryBase
{
41 public function __construct( $query, $moduleName ) {
42 parent
::__construct( $query, $moduleName, 'bk' );
45 public function execute() {
48 $params = $this->extractRequestParams();
49 if ( isset( $params['users'] ) && isset( $params['ip'] ) ) {
50 $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
53 $prop = array_flip( $params['prop'] );
54 $fld_id = isset( $prop['id'] );
55 $fld_user = isset( $prop['user'] );
56 $fld_by = isset( $prop['by'] );
57 $fld_timestamp = isset( $prop['timestamp'] );
58 $fld_expiry = isset( $prop['expiry'] );
59 $fld_reason = isset( $prop['reason'] );
60 $fld_range = isset( $prop['range'] );
61 $fld_flags = isset( $prop['flags'] );
63 $result = $this->getResult();
65 $this->addTables( 'ipblocks' );
66 $this->addFields( 'ipb_auto' );
69 $this->addFields( 'ipb_id' );
72 $this->addFields( array( 'ipb_address', 'ipb_user' ) );
75 $this->addTables( 'user' );
76 $this->addFields( array( 'ipb_by', 'user_name' ) );
77 $this->addWhere( 'user_id = ipb_by' );
79 if ( $fld_timestamp ) {
80 $this->addFields( 'ipb_timestamp' );
83 $this->addFields( 'ipb_expiry' );
86 $this->addFields( 'ipb_reason' );
89 $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
92 $this->addFields( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
95 $this->addOption( 'LIMIT', $params['limit'] +
1 );
96 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
97 if ( isset( $params['ids'] ) ) {
98 $this->addWhereFld( 'ipb_id', $params['ids'] );
100 if ( isset( $params['users'] ) ) {
101 foreach ( (array)$params['users'] as $u ) {
102 $this->prepareUsername( $u );
104 $this->addWhereFld( 'ipb_address', $this->usernames
);
105 $this->addWhereFld( 'ipb_auto', 0 );
107 if ( isset( $params['ip'] ) ) {
108 list( $ip, $range ) = IP
::parseCIDR( $params['ip'] );
109 if ( $ip && $range ) {
110 // We got a CIDR range
112 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
113 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
114 $upper = wfBaseConvert( $ip +
pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
116 $lower = $upper = IP
::toHex( $params['ip'] );
118 $prefix = substr( $lower, 0, 4 );
120 $db = $this->getDB();
121 $this->addWhere( array(
122 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
123 "ipb_range_start <= '$lower'",
124 "ipb_range_end >= '$upper'",
129 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
130 $this->addWhereFld( 'ipb_deleted', 0 );
133 // Purge expired entries on one in every 10 queries
134 if ( !mt_rand( 0, 10 ) ) {
135 Block
::purgeExpired();
138 $res = $this->select( __METHOD__
);
141 foreach ( $res as $row ) {
142 if ( ++
$count > $params['limit'] ) {
144 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
149 $block['id'] = $row->ipb_id
;
151 if ( $fld_user && !$row->ipb_auto
) {
152 $block['user'] = $row->ipb_address
;
155 $block['by'] = $row->user_name
;
157 if ( $fld_timestamp ) {
158 $block['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
161 $block['expiry'] = Block
::decodeExpiry( $row->ipb_expiry
, TS_ISO_8601
);
164 $block['reason'] = $row->ipb_reason
;
166 if ( $fld_range && !$row->ipb_auto
) {
167 $block['rangestart'] = IP
::hexToQuad( $row->ipb_range_start
);
168 $block['rangeend'] = IP
::hexToQuad( $row->ipb_range_end
);
171 // For clarity, these flags use the same names as their action=block counterparts
172 if ( $row->ipb_auto
) {
173 $block['automatic'] = '';
175 if ( $row->ipb_anon_only
) {
176 $block['anononly'] = '';
178 if ( $row->ipb_create_account
) {
179 $block['nocreate'] = '';
181 if ( $row->ipb_enable_autoblock
) {
182 $block['autoblock'] = '';
184 if ( $row->ipb_block_email
) {
185 $block['noemail'] = '';
187 if ( $row->ipb_deleted
) {
188 $block['hidden'] = '';
190 if ( $row->ipb_allow_usertalk
) {
191 $block['allowusertalk'] = '';
194 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
196 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
200 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
203 protected function prepareUsername( $user ) {
205 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
207 $name = User
::isIP( $user )
209 : User
::getCanonicalName( $user, 'valid' );
210 if ( $name === false ) {
211 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
213 $this->usernames
[] = $name;
216 public function getAllowedParams() {
219 ApiBase
::PARAM_TYPE
=> 'timestamp'
222 ApiBase
::PARAM_TYPE
=> 'timestamp',
225 ApiBase
::PARAM_TYPE
=> array(
229 ApiBase
::PARAM_DFLT
=> 'older'
232 ApiBase
::PARAM_TYPE
=> 'integer',
233 ApiBase
::PARAM_ISMULTI
=> true
236 ApiBase
::PARAM_ISMULTI
=> true
240 ApiBase
::PARAM_DFLT
=> 10,
241 ApiBase
::PARAM_TYPE
=> 'limit',
242 ApiBase
::PARAM_MIN
=> 1,
243 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
244 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
247 ApiBase
::PARAM_DFLT
=> 'id|user|by|timestamp|expiry|reason|flags',
248 ApiBase
::PARAM_TYPE
=> array(
258 ApiBase
::PARAM_ISMULTI
=> true
263 public function getParamDescription() {
265 'start' => 'The timestamp to start enumerating from',
266 'end' => 'The timestamp to stop enumerating at',
267 'dir' => 'The direction in which to enumerate',
268 'ids' => 'Pipe-separated list of block IDs to list (optional)',
269 'users' => 'Pipe-separated list of users to search for (optional)',
270 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
271 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
272 'limit' => 'The maximum amount of blocks to list',
274 'Which properties to get',
275 ' id - Adds the ID of the block',
276 ' user - Adds the username of the blocked user',
277 ' by - Adds the username of the blocking admin',
278 ' timestamp - Adds the timestamp of when the block was given',
279 ' expiry - Adds the timestamp of when the block expires',
280 ' reason - Adds the reason given for the block',
281 ' range - Adds the range of IPs affected by the block',
282 ' flags - Tags the ban with (autoblock, anononly, etc)',
287 public function getDescription() {
288 return 'List all blocked users and IP addresses';
291 public function getPossibleErrors() {
292 return array_merge( parent
::getPossibleErrors(), array(
293 array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
294 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
295 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
296 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
300 protected function getExamples() {
302 'api.php?action=query&list=blocks',
303 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
307 public function getVersion() {
308 return __CLASS__
. ': $Id$';