8f5536888acce1c4d8242f1bafb11786f6d5cc29
[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 * @addtogroup API
35 */
36 class ApiQueryBlocks extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'bk');
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 private function run() {
47 global $wgUser;
48
49 $params = $this->extractRequestParams();
50 $prop = array_flip($params['prop']);
51 $fld_id = isset($prop['id']);
52 $fld_user = isset($prop['user']);
53 $fld_by = isset($prop['by']);
54 $fld_timestamp = isset($prop['timestamp']);
55 $fld_expiry = isset($prop['expiry']);
56 $fld_reason = isset($prop['reason']);
57 $fld_range = isset($prop['range']);
58 $fld_flags = isset($prop['flags']);
59
60 $result = $this->getResult();
61 $pageSet = $this->getPageSet();
62 $titles = $pageSet->getTitles();
63 $data = array();
64
65 $this->addTables('ipblocks');
66 if($fld_id)
67 $this->addFields('ipb_id');
68 if($fld_user)
69 $this->addFields(array('ipb_address', 'ipb_user'));
70 if($fld_by)
71 {
72 $this->addTables('user');
73 $this->addFields(array('ipb_by', 'user_name'));
74 $this->addWhere('user_id = ipb_by');
75 }
76 if($fld_timestamp)
77 $this->addFields('ipb_timestamp');
78 if($fld_expiry)
79 $this->addFields('ipb_expiry');
80 if($fld_reason)
81 $this->addFields('ipb_reason');
82 if($fld_range)
83 $this->addFields(array('ipb_range_start', 'ipb_range_end'));
84 if($fld_flags)
85 $this->addFields(array('ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted'));
86
87 $this->addOption('LIMIT', $params['limit'] + 1);
88 $this->addWhereRange('ipb_timestamp', $params['dir'], $params['start'], $params['end']);
89 if(isset($params['ids']))
90 $this->addWhere(array('ipb_id' => $params['ids']));
91 if(isset($params['users']))
92 $this->addWhere(array('ipb_address' => $params['users']));
93 if(!$wgUser->isAllowed('oversight'))
94 $this->addWhere(array('ipb_deleted' => 0));
95
96 // Purge expired entries on one in every 10 queries
97 if(!mt_rand(0, 10))
98 Block::purgeExpired();
99
100 $res = $this->select(__METHOD__);
101 $db = wfGetDB();
102
103 $count = 0;
104 while($row = $db->fetchObject($res))
105 {
106 if($count++ == $params['limit'])
107 {
108 // We've had enough
109 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ipb_timestamp));
110 break;
111 }
112 $block = array();
113 if($fld_id)
114 $block['id'] = $row->ipb_id;
115 if($fld_user)
116 {
117 $block['user'] = $row->ipb_address;
118 $block['userid'] = $row->ipb_user;
119 }
120 if($fld_by)
121 {
122 $block['by'] = $row->user_name;
123 $block['byuserid'] = $row->ipb_by;
124 }
125 if($fld_timestamp)
126 $block['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ipb_timestamp);
127 if($fld_expiry)
128 $block['expiry'] = Block::decodeExpiry($row->ipb_expiry, TS_ISO_8601);
129 if($fld_reason)
130 $block['reason'] = $row->ipb_reason;
131 if($fld_range)
132 {
133 $block['rangestart'] = $this->convertHexIP($row->ipb_range_start);
134 $block['rangeend'] = $this->convertHexIP($row->ipb_range_end);
135 }
136 if($fld_flags)
137 {
138 // For clarity, these flags use the same names as their action=block counterparts
139 if($row->ipb_auto)
140 $block['automatic'] = '';
141 if($row->ipb_anon_only)
142 $block['anononly'] = '';
143 if($row->ipb_create_account)
144 $block['nocreate'] = '';
145 if($row->ipb_enable_autoblock)
146 $block['autoblock'] = '';
147 if($row->ipb_block_email)
148 $block['noemail'] = '';
149 if($row->ipb_deleted)
150 $block['hidden'] = '';
151 }
152 $data[] = $block;
153 }
154 $result->setIndexedTagName($data, 'block');
155 $result->addValue('query', $this->getModuleName(), $data);
156 }
157
158 protected function convertHexIP($ip)
159 {
160 // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format
161 $dec = wfBaseConvert($ip, 16, 10);
162 $parts[0] = (int)($dec / (256*256*256));
163 $dec %= 256*256*256;
164 $parts[1] = (int)($dec / (256*256));
165 $dec %= 256*256;
166 $parts[2] = (int)($dec / 256);
167 $parts[3] = $dec % 256;
168 return implode('.', $parts);
169 }
170
171 protected function getAllowedParams() {
172 return array (
173 'start' => array(
174 ApiBase :: PARAM_TYPE => 'timestamp'
175 ),
176 'end' => array(
177 ApiBase :: PARAM_TYPE => 'timestamp',
178 ),
179 'dir' => array(
180 ApiBase :: PARAM_TYPE => array(
181 'newer',
182 'older'
183 ),
184 ApiBase :: PARAM_DFLT => 'older'
185 ),
186 'ids' => array(
187 ApiBase :: PARAM_TYPE => 'integer',
188 ApiBase :: PARAM_ISMULTI => true
189 ),
190 'users' => array(
191 ApiBase :: PARAM_ISMULTI => true
192 ),
193 'limit' => array(
194 ApiBase :: PARAM_DFLT => 10,
195 ApiBase :: PARAM_TYPE => 'limit',
196 ApiBase :: PARAM_MIN => 1,
197 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
198 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
199 ),
200 'prop' => array(
201 ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
202 ApiBase :: PARAM_TYPE => array(
203 'id',
204 'user',
205 'by',
206 'timestamp',
207 'expiry',
208 'reason',
209 'range',
210 'flags'
211 ),
212 ApiBase :: PARAM_ISMULTI => true
213 )
214 );
215 }
216
217 protected function getParamDescription() {
218 return array (
219 'start' => 'The timestamp to start enumerating from',
220 'end' => 'The timestamp to stop enumerating at',
221 'dir' => 'The direction in which to enumerate',
222 'ids' => 'Pipe-separated list of block IDs to list (optional)',
223 'users' => 'Pipe-separated list of users to search for (optional)',
224 'limit' => 'The maximum amount of blocks to list',
225 'prop' => 'Which properties to get',
226 );
227 }
228
229 protected function getDescription() {
230 return 'List all blocked users and IP addresses.';
231 }
232
233 protected function getExamples() {
234 return array (
235 );
236 }
237
238 public function getVersion() {
239 return __CLASS__ . ': $Id$';
240 }
241 }