* (bug 27611) list=blocks: Use ipb_by_text instead of join with user table
[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 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->addFields( 'ipb_by_text' );
76 }
77 if ( $fld_timestamp ) {
78 $this->addFields( 'ipb_timestamp' );
79 }
80 if ( $fld_expiry ) {
81 $this->addFields( 'ipb_expiry' );
82 }
83 if ( $fld_reason ) {
84 $this->addFields( 'ipb_reason' );
85 }
86 if ( $fld_range ) {
87 $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
88 }
89 if ( $fld_flags ) {
90 $this->addFields( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
91 }
92
93 $this->addOption( 'LIMIT', $params['limit'] + 1 );
94 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
95 if ( isset( $params['ids'] ) ) {
96 $this->addWhereFld( 'ipb_id', $params['ids'] );
97 }
98 if ( isset( $params['users'] ) ) {
99 foreach ( (array)$params['users'] as $u ) {
100 $this->prepareUsername( $u );
101 }
102 $this->addWhereFld( 'ipb_address', $this->usernames );
103 $this->addWhereFld( 'ipb_auto', 0 );
104 }
105 if ( isset( $params['ip'] ) ) {
106 list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
107 if ( $ip && $range ) {
108 // We got a CIDR range
109 if ( $range < 16 )
110 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
111 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
112 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
113 } else {
114 $lower = $upper = IP::toHex( $params['ip'] );
115 }
116 $prefix = substr( $lower, 0, 4 );
117
118 $db = $this->getDB();
119 $this->addWhere( array(
120 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
121 "ipb_range_start <= '$lower'",
122 "ipb_range_end >= '$upper'",
123 'ipb_auto' => 0
124 ) );
125 }
126
127 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
128 $this->addWhereFld( 'ipb_deleted', 0 );
129 }
130
131 // Purge expired entries on one in every 10 queries
132 if ( !mt_rand( 0, 10 ) ) {
133 Block::purgeExpired();
134 }
135
136 $res = $this->select( __METHOD__ );
137
138 $count = 0;
139 foreach ( $res as $row ) {
140 if ( ++$count > $params['limit'] ) {
141 // We've had enough
142 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
143 break;
144 }
145 $block = array();
146 if ( $fld_id ) {
147 $block['id'] = $row->ipb_id;
148 }
149 if ( $fld_user && !$row->ipb_auto ) {
150 $block['user'] = $row->ipb_address;
151 }
152 if ( $fld_by ) {
153 $block['by'] = $row->ipb_by_text;
154 }
155 if ( $fld_timestamp ) {
156 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
157 }
158 if ( $fld_expiry ) {
159 $block['expiry'] = Block::decodeExpiry( $row->ipb_expiry, TS_ISO_8601 );
160 }
161 if ( $fld_reason ) {
162 $block['reason'] = $row->ipb_reason;
163 }
164 if ( $fld_range && !$row->ipb_auto ) {
165 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start );
166 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end );
167 }
168 if ( $fld_flags ) {
169 // For clarity, these flags use the same names as their action=block counterparts
170 if ( $row->ipb_auto ) {
171 $block['automatic'] = '';
172 }
173 if ( $row->ipb_anon_only ) {
174 $block['anononly'] = '';
175 }
176 if ( $row->ipb_create_account ) {
177 $block['nocreate'] = '';
178 }
179 if ( $row->ipb_enable_autoblock ) {
180 $block['autoblock'] = '';
181 }
182 if ( $row->ipb_block_email ) {
183 $block['noemail'] = '';
184 }
185 if ( $row->ipb_deleted ) {
186 $block['hidden'] = '';
187 }
188 if ( $row->ipb_allow_usertalk ) {
189 $block['allowusertalk'] = '';
190 }
191 }
192 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
193 if ( !$fit ) {
194 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
195 break;
196 }
197 }
198 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
199 }
200
201 protected function prepareUsername( $user ) {
202 if ( !$user ) {
203 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
204 }
205 $name = User::isIP( $user )
206 ? $user
207 : User::getCanonicalName( $user, 'valid' );
208 if ( $name === false ) {
209 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
210 }
211 $this->usernames[] = $name;
212 }
213
214 public function getAllowedParams() {
215 return array(
216 'start' => array(
217 ApiBase::PARAM_TYPE => 'timestamp'
218 ),
219 'end' => array(
220 ApiBase::PARAM_TYPE => 'timestamp',
221 ),
222 'dir' => array(
223 ApiBase::PARAM_TYPE => array(
224 'newer',
225 'older'
226 ),
227 ApiBase::PARAM_DFLT => 'older'
228 ),
229 'ids' => array(
230 ApiBase::PARAM_TYPE => 'integer',
231 ApiBase::PARAM_ISMULTI => true
232 ),
233 'users' => array(
234 ApiBase::PARAM_ISMULTI => true
235 ),
236 'ip' => null,
237 'limit' => array(
238 ApiBase::PARAM_DFLT => 10,
239 ApiBase::PARAM_TYPE => 'limit',
240 ApiBase::PARAM_MIN => 1,
241 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
242 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
243 ),
244 'prop' => array(
245 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
246 ApiBase::PARAM_TYPE => array(
247 'id',
248 'user',
249 'by',
250 'timestamp',
251 'expiry',
252 'reason',
253 'range',
254 'flags'
255 ),
256 ApiBase::PARAM_ISMULTI => true
257 )
258 );
259 }
260
261 public function getParamDescription() {
262 return array(
263 'start' => 'The timestamp to start enumerating from',
264 'end' => 'The timestamp to stop enumerating at',
265 'dir' => 'The direction in which to enumerate',
266 'ids' => 'Pipe-separated list of block IDs to list (optional)',
267 'users' => 'Pipe-separated list of users to search for (optional)',
268 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
269 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
270 'limit' => 'The maximum amount of blocks to list',
271 'prop' => array(
272 'Which properties to get',
273 ' id - Adds the ID of the block',
274 ' user - Adds the username of the blocked user',
275 ' by - Adds the username of the blocking admin',
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 array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
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 }