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