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