URL-escape the main page link on the logo
[mediawiki.git] / includes / Block.php
blobfaffc641ac497c6d9e5479db0923a71c62bda75a
1 <?
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
11 class Block
13 var $mAddress, $mUser, $mBy, $mReason, $mTimestamp;
15 function Block( $address = "", $user = "", $by = 0, $reason = "", $timestamp = "" )
17 $this->mAddress = $address;
18 $this->mUser = $user;
19 $this->mBy = $by;
20 $this->mReason = $reason;
21 $this->mTimestamp = $timestamp;
24 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
26 $ban = new Block();
27 $ban->load( $address, $user, $killExpired );
28 return $ban;
31 function clear()
33 $mAddress = $mReason = $mTimestamp = "";
34 $mUser = $mBy = 0;
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";
41 $ret = false;
42 $killed = false;
44 if ( 0 == $user ) {
45 $sql = "SELECT * FROM ipblocks WHERE ipb_address='$address'";
46 } else {
47 $sql = "SELECT * FROM ipblocks WHERE (ipb_address='$address' OR ipb_user={$user})";
51 $res = wfQuery( $sql, $fname );
52 if ( 0 == wfNumRows( $res ) ) {
53 # User is not blocked
54 $this->clear();
55 } else {
56 # Get first block
57 $row = wfFetchObject( $res );
58 $this->initFromRow( $row );
60 if ( $killExpired ) {
62 # If requested, delete expired rows
63 do {
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 ) {
70 $ret = false;
71 $this->clear();
72 } else {
73 $ret = true;
75 } else {
76 $ret = true;
79 wfFreeResult( $res );
80 return $ret;
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" );
97 $block = new Block();
99 while ( $row = wfFetchObject( $res ) ) {
100 $block->initFromRow( $row );
101 if ( $killExpired ) {
102 if ( !$block->deleteIfExpired() ) {
103 $callback( $block, $tag );
105 } else {
106 $callback( $block, $tag );
109 wfFreeResult( $res );
112 function delete()
114 wfQuery( "DELETE FROM ipblocks WHERE ipb_address='{$this->mAddress}'",
115 "Block::delete" );
118 function insert()
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() ) {
129 $this->delete();
130 return true;
131 } else {
132 return false;
136 function isExpired()
138 global $wgIPBlockExpiration, $wgUserBlockExpiration;
140 $period = $this->mUser ? $wgUserBlockExpiration : $wgIPBlockExpiration;
142 # Period==0 means no expiry
143 if ( !$period ) {
144 return false;
146 $expiry = wfTimestamp2Unix( $this->mTimestamp ) + $period;
147 $now = wfTimestamp2Unix( wfTimestampNow() );
148 if ( $now > $expiry ) {
149 return true;
150 } else {
151 return false;
155 function isValid()
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" );