WARNING: HUGE COMMIT
[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 $this->run();
46 }
47
48 private function run() {
49 global $wgUser;
50
51 $params = $this->extractRequestParams();
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 if($fld_id)
69 $this->addFields('ipb_id');
70 if($fld_user)
71 $this->addFields(array('ipb_address', 'ipb_user'));
72 if($fld_by)
73 {
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 if($fld_expiry)
81 $this->addFields('ipb_expiry');
82 if($fld_reason)
83 $this->addFields('ipb_reason');
84 if($fld_range)
85 $this->addFields(array('ipb_range_start', 'ipb_range_end'));
86 if($fld_flags)
87 $this->addFields(array('ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted'));
88
89 $this->addOption('LIMIT', $params['limit'] + 1);
90 $this->addWhereRange('ipb_timestamp', $params['dir'], $params['start'], $params['end']);
91 if(isset($params['ids']))
92 $this->addWhere(array('ipb_id' => $params['ids']));
93 if(isset($params['users']))
94 {
95 foreach((array)$params['users'] as $u)
96 $this->prepareUsername($u);
97 $this->addWhere(array('ipb_address' => $this->usernames));
98 }
99 if(!$wgUser->isAllowed('suppress'))
100 $this->addWhere(array('ipb_deleted' => 0));
101
102 // Purge expired entries on one in every 10 queries
103 if(!mt_rand(0, 10))
104 Block::purgeExpired();
105
106 $res = $this->select(__METHOD__);
107 $db = wfGetDB();
108
109 $count = 0;
110 while($row = $db->fetchObject($res))
111 {
112 if($count++ == $params['limit'])
113 {
114 // We've had enough
115 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ipb_timestamp));
116 break;
117 }
118 $block = array();
119 if($fld_id)
120 $block['id'] = $row->ipb_id;
121 if($fld_user && !$row->ipb_auto)
122 {
123 $block['user'] = $row->ipb_address;
124 }
125 if($fld_by)
126 {
127 $block['by'] = $row->user_name;
128 }
129 if($fld_timestamp)
130 $block['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ipb_timestamp);
131 if($fld_expiry)
132 $block['expiry'] = Block::decodeExpiry($row->ipb_expiry, TS_ISO_8601);
133 if($fld_reason)
134 $block['reason'] = $row->ipb_reason;
135 if($fld_range)
136 {
137 $block['rangestart'] = $this->convertHexIP($row->ipb_range_start);
138 $block['rangeend'] = $this->convertHexIP($row->ipb_range_end);
139 }
140 if($fld_flags)
141 {
142 // For clarity, these flags use the same names as their action=block counterparts
143 if($row->ipb_auto)
144 $block['automatic'] = '';
145 if($row->ipb_anon_only)
146 $block['anononly'] = '';
147 if($row->ipb_create_account)
148 $block['nocreate'] = '';
149 if($row->ipb_enable_autoblock)
150 $block['autoblock'] = '';
151 if($row->ipb_block_email)
152 $block['noemail'] = '';
153 if($row->ipb_deleted)
154 $block['hidden'] = '';
155 }
156 $data[] = $block;
157 }
158 $result->setIndexedTagName($data, 'block');
159 $result->addValue('query', $this->getModuleName(), $data);
160 }
161
162 protected function prepareUsername($user) {
163 if( $user ) {
164 $name = User::isIP( $user )
165 ? $user
166 : User::getCanonicalName( $user, 'valid' );
167 if( $name === false ) {
168 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
169 } else {
170 $this->usernames[] = $name;
171 }
172 } else {
173 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
174 }
175 }
176
177 protected function convertHexIP($ip)
178 {
179 // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format
180 $dec = wfBaseConvert($ip, 16, 10);
181 $parts[0] = (int)($dec / (256*256*256));
182 $dec %= 256*256*256;
183 $parts[1] = (int)($dec / (256*256));
184 $dec %= 256*256;
185 $parts[2] = (int)($dec / 256);
186 $parts[3] = $dec % 256;
187 return implode('.', $parts);
188 }
189
190 public function getAllowedParams() {
191 return array (
192 'start' => array(
193 ApiBase :: PARAM_TYPE => 'timestamp'
194 ),
195 'end' => array(
196 ApiBase :: PARAM_TYPE => 'timestamp',
197 ),
198 'dir' => array(
199 ApiBase :: PARAM_TYPE => array(
200 'newer',
201 'older'
202 ),
203 ApiBase :: PARAM_DFLT => 'older'
204 ),
205 'ids' => array(
206 ApiBase :: PARAM_TYPE => 'integer',
207 ApiBase :: PARAM_ISMULTI => true
208 ),
209 'users' => array(
210 ApiBase :: PARAM_ISMULTI => true
211 ),
212 'limit' => array(
213 ApiBase :: PARAM_DFLT => 10,
214 ApiBase :: PARAM_TYPE => 'limit',
215 ApiBase :: PARAM_MIN => 1,
216 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
217 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
218 ),
219 'prop' => array(
220 ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
221 ApiBase :: PARAM_TYPE => array(
222 'id',
223 'user',
224 'by',
225 'timestamp',
226 'expiry',
227 'reason',
228 'range',
229 'flags'
230 ),
231 ApiBase :: PARAM_ISMULTI => true
232 )
233 );
234 }
235
236 public function getParamDescription() {
237 return array (
238 'start' => 'The timestamp to start enumerating from',
239 'end' => 'The timestamp to stop enumerating at',
240 'dir' => 'The direction in which to enumerate',
241 'ids' => 'Pipe-separated list of block IDs to list (optional)',
242 'users' => 'Pipe-separated list of users to search for (optional)',
243 'limit' => 'The maximum amount of blocks to list',
244 'prop' => 'Which properties to get',
245 );
246 }
247
248 public function getDescription() {
249 return 'List all blocked users and IP addresses.';
250 }
251
252 protected function getExamples() {
253 return array ( 'api.php?action=query&list=blocks',
254 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
255 );
256 }
257
258 public function getVersion() {
259 return __CLASS__ . ': $Id$';
260 }
261 }