Space
[lhc/web/wiklou.git] / includes / api / ApiQueryBlocks.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Sep 10, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 var $users;
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'bk' );
43 }
44
45 public function execute() {
46 global $wgUser;
47
48 $params = $this->extractRequestParams();
49 if ( isset( $params['users'] ) && isset( $params['ip'] ) ) {
50 $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
51 }
52
53 $prop = array_flip( $params['prop'] );
54 $fld_id = isset( $prop['id'] );
55 $fld_user = isset( $prop['user'] );
56 $fld_by = isset( $prop['by'] );
57 $fld_timestamp = isset( $prop['timestamp'] );
58 $fld_expiry = isset( $prop['expiry'] );
59 $fld_reason = isset( $prop['reason'] );
60 $fld_range = isset( $prop['range'] );
61 $fld_flags = isset( $prop['flags'] );
62
63 $result = $this->getResult();
64
65 $this->addTables( 'ipblocks' );
66 $this->addFields( 'ipb_auto' );
67
68 if ( $fld_id ) {
69 $this->addFields( 'ipb_id' );
70 }
71 if ( $fld_user ) {
72 $this->addFields( array( 'ipb_address', 'ipb_user' ) );
73 }
74 if ( $fld_by ) {
75 $this->addTables( 'user' );
76 $this->addFields( array( 'ipb_by', 'user_name' ) );
77 $this->addWhere( 'user_id = ipb_by' );
78 }
79 if ( $fld_timestamp ) {
80 $this->addFields( 'ipb_timestamp' );
81 }
82 if ( $fld_expiry ) {
83 $this->addFields( 'ipb_expiry' );
84 }
85 if ( $fld_reason ) {
86 $this->addFields( 'ipb_reason' );
87 }
88 if ( $fld_range ) {
89 $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
90 }
91 if ( $fld_flags ) {
92 $this->addFields( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
93 }
94
95 $this->addOption( 'LIMIT', $params['limit'] + 1 );
96 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
97 if ( isset( $params['ids'] ) ) {
98 $this->addWhereFld( 'ipb_id', $params['ids'] );
99 }
100 if ( isset( $params['users'] ) ) {
101 foreach ( (array)$params['users'] as $u ) {
102 $this->prepareUsername( $u );
103 }
104 $this->addWhereFld( 'ipb_address', $this->usernames );
105 $this->addWhereFld( 'ipb_auto', 0 );
106 }
107 if ( isset( $params['ip'] ) ) {
108 list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
109 if ( $ip && $range ) {
110 // We got a CIDR range
111 if ( $range < 16 )
112 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
113 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
114 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
115 } else {
116 $lower = $upper = IP::toHex( $params['ip'] );
117 }
118 $prefix = substr( $lower, 0, 4 );
119
120 $db = $this->getDB();
121 $this->addWhere( array(
122 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
123 "ipb_range_start <= '$lower'",
124 "ipb_range_end >= '$upper'",
125 'ipb_auto' => 0
126 ) );
127 }
128
129 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
130 $this->addWhereFld( 'ipb_deleted', 0 );
131 }
132
133 // Purge expired entries on one in every 10 queries
134 if ( !mt_rand( 0, 10 ) ) {
135 Block::purgeExpired();
136 }
137
138 $res = $this->select( __METHOD__ );
139
140 $count = 0;
141 foreach ( $res as $row ) {
142 if ( ++$count > $params['limit'] ) {
143 // We've had enough
144 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
145 break;
146 }
147 $block = array();
148 if ( $fld_id ) {
149 $block['id'] = $row->ipb_id;
150 }
151 if ( $fld_user && !$row->ipb_auto ) {
152 $block['user'] = $row->ipb_address;
153 }
154 if ( $fld_by ) {
155 $block['by'] = $row->user_name;
156 }
157 if ( $fld_timestamp ) {
158 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
159 }
160 if ( $fld_expiry ) {
161 $block['expiry'] = Block::decodeExpiry( $row->ipb_expiry, TS_ISO_8601 );
162 }
163 if ( $fld_reason ) {
164 $block['reason'] = $row->ipb_reason;
165 }
166 if ( $fld_range && !$row->ipb_auto ) {
167 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start );
168 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end );
169 }
170 if ( $fld_flags ) {
171 // For clarity, these flags use the same names as their action=block counterparts
172 if ( $row->ipb_auto ) {
173 $block['automatic'] = '';
174 }
175 if ( $row->ipb_anon_only ) {
176 $block['anononly'] = '';
177 }
178 if ( $row->ipb_create_account ) {
179 $block['nocreate'] = '';
180 }
181 if ( $row->ipb_enable_autoblock ) {
182 $block['autoblock'] = '';
183 }
184 if ( $row->ipb_block_email ) {
185 $block['noemail'] = '';
186 }
187 if ( $row->ipb_deleted ) {
188 $block['hidden'] = '';
189 }
190 if ( $row->ipb_allow_usertalk ) {
191 $block['allowusertalk'] = '';
192 }
193 }
194 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
195 if ( !$fit ) {
196 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
197 break;
198 }
199 }
200 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
201 }
202
203 protected function prepareUsername( $user ) {
204 if ( !$user ) {
205 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
206 }
207 $name = User::isIP( $user )
208 ? $user
209 : User::getCanonicalName( $user, 'valid' );
210 if ( $name === false ) {
211 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
212 }
213 $this->usernames[] = $name;
214 }
215
216 public function getAllowedParams() {
217 return array(
218 'start' => array(
219 ApiBase::PARAM_TYPE => 'timestamp'
220 ),
221 'end' => array(
222 ApiBase::PARAM_TYPE => 'timestamp',
223 ),
224 'dir' => array(
225 ApiBase::PARAM_TYPE => array(
226 'newer',
227 'older'
228 ),
229 ApiBase::PARAM_DFLT => 'older'
230 ),
231 'ids' => array(
232 ApiBase::PARAM_TYPE => 'integer',
233 ApiBase::PARAM_ISMULTI => true
234 ),
235 'users' => array(
236 ApiBase::PARAM_ISMULTI => true
237 ),
238 'ip' => null,
239 'limit' => array(
240 ApiBase::PARAM_DFLT => 10,
241 ApiBase::PARAM_TYPE => 'limit',
242 ApiBase::PARAM_MIN => 1,
243 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
244 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
245 ),
246 'prop' => array(
247 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
248 ApiBase::PARAM_TYPE => array(
249 'id',
250 'user',
251 'by',
252 'timestamp',
253 'expiry',
254 'reason',
255 'range',
256 'flags'
257 ),
258 ApiBase::PARAM_ISMULTI => true
259 )
260 );
261 }
262
263 public function getParamDescription() {
264 return array(
265 'start' => 'The timestamp to start enumerating from',
266 'end' => 'The timestamp to stop enumerating at',
267 'dir' => 'The direction in which to enumerate',
268 'ids' => 'Pipe-separated list of block IDs to list (optional)',
269 'users' => 'Pipe-separated list of users to search for (optional)',
270 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
271 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
272 'limit' => 'The maximum amount of blocks to list',
273 'prop' => array(
274 'Which properties to get',
275 ' id - Adds the id of the block',
276 ' user - Adds the username of the blocked user',
277 ' by - Adds the username of the blocking admin',
278 ' timestamp - Adds the timestamp of when the block was given',
279 ' expiry - Adds the timestamp of when the block expires',
280 ' reason - Adds the reason given for the block',
281 ' range - Adds the range of IPs affected by the block',
282 ' flags - Tags the ban with (autoblock, anononly, etc)',
283 ),
284 );
285 }
286
287 public function getDescription() {
288 return 'List all blocked users and IP addresses';
289 }
290
291 public function getPossibleErrors() {
292 return array_merge( parent::getPossibleErrors(), array(
293 array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
294 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
295 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
296 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
297 ) );
298 }
299
300 protected function getExamples() {
301 return array(
302 'api.php?action=query&list=blocks',
303 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
304 );
305 }
306
307 public function getVersion() {
308 return __CLASS__ . ': $Id$';
309 }
310 }