2 # Blocks and bans object
4 #TODO: This could be used everywhere, but it isn't.
6 # All the functions in this class assume the object is either explicitly
7 # loaded or filled. It is not load-on-demand.
9 # To use delete(), you only need to fill $mAddress
13 var $mAddress, $mUser, $mBy, $mReason, $mTimestamp;
15 function Block( $address = "", $user = "", $by = 0, $reason = "", $timestamp = "" )
17 $this->mAddress
= $address;
20 $this->mReason
= $reason;
21 $this->mTimestamp
= $timestamp;
24 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
27 $ban->load( $address, $user, $killExpired );
33 $mAddress = $mReason = $mTimestamp = "";
37 # Get a ban from the DB, with either the given address or the given username
38 function load( $address, $user = 0, $killExpired = true )
40 $fname = "Block::load";
45 $sql = "SELECT * FROM ipblocks WHERE ipb_address='$address'";
47 $sql = "SELECT * FROM ipblocks WHERE (ipb_address='$address' OR ipb_user={$user})";
51 $res = wfQuery( $sql, $fname );
52 if ( 0 == wfNumRows( $res ) ) {
57 $row = wfFetchObject( $res );
58 $this->initFromRow( $row );
62 # If requested, delete expired rows
64 $killed = $this->deleteIfExpired();
65 $row = wfFetchObject( $res );
66 } while ( $killed && $row );
68 # If there were any left after the killing finished, return true
69 if ( $row == false ) {
83 function initFromRow( $row )
85 $this->mAddress
= $row->ipb_address
;
86 $this->mReason
= $row->ipb_reason
;
87 $this->mTimestamp
= $row->ipb_timestamp
;
88 $this->mUser
= $row->ipb_user
;
89 $this->mBy
= $row->ipb_by
;
92 # Callback with a Block object for every block
93 /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
95 $sql = "SELECT * FROM ipblocks ORDER BY ipb_timestamp";
96 $res = wfQuery( $sql, "Block::enumBans" );
99 while ( $row = wfFetchObject( $res ) ) {
100 $block->initFromRow( $row );
101 if ( $killExpired ) {
102 if ( !$block->deleteIfExpired() ) {
103 $callback( $block, $tag );
106 $callback( $block, $tag );
109 wfFreeResult( $res );
114 wfQuery( "DELETE FROM ipblocks WHERE ipb_address='{$this->mAddress}'",
120 $sql = "INSERT INTO ipblocks (ipb_address, ipb_user, ipb_by, " .
121 "ipb_reason, ipb_timestamp ) VALUES ('{$this->mAddress}', {$this->mUser}, " .
122 "{$this->mBy}, '" . wfStrencode( $this->mReason
) . "','{$this->mTimestamp}')";
123 wfQuery( $sql, "Block::insert" );
126 function deleteIfExpired()
128 if ( $this->isExpired() ) {
138 global $wgIPBlockExpiration, $wgUserBlockExpiration;
140 $period = $this->mUser ?
$wgUserBlockExpiration : $wgIPBlockExpiration;
142 # Period==0 means no expiry
146 $expiry = wfTimestamp2Unix( $this->mTimestamp
) +
$period;
147 $now = wfTimestamp2Unix( wfTimestampNow() );
148 if ( $now > $expiry ) {
157 return $this->mAddress
!= "";
161 function updateTimestamp()
163 $sql = "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() . "' WHERE ipb_address='{$this->mAddress}'";
164 wfQuery( "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() .
165 "' WHERE ipb_address='{$this->mAddress}'", "Block::updateTimestamp" );