Made BlockCache::loadFromDB() 100x faster. Wasn't an issue before ryo commented out...
[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: $wgBlockCache, $wgAutoblockExpiry
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 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
29
30 function Block( $address = '', $user = '', $by = 0, $reason = '',
31 $timestamp = '' , $auto = 0, $expiry = '' )
32 {
33 $this->mAddress = $address;
34 $this->mUser = $user;
35 $this->mBy = $by;
36 $this->mReason = $reason;
37 $this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
38 $this->mAuto = $auto;
39 if( empty( $expiry ) ) {
40 $this->mExpiry = $expiry;
41 } else {
42 $this->mExpiry = wfTimestamp( TS_MW, $expiry );
43 }
44
45 $this->mForUpdate = false;
46 $this->initialiseRange();
47 }
48
49 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
50 {
51 $ban = new Block();
52 $ban->load( $address, $user, $killExpired );
53 return $ban;
54 }
55
56 function clear()
57 {
58 $mAddress = $mReason = $mTimestamp = '';
59 $mUser = $mBy = 0;
60 }
61
62 /**
63 * Get a ban from the DB, with either the given address or the given username
64 */
65 function load( $address = '', $user = 0, $killExpired = true )
66 {
67 global $wgDBmysql4, $wgAntiLockFlags;
68 $fname = 'Block::load';
69 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
70
71 $ret = false;
72 $killed = false;
73 if ( $this->forUpdate() ) {
74 $db =& wfGetDB( DB_MASTER );
75 if ( $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 $ipblocks = $db->tableName( 'ipblocks' );
85
86 if ( 0 == $user && $address=='' ) {
87 $sql = "SELECT * from $ipblocks $options";
88 } elseif ($address=="") {
89 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
90 } elseif ($user=="") {
91 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
92 } elseif ( $options=='' && $wgDBmysql4 ) {
93 # If there are no optiones (e.g. FOR UPDATE), use a UNION
94 # so that the query can make efficient use of indices
95 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
96 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
97 } else {
98 # If there are options, a UNION can not be used, use one
99 # SELECT instead. Will do a full table scan.
100 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
101 "' OR ipb_user={$user}) $options";
102 }
103
104 $res = $db->query( $sql, $fname );
105 if ( 0 == $db->numRows( $res ) ) {
106 # User is not blocked
107 $this->clear();
108 } else {
109 # Get first block
110 $row = $db->fetchObject( $res );
111 $this->initFromRow( $row );
112
113 if ( $killExpired ) {
114 # If requested, delete expired rows
115 do {
116 $killed = $this->deleteIfExpired();
117 if ( $killed ) {
118 $row = $db->fetchObject( $res );
119 if ( $row ) {
120 $this->initFromRow( $row );
121 }
122 }
123 } while ( $killed && $row );
124
125 # If there were any left after the killing finished, return true
126 if ( !$row ) {
127 $ret = false;
128 $this->clear();
129 } else {
130 $ret = true;
131 }
132 } else {
133 $ret = true;
134 }
135 }
136 $db->freeResult( $res );
137 return $ret;
138 }
139
140 function initFromRow( $row )
141 {
142 $this->mAddress = $row->ipb_address;
143 $this->mReason = $row->ipb_reason;
144 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
145 $this->mUser = $row->ipb_user;
146 $this->mBy = $row->ipb_by;
147 $this->mAuto = $row->ipb_auto;
148 $this->mId = $row->ipb_id;
149 $this->mExpiry = $row->ipb_expiry ?
150 wfTimestamp(TS_MW,$row->ipb_expiry) :
151 $row->ipb_expiry;
152
153 $this->initialiseRange();
154 }
155
156 function initialiseRange()
157 {
158 if ( $this->mUser == 0 ) {
159 $rangeParts = explode( '/', $this->mAddress );
160 if ( count( $rangeParts ) == 2 ) {
161 $this->mNetworkBits = $rangeParts[1];
162 } else {
163 $this->mNetworkBits = 32;
164 }
165 $this->mIntegerAddr = ip2long( $rangeParts[0] );
166 } else {
167 $this->mNetworkBits = false;
168 $this->mIntegerAddr = false;
169 }
170 }
171
172 /**
173 * Callback with a Block object for every block
174 * @return integer number of blocks;
175 */
176 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
177 {
178 global $wgAntiLockFlags;
179
180 $block = new Block();
181 if ( $flags & EB_FOR_UPDATE ) {
182 $db =& wfGetDB( DB_MASTER );
183 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
184 $options = '';
185 } else {
186 $options = 'FOR UPDATE';
187 }
188 $block->forUpdate( true );
189 } else {
190 $db =& wfGetDB( DB_SLAVE );
191 $options = '';
192 }
193 if ( $flags & EB_RANGE_ONLY ) {
194 $cond = " WHERE ipb_address LIKE '%/%'";
195 } else {
196 $cond = '';
197 }
198
199 $ipblocks = $db->tableName( 'ipblocks' );
200
201 $sql = "SELECT * FROM $ipblocks $cond ORDER BY ipb_timestamp DESC $options";
202 $res = $db->query( $sql, 'Block::enumBans' );
203 $num_rows = $db->numRows( $res );
204
205 while ( $row = $db->fetchObject( $res ) ) {
206 $block->initFromRow( $row );
207 if ( ( $flags & EB_RANGE_ONLY ) && $block->getNetworkBits() == 32 ) {
208 continue;
209 }
210
211 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
212 if ( !$block->deleteIfExpired() ) {
213 $callback( $block, $tag );
214 }
215 } else {
216 $callback( $block, $tag );
217 }
218 }
219 wfFreeResult( $res );
220 return $num_rows;
221 }
222
223 function delete()
224 {
225 $fname = 'Block::delete';
226 if (wfReadOnly()) {
227 return;
228 }
229 $dbw =& wfGetDB( DB_MASTER );
230
231 if ( $this->mAddress == '' ) {
232 $condition = array( 'ipb_id' => $this->mId );
233 } else {
234 $condition = array( 'ipb_address' => $this->mAddress );
235 }
236 $dbw->delete( 'ipblocks', $condition, $fname );
237 $this->clearCache();
238 }
239
240 function insert()
241 {
242 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
243 $dbw =& wfGetDB( DB_MASTER );
244 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
245 $dbw->insert( 'ipblocks',
246 array(
247 'ipb_id' => $ipb_id,
248 'ipb_address' => $this->mAddress,
249 'ipb_user' => $this->mUser,
250 'ipb_by' => $this->mBy,
251 'ipb_reason' => $this->mReason,
252 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
253 'ipb_auto' => $this->mAuto,
254 'ipb_expiry' => $this->mExpiry ?
255 $dbw->timestamp($this->mExpiry) :
256 $this->mExpiry,
257 ), 'Block::insert'
258 );
259
260 $this->clearCache();
261 }
262
263 function deleteIfExpired()
264 {
265 if ( $this->isExpired() ) {
266 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
267 $this->delete();
268 return true;
269 } else {
270 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
271 return false;
272 }
273 }
274
275 function isExpired()
276 {
277 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
278 if ( !$this->mExpiry ) {
279 return false;
280 } else {
281 return wfTimestampNow() > $this->mExpiry;
282 }
283 }
284
285 function isValid()
286 {
287 return $this->mAddress != '';
288 }
289
290 function updateTimestamp()
291 {
292 if ( $this->mAuto ) {
293 $this->mTimestamp = wfTimestamp();
294 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
295
296 $dbw =& wfGetDB( DB_MASTER );
297 $dbw->update( 'ipblocks',
298 array( /* SET */
299 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
300 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
301 ), array( /* WHERE */
302 'ipb_address' => $this->mAddress
303 ), 'Block::updateTimestamp'
304 );
305
306 $this->clearCache();
307 }
308 }
309
310 /* private */ function clearCache()
311 {
312 global $wgBlockCache;
313 if ( is_object( $wgBlockCache ) ) {
314 $wgBlockCache->loadFromDB();
315 }
316 }
317
318 function getIntegerAddr()
319 {
320 return $this->mIntegerAddr;
321 }
322
323 function getNetworkBits()
324 {
325 return $this->mNetworkBits;
326 }
327
328 function forUpdate( $x = NULL ) {
329 return wfSetVar( $this->mForUpdate, $x );
330 }
331
332 /* static */ function getAutoblockExpiry( $timestamp )
333 {
334 global $wgAutoblockExpiry;
335 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
336 }
337
338 /* static */ function normaliseRange( $range )
339 {
340 $parts = explode( '/', $range );
341 if ( count( $parts ) == 2 ) {
342 $shift = 32 - $parts[1];
343 $ipint = ip2long( $parts[0] );
344 $ipint = $ipint >> $shift << $shift;
345 $newip = long2ip( $ipint );
346 $range = "$newip/{$parts[1]}";
347 }
348 return $range;
349 }
350
351 }
352 ?>