*
* @todo This could be used everywhere, but it isn't.
*/
-class Block
-{
+class Block {
/* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
$mRangeStart, $mRangeEnd, $mAnonOnly, $mEnableAutoblock, $mHideName,
$mBlockEmail, $mByName, $mAngryAutoblock;
/**
* Load a block from the database, using either the IP address or
- * userid.
+ * user ID. Tries the user ID first, and if that doesn't work, tries
+ * the address.
*
* @return Block Object
* @param $address string IP address of user/anon
* @param $user int User id of user
* @param $killExpired bool Delete expired blocks on load
*/
- static function newFromDB( $address, $user = 0, $killExpired = true )
- {
+ static function newFromDB( $address, $user = 0, $killExpired = true ) {
$block = new Block();
$block->load( $address, $user, $killExpired );
if ( $block->isValid() ) {
/**
* Load a blocked user from their block id.
+ *
* @return Block object
* @param $id int Block id to search for
*/
- static function newFromID( $id )
- {
+ static function newFromID( $id ) {
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
array( 'ipb_id' => $id ), __METHOD__ ) );
* Clear all member variables in the current object. Does not clear
* the block from the DB.
*/
- function clear()
- {
+ function clear() {
$this->mAddress = $this->mReason = $this->mTimestamp = '';
$this->mId = $this->mAnonOnly = $this->mCreateAccount =
$this->mEnableAutoblock = $this->mAuto = $this->mUser =
}
/**
- * Get the DB object and set the reference parameter to the query options
+ * Get the DB object and set the reference parameter to the select options.
+ * The options array will contain FOR UPDATE if appropriate.
+ *
+ * @param array $options
* @return Database
*/
function &getDBOptions( &$options ) {
}
/**
- * Get a ban from the DB, with either the given address or the given username
+ * Get a block from the DB, with either the given address or the given username
*
* @param $address string The IP address of the user, or blank to skip IP blocks
* @param $user int The user ID, or zero for anonymous users
* @param $killExpired bool Whether to delete expired rows while loading
*
*/
- function load( $address = '', $user = 0, $killExpired = true )
- {
+ function load( $address = '', $user = 0, $killExpired = true ) {
wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
$options = array();
/**
* Fill in member variables from a result wrapper
+ *
* @return bool
* @param $res ResultWrapper Row from the ipblocks table
* @param $killExpired bool Whether to delete expired rows while loading
*/
- function loadFromResult( ResultWrapper $res, $killExpired = true )
- {
+ function loadFromResult( ResultWrapper $res, $killExpired = true ) {
$ret = false;
if ( 0 != $res->numRows() ) {
# Get first block
/**
* Search the database for any range blocks matching the given address, and
* load the row if one is found.
+ *
* @return bool
* @param $address string IP address range
* @param $killExpired bool Whether to delete expired rows while loading
* @param $userid int If not 0, then sets ipb_anon_only
*/
- function loadRange( $address, $killExpired = true, $user = 0 )
- {
+ function loadRange( $address, $killExpired = true, $user = 0 ) {
$iaddr = IP::toHex( $address );
if ( $iaddr === false ) {
# Invalid address
/**
* Given a database row from the ipblocks table, initialize
* member variables
+ *
* @param $row ResultWrapper A row from the ipblocks table
*/
- function initFromRow( $row )
- {
+ function initFromRow( $row ) {
$this->mAddress = $row->ipb_address;
$this->mReason = $row->ipb_reason;
$this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
* Once $mAddress has been set, get the range they came from.
* Wrapper for IP::parseRange
*/
- function initialiseRange()
- {
+ function initialiseRange() {
$this->mRangeStart = '';
$this->mRangeEnd = '';
/**
* Callback with a Block object for every block
- * @return integer number of blocks;
+ *
+ * @deprecated Unlimited row count, do your own queries instead
+ *
+ * @return integer number of blocks
*/
- /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
- {
+ /*static*/ function enumBlocks( $callback, $tag, $flags = 0 ) {
global $wgAntiLockFlags;
$block = new Block();
/**
* Delete the row from the IP blocks table.
+ *
* @return bool
*/
- function delete()
- {
+ function delete() {
if (wfReadOnly()) {
return false;
}
}
/**
- * Insert a block into the block table.
- * @return bool Whether or not the insertion was successful.
- */
- function insert()
- {
+ * Insert a block into the block table. Will fail if there is a conflicting
+ * block (same name and options) already in the database.
+ *
+ * @return bool Whether or not the insertion was successful.
+ */
+ function insert() {
wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
$dbw = wfGetDB( DB_MASTER );
if ($affected)
$this->doRetroactiveAutoblock();
- return $affected;
+ return (bool)$affected;
}
/**
- * Update block with new parameters.
+ * Update a block in the DB with new parameters.
+ * The ID field needs to be loaded first.
*/
public function update() {
wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
/**
* Retroactively autoblocks the last IP used by the user (if it is a user)
* blocked by this Block.
+ *
* @return bool Whether or not a retroactive autoblock was made.
*/
function doRetroactiveAutoblock() {
/**
* Checks whether a given IP is on the autoblock whitelist.
+ *
* @return bool
- * @param string $autoblockip The IP to check
+ * @param string $ip The IP to check
*/
function isWhitelistedFromAutoblocks( $ip ) {
global $wgMemc;
}
/**
- * Autoblocks the given IP, referring to this Block.
- * @param string $autoblockip The IP to autoblock.
- * @param bool $justInserted The main block was just inserted
- * @return bool Whether or not an autoblock was inserted.
- */
- function doAutoblock( $autoblockip, $justInserted = false ) {
+ * Autoblocks the given IP, referring to this Block.
+ *
+ * @param string $autoblockIP The IP to autoblock.
+ * @param bool $justInserted The main block was just inserted
+ * @return bool Whether or not an autoblock was inserted.
+ */
+ function doAutoblock( $autoblockIP, $justInserted = false ) {
# If autoblocks are disabled, go away.
if ( !$this->mEnableAutoblock ) {
return;
}
# Check for presence on the autoblock whitelist
- if (Block::isWhitelistedFromAutoblocks($autoblockip)) {
+ if (Block::isWhitelistedFromAutoblocks($autoblockIP)) {
return;
}
## Allow hooks to cancel the autoblock.
- if (!wfRunHooks( 'AbortAutoblock', array( $autoblockip, &$this ) )) {
+ if (!wfRunHooks( 'AbortAutoblock', array( $autoblockIP, &$this ) )) {
wfDebug( "Autoblock aborted by hook." );
return false;
}
# It's okay to autoblock. Go ahead and create/insert the block.
- $ipblock = Block::newFromDB( $autoblockip );
+ $ipblock = Block::newFromDB( $autoblockIP );
if ( $ipblock ) {
# If the user is already blocked. Then check if the autoblock would
# exceed the user block. If it would exceed, then do nothing, else
}
# Make a new block object with the desired properties
- wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockip . "\n" );
- $ipblock->mAddress = $autoblockip;
+ wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockIP . "\n" );
+ $ipblock->mAddress = $autoblockIP;
$ipblock->mUser = 0;
$ipblock->mBy = $this->mBy;
$ipblock->mByName = $this->mByName;
* Check if a block has expired. Delete it if it is.
* @return bool
*/
- function deleteIfExpired()
- {
+ function deleteIfExpired() {
$fname = 'Block::deleteIfExpired';
wfProfileIn( $fname );
if ( $this->isExpired() ) {
* Has the block expired?
* @return bool
*/
- function isExpired()
- {
+ function isExpired() {
wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
if ( !$this->mExpiry ) {
return false;
* Is the block address valid (i.e. not a null string?)
* @return bool
*/
- function isValid()
- {
+ function isValid() {
return $this->mAddress != '';
}
/**
* Update the timestamp on autoblocks.
*/
- function updateTimestamp()
- {
+ function updateTimestamp() {
if ( $this->mAuto ) {
$this->mTimestamp = wfTimestamp();
$this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
}
}
- /*
- function getIntegerAddr()
- {
- return $this->mIntegerAddr;
- }
-
- function getNetworkBits()
- {
- return $this->mNetworkBits;
- }*/
-
/**
* Get the user id of the blocking sysop
+ *
* @return int
*/
public function getBy() {
/**
* Get the username of the blocking sysop
+ *
* @return string
*/
- function getByName()
- {
+ function getByName() {
return $this->mByName;
}
+ /**
+ * Get/set the SELECT ... FOR UPDATE flag
+ */
function forUpdate( $x = NULL ) {
return wfSetVar( $this->mForUpdate, $x );
}
+ /**
+ * Get/set a flag determining whether the master is used for reads
+ */
function fromMaster( $x = NULL ) {
return wfSetVar( $this->mFromMaster, $x );
}
+ /**
+ * Get the block name, but with autoblocked IPs hidden as per standard privacy policy
+ * @return string
+ */
function getRedactedName() {
if ( $this->mAuto ) {
return '#' . $this->mId;
/**
* Encode expiry for DB
+ *
* @return string
* @param $expiry string Timestamp for expiry, or
* @param $db Database object
/**
* Decode expiry which has come from the DB
+ *
* @return string
* @param $expiry string Database expiry format
* @param $timestampType Requested timestamp format
/**
* Get a timestamp of the expiry for autoblocks
+ *
* @return string
*/
- static function getAutoblockExpiry( $timestamp )
- {
+ static function getAutoblockExpiry( $timestamp ) {
global $wgAutoblockExpiry;
return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
}
}
/**
- * This is a special keyword for timestamps in PostgreSQL, and
- * works with CHAR(14) as well because "i" sorts after all numbers.
+ * Get a value to insert into expiry field of the database when infinite expiry
+ * is desired. In principle this could be DBMS-dependant, but currently all
+ * supported DBMS's support the string "infinity", so we just use that.
+ *
* @return string
*/
static function infinity() {
+ # This is a special keyword for timestamps in PostgreSQL, and
+ # works with CHAR(14) as well because "i" sorts after all numbers.
return 'infinity';
}
/**
* Convert a DB-encoded expiry into a real string that humans can read.
+ *
* @param $encoded_expiry string Database encoded expiry time
* @return string
*/