89242896788e4091895619c04a386fff9659d24f
[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 && !$row->ipb_auto)
116 {
117 $block['user'] = $row->ipb_address;
118 }
119 if($fld_by)
120 {
121 $block['by'] = $row->user_name;
122 }
123 if($fld_timestamp)
124 $block['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ipb_timestamp);
125 if($fld_expiry)
126 $block['expiry'] = Block::decodeExpiry($row->ipb_expiry, TS_ISO_8601);
127 if($fld_reason)
128 $block['reason'] = $row->ipb_reason;
129 if($fld_range)
130 {
131 $block['rangestart'] = $this->convertHexIP($row->ipb_range_start);
132 $block['rangeend'] = $this->convertHexIP($row->ipb_range_end);
133 }
134 if($fld_flags)
135 {
136 // For clarity, these flags use the same names as their action=block counterparts
137 if($row->ipb_auto)
138 $block['automatic'] = '';
139 if($row->ipb_anon_only)
140 $block['anononly'] = '';
141 if($row->ipb_create_account)
142 $block['nocreate'] = '';
143 if($row->ipb_enable_autoblock)
144 $block['autoblock'] = '';
145 if($row->ipb_block_email)
146 $block['noemail'] = '';
147 if($row->ipb_deleted)
148 $block['hidden'] = '';
149 }
150 $data[] = $block;
151 }
152 $result->setIndexedTagName($data, 'block');
153 $result->addValue('query', $this->getModuleName(), $data);
154 }
155
156 protected function convertHexIP($ip)
157 {
158 // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format
159 $dec = wfBaseConvert($ip, 16, 10);
160 $parts[0] = (int)($dec / (256*256*256));
161 $dec %= 256*256*256;
162 $parts[1] = (int)($dec / (256*256));
163 $dec %= 256*256;
164 $parts[2] = (int)($dec / 256);
165 $parts[3] = $dec % 256;
166 return implode('.', $parts);
167 }
168
169 public function getAllowedParams() {
170 return array (
171 'start' => array(
172 ApiBase :: PARAM_TYPE => 'timestamp'
173 ),
174 'end' => array(
175 ApiBase :: PARAM_TYPE => 'timestamp',
176 ),
177 'dir' => array(
178 ApiBase :: PARAM_TYPE => array(
179 'newer',
180 'older'
181 ),
182 ApiBase :: PARAM_DFLT => 'older'
183 ),
184 'ids' => array(
185 ApiBase :: PARAM_TYPE => 'integer',
186 ApiBase :: PARAM_ISMULTI => true
187 ),
188 'users' => array(
189 ApiBase :: PARAM_ISMULTI => true
190 ),
191 'limit' => array(
192 ApiBase :: PARAM_DFLT => 10,
193 ApiBase :: PARAM_TYPE => 'limit',
194 ApiBase :: PARAM_MIN => 1,
195 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
196 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
197 ),
198 'prop' => array(
199 ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
200 ApiBase :: PARAM_TYPE => array(
201 'id',
202 'user',
203 'by',
204 'timestamp',
205 'expiry',
206 'reason',
207 'range',
208 'flags'
209 ),
210 ApiBase :: PARAM_ISMULTI => true
211 )
212 );
213 }
214
215 public function getParamDescription() {
216 return array (
217 'start' => 'The timestamp to start enumerating from',
218 'end' => 'The timestamp to stop enumerating at',
219 'dir' => 'The direction in which to enumerate',
220 'ids' => 'Pipe-separated list of block IDs to list (optional)',
221 'users' => 'Pipe-separated list of users to search for (optional)',
222 'limit' => 'The maximum amount of blocks to list',
223 'prop' => 'Which properties to get',
224 );
225 }
226
227 public function getDescription() {
228 return 'List all blocked users and IP addresses.';
229 }
230
231 protected function getExamples() {
232 return array (
233 );
234 }
235
236 public function getVersion() {
237 return __CLASS__ . ': $Id$';
238 }
239 }