4 * Created on Sep 10, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
32 * Query module to enumerate all available pages.
36 class ApiQueryBlocks
extends ApiQueryBase
{
40 public function __construct( $query, $moduleName ) {
41 parent
:: __construct( $query, $moduleName, 'bk' );
44 public function execute() {
47 $params = $this->extractRequestParams();
48 if ( isset( $params['users'] ) && isset( $params['ip'] ) )
49 $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
51 $prop = array_flip( $params['prop'] );
52 $fld_id = isset( $prop['id'] );
53 $fld_user = isset( $prop['user'] );
54 $fld_by = isset( $prop['by'] );
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();
62 $pageSet = $this->getPageSet();
63 $titles = $pageSet->getTitles();
66 $this->addTables( 'ipblocks' );
68 $this->addFields( 'ipb_id' );
70 $this->addFields( array( 'ipb_address', 'ipb_user', 'ipb_auto' ) );
73 $this->addTables( 'user' );
74 $this->addFields( array( 'ipb_by', 'user_name' ) );
75 $this->addWhere( 'user_id = ipb_by' );
78 $this->addFields( 'ipb_timestamp' );
80 $this->addFields( 'ipb_expiry' );
82 $this->addFields( 'ipb_reason' );
84 $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
86 $this->addFields( array( 'ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
88 $this->addOption( 'LIMIT', $params['limit'] +
1 );
89 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
90 if ( isset( $params['ids'] ) )
91 $this->addWhereFld( 'ipb_id', $params['ids'] );
92 if ( isset( $params['users'] ) )
94 foreach ( (array)$params['users'] as $u )
95 $this->prepareUsername( $u );
96 $this->addWhereFld( 'ipb_address', $this->usernames
);
98 if ( isset( $params['ip'] ) )
100 list( $ip, $range ) = IP
::parseCIDR( $params['ip'] );
103 // We got a CIDR range
105 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
106 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
107 $upper = wfBaseConvert( $ip +
pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
110 $lower = $upper = IP
::toHex( $params['ip'] );
111 $prefix = substr( $lower, 0, 4 );
113 $db = $this->getDB();
114 $this->addWhere( array(
115 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
116 "ipb_range_start <= '$lower'",
117 "ipb_range_end >= '$upper'"
120 if ( !$wgUser->isAllowed( 'hideuser' ) )
121 $this->addWhereFld( 'ipb_deleted', 0 );
123 // Purge expired entries on one in every 10 queries
124 if ( !mt_rand( 0, 10 ) )
125 Block
::purgeExpired();
127 $res = $this->select( __METHOD__
);
130 while ( $row = $res->fetchObject() )
132 if ( ++
$count > $params['limit'] )
135 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
140 $block['id'] = $row->ipb_id
;
141 if ( $fld_user && !$row->ipb_auto
)
142 $block['user'] = $row->ipb_address
;
144 $block['by'] = $row->user_name
;
145 if ( $fld_timestamp )
146 $block['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
148 $block['expiry'] = Block
::decodeExpiry( $row->ipb_expiry
, TS_ISO_8601
);
150 $block['reason'] = $row->ipb_reason
;
153 $block['rangestart'] = IP
::hexToQuad( $row->ipb_range_start
);
154 $block['rangeend'] = IP
::hexToQuad( $row->ipb_range_end
);
158 // For clarity, these flags use the same names as their action=block counterparts
159 if ( $row->ipb_auto
)
160 $block['automatic'] = '';
161 if ( $row->ipb_anon_only
)
162 $block['anononly'] = '';
163 if ( $row->ipb_create_account
)
164 $block['nocreate'] = '';
165 if ( $row->ipb_enable_autoblock
)
166 $block['autoblock'] = '';
167 if ( $row->ipb_block_email
)
168 $block['noemail'] = '';
169 if ( $row->ipb_deleted
)
170 $block['hidden'] = '';
171 if ( $row->ipb_allow_usertalk
)
172 $block['allowusertalk'] = '';
174 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
177 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
181 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
184 protected function prepareUsername( $user )
187 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
188 $name = User
::isIP( $user )
190 : User
::getCanonicalName( $user, 'valid' );
191 if ( $name === false )
192 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
193 $this->usernames
[] = $name;
196 public function getAllowedParams() {
199 ApiBase
:: PARAM_TYPE
=> 'timestamp'
202 ApiBase
:: PARAM_TYPE
=> 'timestamp',
205 ApiBase
:: PARAM_TYPE
=> array(
209 ApiBase
:: PARAM_DFLT
=> 'older'
212 ApiBase
:: PARAM_TYPE
=> 'integer',
213 ApiBase
:: PARAM_ISMULTI
=> true
216 ApiBase
:: PARAM_ISMULTI
=> true
220 ApiBase
:: PARAM_DFLT
=> 10,
221 ApiBase
:: PARAM_TYPE
=> 'limit',
222 ApiBase
:: PARAM_MIN
=> 1,
223 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
224 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
227 ApiBase
:: PARAM_DFLT
=> 'id|user|by|timestamp|expiry|reason|flags',
228 ApiBase
:: PARAM_TYPE
=> array(
238 ApiBase
:: PARAM_ISMULTI
=> true
243 public function getParamDescription() {
245 'start' => 'The timestamp to start enumerating from',
246 'end' => 'The timestamp to stop enumerating at',
247 'dir' => 'The direction in which to enumerate',
248 'ids' => 'Pipe-separated list of block IDs to list (optional)',
249 'users' => 'Pipe-separated list of users to search for (optional)',
250 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
251 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted.' ),
252 'limit' => 'The maximum amount of blocks to list',
253 'prop' => 'Which properties to get',
257 public function getDescription() {
258 return 'List all blocked users and IP addresses.';
261 public function getPossibleErrors() {
262 return array_merge( parent
::getPossibleErrors(), array(
263 array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
264 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
265 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
266 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
270 protected function getExamples() {
271 return array ( 'api.php?action=query&list=blocks',
272 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
276 public function getVersion() {
277 return __CLASS__
. ': $Id$';