r86001, now with less scariness :P I took out the delete action and did purge instea...
[lhc/web/wiklou.git] / includes / api / ApiQueryBlocks.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 10, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate all available pages.
34 *
35 * @ingroup API
36 */
37 class ApiQueryBlocks extends ApiQueryBase {
38
39 /**
40 * @var Array
41 */
42 protected $usernames;
43
44 public function __construct( $query, $moduleName ) {
45 parent::__construct( $query, $moduleName, 'bk' );
46 }
47
48 public function execute() {
49 global $wgUser, $wgContLang;
50
51 $params = $this->extractRequestParams();
52 if ( isset( $params['users'] ) && isset( $params['ip'] ) ) {
53 $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
54 }
55
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'] );
67
68 $result = $this->getResult();
69
70 $this->addTables( 'ipblocks' );
71 $this->addFields( 'ipb_auto' );
72
73 if ( $fld_id ) {
74 $this->addFields( 'ipb_id' );
75 }
76 if ( $fld_user || $fld_userid ) {
77 $this->addFields( array( 'ipb_address', 'ipb_user' ) );
78 }
79 if ( $fld_by ) {
80 $this->addFields( 'ipb_by_text' );
81 }
82 if ( $fld_byid ) {
83 $this->addFields( 'ipb_by' );
84 }
85 if ( $fld_timestamp ) {
86 $this->addFields( 'ipb_timestamp' );
87 }
88 if ( $fld_expiry ) {
89 $this->addFields( 'ipb_expiry' );
90 }
91 if ( $fld_reason ) {
92 $this->addFields( 'ipb_reason' );
93 }
94 if ( $fld_range ) {
95 $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
96 }
97 if ( $fld_flags ) {
98 $this->addFields( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
99 }
100
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'] );
105 }
106 if ( isset( $params['users'] ) ) {
107 foreach ( (array)$params['users'] as $u ) {
108 $this->prepareUsername( $u );
109 }
110 $this->addWhereFld( 'ipb_address', $this->usernames );
111 $this->addWhereFld( 'ipb_auto', 0 );
112 }
113 if ( isset( $params['ip'] ) ) {
114 list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
115 if ( $ip && $range ) {
116 // We got a CIDR range
117 if ( $range < 16 )
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 );
121 } else {
122 $lower = $upper = IP::toHex( $params['ip'] );
123 }
124 $prefix = substr( $lower, 0, 4 );
125
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'",
131 'ipb_auto' => 0
132 ) );
133 }
134
135 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
136 $this->addWhereFld( 'ipb_deleted', 0 );
137 }
138
139 // Purge expired entries on one in every 10 queries
140 if ( !mt_rand( 0, 10 ) ) {
141 Block::purgeExpired();
142 }
143
144 $res = $this->select( __METHOD__ );
145
146 $count = 0;
147 foreach ( $res as $row ) {
148 if ( ++$count > $params['limit'] ) {
149 // We've had enough
150 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
151 break;
152 }
153 $block = array();
154 if ( $fld_id ) {
155 $block['id'] = $row->ipb_id;
156 }
157 if ( $fld_user && !$row->ipb_auto ) {
158 $block['user'] = $row->ipb_address;
159 }
160 if ( $fld_userid && !$row->ipb_auto ) {
161 $block['userid'] = $row->ipb_user;
162 }
163 if ( $fld_by ) {
164 $block['by'] = $row->ipb_by_text;
165 }
166 if ( $fld_byid ) {
167 $block['byid'] = $row->ipb_by;
168 }
169 if ( $fld_timestamp ) {
170 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
171 }
172 if ( $fld_expiry ) {
173 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
174 }
175 if ( $fld_reason ) {
176 $block['reason'] = $row->ipb_reason;
177 }
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 );
181 }
182 if ( $fld_flags ) {
183 // For clarity, these flags use the same names as their action=block counterparts
184 if ( $row->ipb_auto ) {
185 $block['automatic'] = '';
186 }
187 if ( $row->ipb_anon_only ) {
188 $block['anononly'] = '';
189 }
190 if ( $row->ipb_create_account ) {
191 $block['nocreate'] = '';
192 }
193 if ( $row->ipb_enable_autoblock ) {
194 $block['autoblock'] = '';
195 }
196 if ( $row->ipb_block_email ) {
197 $block['noemail'] = '';
198 }
199 if ( $row->ipb_deleted ) {
200 $block['hidden'] = '';
201 }
202 if ( $row->ipb_allow_usertalk ) {
203 $block['allowusertalk'] = '';
204 }
205 }
206 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
207 if ( !$fit ) {
208 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
209 break;
210 }
211 }
212 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
213 }
214
215 protected function prepareUsername( $user ) {
216 if ( !$user ) {
217 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
218 }
219 $name = User::isIP( $user )
220 ? $user
221 : User::getCanonicalName( $user, 'valid' );
222 if ( $name === false ) {
223 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
224 }
225 $this->usernames[] = $name;
226 }
227
228 public function getAllowedParams() {
229 return array(
230 'start' => array(
231 ApiBase::PARAM_TYPE => 'timestamp'
232 ),
233 'end' => array(
234 ApiBase::PARAM_TYPE => 'timestamp',
235 ),
236 'dir' => array(
237 ApiBase::PARAM_TYPE => array(
238 'newer',
239 'older'
240 ),
241 ApiBase::PARAM_DFLT => 'older'
242 ),
243 'ids' => array(
244 ApiBase::PARAM_TYPE => 'integer',
245 ApiBase::PARAM_ISMULTI => true
246 ),
247 'users' => array(
248 ApiBase::PARAM_ISMULTI => true
249 ),
250 'ip' => null,
251 'limit' => array(
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
257 ),
258 'prop' => array(
259 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
260 ApiBase::PARAM_TYPE => array(
261 'id',
262 'user',
263 'userid',
264 'by',
265 'byid',
266 'timestamp',
267 'expiry',
268 'reason',
269 'range',
270 'flags'
271 ),
272 ApiBase::PARAM_ISMULTI => true
273 )
274 );
275 }
276
277 public function getParamDescription() {
278 return array(
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',
287 'prop' => array(
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)',
299 ),
300 );
301 }
302
303 public function getDescription() {
304 return 'List all blocked users and IP addresses';
305 }
306
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' ),
313 ) );
314 }
315
316 protected function getExamples() {
317 return array(
318 'api.php?action=query&list=blocks',
319 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
320 );
321 }
322
323 public function getVersion() {
324 return __CLASS__ . ': $Id$';
325 }
326 }