Call Linker methods statically
[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 user blocks
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 $this->requireMaxOneParameter( $params, 'users', 'ip' );
53
54 $prop = array_flip( $params['prop'] );
55 $fld_id = isset( $prop['id'] );
56 $fld_user = isset( $prop['user'] );
57 $fld_userid = isset( $prop['userid'] );
58 $fld_by = isset( $prop['by'] );
59 $fld_byid = isset( $prop['byid'] );
60 $fld_timestamp = isset( $prop['timestamp'] );
61 $fld_expiry = isset( $prop['expiry'] );
62 $fld_reason = isset( $prop['reason'] );
63 $fld_range = isset( $prop['range'] );
64 $fld_flags = isset( $prop['flags'] );
65
66 $result = $this->getResult();
67
68 $this->addTables( 'ipblocks' );
69 $this->addFields( 'ipb_auto' );
70
71 $this->addFieldsIf ( 'ipb_id', $fld_id );
72 $this->addFieldsIf( array( 'ipb_address', 'ipb_user' ), $fld_user || $fld_userid );
73 $this->addFieldsIf( 'ipb_by_text', $fld_by );
74 $this->addFieldsIf( 'ipb_by', $fld_byid );
75 $this->addFieldsIf( 'ipb_timestamp', $fld_timestamp );
76 $this->addFieldsIf( 'ipb_expiry', $fld_expiry );
77 $this->addFieldsIf( 'ipb_reason', $fld_reason );
78 $this->addFieldsIf( array( 'ipb_range_start', 'ipb_range_end' ), $fld_range );
79 $this->addFieldsIf( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock',
80 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ),
81 $fld_flags );
82
83 $this->addOption( 'LIMIT', $params['limit'] + 1 );
84 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
85 if ( isset( $params['ids'] ) ) {
86 $this->addWhereFld( 'ipb_id', $params['ids'] );
87 }
88 if ( isset( $params['users'] ) ) {
89 foreach ( (array)$params['users'] as $u ) {
90 $this->prepareUsername( $u );
91 }
92 $this->addWhereFld( 'ipb_address', $this->usernames );
93 $this->addWhereFld( 'ipb_auto', 0 );
94 }
95 if ( isset( $params['ip'] ) ) {
96 list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
97 if ( $ip && $range ) {
98 // We got a CIDR range
99 if ( $range < 16 )
100 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
101 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
102 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
103 } else {
104 $lower = $upper = IP::toHex( $params['ip'] );
105 }
106 $prefix = substr( $lower, 0, 4 );
107
108 $db = $this->getDB();
109 $this->addWhere( array(
110 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
111 "ipb_range_start <= '$lower'",
112 "ipb_range_end >= '$upper'",
113 'ipb_auto' => 0
114 ) );
115 }
116
117 if ( !is_null( $params['show'] ) ) {
118 $show = array_flip( $params['show'] );
119
120 /* Check for conflicting parameters. */
121 if ( ( isset ( $show['account'] ) && isset ( $show['!account'] ) )
122 || ( isset ( $show['ip'] ) && isset ( $show['!ip'] ) )
123 || ( isset ( $show['range'] ) && isset ( $show['!range'] ) )
124 || ( isset ( $show['temp'] ) && isset ( $show['!temp'] ) )
125 ) {
126 $this->dieUsageMsg( 'show' );
127 }
128
129 $this->addWhereIf( 'ipb_user = 0', isset( $show['!account'] ) );
130 $this->addWhereIf( 'ipb_user != 0', isset( $show['account'] ) );
131 $this->addWhereIf( 'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show['!ip'] ) );
132 $this->addWhereIf( 'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show['ip'] ) );
133 $this->addWhereIf( "ipb_expiry = 'infinity'", isset( $show['!temp'] ) );
134 $this->addWhereIf( "ipb_expiry != 'infinity'", isset( $show['temp'] ) );
135 $this->addWhereIf( "ipb_range_end = ipb_range_start", isset( $show['!range'] ) );
136 $this->addWhereIf( "ipb_range_end > ipb_range_start", isset( $show['range'] ) );
137 }
138
139 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
140 $this->addWhereFld( 'ipb_deleted', 0 );
141 }
142
143 // Purge expired entries on one in every 10 queries
144 if ( !mt_rand( 0, 10 ) ) {
145 Block::purgeExpired();
146 }
147
148 $res = $this->select( __METHOD__ );
149
150 $count = 0;
151 foreach ( $res as $row ) {
152 if ( ++$count > $params['limit'] ) {
153 // We've had enough
154 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
155 break;
156 }
157 $block = array();
158 if ( $fld_id ) {
159 $block['id'] = $row->ipb_id;
160 }
161 if ( $fld_user && !$row->ipb_auto ) {
162 $block['user'] = $row->ipb_address;
163 }
164 if ( $fld_userid && !$row->ipb_auto ) {
165 $block['userid'] = $row->ipb_user;
166 }
167 if ( $fld_by ) {
168 $block['by'] = $row->ipb_by_text;
169 }
170 if ( $fld_byid ) {
171 $block['byid'] = $row->ipb_by;
172 }
173 if ( $fld_timestamp ) {
174 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
175 }
176 if ( $fld_expiry ) {
177 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
178 }
179 if ( $fld_reason ) {
180 $block['reason'] = $row->ipb_reason;
181 }
182 if ( $fld_range && !$row->ipb_auto ) {
183 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start );
184 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end );
185 }
186 if ( $fld_flags ) {
187 // For clarity, these flags use the same names as their action=block counterparts
188 if ( $row->ipb_auto ) {
189 $block['automatic'] = '';
190 }
191 if ( $row->ipb_anon_only ) {
192 $block['anononly'] = '';
193 }
194 if ( $row->ipb_create_account ) {
195 $block['nocreate'] = '';
196 }
197 if ( $row->ipb_enable_autoblock ) {
198 $block['autoblock'] = '';
199 }
200 if ( $row->ipb_block_email ) {
201 $block['noemail'] = '';
202 }
203 if ( $row->ipb_deleted ) {
204 $block['hidden'] = '';
205 }
206 if ( $row->ipb_allow_usertalk ) {
207 $block['allowusertalk'] = '';
208 }
209 }
210 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
211 if ( !$fit ) {
212 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
213 break;
214 }
215 }
216 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
217 }
218
219 protected function prepareUsername( $user ) {
220 if ( !$user ) {
221 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
222 }
223 $name = User::isIP( $user )
224 ? $user
225 : User::getCanonicalName( $user, 'valid' );
226 if ( $name === false ) {
227 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
228 }
229 $this->usernames[] = $name;
230 }
231
232 public function getAllowedParams() {
233 return array(
234 'start' => array(
235 ApiBase::PARAM_TYPE => 'timestamp'
236 ),
237 'end' => array(
238 ApiBase::PARAM_TYPE => 'timestamp',
239 ),
240 'dir' => array(
241 ApiBase::PARAM_TYPE => array(
242 'newer',
243 'older'
244 ),
245 ApiBase::PARAM_DFLT => 'older'
246 ),
247 'ids' => array(
248 ApiBase::PARAM_TYPE => 'integer',
249 ApiBase::PARAM_ISMULTI => true
250 ),
251 'users' => array(
252 ApiBase::PARAM_ISMULTI => true
253 ),
254 'ip' => null,
255 'limit' => array(
256 ApiBase::PARAM_DFLT => 10,
257 ApiBase::PARAM_TYPE => 'limit',
258 ApiBase::PARAM_MIN => 1,
259 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
260 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
261 ),
262 'prop' => array(
263 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
264 ApiBase::PARAM_TYPE => array(
265 'id',
266 'user',
267 'userid',
268 'by',
269 'byid',
270 'timestamp',
271 'expiry',
272 'reason',
273 'range',
274 'flags'
275 ),
276 ApiBase::PARAM_ISMULTI => true
277 ),
278 'show' => array(
279 ApiBase::PARAM_TYPE => array(
280 'account',
281 '!account',
282 'temp',
283 '!temp',
284 'ip',
285 '!ip',
286 'range',
287 '!range',
288 ),
289 ApiBase::PARAM_ISMULTI => true
290 ),
291 );
292 }
293
294 public function getParamDescription() {
295 $p = $this->getModulePrefix();
296 return array(
297 'start' => 'The timestamp to start enumerating from',
298 'end' => 'The timestamp to stop enumerating at',
299 'dir' => $this->getDirectionDescription( $p ),
300 'ids' => 'Pipe-separated list of block IDs to list (optional)',
301 'users' => 'Pipe-separated list of users to search for (optional)',
302 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
303 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
304 'limit' => 'The maximum amount of blocks to list',
305 'prop' => array(
306 'Which properties to get',
307 ' id - Adds the ID of the block',
308 ' user - Adds the username of the blocked user',
309 ' userid - Adds the user ID of the blocked user',
310 ' by - Adds the username of the blocking user',
311 ' byid - Adds the user ID of the blocking user',
312 ' timestamp - Adds the timestamp of when the block was given',
313 ' expiry - Adds the timestamp of when the block expires',
314 ' reason - Adds the reason given for the block',
315 ' range - Adds the range of IPs affected by the block',
316 ' flags - Tags the ban with (autoblock, anononly, etc)',
317 ),
318 'show' => array(
319 'Show only items that meet this criteria.',
320 "For example, to see only indefinite blocks on IPs, set {$p}show=ip|!temp"
321 ),
322 );
323 }
324
325 public function getDescription() {
326 return 'List all blocked users and IP addresses';
327 }
328
329 public function getPossibleErrors() {
330 return array_merge( parent::getPossibleErrors(), array(
331 $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
332 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
333 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
334 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
335 array( 'show' ),
336 ) );
337 }
338
339 public function getExamples() {
340 return array(
341 'api.php?action=query&list=blocks',
342 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
343 );
344 }
345
346 public function getHelpUrls() {
347 return 'http://www.mediawiki.org/wiki/API:Blocks';
348 }
349
350 public function getVersion() {
351 return __CLASS__ . ': $Id$';
352 }
353 }