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. There are no accessors.
9 # To use delete(), you only need to fill $mAddress
13 var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId;
15 function Block( $address = "", $user = "", $by = 0, $reason = "",
16 $timestamp = "" , $auto = 0)
18 $this->mAddress
= $address;
21 $this->mReason
= $reason;
22 $this->mTimestamp
= $timestamp;
26 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true ) {
28 $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 ) {
39 $fname = "Block::load";
44 $sql = "SELECT * FROM ipblocks WHERE ipb_address='" . wfStrencode( $address ) . "'";
46 $sql = "SELECT * FROM ipblocks WHERE (ipb_address='" . wfStrencode( $address ) .
47 "' OR ipb_user={$user})";
50 $res = wfQuery( $sql, DB_READ
, $fname );
51 if ( 0 == wfNumRows( $res ) ) {
56 $row = wfFetchObject( $res );
57 $this->initFromRow( $row );
60 # If requested, delete expired rows
62 $killed = $this->deleteIfExpired();
64 $row = wfFetchObject( $res );
66 $this->initFromRow( $row );
69 } while ( $killed && $row );
71 # If there were any left after the killing finished, return true
86 function initFromRow( $row ) {
87 $this->mAddress
= $row->ipb_address
;
88 $this->mReason
= $row->ipb_reason
;
89 $this->mTimestamp
= $row->ipb_timestamp
;
90 $this->mUser
= $row->ipb_user
;
91 $this->mBy
= $row->ipb_by
;
92 $this->mAuto
= $row->ipb_auto
;
93 $this->mId
= $row->ipb_id
;
96 # Callback with a Block object for every block
97 /*static*/ function enumBlocks( $callback, $tag, $killExpired = true ) {
98 $sql = "SELECT * FROM ipblocks ORDER BY ipb_timestamp DESC";
99 $res = wfQuery( $sql, DB_READ
, "Block::enumBans" );
100 $block = new Block();
102 while ( $row = wfFetchObject( $res ) ) {
103 $block->initFromRow( $row );
104 if ( $killExpired ) {
105 if ( !$block->deleteIfExpired() ) {
106 $callback( $block, $tag );
109 $callback( $block, $tag );
112 wfFreeResult( $res );
116 $fname = "Block::delete";
117 if ( $this->mAddress
== "" ) {
118 $sql = "DELETE FROM ipblocks WHERE ipb_id={$this->mId}";
120 $sql = "DELETE FROM ipblocks WHERE ipb_address='" .
121 wfStrencode( $this->mAddress
) . "'";
123 wfQuery( $sql, DB_WRITE
, "Block::delete" );
127 $sql = "INSERT INTO ipblocks
128 (ipb_address, ipb_user, ipb_by, ipb_reason, ipb_timestamp, ipb_auto )
129 VALUES ('" . wfStrencode( $this->mAddress
) . "', {$this->mUser}, {$this->mBy}, '" .
130 wfStrencode( $this->mReason
) . "','{$this->mTimestamp}', {$this->mAuto})";
131 wfQuery( $sql, DB_WRITE
, "Block::insert" );
134 function deleteIfExpired() {
135 if ( $this->isExpired() ) {
143 function isExpired() {
144 global $wgIPBlockExpiration, $wgUserBlockExpiration;
146 $period = $this->mUser ?
$wgUserBlockExpiration : $wgIPBlockExpiration;
148 # Period==0 means no expiry
152 $expiry = wfTimestamp2Unix( $this->mTimestamp
) +
$period;
153 $now = wfTimestamp2Unix( wfTimestampNow() );
154 if ( $now > $expiry ) {
162 return $this->mAddress
!= "";
165 function updateTimestamp() {
166 wfQuery( "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() .
167 "' WHERE ipb_address='" . wfStrencode( $this->mAddress
) . "'", DB_WRITE
, "Block::updateTimestamp" );