/**
* Blocks and bans object
* @package MediaWiki
- * $Id$
*/
/**
*/
define ( 'EB_KEEP_EXPIRED', 1 );
define ( 'EB_FOR_UPDATE', 2 );
+define ( 'EB_RANGE_ONLY', 4 );
/**
* The block class
- * All the functions in this class assume the object is either explicitly
+ * All the functions in this class assume the object is either explicitly
* loaded or filled. It is not load-on-demand. There are no accessors.
- *
+ *
* To use delete(), you only need to fill $mAddress
* Globals used: $wgBlockCache, $wgAutoblockExpiry
*
class Block
{
/* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
- /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
-
- function Block( $address = '', $user = '', $by = 0, $reason = '',
- $timestamp = '' , $auto = 0, $expiry = '' )
+ /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mByName;
+
+ function Block( $address = '', $user = '', $by = 0, $reason = '',
+ $timestamp = '' , $auto = 0, $expiry = '' )
{
$this->mAddress = $address;
$this->mUser = $user;
$this->mReason = $reason;
$this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
$this->mAuto = $auto;
- $this->mExpiry = wfTimestamp(TS_MW,$expiry);
-
+ if( empty( $expiry ) ) {
+ $this->mExpiry = $expiry;
+ } else {
+ $this->mExpiry = wfTimestamp( TS_MW, $expiry );
+ }
+
$this->mForUpdate = false;
+ $this->mByName = false;
$this->initialiseRange();
}
-
- /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
+
+ /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
{
$ban = new Block();
$ban->load( $address, $user, $killExpired );
return $ban;
}
-
- function clear()
+
+ function clear()
{
- $mAddress = $mReason = $mTimestamp = '';
- $mUser = $mBy = 0;
+ $this->mAddress = $this->mReason = $this->mTimestamp = '';
+ $this->mUser = $this->mBy = 0;
+ $this->mByName = false;
+
}
- # Get a ban from the DB, with either the given address or the given username
- function load( $address = '', $user = 0, $killExpired = true )
+ /**
+ * Get a ban from the DB, with either the given address or the given username
+ */
+ function load( $address = '', $user = 0, $killExpired = true )
{
+ global $wgAntiLockFlags;
$fname = 'Block::load';
+ wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
$ret = false;
$killed = false;
if ( $this->forUpdate() ) {
$db =& wfGetDB( DB_MASTER );
- $options = 'FOR UPDATE';
+ if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
+ $options = '';
+ } else {
+ $options = 'FOR UPDATE';
+ }
} else {
$db =& wfGetDB( DB_SLAVE );
$options = '';
$sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
} elseif ($user=="") {
$sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
+ } elseif ( $options=='' ) {
+ # If there are no options (e.g. FOR UPDATE), use a UNION
+ # so that the query can make efficient use of indices
+ $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
+ "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
} else {
- $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
+ # If there are options, a UNION can not be used, use one
+ # SELECT instead. Will do a full table scan.
+ $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
"' OR ipb_user={$user}) $options";
}
}
}
} while ( $killed && $row );
-
+
# If there were any left after the killing finished, return true
if ( !$row ) {
$ret = false;
$db->freeResult( $res );
return $ret;
}
-
- function initFromRow( $row )
+
+ function initFromRow( $row )
{
$this->mAddress = $row->ipb_address;
$this->mReason = $row->ipb_reason;
$this->mBy = $row->ipb_by;
$this->mAuto = $row->ipb_auto;
$this->mId = $row->ipb_id;
- $this->mExpiry = wfTimestamp(TS_MW,$row->ipb_expiry);
+ $this->mExpiry = $row->ipb_expiry ?
+ wfTimestamp(TS_MW,$row->ipb_expiry) :
+ $row->ipb_expiry;
+ if ( isset( $row->user_name ) ) {
+ $this->mByName = $row->user_name;
+ } else {
+ $this->mByName = false;
+ }
$this->initialiseRange();
- }
+ }
function initialiseRange()
{
$this->mIntegerAddr = false;
}
}
-
- # Callback with a Block object for every block
- /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
+
+ /**
+ * Callback with a Block object for every block
+ * @return integer number of blocks;
+ */
+ /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
{
+ global $wgAntiLockFlags;
+
$block = new Block();
if ( $flags & EB_FOR_UPDATE ) {
$db =& wfGetDB( DB_MASTER );
- $options = 'FOR UPDATE';
+ if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
+ $options = '';
+ } else {
+ $options = 'FOR UPDATE';
+ }
$block->forUpdate( true );
} else {
$db =& wfGetDB( DB_SLAVE );
$options = '';
- }
- $ipblocks = $db->tableName( 'ipblocks' );
-
- $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
+ }
+ if ( $flags & EB_RANGE_ONLY ) {
+ $cond = " AND ipb_address LIKE '%/%'";
+ } else {
+ $cond = '';
+ }
+
+ extract( $db->tableNames( 'ipblocks', 'user' ) );
+
+ $sql = "SELECT $ipblocks.*,user_name FROM $ipblocks,$user " .
+ "WHERE user_id=ipb_by $cond ORDER BY ipb_timestamp DESC $options";
$res = $db->query( $sql, 'Block::enumBans' );
+ $num_rows = $db->numRows( $res );
while ( $row = $db->fetchObject( $res ) ) {
$block->initFromRow( $row );
+ if ( ( $flags & EB_RANGE_ONLY ) && $block->getNetworkBits() == 32 ) {
+ continue;
+ }
+
if ( !( $flags & EB_KEEP_EXPIRED ) ) {
if ( !$block->deleteIfExpired() ) {
- $callback( $block, $tag );
+ call_user_func( $callback, $block, $tag );
}
} else {
- $callback( $block, $tag );
+ call_user_func( $callback, $block, $tag );
}
}
wfFreeResult( $res );
+ return $num_rows;
}
- function delete()
+ function delete()
{
$fname = 'Block::delete';
+ if (wfReadOnly()) {
+ return;
+ }
$dbw =& wfGetDB( DB_MASTER );
if ( $this->mAddress == '' ) {
$this->clearCache();
}
- function insert()
+ function insert()
{
+ wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
$dbw =& wfGetDB( DB_MASTER );
+ $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
$dbw->insert( 'ipblocks',
array(
+ 'ipb_id' => $ipb_id,
'ipb_address' => $this->mAddress,
'ipb_user' => $this->mUser,
'ipb_by' => $this->mBy,
'ipb_reason' => $this->mReason,
'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
'ipb_auto' => $this->mAuto,
- 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
- ), 'Block::insert'
+ 'ipb_expiry' => $this->mExpiry ?
+ $dbw->timestamp($this->mExpiry) :
+ $this->mExpiry,
+ ), 'Block::insert'
);
$this->clearCache();
}
- function deleteIfExpired()
+ function deleteIfExpired()
{
if ( $this->isExpired() ) {
+ wfDebug( "Block::deleteIfExpired() -- deleting\n" );
$this->delete();
return true;
} else {
+ wfDebug( "Block::deleteIfExpired() -- not expired\n" );
return false;
}
}
- function isExpired()
- {
+ function isExpired()
+ {
+ wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
if ( !$this->mExpiry ) {
return false;
} else {
- return wfTimestamp() > $this->mExpiry;
+ return wfTimestampNow() > $this->mExpiry;
}
}
- function isValid()
+ function isValid()
{
return $this->mAddress != '';
}
-
- function updateTimestamp()
+
+ function updateTimestamp()
{
if ( $this->mAuto ) {
$this->mTimestamp = wfTimestamp();
$this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
$dbw =& wfGetDB( DB_MASTER );
- $dbw->update( 'ipblocks',
- array( /* SET */
+ $dbw->update( 'ipblocks',
+ array( /* SET */
'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
'ipb_expiry' => $dbw->timestamp($this->mExpiry),
), array( /* WHERE */
'ipb_address' => $this->mAddress
- ), 'Block::updateTimestamp'
+ ), 'Block::updateTimestamp'
);
-
+
$this->clearCache();
}
}
$wgBlockCache->loadFromDB();
}
}
-
+
function getIntegerAddr()
{
return $this->mIntegerAddr;
}
-
+
function getNetworkBits()
{
return $this->mNetworkBits;
}
+ function getByName()
+ {
+ if ( $this->mByName === false ) {
+ $this->mByName = User::whoIs( $this->mBy );
+ }
+ return $this->mByName;
+ }
+
function forUpdate( $x = NULL ) {
return wfSetVar( $this->mForUpdate, $x );
}