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