Live hack: Skip some work on empty category/link sets
[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 define ( 'EB_RANGE_ONLY', 4 );
13
14 /**
15 * The block class
16 * All the functions in this class assume the object is either explicitly
17 * loaded or filled. It is not load-on-demand. There are no accessors.
18 *
19 * To use delete(), you only need to fill $mAddress
20 * Globals used: $wgAutoblockExpiry, $wgAntiLockFlags
21 *
22 * @todo This could be used everywhere, but it isn't.
23 * @package MediaWiki
24 */
25 class Block
26 {
27 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
28 $mRangeStart, $mRangeEnd;
29 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mFromMaster, $mByName;
30
31 function Block( $address = '', $user = '', $by = 0, $reason = '',
32 $timestamp = '' , $auto = 0, $expiry = '' )
33 {
34 $this->mAddress = $address;
35 $this->mUser = $user;
36 $this->mBy = $by;
37 $this->mReason = $reason;
38 $this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
39 $this->mAuto = $auto;
40 if( empty( $expiry ) ) {
41 $this->mExpiry = $expiry;
42 } else {
43 $this->mExpiry = wfTimestamp( TS_MW, $expiry );
44 }
45
46 $this->mForUpdate = false;
47 $this->mFromMaster = false;
48 $this->mByName = false;
49 $this->initialiseRange();
50 }
51
52 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
53 {
54 $ban = new Block();
55 $ban->load( $address, $user, $killExpired );
56 return $ban;
57 }
58
59 function clear()
60 {
61 $this->mAddress = $this->mReason = $this->mTimestamp = '';
62 $this->mUser = $this->mBy = 0;
63 $this->mByName = false;
64
65 }
66
67 /**
68 * Get the DB object and set the reference parameter to the query options
69 */
70 function &getDBOptions( &$options )
71 {
72 global $wgAntiLockFlags;
73 if ( $this->mForUpdate || $this->mFromMaster ) {
74 $db =& wfGetDB( DB_MASTER );
75 if ( !$this->mForUpdate || ($wgAntiLockFlags & ALF_NO_BLOCK_LOCK) ) {
76 $options = '';
77 } else {
78 $options = 'FOR UPDATE';
79 }
80 } else {
81 $db =& wfGetDB( DB_SLAVE );
82 $options = '';
83 }
84 return $db;
85 }
86
87 /**
88 * Get a ban from the DB, with either the given address or the given username
89 */
90 function load( $address = '', $user = 0, $killExpired = true )
91 {
92 $fname = 'Block::load';
93 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
94
95 $options = '';
96 $db =& $this->getDBOptions( $options );
97
98 $ret = false;
99 $killed = false;
100 $ipblocks = $db->tableName( 'ipblocks' );
101
102 if ( 0 == $user && $address == '' ) {
103 # Invalid user specification, not blocked
104 $this->clear();
105 return false;
106 } elseif ( $address == '' ) {
107 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
108 } elseif ( $user == '' ) {
109 $sql = "SELECT * FROM $ipblocks WHERE ipb_address=" . $db->addQuotes( $address ) . " $options";
110 } elseif ( $options == '' ) {
111 # If there are no options (e.g. FOR UPDATE), use a UNION
112 # so that the query can make efficient use of indices
113 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
114 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
115 } else {
116 # If there are options, a UNION can not be used, use one
117 # SELECT instead. Will do a full table scan.
118 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
119 "' OR ipb_user={$user}) $options";
120 }
121
122 $res = $db->query( $sql, $fname );
123 if ( 0 != $db->numRows( $res ) ) {
124 # Get first block
125 $row = $db->fetchObject( $res );
126 $this->initFromRow( $row );
127
128 if ( $killExpired ) {
129 # If requested, delete expired rows
130 do {
131 $killed = $this->deleteIfExpired();
132 if ( $killed ) {
133 $row = $db->fetchObject( $res );
134 if ( $row ) {
135 $this->initFromRow( $row );
136 }
137 }
138 } while ( $killed && $row );
139
140 # If there were any left after the killing finished, return true
141 if ( !$row ) {
142 $ret = false;
143 $this->clear();
144 } else {
145 $ret = true;
146 }
147 } else {
148 $ret = true;
149 }
150 }
151 $db->freeResult( $res );
152
153 # No blocks found yet? Try looking for range blocks
154 if ( !$ret && $address != '' ) {
155 $ret = $this->loadRange( $address, $killExpired );
156 }
157 if ( !$ret ) {
158 $this->clear();
159 }
160
161 return $ret;
162 }
163
164 /**
165 * Search the database for any range blocks matching the given address, and
166 * load the row if one is found.
167 */
168 function loadRange( $address, $killExpired = true )
169 {
170 $fname = 'Block::loadRange';
171
172 $iaddr = wfIP2Hex( $address );
173 if ( $iaddr === false ) {
174 # Invalid address
175 return false;
176 }
177
178 # Only scan ranges which start in this /16, this improves search speed
179 # Blocks should not cross a /16 boundary.
180 $range = substr( $iaddr, 0, 4 );
181
182 $options = '';
183 $db =& $this->getDBOptions( $options );
184 $ipblocks = $db->tableName( 'ipblocks' );
185 $sql = "SELECT * FROM $ipblocks WHERE ipb_range_start LIKE '$range%' ".
186 "AND ipb_range_start <= '$iaddr' AND ipb_range_end >= '$iaddr' $options";
187 $res = $db->query( $sql, $fname );
188 $row = $db->fetchObject( $res );
189
190 $success = false;
191 if ( $row ) {
192 # Found a row, initialise this object
193 $this->initFromRow( $row );
194
195 # Is it expired?
196 if ( !$killExpired || !$this->deleteIfExpired() ) {
197 # No, return true
198 $success = true;
199 }
200 }
201
202 $db->freeResult( $res );
203 return $success;
204 }
205
206 /**
207 * Determine if a given integer IPv4 address is in a given CIDR network
208 */
209 function isAddressInRange( $addr, $range ) {
210 list( $network, $bits ) = wfParseCIDR( $range );
211 if ( $network !== false && $addr >> ( 32 - $bits ) == $network >> ( 32 - $bits ) ) {
212 return true;
213 } else {
214 return false;
215 }
216 }
217
218 function initFromRow( $row )
219 {
220 $this->mAddress = $row->ipb_address;
221 $this->mReason = $row->ipb_reason;
222 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
223 $this->mUser = $row->ipb_user;
224 $this->mBy = $row->ipb_by;
225 $this->mAuto = $row->ipb_auto;
226 $this->mId = $row->ipb_id;
227 $this->mExpiry = $row->ipb_expiry ?
228 wfTimestamp(TS_MW,$row->ipb_expiry) :
229 $row->ipb_expiry;
230 if ( isset( $row->user_name ) ) {
231 $this->mByName = $row->user_name;
232 } else {
233 $this->mByName = false;
234 }
235 $this->mRangeStart = $row->ipb_range_start;
236 $this->mRangeEnd = $row->ipb_range_end;
237 }
238
239 function initialiseRange()
240 {
241 $this->mRangeStart = '';
242 $this->mRangeEnd = '';
243 if ( $this->mUser == 0 ) {
244 list( $network, $bits ) = wfParseCIDR( $this->mAddress );
245 if ( $network !== false ) {
246 $this->mRangeStart = sprintf( '%08X', $network );
247 $this->mRangeEnd = sprintf( '%08X', $network + (1 << (32 - $bits)) - 1 );
248 }
249 }
250 }
251
252 /**
253 * Callback with a Block object for every block
254 * @return integer number of blocks;
255 */
256 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
257 {
258 global $wgAntiLockFlags;
259
260 $block = new Block();
261 if ( $flags & EB_FOR_UPDATE ) {
262 $db =& wfGetDB( DB_MASTER );
263 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
264 $options = '';
265 } else {
266 $options = 'FOR UPDATE';
267 }
268 $block->forUpdate( true );
269 } else {
270 $db =& wfGetDB( DB_SLAVE );
271 $options = '';
272 }
273 if ( $flags & EB_RANGE_ONLY ) {
274 $cond = " AND ipb_range_start <> ''";
275 } else {
276 $cond = '';
277 }
278
279 $now = wfTimestampNow();
280
281 extract( $db->tableNames( 'ipblocks', 'user' ) );
282
283 $sql = "SELECT $ipblocks.*,user_name FROM $ipblocks,$user " .
284 "WHERE user_id=ipb_by $cond ORDER BY ipb_timestamp DESC $options";
285 $res = $db->query( $sql, 'Block::enumBlocks' );
286 $num_rows = $db->numRows( $res );
287
288 while ( $row = $db->fetchObject( $res ) ) {
289 $block->initFromRow( $row );
290 if ( ( $flags & EB_RANGE_ONLY ) && $block->mRangeStart == '' ) {
291 continue;
292 }
293
294 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
295 if ( $block->mExpiry && $now > $block->mExpiry ) {
296 $block->delete();
297 } else {
298 call_user_func( $callback, $block, $tag );
299 }
300 } else {
301 call_user_func( $callback, $block, $tag );
302 }
303 }
304 wfFreeResult( $res );
305 return $num_rows;
306 }
307
308 function delete()
309 {
310 $fname = 'Block::delete';
311 if (wfReadOnly()) {
312 return;
313 }
314 $dbw =& wfGetDB( DB_MASTER );
315
316 if ( $this->mAddress == '' ) {
317 $condition = array( 'ipb_id' => $this->mId );
318 } else {
319 $condition = array( 'ipb_address' => $this->mAddress );
320 }
321 return( $dbw->delete( 'ipblocks', $condition, $fname ) > 0 ? true : false );
322 }
323
324 function insert()
325 {
326 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
327 $dbw =& wfGetDB( DB_MASTER );
328 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
329 $dbw->insert( 'ipblocks',
330 array(
331 'ipb_id' => $ipb_id,
332 'ipb_address' => $this->mAddress,
333 'ipb_user' => $this->mUser,
334 'ipb_by' => $this->mBy,
335 'ipb_reason' => $this->mReason,
336 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
337 'ipb_auto' => $this->mAuto,
338 'ipb_expiry' => $this->mExpiry ?
339 $dbw->timestamp($this->mExpiry) :
340 $this->mExpiry,
341 'ipb_range_start' => $this->mRangeStart,
342 'ipb_range_end' => $this->mRangeEnd,
343 ), 'Block::insert'
344 );
345 }
346
347 function deleteIfExpired()
348 {
349 $fname = 'Block::deleteIfExpired';
350 wfProfileIn( $fname );
351 if ( $this->isExpired() ) {
352 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
353 $this->delete();
354 $retVal = true;
355 } else {
356 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
357 $retVal = false;
358 }
359 wfProfileOut( $fname );
360 return $retVal;
361 }
362
363 function isExpired()
364 {
365 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
366 if ( !$this->mExpiry ) {
367 return false;
368 } else {
369 return wfTimestampNow() > $this->mExpiry;
370 }
371 }
372
373 function isValid()
374 {
375 return $this->mAddress != '';
376 }
377
378 function updateTimestamp()
379 {
380 if ( $this->mAuto ) {
381 $this->mTimestamp = wfTimestamp();
382 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
383
384 $dbw =& wfGetDB( DB_MASTER );
385 $dbw->update( 'ipblocks',
386 array( /* SET */
387 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
388 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
389 ), array( /* WHERE */
390 'ipb_address' => $this->mAddress
391 ), 'Block::updateTimestamp'
392 );
393 }
394 }
395
396 /*
397 function getIntegerAddr()
398 {
399 return $this->mIntegerAddr;
400 }
401
402 function getNetworkBits()
403 {
404 return $this->mNetworkBits;
405 }*/
406
407 function getByName()
408 {
409 if ( $this->mByName === false ) {
410 $this->mByName = User::whoIs( $this->mBy );
411 }
412 return $this->mByName;
413 }
414
415 function forUpdate( $x = NULL ) {
416 return wfSetVar( $this->mForUpdate, $x );
417 }
418
419 function fromMaster( $x = NULL ) {
420 return wfSetVar( $this->mFromMaster, $x );
421 }
422
423 /* static */ function getAutoblockExpiry( $timestamp )
424 {
425 global $wgAutoblockExpiry;
426 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
427 }
428
429 /* static */ function normaliseRange( $range )
430 {
431 $parts = explode( '/', $range );
432 if ( count( $parts ) == 2 ) {
433 $shift = 32 - $parts[1];
434 $ipint = wfIP2Unsigned( $parts[0] );
435 $ipint = $ipint >> $shift << $shift;
436 $newip = long2ip( $ipint );
437 $range = "$newip/{$parts[1]}";
438 }
439 return $range;
440 }
441
442 }
443 ?>