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
{
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 if ( isset( $params['users'] ) && isset( $params['ip'] ) ) {
53 $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
56 $prop = array_flip( $params['prop'] );
57 $fld_id = isset( $prop['id'] );
58 $fld_user = isset( $prop['user'] );
59 $fld_userid = isset( $prop['userid'] );
60 $fld_by = isset( $prop['by'] );
61 $fld_byid = isset( $prop['byid'] );
62 $fld_timestamp = isset( $prop['timestamp'] );
63 $fld_expiry = isset( $prop['expiry'] );
64 $fld_reason = isset( $prop['reason'] );
65 $fld_range = isset( $prop['range'] );
66 $fld_flags = isset( $prop['flags'] );
68 $result = $this->getResult();
70 $this->addTables( 'ipblocks' );
71 $this->addFields( 'ipb_auto' );
74 $this->addFields( 'ipb_id' );
76 if ( $fld_user ||
$fld_userid ) {
77 $this->addFields( array( 'ipb_address', 'ipb_user' ) );
80 $this->addFields( 'ipb_by_text' );
83 $this->addFields( 'ipb_by' );
85 if ( $fld_timestamp ) {
86 $this->addFields( 'ipb_timestamp' );
89 $this->addFields( 'ipb_expiry' );
92 $this->addFields( 'ipb_reason' );
95 $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
98 $this->addFields( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
101 $this->addOption( 'LIMIT', $params['limit'] +
1 );
102 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
103 if ( isset( $params['ids'] ) ) {
104 $this->addWhereFld( 'ipb_id', $params['ids'] );
106 if ( isset( $params['users'] ) ) {
107 foreach ( (array)$params['users'] as $u ) {
108 $this->prepareUsername( $u );
110 $this->addWhereFld( 'ipb_address', $this->usernames
);
111 $this->addWhereFld( 'ipb_auto', 0 );
113 if ( isset( $params['ip'] ) ) {
114 list( $ip, $range ) = IP
::parseCIDR( $params['ip'] );
115 if ( $ip && $range ) {
116 // We got a CIDR range
118 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
119 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
120 $upper = wfBaseConvert( $ip +
pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
122 $lower = $upper = IP
::toHex( $params['ip'] );
124 $prefix = substr( $lower, 0, 4 );
126 $db = $this->getDB();
127 $this->addWhere( array(
128 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
129 "ipb_range_start <= '$lower'",
130 "ipb_range_end >= '$upper'",
135 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
136 $this->addWhereFld( 'ipb_deleted', 0 );
139 // Purge expired entries on one in every 10 queries
140 if ( !mt_rand( 0, 10 ) ) {
141 Block
::purgeExpired();
144 $res = $this->select( __METHOD__
);
147 foreach ( $res as $row ) {
148 if ( ++
$count > $params['limit'] ) {
150 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
155 $block['id'] = $row->ipb_id
;
157 if ( $fld_user && !$row->ipb_auto
) {
158 $block['user'] = $row->ipb_address
;
160 if ( $fld_userid && !$row->ipb_auto
) {
161 $block['userid'] = $row->ipb_user
;
164 $block['by'] = $row->ipb_by_text
;
167 $block['byid'] = $row->ipb_by
;
169 if ( $fld_timestamp ) {
170 $block['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
173 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry
, TS_ISO_8601
);
176 $block['reason'] = $row->ipb_reason
;
178 if ( $fld_range && !$row->ipb_auto
) {
179 $block['rangestart'] = IP
::hexToQuad( $row->ipb_range_start
);
180 $block['rangeend'] = IP
::hexToQuad( $row->ipb_range_end
);
183 // For clarity, these flags use the same names as their action=block counterparts
184 if ( $row->ipb_auto
) {
185 $block['automatic'] = '';
187 if ( $row->ipb_anon_only
) {
188 $block['anononly'] = '';
190 if ( $row->ipb_create_account
) {
191 $block['nocreate'] = '';
193 if ( $row->ipb_enable_autoblock
) {
194 $block['autoblock'] = '';
196 if ( $row->ipb_block_email
) {
197 $block['noemail'] = '';
199 if ( $row->ipb_deleted
) {
200 $block['hidden'] = '';
202 if ( $row->ipb_allow_usertalk
) {
203 $block['allowusertalk'] = '';
206 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
208 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
) );
212 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
215 protected function prepareUsername( $user ) {
217 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
219 $name = User
::isIP( $user )
221 : User
::getCanonicalName( $user, 'valid' );
222 if ( $name === false ) {
223 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
225 $this->usernames
[] = $name;
228 public function getAllowedParams() {
231 ApiBase
::PARAM_TYPE
=> 'timestamp'
234 ApiBase
::PARAM_TYPE
=> 'timestamp',
237 ApiBase
::PARAM_TYPE
=> array(
241 ApiBase
::PARAM_DFLT
=> 'older'
244 ApiBase
::PARAM_TYPE
=> 'integer',
245 ApiBase
::PARAM_ISMULTI
=> true
248 ApiBase
::PARAM_ISMULTI
=> true
252 ApiBase
::PARAM_DFLT
=> 10,
253 ApiBase
::PARAM_TYPE
=> 'limit',
254 ApiBase
::PARAM_MIN
=> 1,
255 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
256 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
259 ApiBase
::PARAM_DFLT
=> 'id|user|by|timestamp|expiry|reason|flags',
260 ApiBase
::PARAM_TYPE
=> array(
272 ApiBase
::PARAM_ISMULTI
=> true
277 public function getParamDescription() {
279 'start' => 'The timestamp to start enumerating from',
280 'end' => 'The timestamp to stop enumerating at',
281 'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
282 'ids' => 'Pipe-separated list of block IDs to list (optional)',
283 'users' => 'Pipe-separated list of users to search for (optional)',
284 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
285 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
286 'limit' => 'The maximum amount of blocks to list',
288 'Which properties to get',
289 ' id - Adds the ID of the block',
290 ' user - Adds the username of the blocked user',
291 ' userid - Adds the user ID of the blocked user',
292 ' by - Adds the username of the blocking user',
293 ' byid - Adds the user ID of the blocking user',
294 ' timestamp - Adds the timestamp of when the block was given',
295 ' expiry - Adds the timestamp of when the block expires',
296 ' reason - Adds the reason given for the block',
297 ' range - Adds the range of IPs affected by the block',
298 ' flags - Tags the ban with (autoblock, anononly, etc)',
303 public function getDescription() {
304 return 'List all blocked users and IP addresses';
307 public function getPossibleErrors() {
308 return array_merge( parent
::getPossibleErrors(), array(
309 array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
310 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
311 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
312 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
316 protected function getExamples() {
318 'api.php?action=query&list=blocks',
319 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
323 public function getVersion() {
324 return __CLASS__
. ': $Id$';