Fix IE freezing rendering whilst waiting for CSS with MonoBook (bug 624) (take 3)
[lhc/web/wiklou.git] / includes / Block.php
1 <?php
2 /**
3 * Blocks and bans object
4 * @package MediaWiki
5 */
6
7 /**
8 * Some globals
9 */
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
12
13 /**
14 * The block class
15 * All the functions in this class assume the object is either explicitly
16 * loaded or filled. It is not load-on-demand. There are no accessors.
17 *
18 * To use delete(), you only need to fill $mAddress
19 * Globals used: $wgBlockCache, $wgAutoblockExpiry
20 *
21 * @todo This could be used everywhere, but it isn't.
22 * @package MediaWiki
23 */
24 class Block
25 {
26 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
27 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
28
29 function Block( $address = '', $user = '', $by = 0, $reason = '',
30 $timestamp = '' , $auto = 0, $expiry = '' )
31 {
32 $this->mAddress = $address;
33 $this->mUser = $user;
34 $this->mBy = $by;
35 $this->mReason = $reason;
36 $this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
37 $this->mAuto = $auto;
38 if( empty( $expiry ) ) {
39 $this->mExpiry = $expiry;
40 } else {
41 $this->mExpiry = wfTimestamp( TS_MW, $expiry );
42 }
43
44 $this->mForUpdate = false;
45 $this->initialiseRange();
46 }
47
48 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
49 {
50 $ban = new Block();
51 $ban->load( $address, $user, $killExpired );
52 return $ban;
53 }
54
55 function clear()
56 {
57 $mAddress = $mReason = $mTimestamp = '';
58 $mUser = $mBy = 0;
59 }
60
61 # Get a ban from the DB, with either the given address or the given username
62 function load( $address = '', $user = 0, $killExpired = true )
63 {
64 $fname = 'Block::load';
65 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
66
67 $ret = false;
68 $killed = false;
69 if ( $this->forUpdate() ) {
70 $db =& wfGetDB( DB_MASTER );
71 $options = 'FOR UPDATE';
72 } else {
73 $db =& wfGetDB( DB_SLAVE );
74 $options = '';
75 }
76 $ipblocks = $db->tableName( 'ipblocks' );
77
78 if ( 0 == $user && $address=='' ) {
79 $sql = "SELECT * from $ipblocks $options";
80 } elseif ($address=="") {
81 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
82 } elseif ($user=="") {
83 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
84 } elseif ( $options=='' ) {
85 # If there are no optiones (e.g. FOR UPDATE), use a UNION
86 # so that the query can make efficient use of indices
87 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
88 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
89 } else {
90 # If there are options, a UNION can not be used, use one
91 # SELECT instead. Will do a full table scan.
92 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
93 "' OR ipb_user={$user}) $options";
94 }
95
96 $res = $db->query( $sql, $fname );
97 if ( 0 == $db->numRows( $res ) ) {
98 # User is not blocked
99 $this->clear();
100 } else {
101 # Get first block
102 $row = $db->fetchObject( $res );
103 $this->initFromRow( $row );
104
105 if ( $killExpired ) {
106 # If requested, delete expired rows
107 do {
108 $killed = $this->deleteIfExpired();
109 if ( $killed ) {
110 $row = $db->fetchObject( $res );
111 if ( $row ) {
112 $this->initFromRow( $row );
113 }
114 }
115 } while ( $killed && $row );
116
117 # If there were any left after the killing finished, return true
118 if ( !$row ) {
119 $ret = false;
120 $this->clear();
121 } else {
122 $ret = true;
123 }
124 } else {
125 $ret = true;
126 }
127 }
128 $db->freeResult( $res );
129 return $ret;
130 }
131
132 function initFromRow( $row )
133 {
134 $this->mAddress = $row->ipb_address;
135 $this->mReason = $row->ipb_reason;
136 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
137 $this->mUser = $row->ipb_user;
138 $this->mBy = $row->ipb_by;
139 $this->mAuto = $row->ipb_auto;
140 $this->mId = $row->ipb_id;
141 $this->mExpiry = $row->ipb_expiry ?
142 wfTimestamp(TS_MW,$row->ipb_expiry) :
143 $row->ipb_expiry;
144
145 $this->initialiseRange();
146 }
147
148 function initialiseRange()
149 {
150 if ( $this->mUser == 0 ) {
151 $rangeParts = explode( '/', $this->mAddress );
152 if ( count( $rangeParts ) == 2 ) {
153 $this->mNetworkBits = $rangeParts[1];
154 } else {
155 $this->mNetworkBits = 32;
156 }
157 $this->mIntegerAddr = ip2long( $rangeParts[0] );
158 } else {
159 $this->mNetworkBits = false;
160 $this->mIntegerAddr = false;
161 }
162 }
163
164 # Callback with a Block object for every block
165 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
166 {
167 $block = new Block();
168 if ( $flags & EB_FOR_UPDATE ) {
169 $db =& wfGetDB( DB_MASTER );
170 $options = 'FOR UPDATE';
171 $block->forUpdate( true );
172 } else {
173 $db =& wfGetDB( DB_SLAVE );
174 $options = '';
175 }
176 $ipblocks = $db->tableName( 'ipblocks' );
177
178 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
179 $res = $db->query( $sql, 'Block::enumBans' );
180
181 while ( $row = $db->fetchObject( $res ) ) {
182 $block->initFromRow( $row );
183 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
184 if ( !$block->deleteIfExpired() ) {
185 $callback( $block, $tag );
186 }
187 } else {
188 $callback( $block, $tag );
189 }
190 }
191 wfFreeResult( $res );
192 }
193
194 function delete()
195 {
196 $fname = 'Block::delete';
197 $dbw =& wfGetDB( DB_MASTER );
198
199 if ( $this->mAddress == '' ) {
200 $condition = array( 'ipb_id' => $this->mId );
201 } else {
202 $condition = array( 'ipb_address' => $this->mAddress );
203 }
204 $dbw->delete( 'ipblocks', $condition, $fname );
205 $this->clearCache();
206 }
207
208 function insert()
209 {
210 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
211 $dbw =& wfGetDB( DB_MASTER );
212 $dbw->insert( 'ipblocks',
213 array(
214 'ipb_address' => $this->mAddress,
215 'ipb_user' => $this->mUser,
216 'ipb_by' => $this->mBy,
217 'ipb_reason' => $this->mReason,
218 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
219 'ipb_auto' => $this->mAuto,
220 'ipb_expiry' => $this->mExpiry ?
221 $dbw->timestamp($this->mExpiry) :
222 $this->mExpiry,
223 ), 'Block::insert'
224 );
225
226 $this->clearCache();
227 }
228
229 function deleteIfExpired()
230 {
231 if ( $this->isExpired() ) {
232 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
233 $this->delete();
234 return true;
235 } else {
236 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
237 return false;
238 }
239 }
240
241 function isExpired()
242 {
243 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
244 if ( !$this->mExpiry ) {
245 return false;
246 } else {
247 return wfTimestampNow() > $this->mExpiry;
248 }
249 }
250
251 function isValid()
252 {
253 return $this->mAddress != '';
254 }
255
256 function updateTimestamp()
257 {
258 if ( $this->mAuto ) {
259 $this->mTimestamp = wfTimestamp();
260 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
261
262 $dbw =& wfGetDB( DB_MASTER );
263 $dbw->update( 'ipblocks',
264 array( /* SET */
265 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
266 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
267 ), array( /* WHERE */
268 'ipb_address' => $this->mAddress
269 ), 'Block::updateTimestamp'
270 );
271
272 $this->clearCache();
273 }
274 }
275
276 /* private */ function clearCache()
277 {
278 global $wgBlockCache;
279 if ( is_object( $wgBlockCache ) ) {
280 $wgBlockCache->loadFromDB();
281 }
282 }
283
284 function getIntegerAddr()
285 {
286 return $this->mIntegerAddr;
287 }
288
289 function getNetworkBits()
290 {
291 return $this->mNetworkBits;
292 }
293
294 function forUpdate( $x = NULL ) {
295 return wfSetVar( $this->mForUpdate, $x );
296 }
297
298 /* static */ function getAutoblockExpiry( $timestamp )
299 {
300 global $wgAutoblockExpiry;
301 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
302 }
303
304 /* static */ function normaliseRange( $range )
305 {
306 $parts = explode( '/', $range );
307 if ( count( $parts ) == 2 ) {
308 $shift = 32 - $parts[1];
309 $ipint = ip2long( $parts[0] );
310 $ipint = $ipint >> $shift << $shift;
311 $newip = long2ip( $ipint );
312 $range = "$newip/{$parts[1]}";
313 }
314 return $range;
315 }
316
317 }
318 ?>