MediaWiki
REL1_20
|
00001 <?php 00032 class ApiQueryBlocks extends ApiQueryBase { 00033 00037 protected $usernames; 00038 00039 public function __construct( $query, $moduleName ) { 00040 parent::__construct( $query, $moduleName, 'bk' ); 00041 } 00042 00043 public function execute() { 00044 global $wgContLang; 00045 00046 $params = $this->extractRequestParams(); 00047 $this->requireMaxOneParameter( $params, 'users', 'ip' ); 00048 00049 $prop = array_flip( $params['prop'] ); 00050 $fld_id = isset( $prop['id'] ); 00051 $fld_user = isset( $prop['user'] ); 00052 $fld_userid = isset( $prop['userid'] ); 00053 $fld_by = isset( $prop['by'] ); 00054 $fld_byid = isset( $prop['byid'] ); 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 00063 $this->addTables( 'ipblocks' ); 00064 $this->addFields( 'ipb_auto' ); 00065 00066 $this->addFieldsIf ( 'ipb_id', $fld_id ); 00067 $this->addFieldsIf( array( 'ipb_address', 'ipb_user' ), $fld_user || $fld_userid ); 00068 $this->addFieldsIf( 'ipb_by_text', $fld_by ); 00069 $this->addFieldsIf( 'ipb_by', $fld_byid ); 00070 $this->addFieldsIf( 'ipb_timestamp', $fld_timestamp ); 00071 $this->addFieldsIf( 'ipb_expiry', $fld_expiry ); 00072 $this->addFieldsIf( 'ipb_reason', $fld_reason ); 00073 $this->addFieldsIf( array( 'ipb_range_start', 'ipb_range_end' ), $fld_range ); 00074 $this->addFieldsIf( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 00075 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ), 00076 $fld_flags ); 00077 00078 $this->addOption( 'LIMIT', $params['limit'] + 1 ); 00079 $this->addTimestampWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] ); 00080 00081 $db = $this->getDB(); 00082 00083 if ( isset( $params['ids'] ) ) { 00084 $this->addWhereFld( 'ipb_id', $params['ids'] ); 00085 } 00086 if ( isset( $params['users'] ) ) { 00087 foreach ( (array)$params['users'] as $u ) { 00088 $this->prepareUsername( $u ); 00089 } 00090 $this->addWhereFld( 'ipb_address', $this->usernames ); 00091 $this->addWhereFld( 'ipb_auto', 0 ); 00092 } 00093 if ( isset( $params['ip'] ) ) { 00094 list( $ip, $range ) = IP::parseCIDR( $params['ip'] ); 00095 if ( $ip && $range ) { 00096 // We got a CIDR range 00097 if ( $range < 16 ) 00098 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' ); 00099 $lower = wfBaseConvert( $ip, 10, 16, 8, false ); 00100 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false ); 00101 } else { 00102 $lower = $upper = IP::toHex( $params['ip'] ); 00103 } 00104 $prefix = substr( $lower, 0, 4 ); 00105 00106 # Fairly hard to make a malicious SQL statement out of hex characters, 00107 # but it is good practice to add quotes 00108 $lower = $db->addQuotes( $lower ); 00109 $upper = $db->addQuotes( $upper ); 00110 00111 $this->addWhere( array( 00112 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ), 00113 'ipb_range_start <= ' . $lower, 00114 'ipb_range_end >= ' . $upper, 00115 'ipb_auto' => 0 00116 ) ); 00117 } 00118 00119 if ( !is_null( $params['show'] ) ) { 00120 $show = array_flip( $params['show'] ); 00121 00122 /* Check for conflicting parameters. */ 00123 if ( ( isset ( $show['account'] ) && isset ( $show['!account'] ) ) 00124 || ( isset ( $show['ip'] ) && isset ( $show['!ip'] ) ) 00125 || ( isset ( $show['range'] ) && isset ( $show['!range'] ) ) 00126 || ( isset ( $show['temp'] ) && isset ( $show['!temp'] ) ) 00127 ) { 00128 $this->dieUsageMsg( 'show' ); 00129 } 00130 00131 $this->addWhereIf( 'ipb_user = 0', isset( $show['!account'] ) ); 00132 $this->addWhereIf( 'ipb_user != 0', isset( $show['account'] ) ); 00133 $this->addWhereIf( 'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show['!ip'] ) ); 00134 $this->addWhereIf( 'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show['ip'] ) ); 00135 $this->addWhereIf( 'ipb_expiry = '.$db->addQuotes($db->getInfinity()), isset( $show['!temp'] ) ); 00136 $this->addWhereIf( 'ipb_expiry != '.$db->addQuotes($db->getInfinity()), isset( $show['temp'] ) ); 00137 $this->addWhereIf( "ipb_range_end = ipb_range_start", isset( $show['!range'] ) ); 00138 $this->addWhereIf( "ipb_range_end > ipb_range_start", isset( $show['range'] ) ); 00139 } 00140 00141 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) { 00142 $this->addWhereFld( 'ipb_deleted', 0 ); 00143 } 00144 00145 // Purge expired entries on one in every 10 queries 00146 if ( !mt_rand( 0, 10 ) ) { 00147 Block::purgeExpired(); 00148 } 00149 00150 $res = $this->select( __METHOD__ ); 00151 00152 $count = 0; 00153 foreach ( $res as $row ) { 00154 if ( ++$count > $params['limit'] ) { 00155 // We've had enough 00156 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) ); 00157 break; 00158 } 00159 $block = array(); 00160 if ( $fld_id ) { 00161 $block['id'] = $row->ipb_id; 00162 } 00163 if ( $fld_user && !$row->ipb_auto ) { 00164 $block['user'] = $row->ipb_address; 00165 } 00166 if ( $fld_userid && !$row->ipb_auto ) { 00167 $block['userid'] = $row->ipb_user; 00168 } 00169 if ( $fld_by ) { 00170 $block['by'] = $row->ipb_by_text; 00171 } 00172 if ( $fld_byid ) { 00173 $block['byid'] = $row->ipb_by; 00174 } 00175 if ( $fld_timestamp ) { 00176 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ); 00177 } 00178 if ( $fld_expiry ) { 00179 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 ); 00180 } 00181 if ( $fld_reason ) { 00182 $block['reason'] = $row->ipb_reason; 00183 } 00184 if ( $fld_range && !$row->ipb_auto ) { 00185 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start ); 00186 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end ); 00187 } 00188 if ( $fld_flags ) { 00189 // For clarity, these flags use the same names as their action=block counterparts 00190 if ( $row->ipb_auto ) { 00191 $block['automatic'] = ''; 00192 } 00193 if ( $row->ipb_anon_only ) { 00194 $block['anononly'] = ''; 00195 } 00196 if ( $row->ipb_create_account ) { 00197 $block['nocreate'] = ''; 00198 } 00199 if ( $row->ipb_enable_autoblock ) { 00200 $block['autoblock'] = ''; 00201 } 00202 if ( $row->ipb_block_email ) { 00203 $block['noemail'] = ''; 00204 } 00205 if ( $row->ipb_deleted ) { 00206 $block['hidden'] = ''; 00207 } 00208 if ( $row->ipb_allow_usertalk ) { 00209 $block['allowusertalk'] = ''; 00210 } 00211 } 00212 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block ); 00213 if ( !$fit ) { 00214 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) ); 00215 break; 00216 } 00217 } 00218 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' ); 00219 } 00220 00221 protected function prepareUsername( $user ) { 00222 if ( !$user ) { 00223 $this->dieUsage( 'User parameter may not be empty', 'param_user' ); 00224 } 00225 $name = User::isIP( $user ) 00226 ? $user 00227 : User::getCanonicalName( $user, 'valid' ); 00228 if ( $name === false ) { 00229 $this->dieUsage( "User name {$user} is not valid", 'param_user' ); 00230 } 00231 $this->usernames[] = $name; 00232 } 00233 00234 public function getAllowedParams() { 00235 return array( 00236 'start' => array( 00237 ApiBase::PARAM_TYPE => 'timestamp' 00238 ), 00239 'end' => array( 00240 ApiBase::PARAM_TYPE => 'timestamp', 00241 ), 00242 'dir' => array( 00243 ApiBase::PARAM_TYPE => array( 00244 'newer', 00245 'older' 00246 ), 00247 ApiBase::PARAM_DFLT => 'older' 00248 ), 00249 'ids' => array( 00250 ApiBase::PARAM_TYPE => 'integer', 00251 ApiBase::PARAM_ISMULTI => true 00252 ), 00253 'users' => array( 00254 ApiBase::PARAM_ISMULTI => true 00255 ), 00256 'ip' => null, 00257 'limit' => array( 00258 ApiBase::PARAM_DFLT => 10, 00259 ApiBase::PARAM_TYPE => 'limit', 00260 ApiBase::PARAM_MIN => 1, 00261 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00262 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00263 ), 00264 'prop' => array( 00265 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags', 00266 ApiBase::PARAM_TYPE => array( 00267 'id', 00268 'user', 00269 'userid', 00270 'by', 00271 'byid', 00272 'timestamp', 00273 'expiry', 00274 'reason', 00275 'range', 00276 'flags' 00277 ), 00278 ApiBase::PARAM_ISMULTI => true 00279 ), 00280 'show' => array( 00281 ApiBase::PARAM_TYPE => array( 00282 'account', 00283 '!account', 00284 'temp', 00285 '!temp', 00286 'ip', 00287 '!ip', 00288 'range', 00289 '!range', 00290 ), 00291 ApiBase::PARAM_ISMULTI => true 00292 ), 00293 ); 00294 } 00295 00296 public function getParamDescription() { 00297 $p = $this->getModulePrefix(); 00298 return array( 00299 'start' => 'The timestamp to start enumerating from', 00300 'end' => 'The timestamp to stop enumerating at', 00301 'dir' => $this->getDirectionDescription( $p ), 00302 'ids' => 'List of block IDs to list (optional)', 00303 'users' => 'List of users to search for (optional)', 00304 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.', 00305 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ), 00306 'limit' => 'The maximum amount of blocks to list', 00307 'prop' => array( 00308 'Which properties to get', 00309 ' id - Adds the ID of the block', 00310 ' user - Adds the username of the blocked user', 00311 ' userid - Adds the user ID of the blocked user', 00312 ' by - Adds the username of the blocking user', 00313 ' byid - Adds the user ID of the blocking user', 00314 ' timestamp - Adds the timestamp of when the block was given', 00315 ' expiry - Adds the timestamp of when the block expires', 00316 ' reason - Adds the reason given for the block', 00317 ' range - Adds the range of IPs affected by the block', 00318 ' flags - Tags the ban with (autoblock, anononly, etc)', 00319 ), 00320 'show' => array( 00321 'Show only items that meet this criteria.', 00322 "For example, to see only indefinite blocks on IPs, set {$p}show=ip|!temp" 00323 ), 00324 ); 00325 } 00326 00327 public function getResultProperties() { 00328 return array( 00329 'id' => array( 00330 'id' => 'integer' 00331 ), 00332 'user' => array( 00333 'user' => array( 00334 ApiBase::PROP_TYPE => 'string', 00335 ApiBase::PROP_NULLABLE => true 00336 ) 00337 ), 00338 'userid' => array( 00339 'userid' => array( 00340 ApiBase::PROP_TYPE => 'integer', 00341 ApiBase::PROP_NULLABLE => true 00342 ) 00343 ), 00344 'by' => array( 00345 'by' => 'string' 00346 ), 00347 'byid' => array( 00348 'byid' => 'integer' 00349 ), 00350 'timestamp' => array( 00351 'timestamp' => 'timestamp' 00352 ), 00353 'expiry' => array( 00354 'expiry' => 'timestamp' 00355 ), 00356 'reason' => array( 00357 'reason' => 'string' 00358 ), 00359 'range' => array( 00360 'rangestart' => array( 00361 ApiBase::PROP_TYPE => 'string', 00362 ApiBase::PROP_NULLABLE => true 00363 ), 00364 'rangeend' => array( 00365 ApiBase::PROP_TYPE => 'string', 00366 ApiBase::PROP_NULLABLE => true 00367 ) 00368 ), 00369 'flags' => array( 00370 'automatic' => 'boolean', 00371 'anononly' => 'boolean', 00372 'nocreate' => 'boolean', 00373 'autoblock' => 'boolean', 00374 'noemail' => 'boolean', 00375 'hidden' => 'boolean', 00376 'allowusertalk' => 'boolean' 00377 ) 00378 ); 00379 } 00380 00381 public function getDescription() { 00382 return 'List all blocked users and IP addresses'; 00383 } 00384 00385 public function getPossibleErrors() { 00386 return array_merge( parent::getPossibleErrors(), 00387 $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ), 00388 array( 00389 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ), 00390 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ), 00391 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ), 00392 array( 'show' ), 00393 ) 00394 ); 00395 } 00396 00397 public function getExamples() { 00398 return array( 00399 'api.php?action=query&list=blocks', 00400 'api.php?action=query&list=blocks&bkusers=Alice|Bob' 00401 ); 00402 } 00403 00404 public function getHelpUrls() { 00405 return 'https://www.mediawiki.org/wiki/API:Blocks'; 00406 } 00407 00408 public function getVersion() { 00409 return __CLASS__ . ': $Id$'; 00410 } 00411 }