Index: trunk/extensions/GlobalBlocking/GlobalBlocking.php |
— | — | @@ -37,6 +37,8 @@ |
38 | 38 | $wgSpecialPages['GlobalBlockStatus'] = 'SpecialGlobalBlockStatus'; |
39 | 39 | $wgAutoloadClasses['SpecialRemoveGlobalBlock'] = "$dir/SpecialRemoveGlobalBlock.php"; |
40 | 40 | $wgSpecialPages['RemoveGlobalBlock'] = 'SpecialRemoveGlobalBlock'; |
| 41 | +$wgAutoloadClasses['ApiGlobalBlocks'] = "$dir/ApiGlobalBlocks.php"; |
| 42 | +$wgAPIListModules['globalblocks'] = 'ApiGlobalBlocks'; |
41 | 43 | |
42 | 44 | ## Add global block log |
43 | 45 | $wgLogTypes[] = 'gblblock'; |
Index: trunk/extensions/GlobalBlocking/ApiGlobalBlocks.php |
— | — | @@ -0,0 +1,225 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * Created on Nov 1, 2008 |
| 6 | + * |
| 7 | + * GlobalBlocking extension |
| 8 | + * |
| 9 | + * Copyright (C) 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl |
| 10 | + * |
| 11 | + * This program is free software; you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU General Public License as published by |
| 13 | + * the Free Software Foundation; either version 2 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU General Public License along |
| 22 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | + * http://www.gnu.org/copyleft/gpl.html |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * Query module to enumerate all available pages. |
| 29 | + * |
| 30 | + * @ingroup API |
| 31 | + * @ingroup Extensions |
| 32 | + */ |
| 33 | +class ApiQueryGlobalBlocks extends ApiQueryBase { |
| 34 | + |
| 35 | + public function __construct($query, $moduleName) { |
| 36 | + parent :: __construct($query, $moduleName, 'bg'); |
| 37 | + } |
| 38 | + |
| 39 | + public function execute() { |
| 40 | + global $wgUser; |
| 41 | + $params = $this->extractRequestParams(); |
| 42 | + |
| 43 | + $prop = array_flip($params['prop']); |
| 44 | + $fld_id = isset($prop['id']); |
| 45 | + $fld_address = isset($prop['address']); |
| 46 | + $fld_by = isset($prop['by']); |
| 47 | + $fld_timestamp = isset($prop['timestamp']); |
| 48 | + $fld_expiry = isset($prop['expiry']); |
| 49 | + $fld_reason = isset($prop['reason']); |
| 50 | + $fld_range = isset($prop['range']); |
| 51 | + |
| 52 | + $result = $this->getResult(); |
| 53 | + $pageSet = $this->getPageSet(); |
| 54 | + $titles = $pageSet->getTitles(); |
| 55 | + $data = array(); |
| 56 | + |
| 57 | + $this->addTables('globalblocks'); |
| 58 | + if($fld_id) |
| 59 | + $this->addFields('gb_id'); |
| 60 | + if($fld_address) |
| 61 | + $this->addFields(array('gb_address', 'gb_anon_only')); |
| 62 | + if($fld_by) |
| 63 | + $this->addFields(array('gb_by', 'gb_by_wiki')); |
| 64 | + if($fld_timestamp) |
| 65 | + $this->addFields('gb_timestamp'); |
| 66 | + if($fld_expiry) |
| 67 | + $this->addFields('gb_expiry'); |
| 68 | + if($fld_reason) |
| 69 | + $this->addFields('gb_reason'); |
| 70 | + if($fld_range) |
| 71 | + $this->addFields(array('gb_range_start', 'gb_range_end')); |
| 72 | + |
| 73 | + $this->addOption('LIMIT', $params['limit'] + 1); |
| 74 | + $this->addWhereRange('gb_timestamp', $params['dir'], $params['start'], $params['end']); |
| 75 | + if(isset($params['ids'])) |
| 76 | + $this->addWhereFld('gb_id', $params['ids']); |
| 77 | + if(isset($params['addresses'])) |
| 78 | + $this->addWhereFld('gb_address', $params['addresses']); |
| 79 | + if(isset($params['ip'])) |
| 80 | + { |
| 81 | + list($ip, $range) = IP::parseCIDR($params['ip']); |
| 82 | + if($ip && $range) |
| 83 | + { |
| 84 | + # We got a CIDR range |
| 85 | + if($range < 16) |
| 86 | + $this->dieUsage('CIDR ranges broader than /16 are not accepted', 'cidrtoobroad'); |
| 87 | + $lower = wfBaseConvert($ip, 10, 16, 8, false); |
| 88 | + $upper = wfBaseConvert($ip + pow(2, 32 - $range) - 1, 10, 16, 8, false); |
| 89 | + } |
| 90 | + else |
| 91 | + $lower = $upper = IP::toHex($params['ip']); |
| 92 | + $prefix = substr($lower, 0, 4); |
| 93 | + $this->addWhere(array( |
| 94 | + "gb_range_start LIKE '$prefix%'", |
| 95 | + "gb_range_start <= '$lower'", |
| 96 | + "gb_range_end >= '$upper'" |
| 97 | + )); |
| 98 | + } |
| 99 | + |
| 100 | + $res = $this->select(__METHOD__); |
| 101 | + |
| 102 | + $count = 0; |
| 103 | + while($row = $res->fetchObject()) |
| 104 | + { |
| 105 | + if(++$count > $params['limit']) |
| 106 | + { |
| 107 | + // We've had enough |
| 108 | + $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->gb_timestamp)); |
| 109 | + break; |
| 110 | + } |
| 111 | + $block = array(); |
| 112 | + if($fld_id) |
| 113 | + $block['id'] = $row->gb_id; |
| 114 | + if($fld_address) |
| 115 | + { |
| 116 | + $block['address'] = $row->gb_address; |
| 117 | + if($row->gb_anon_only) |
| 118 | + $block['anononly'] = ''; |
| 119 | + } |
| 120 | + if($fld_by) |
| 121 | + { |
| 122 | + $block['by'] = $row->gb_by; |
| 123 | + $block['bywiki'] = $row->gb_by_wiki; |
| 124 | + } |
| 125 | + if($fld_timestamp) |
| 126 | + $block['timestamp'] = wfTimestamp(TS_ISO_8601, $row->gb_timestamp); |
| 127 | + if($fld_expiry) |
| 128 | + $block['expiry'] = Block::decodeExpiry($row->gb_expiry, TS_ISO_8601); |
| 129 | + if($fld_reason) |
| 130 | + $block['reason'] = $row->gb_reason; |
| 131 | + if($fld_range) |
| 132 | + { |
| 133 | + $block['rangestart'] = self::convertHexIP($row->gb_range_start); |
| 134 | + $block['rangeend'] = self::convertHexIP($row->gb_range_end); |
| 135 | + } |
| 136 | + $data[] = $block; |
| 137 | + } |
| 138 | + $result->setIndexedTagName($data, 'block'); |
| 139 | + $result->addValue('query', $this->getModuleName(), $data); |
| 140 | + } |
| 141 | + |
| 142 | + protected static function convertHexIP($ip) |
| 143 | + { |
| 144 | + // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format |
| 145 | + $dec = wfBaseConvert($ip, 16, 10); |
| 146 | + $parts[3] = $dec % 256; |
| 147 | + $dec /= 256; |
| 148 | + $parts[2] = $dec % 256; |
| 149 | + $dec /= 256; |
| 150 | + $parts[1] = $dec % 256; |
| 151 | + $parts[0] = $dec / 256; |
| 152 | + } |
| 153 | + |
| 154 | + public function getAllowedParams() { |
| 155 | + return array ( |
| 156 | + 'start' => array( |
| 157 | + ApiBase :: PARAM_TYPE => 'timestamp' |
| 158 | + ), |
| 159 | + 'end' => array( |
| 160 | + ApiBase :: PARAM_TYPE => 'timestamp', |
| 161 | + ), |
| 162 | + 'dir' => array( |
| 163 | + ApiBase :: PARAM_TYPE => array( |
| 164 | + 'newer', |
| 165 | + 'older' |
| 166 | + ), |
| 167 | + ApiBase :: PARAM_DFLT => 'older' |
| 168 | + ), |
| 169 | + 'ids' => array( |
| 170 | + ApiBase :: PARAM_TYPE => 'integer', |
| 171 | + ApiBase :: PARAM_ISMULTI => true |
| 172 | + ), |
| 173 | + 'addresses' => array( |
| 174 | + ApiBase :: PARAM_ISMULTI => true |
| 175 | + ), |
| 176 | + 'limit' => array( |
| 177 | + ApiBase :: PARAM_DFLT => 10, |
| 178 | + ApiBase :: PARAM_TYPE => 'limit', |
| 179 | + ApiBase :: PARAM_MIN => 1, |
| 180 | + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, |
| 181 | + ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 |
| 182 | + ), |
| 183 | + 'prop' => array( |
| 184 | + ApiBase :: PARAM_DFLT => 'id|address|by|timestamp|expiry|reason', |
| 185 | + ApiBase :: PARAM_TYPE => array( |
| 186 | + 'id', |
| 187 | + 'address', |
| 188 | + 'by', |
| 189 | + 'timestamp', |
| 190 | + 'expiry', |
| 191 | + 'reason', |
| 192 | + 'range', |
| 193 | + ), |
| 194 | + ApiBase :: PARAM_ISMULTI => true |
| 195 | + ) |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + public function getParamDescription() { |
| 200 | + return array ( |
| 201 | + 'start' => 'The timestamp to start enumerating from', |
| 202 | + 'end' => 'The timestamp to stop enumerating at', |
| 203 | + 'dir' => 'The direction in which to enumerate', |
| 204 | + 'ids' => 'Pipe-separated list of block IDs to list (optional)', |
| 205 | + 'addresses' => 'Pipe-separated list of addresses to search for (optional)', |
| 206 | + 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.', |
| 207 | + 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted.'), |
| 208 | + 'limit' => 'The maximum amount of blocks to list', |
| 209 | + 'prop' => 'Which properties to get', |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + public function getDescription() { |
| 214 | + return 'List all globally blocked IP addresses.'; |
| 215 | + } |
| 216 | + |
| 217 | + protected function getExamples() { |
| 218 | + return array ( 'api.php?action=query&list=globalblocks', |
| 219 | + 'api.php?action=query&list=globalblocks&bgip=217.121.114.116' |
| 220 | + ); |
| 221 | + } |
| 222 | + |
| 223 | + public function getVersion() { |
| 224 | + return __CLASS__ . ': $Id$'; |
| 225 | + } |
| 226 | +} |
Property changes on: trunk/extensions/GlobalBlocking/ApiGlobalBlocks.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 227 | + native |
Name: svn:keywords |
2 | 228 | + Id |