Changed the way SQL queries are logged by profiling
[lhc/web/wiklou.git] / includes / Block.php
1 <?
2 # Blocks and bans object
3 #
4 #TODO: This could be used everywhere, but it isn't.
5 #
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.
8 #
9 # To use delete(), you only need to fill $mAddress
10
11 class Block
12 {
13 var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId;
14
15 function Block( $address = "", $user = "", $by = 0, $reason = "",
16 $timestamp = "" , $auto = 0)
17 {
18 $this->mAddress = $address;
19 $this->mUser = $user;
20 $this->mBy = $by;
21 $this->mReason = $reason;
22 $this->mTimestamp = $timestamp;
23 $this->mAuto = $auto;
24 }
25
26 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true ) {
27 $ban = new Block();
28 $ban->load( $address, $user, $killExpired );
29 return $ban;
30 }
31
32 function clear() {
33 $mAddress = $mReason = $mTimestamp = "";
34 $mUser = $mBy = 0;
35 }
36
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";
40 $ret = false;
41 $killed = false;
42
43 if ( 0 == $user ) {
44 $sql = "SELECT * FROM ipblocks WHERE ipb_address='" . wfStrencode( $address ) . "'";
45 } else {
46 $sql = "SELECT * FROM ipblocks WHERE (ipb_address='" . wfStrencode( $address ) .
47 "' OR ipb_user={$user})";
48 }
49
50 $res = wfQuery( $sql, DB_READ, $fname );
51 if ( 0 == wfNumRows( $res ) ) {
52 # User is not blocked
53 $this->clear();
54 } else {
55 # Get first block
56 $row = wfFetchObject( $res );
57 $this->initFromRow( $row );
58
59 if ( $killExpired ) {
60 # If requested, delete expired rows
61 do {
62 $killed = $this->deleteIfExpired();
63 if ( $killed ) {
64 $row = wfFetchObject( $res );
65 if ( $row ) {
66 $this->initFromRow( $row );
67 }
68 }
69 } while ( $killed && $row );
70
71 # If there were any left after the killing finished, return true
72 if ( !$row ) {
73 $ret = false;
74 $this->clear();
75 } else {
76 $ret = true;
77 }
78 } else {
79 $ret = true;
80 }
81 }
82 wfFreeResult( $res );
83 return $ret;
84 }
85
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;
94 }
95
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";
99 $res = wfQuery( $sql, DB_READ, "Block::enumBans" );
100 $block = new Block();
101
102 while ( $row = wfFetchObject( $res ) ) {
103 $block->initFromRow( $row );
104 if ( $killExpired ) {
105 if ( !$block->deleteIfExpired() ) {
106 $callback( $block, $tag );
107 }
108 } else {
109 $callback( $block, $tag );
110 }
111 }
112 wfFreeResult( $res );
113 }
114
115 function delete() {
116 $fname = "Block::delete";
117 if ( $this->mAddress == "" ) {
118 $sql = "DELETE FROM ipblocks WHERE ipb_id={$this->mId}";
119 } else {
120 $sql = "DELETE FROM ipblocks WHERE ipb_address='" .
121 wfStrencode( $this->mAddress ) . "'";
122 }
123 wfQuery( $sql, DB_WRITE, "Block::delete" );
124 }
125
126 function insert() {
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" );
132 }
133
134 function deleteIfExpired() {
135 if ( $this->isExpired() ) {
136 $this->delete();
137 return true;
138 } else {
139 return false;
140 }
141 }
142
143 function isExpired() {
144 global $wgIPBlockExpiration, $wgUserBlockExpiration;
145
146 $period = $this->mUser ? $wgUserBlockExpiration : $wgIPBlockExpiration;
147
148 # Period==0 means no expiry
149 if ( !$period ) {
150 return false;
151 }
152 $expiry = wfTimestamp2Unix( $this->mTimestamp ) + $period;
153 $now = wfTimestamp2Unix( wfTimestampNow() );
154 if ( $now > $expiry ) {
155 return true;
156 } else {
157 return false;
158 }
159 }
160
161 function isValid() {
162 return $this->mAddress != "";
163 }
164
165 function updateTimestamp() {
166 wfQuery( "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() .
167 "' WHERE ipb_address='" . wfStrencode( $this->mAddress ) . "'", DB_WRITE, "Block::updateTimestamp" );
168 }
169
170 }
171 ?>