* (bug 27716) Make a method to do checking of 0 or 1 of the parameters existence...
[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 ( !$wgUser->isAllowed( 'hideuser' ) ) {
118 $this->addWhereFld( 'ipb_deleted', 0 );
119 }
120
121 // Purge expired entries on one in every 10 queries
122 if ( !mt_rand( 0, 10 ) ) {
123 Block::purgeExpired();
124 }
125
126 $res = $this->select( __METHOD__ );
127
128 $count = 0;
129 foreach ( $res as $row ) {
130 if ( ++$count > $params['limit'] ) {
131 // We've had enough
132 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
133 break;
134 }
135 $block = array();
136 if ( $fld_id ) {
137 $block['id'] = $row->ipb_id;
138 }
139 if ( $fld_user && !$row->ipb_auto ) {
140 $block['user'] = $row->ipb_address;
141 }
142 if ( $fld_userid && !$row->ipb_auto ) {
143 $block['userid'] = $row->ipb_user;
144 }
145 if ( $fld_by ) {
146 $block['by'] = $row->ipb_by_text;
147 }
148 if ( $fld_byid ) {
149 $block['byid'] = $row->ipb_by;
150 }
151 if ( $fld_timestamp ) {
152 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
153 }
154 if ( $fld_expiry ) {
155 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
156 }
157 if ( $fld_reason ) {
158 $block['reason'] = $row->ipb_reason;
159 }
160 if ( $fld_range && !$row->ipb_auto ) {
161 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start );
162 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end );
163 }
164 if ( $fld_flags ) {
165 // For clarity, these flags use the same names as their action=block counterparts
166 if ( $row->ipb_auto ) {
167 $block['automatic'] = '';
168 }
169 if ( $row->ipb_anon_only ) {
170 $block['anononly'] = '';
171 }
172 if ( $row->ipb_create_account ) {
173 $block['nocreate'] = '';
174 }
175 if ( $row->ipb_enable_autoblock ) {
176 $block['autoblock'] = '';
177 }
178 if ( $row->ipb_block_email ) {
179 $block['noemail'] = '';
180 }
181 if ( $row->ipb_deleted ) {
182 $block['hidden'] = '';
183 }
184 if ( $row->ipb_allow_usertalk ) {
185 $block['allowusertalk'] = '';
186 }
187 }
188 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
189 if ( !$fit ) {
190 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
191 break;
192 }
193 }
194 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
195 }
196
197 protected function prepareUsername( $user ) {
198 if ( !$user ) {
199 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
200 }
201 $name = User::isIP( $user )
202 ? $user
203 : User::getCanonicalName( $user, 'valid' );
204 if ( $name === false ) {
205 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
206 }
207 $this->usernames[] = $name;
208 }
209
210 public function getAllowedParams() {
211 return array(
212 'start' => array(
213 ApiBase::PARAM_TYPE => 'timestamp'
214 ),
215 'end' => array(
216 ApiBase::PARAM_TYPE => 'timestamp',
217 ),
218 'dir' => array(
219 ApiBase::PARAM_TYPE => array(
220 'newer',
221 'older'
222 ),
223 ApiBase::PARAM_DFLT => 'older'
224 ),
225 'ids' => array(
226 ApiBase::PARAM_TYPE => 'integer',
227 ApiBase::PARAM_ISMULTI => true
228 ),
229 'users' => array(
230 ApiBase::PARAM_ISMULTI => true
231 ),
232 'ip' => null,
233 'limit' => array(
234 ApiBase::PARAM_DFLT => 10,
235 ApiBase::PARAM_TYPE => 'limit',
236 ApiBase::PARAM_MIN => 1,
237 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
238 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
239 ),
240 'prop' => array(
241 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
242 ApiBase::PARAM_TYPE => array(
243 'id',
244 'user',
245 'userid',
246 'by',
247 'byid',
248 'timestamp',
249 'expiry',
250 'reason',
251 'range',
252 'flags'
253 ),
254 ApiBase::PARAM_ISMULTI => true
255 )
256 );
257 }
258
259 public function getParamDescription() {
260 return array(
261 'start' => 'The timestamp to start enumerating from',
262 'end' => 'The timestamp to stop enumerating at',
263 'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
264 'ids' => 'Pipe-separated list of block IDs to list (optional)',
265 'users' => 'Pipe-separated list of users to search for (optional)',
266 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
267 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
268 'limit' => 'The maximum amount of blocks to list',
269 'prop' => array(
270 'Which properties to get',
271 ' id - Adds the ID of the block',
272 ' user - Adds the username of the blocked user',
273 ' userid - Adds the user ID of the blocked user',
274 ' by - Adds the username of the blocking user',
275 ' byid - Adds the user ID of the blocking user',
276 ' timestamp - Adds the timestamp of when the block was given',
277 ' expiry - Adds the timestamp of when the block expires',
278 ' reason - Adds the reason given for the block',
279 ' range - Adds the range of IPs affected by the block',
280 ' flags - Tags the ban with (autoblock, anononly, etc)',
281 ),
282 );
283 }
284
285 public function getDescription() {
286 return 'List all blocked users and IP addresses';
287 }
288
289 public function getPossibleErrors() {
290 return array_merge( parent::getPossibleErrors(), array(
291 $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
292 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
293 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
294 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
295 ) );
296 }
297
298 protected function getExamples() {
299 return array(
300 'api.php?action=query&list=blocks',
301 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
302 );
303 }
304
305 public function getVersion() {
306 return __CLASS__ . ': $Id$';
307 }
308 }