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