00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00036 class ApiQueryBlocks extends ApiQueryBase {
00037
00038 var $users;
00039
00040 public function __construct($query, $moduleName) {
00041 parent :: __construct($query, $moduleName, 'bk');
00042 }
00043
00044 public function execute() {
00045 global $wgUser;
00046
00047 $params = $this->extractRequestParams();
00048 if(isset($params['users']) && isset($params['ip']))
00049 $this->dieUsage('bkusers and bkip cannot be used together', 'usersandip');
00050
00051 $prop = array_flip($params['prop']);
00052 $fld_id = isset($prop['id']);
00053 $fld_user = isset($prop['user']);
00054 $fld_by = isset($prop['by']);
00055 $fld_timestamp = isset($prop['timestamp']);
00056 $fld_expiry = isset($prop['expiry']);
00057 $fld_reason = isset($prop['reason']);
00058 $fld_range = isset($prop['range']);
00059 $fld_flags = isset($prop['flags']);
00060
00061 $result = $this->getResult();
00062 $pageSet = $this->getPageSet();
00063 $titles = $pageSet->getTitles();
00064 $data = array();
00065
00066 $this->addTables('ipblocks');
00067 if($fld_id)
00068 $this->addFields('ipb_id');
00069 if($fld_user)
00070 $this->addFields(array('ipb_address', 'ipb_user', 'ipb_auto'));
00071 if($fld_by)
00072 {
00073 $this->addTables('user');
00074 $this->addFields(array('ipb_by', 'user_name'));
00075 $this->addWhere('user_id = ipb_by');
00076 }
00077 if($fld_timestamp)
00078 $this->addFields('ipb_timestamp');
00079 if($fld_expiry)
00080 $this->addFields('ipb_expiry');
00081 if($fld_reason)
00082 $this->addFields('ipb_reason');
00083 if($fld_range)
00084 $this->addFields(array('ipb_range_start', 'ipb_range_end'));
00085 if($fld_flags)
00086 $this->addFields(array('ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk'));
00087
00088 $this->addOption('LIMIT', $params['limit'] + 1);
00089 $this->addWhereRange('ipb_timestamp', $params['dir'], $params['start'], $params['end']);
00090 if(isset($params['ids']))
00091 $this->addWhereFld('ipb_id', $params['ids']);
00092 if(isset($params['users']))
00093 {
00094 foreach((array)$params['users'] as $u)
00095 $this->prepareUsername($u);
00096 $this->addWhereFld('ipb_address', $this->usernames);
00097 }
00098 if(isset($params['ip']))
00099 {
00100 list($ip, $range) = IP::parseCIDR($params['ip']);
00101 if($ip && $range)
00102 {
00103 # We got a CIDR range
00104 if($range < 16)
00105 $this->dieUsage('CIDR ranges broader than /16 are not accepted', 'cidrtoobroad');
00106 $lower = wfBaseConvert($ip, 10, 16, 8, false);
00107 $upper = wfBaseConvert($ip + pow(2, 32 - $range) - 1, 10, 16, 8, false);
00108 }
00109 else
00110 $lower = $upper = IP::toHex($params['ip']);
00111 $prefix = substr($lower, 0, 4);
00112 $this->addWhere(array(
00113 "ipb_range_start LIKE '$prefix%'",
00114 "ipb_range_start <= '$lower'",
00115 "ipb_range_end >= '$upper'"
00116 ));
00117 }
00118 if(!$wgUser->isAllowed('hideuser'))
00119 $this->addWhereFld('ipb_deleted', 0);
00120
00121
00122 if(!mt_rand(0, 10))
00123 Block::purgeExpired();
00124
00125 $res = $this->select(__METHOD__);
00126
00127 $count = 0;
00128 while($row = $res->fetchObject())
00129 {
00130 if(++$count > $params['limit'])
00131 {
00132
00133 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ipb_timestamp));
00134 break;
00135 }
00136 $block = array();
00137 if($fld_id)
00138 $block['id'] = $row->ipb_id;
00139 if($fld_user && !$row->ipb_auto)
00140 $block['user'] = $row->ipb_address;
00141 if($fld_by)
00142 $block['by'] = $row->user_name;
00143 if($fld_timestamp)
00144 $block['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ipb_timestamp);
00145 if($fld_expiry)
00146 $block['expiry'] = Block::decodeExpiry($row->ipb_expiry, TS_ISO_8601);
00147 if($fld_reason)
00148 $block['reason'] = $row->ipb_reason;
00149 if($fld_range)
00150 {
00151 $block['rangestart'] = IP::hexToQuad($row->ipb_range_start);
00152 $block['rangeend'] = IP::hexToQuad($row->ipb_range_end);
00153 }
00154 if($fld_flags)
00155 {
00156
00157 if($row->ipb_auto)
00158 $block['automatic'] = '';
00159 if($row->ipb_anon_only)
00160 $block['anononly'] = '';
00161 if($row->ipb_create_account)
00162 $block['nocreate'] = '';
00163 if($row->ipb_enable_autoblock)
00164 $block['autoblock'] = '';
00165 if($row->ipb_block_email)
00166 $block['noemail'] = '';
00167 if($row->ipb_deleted)
00168 $block['hidden'] = '';
00169 if($row->ipb_allow_usertalk)
00170 $block['allowusertalk'] = '';
00171 }
00172 $fit = $result->addValue(array('query', $this->getModuleName()), null, $block);
00173 if(!$fit)
00174 {
00175 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ipb_timestamp));
00176 break;
00177 }
00178 }
00179 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'block');
00180 }
00181
00182 protected function prepareUsername($user)
00183 {
00184 if(!$user)
00185 $this->dieUsage('User parameter may not be empty', 'param_user');
00186 $name = User::isIP($user)
00187 ? $user
00188 : User::getCanonicalName($user, 'valid');
00189 if($name === false)
00190 $this->dieUsage("User name {$user} is not valid", 'param_user');
00191 $this->usernames[] = $name;
00192 }
00193
00194 public function getAllowedParams() {
00195 return array (
00196 'start' => array(
00197 ApiBase :: PARAM_TYPE => 'timestamp'
00198 ),
00199 'end' => array(
00200 ApiBase :: PARAM_TYPE => 'timestamp',
00201 ),
00202 'dir' => array(
00203 ApiBase :: PARAM_TYPE => array(
00204 'newer',
00205 'older'
00206 ),
00207 ApiBase :: PARAM_DFLT => 'older'
00208 ),
00209 'ids' => array(
00210 ApiBase :: PARAM_TYPE => 'integer',
00211 ApiBase :: PARAM_ISMULTI => true
00212 ),
00213 'users' => array(
00214 ApiBase :: PARAM_ISMULTI => true
00215 ),
00216 'ip' => null,
00217 'limit' => array(
00218 ApiBase :: PARAM_DFLT => 10,
00219 ApiBase :: PARAM_TYPE => 'limit',
00220 ApiBase :: PARAM_MIN => 1,
00221 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00222 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00223 ),
00224 'prop' => array(
00225 ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
00226 ApiBase :: PARAM_TYPE => array(
00227 'id',
00228 'user',
00229 'by',
00230 'timestamp',
00231 'expiry',
00232 'reason',
00233 'range',
00234 'flags'
00235 ),
00236 ApiBase :: PARAM_ISMULTI => true
00237 )
00238 );
00239 }
00240
00241 public function getParamDescription() {
00242 return array (
00243 'start' => 'The timestamp to start enumerating from',
00244 'end' => 'The timestamp to stop enumerating at',
00245 'dir' => 'The direction in which to enumerate',
00246 'ids' => 'Pipe-separated list of block IDs to list (optional)',
00247 'users' => 'Pipe-separated list of users to search for (optional)',
00248 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
00249 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted.'),
00250 'limit' => 'The maximum amount of blocks to list',
00251 'prop' => 'Which properties to get',
00252 );
00253 }
00254
00255 public function getDescription() {
00256 return 'List all blocked users and IP addresses.';
00257 }
00258
00259 protected function getExamples() {
00260 return array ( 'api.php?action=query&list=blocks',
00261 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
00262 );
00263 }
00264
00265 public function getVersion() {
00266 return __CLASS__ . ': $Id: ApiQueryBlocks.php 48213 2009-03-09 10:01:00Z aaron $';
00267 }
00268 }