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