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 $this->addFields( 'ipb_auto' );
00068
00069 if ( $fld_id )
00070 $this->addFields( 'ipb_id' );
00071 if ( $fld_user )
00072 $this->addFields( array( 'ipb_address', 'ipb_user' ) );
00073 if ( $fld_by )
00074 {
00075 $this->addTables( 'user' );
00076 $this->addFields( array( 'ipb_by', 'user_name' ) );
00077 $this->addWhere( 'user_id = ipb_by' );
00078 }
00079 if ( $fld_timestamp )
00080 $this->addFields( 'ipb_timestamp' );
00081 if ( $fld_expiry )
00082 $this->addFields( 'ipb_expiry' );
00083 if ( $fld_reason )
00084 $this->addFields( 'ipb_reason' );
00085 if ( $fld_range )
00086 $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
00087 if ( $fld_flags )
00088 $this->addFields( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
00089
00090 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00091 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
00092 if ( isset( $params['ids'] ) )
00093 $this->addWhereFld( 'ipb_id', $params['ids'] );
00094 if ( isset( $params['users'] ) )
00095 {
00096 foreach ( (array)$params['users'] as $u )
00097 $this->prepareUsername( $u );
00098 $this->addWhereFld( 'ipb_address', $this->usernames );
00099 $this->addWhereFld( 'ipb_auto', 0 );
00100 }
00101 if ( isset( $params['ip'] ) )
00102 {
00103 list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
00104 if ( $ip && $range )
00105 {
00106
00107 if ( $range < 16 )
00108 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
00109 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
00110 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
00111 }
00112 else
00113 $lower = $upper = IP::toHex( $params['ip'] );
00114 $prefix = substr( $lower, 0, 4 );
00115
00116 $db = $this->getDB();
00117 $this->addWhere( array(
00118 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
00119 "ipb_range_start <= '$lower'",
00120 "ipb_range_end >= '$upper'",
00121 'ipb_auto' => 0
00122 ) );
00123 }
00124 if ( !$wgUser->isAllowed( 'hideuser' ) )
00125 $this->addWhereFld( 'ipb_deleted', 0 );
00126
00127
00128 if ( !mt_rand( 0, 10 ) )
00129 Block::purgeExpired();
00130
00131 $res = $this->select( __METHOD__ );
00132
00133 $count = 0;
00134 while ( $row = $res->fetchObject() )
00135 {
00136 if ( ++$count > $params['limit'] )
00137 {
00138
00139 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
00140 break;
00141 }
00142 $block = array();
00143 if ( $fld_id )
00144 $block['id'] = $row->ipb_id;
00145 if ( $fld_user && !$row->ipb_auto )
00146 $block['user'] = $row->ipb_address;
00147 if ( $fld_by )
00148 $block['by'] = $row->user_name;
00149 if ( $fld_timestamp )
00150 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
00151 if ( $fld_expiry )
00152 $block['expiry'] = Block::decodeExpiry( $row->ipb_expiry, TS_ISO_8601 );
00153 if ( $fld_reason )
00154 $block['reason'] = $row->ipb_reason;
00155 if ( $fld_range && !$row->ipb_auto )
00156 {
00157 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start );
00158 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end );
00159 }
00160 if ( $fld_flags )
00161 {
00162
00163 if ( $row->ipb_auto )
00164 $block['automatic'] = '';
00165 if ( $row->ipb_anon_only )
00166 $block['anononly'] = '';
00167 if ( $row->ipb_create_account )
00168 $block['nocreate'] = '';
00169 if ( $row->ipb_enable_autoblock )
00170 $block['autoblock'] = '';
00171 if ( $row->ipb_block_email )
00172 $block['noemail'] = '';
00173 if ( $row->ipb_deleted )
00174 $block['hidden'] = '';
00175 if ( $row->ipb_allow_usertalk )
00176 $block['allowusertalk'] = '';
00177 }
00178 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
00179 if ( !$fit )
00180 {
00181 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
00182 break;
00183 }
00184 }
00185 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
00186 }
00187
00188 protected function prepareUsername( $user )
00189 {
00190 if ( !$user )
00191 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
00192 $name = User::isIP( $user )
00193 ? $user
00194 : User::getCanonicalName( $user, 'valid' );
00195 if ( $name === false )
00196 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
00197 $this->usernames[] = $name;
00198 }
00199
00200 public function getAllowedParams() {
00201 return array (
00202 'start' => array(
00203 ApiBase :: PARAM_TYPE => 'timestamp'
00204 ),
00205 'end' => array(
00206 ApiBase :: PARAM_TYPE => 'timestamp',
00207 ),
00208 'dir' => array(
00209 ApiBase :: PARAM_TYPE => array(
00210 'newer',
00211 'older'
00212 ),
00213 ApiBase :: PARAM_DFLT => 'older'
00214 ),
00215 'ids' => array(
00216 ApiBase :: PARAM_TYPE => 'integer',
00217 ApiBase :: PARAM_ISMULTI => true
00218 ),
00219 'users' => array(
00220 ApiBase :: PARAM_ISMULTI => true
00221 ),
00222 'ip' => null,
00223 'limit' => array(
00224 ApiBase :: PARAM_DFLT => 10,
00225 ApiBase :: PARAM_TYPE => 'limit',
00226 ApiBase :: PARAM_MIN => 1,
00227 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00228 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00229 ),
00230 'prop' => array(
00231 ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
00232 ApiBase :: PARAM_TYPE => array(
00233 'id',
00234 'user',
00235 'by',
00236 'timestamp',
00237 'expiry',
00238 'reason',
00239 'range',
00240 'flags'
00241 ),
00242 ApiBase :: PARAM_ISMULTI => true
00243 )
00244 );
00245 }
00246
00247 public function getParamDescription() {
00248 return array (
00249 'start' => 'The timestamp to start enumerating from',
00250 'end' => 'The timestamp to stop enumerating at',
00251 'dir' => 'The direction in which to enumerate',
00252 'ids' => 'Pipe-separated list of block IDs to list (optional)',
00253 'users' => 'Pipe-separated list of users to search for (optional)',
00254 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
00255 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted.' ),
00256 'limit' => 'The maximum amount of blocks to list',
00257 'prop' => 'Which properties to get',
00258 );
00259 }
00260
00261 public function getDescription() {
00262 return 'List all blocked users and IP addresses.';
00263 }
00264
00265 public function getPossibleErrors() {
00266 return array_merge( parent::getPossibleErrors(), array(
00267 array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
00268 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
00269 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
00270 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
00271 ) );
00272 }
00273
00274 protected function getExamples() {
00275 return array ( 'api.php?action=query&list=blocks',
00276 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
00277 );
00278 }
00279
00280 public function getVersion() {
00281 return __CLASS__ . ': $Id: ApiQueryBlocks.php 69578 2010-07-20 02:46:20Z tstarling $';
00282 }
00283 }