Add some documentation
[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 /**
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 if ( isset( $params['users'] ) && isset( $params['ip'] ) ) {
53 $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
54 }
55
56 $prop = array_flip( $params['prop'] );
57 $fld_id = isset( $prop['id'] );
58 $fld_user = isset( $prop['user'] );
59 $fld_userid = isset( $prop['userid'] );
60 $fld_by = isset( $prop['by'] );
61 $fld_byid = isset( $prop['byid'] );
62 $fld_timestamp = isset( $prop['timestamp'] );
63 $fld_expiry = isset( $prop['expiry'] );
64 $fld_reason = isset( $prop['reason'] );
65 $fld_range = isset( $prop['range'] );
66 $fld_flags = isset( $prop['flags'] );
67
68 $result = $this->getResult();
69
70 $this->addTables( 'ipblocks' );
71 $this->addFields( 'ipb_auto' );
72
73 $this->addFieldsIf ( 'ipb_id', $fld_id );
74 $this->addFieldsIf( array( 'ipb_address', 'ipb_user' ), $fld_user || $fld_userid );
75 $this->addFieldsIf( 'ipb_by_text', $fld_by );
76 $this->addFieldsIf( 'ipb_by', $fld_byid );
77 $this->addFieldsIf( 'ipb_timestamp', $fld_timestamp );
78 $this->addFieldsIf( 'ipb_expiry', $fld_expiry );
79 $this->addFieldsIf( 'ipb_reason', $fld_reason );
80 $this->addFieldsIf( array( 'ipb_range_start', 'ipb_range_end' ), $fld_range );
81 $this->addFieldsIf( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock',
82 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ),
83 $fld_flags );
84
85 $this->addOption( 'LIMIT', $params['limit'] + 1 );
86 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
87 if ( isset( $params['ids'] ) ) {
88 $this->addWhereFld( 'ipb_id', $params['ids'] );
89 }
90 if ( isset( $params['users'] ) ) {
91 foreach ( (array)$params['users'] as $u ) {
92 $this->prepareUsername( $u );
93 }
94 $this->addWhereFld( 'ipb_address', $this->usernames );
95 $this->addWhereFld( 'ipb_auto', 0 );
96 }
97 if ( isset( $params['ip'] ) ) {
98 list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
99 if ( $ip && $range ) {
100 // We got a CIDR range
101 if ( $range < 16 )
102 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
103 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
104 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
105 } else {
106 $lower = $upper = IP::toHex( $params['ip'] );
107 }
108 $prefix = substr( $lower, 0, 4 );
109
110 $db = $this->getDB();
111 $this->addWhere( array(
112 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
113 "ipb_range_start <= '$lower'",
114 "ipb_range_end >= '$upper'",
115 'ipb_auto' => 0
116 ) );
117 }
118
119 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
120 $this->addWhereFld( 'ipb_deleted', 0 );
121 }
122
123 // Purge expired entries on one in every 10 queries
124 if ( !mt_rand( 0, 10 ) ) {
125 Block::purgeExpired();
126 }
127
128 $res = $this->select( __METHOD__ );
129
130 $count = 0;
131 foreach ( $res as $row ) {
132 if ( ++$count > $params['limit'] ) {
133 // We've had enough
134 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
135 break;
136 }
137 $block = array();
138 if ( $fld_id ) {
139 $block['id'] = $row->ipb_id;
140 }
141 if ( $fld_user && !$row->ipb_auto ) {
142 $block['user'] = $row->ipb_address;
143 }
144 if ( $fld_userid && !$row->ipb_auto ) {
145 $block['userid'] = $row->ipb_user;
146 }
147 if ( $fld_by ) {
148 $block['by'] = $row->ipb_by_text;
149 }
150 if ( $fld_byid ) {
151 $block['byid'] = $row->ipb_by;
152 }
153 if ( $fld_timestamp ) {
154 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
155 }
156 if ( $fld_expiry ) {
157 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
158 }
159 if ( $fld_reason ) {
160 $block['reason'] = $row->ipb_reason;
161 }
162 if ( $fld_range && !$row->ipb_auto ) {
163 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start );
164 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end );
165 }
166 if ( $fld_flags ) {
167 // For clarity, these flags use the same names as their action=block counterparts
168 if ( $row->ipb_auto ) {
169 $block['automatic'] = '';
170 }
171 if ( $row->ipb_anon_only ) {
172 $block['anononly'] = '';
173 }
174 if ( $row->ipb_create_account ) {
175 $block['nocreate'] = '';
176 }
177 if ( $row->ipb_enable_autoblock ) {
178 $block['autoblock'] = '';
179 }
180 if ( $row->ipb_block_email ) {
181 $block['noemail'] = '';
182 }
183 if ( $row->ipb_deleted ) {
184 $block['hidden'] = '';
185 }
186 if ( $row->ipb_allow_usertalk ) {
187 $block['allowusertalk'] = '';
188 }
189 }
190 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
191 if ( !$fit ) {
192 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
193 break;
194 }
195 }
196 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
197 }
198
199 protected function prepareUsername( $user ) {
200 if ( !$user ) {
201 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
202 }
203 $name = User::isIP( $user )
204 ? $user
205 : User::getCanonicalName( $user, 'valid' );
206 if ( $name === false ) {
207 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
208 }
209 $this->usernames[] = $name;
210 }
211
212 public function getAllowedParams() {
213 return array(
214 'start' => array(
215 ApiBase::PARAM_TYPE => 'timestamp'
216 ),
217 'end' => array(
218 ApiBase::PARAM_TYPE => 'timestamp',
219 ),
220 'dir' => array(
221 ApiBase::PARAM_TYPE => array(
222 'newer',
223 'older'
224 ),
225 ApiBase::PARAM_DFLT => 'older'
226 ),
227 'ids' => array(
228 ApiBase::PARAM_TYPE => 'integer',
229 ApiBase::PARAM_ISMULTI => true
230 ),
231 'users' => array(
232 ApiBase::PARAM_ISMULTI => true
233 ),
234 'ip' => null,
235 'limit' => array(
236 ApiBase::PARAM_DFLT => 10,
237 ApiBase::PARAM_TYPE => 'limit',
238 ApiBase::PARAM_MIN => 1,
239 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
240 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
241 ),
242 'prop' => array(
243 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
244 ApiBase::PARAM_TYPE => array(
245 'id',
246 'user',
247 'userid',
248 'by',
249 'byid',
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' => $this->getDirectionDescription( $this->getModulePrefix() ),
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 ' userid - Adds the user ID of the blocked user',
276 ' by - Adds the username of the blocking user',
277 ' byid - Adds the user ID of the blocking user',
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 }