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
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'bk');
42 public function execute() {
46 private function run() {
49 $params = $this->extractRequestParams();
50 $prop = array_flip($params['prop']);
51 $fld_id = isset($prop['id']);
52 $fld_user = isset($prop['user']);
53 $fld_by = isset($prop['by']);
54 $fld_timestamp = isset($prop['timestamp']);
55 $fld_expiry = isset($prop['expiry']);
56 $fld_reason = isset($prop['reason']);
57 $fld_range = isset($prop['range']);
58 $fld_flags = isset($prop['flags']);
60 $result = $this->getResult();
61 $pageSet = $this->getPageSet();
62 $titles = $pageSet->getTitles();
65 $this->addTables('ipblocks');
67 $this->addFields('ipb_id');
69 $this->addFields(array('ipb_address', 'ipb_user'));
72 $this->addTables('user');
73 $this->addFields(array('ipb_by', 'user_name'));
74 $this->addWhere('user_id = ipb_by');
77 $this->addFields('ipb_timestamp');
79 $this->addFields('ipb_expiry');
81 $this->addFields('ipb_reason');
83 $this->addFields(array('ipb_range_start', 'ipb_range_end'));
85 $this->addFields(array('ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted'));
87 $this->addOption('LIMIT', $params['limit'] +
1);
88 $this->addWhereRange('ipb_timestamp', $params['dir'], $params['start'], $params['end']);
89 if(isset($params['ids']))
90 $this->addWhere(array('ipb_id' => $params['ids']));
91 if(isset($params['users']))
92 $this->addWhere(array('ipb_address' => $params['users']));
93 if(!$wgUser->isAllowed('oversight'))
94 $this->addWhere(array('ipb_deleted' => 0));
96 // Purge expired entries on one in every 10 queries
98 Block
::purgeExpired();
100 $res = $this->select(__METHOD__
);
104 while($row = $db->fetchObject($res))
106 if($count++
== $params['limit'])
109 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->ipb_timestamp
));
114 $block['id'] = $row->ipb_id
;
115 if($fld_user && !$row->ipb_auto
)
117 $block['user'] = $row->ipb_address
;
121 $block['by'] = $row->user_name
;
124 $block['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->ipb_timestamp
);
126 $block['expiry'] = Block
::decodeExpiry($row->ipb_expiry
, TS_ISO_8601
);
128 $block['reason'] = $row->ipb_reason
;
131 $block['rangestart'] = $this->convertHexIP($row->ipb_range_start
);
132 $block['rangeend'] = $this->convertHexIP($row->ipb_range_end
);
136 // For clarity, these flags use the same names as their action=block counterparts
138 $block['automatic'] = '';
139 if($row->ipb_anon_only
)
140 $block['anononly'] = '';
141 if($row->ipb_create_account
)
142 $block['nocreate'] = '';
143 if($row->ipb_enable_autoblock
)
144 $block['autoblock'] = '';
145 if($row->ipb_block_email
)
146 $block['noemail'] = '';
147 if($row->ipb_deleted
)
148 $block['hidden'] = '';
152 $result->setIndexedTagName($data, 'block');
153 $result->addValue('query', $this->getModuleName(), $data);
156 protected function convertHexIP($ip)
158 // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format
159 $dec = wfBaseConvert($ip, 16, 10);
160 $parts[0] = (int)($dec / (256*256*256));
162 $parts[1] = (int)($dec / (256*256));
164 $parts[2] = (int)($dec / 256);
165 $parts[3] = $dec %
256;
166 return implode('.', $parts);
169 protected function getAllowedParams() {
172 ApiBase
:: PARAM_TYPE
=> 'timestamp'
175 ApiBase
:: PARAM_TYPE
=> 'timestamp',
178 ApiBase
:: PARAM_TYPE
=> array(
182 ApiBase
:: PARAM_DFLT
=> 'older'
185 ApiBase
:: PARAM_TYPE
=> 'integer',
186 ApiBase
:: PARAM_ISMULTI
=> true
189 ApiBase
:: PARAM_ISMULTI
=> true
192 ApiBase
:: PARAM_DFLT
=> 10,
193 ApiBase
:: PARAM_TYPE
=> 'limit',
194 ApiBase
:: PARAM_MIN
=> 1,
195 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
196 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
199 ApiBase
:: PARAM_DFLT
=> 'id|user|by|timestamp|expiry|reason|flags',
200 ApiBase
:: PARAM_TYPE
=> array(
210 ApiBase
:: PARAM_ISMULTI
=> true
215 protected function getParamDescription() {
217 'start' => 'The timestamp to start enumerating from',
218 'end' => 'The timestamp to stop enumerating at',
219 'dir' => 'The direction in which to enumerate',
220 'ids' => 'Pipe-separated list of block IDs to list (optional)',
221 'users' => 'Pipe-separated list of users to search for (optional)',
222 'limit' => 'The maximum amount of blocks to list',
223 'prop' => 'Which properties to get',
227 protected function getDescription() {
228 return 'List all blocked users and IP addresses.';
231 protected function getExamples() {
236 public function getVersion() {
237 return __CLASS__
. ': $Id$';