Change some search index update code to do two regexps backwards.
[mediawiki.git] / includes / BlockCache.php
blob9f099b28467bd87f68e35ae6fa4b7b7a3e9db0e8
1 <?php
3 # Object for fast lookup of IP blocks
4 # Represents a memcached value, and in some sense, the entire ipblocks table
6 class BlockCache
8 var $mData = false, $mMemcKey;
10 function BlockCache( $deferLoad = false, $dbName = '' ) {
11 global $wgDBname;
13 if ( $dbName == '' ) {
14 $dbName = $wgDBname;
17 $this->mMemcKey = $dbName.':ipblocks';
19 if ( !$deferLoad ) {
20 $this->load();
24 # Load the blocks from the database and save them to memcached
25 function loadFromDB() {
26 global $wgUseMemCached, $wgMemc;
27 $this->mData = array();
28 # Selecting FOR UPDATE is a convenient way to serialise the memcached and DB operations,
29 # which is necessary even though we don't update the DB
30 if ( $wgUseMemCached ) {
31 Block::enumBlocks( 'wfBlockCacheInsert', '', EB_FOR_UPDATE );
32 $wgMemc->set( $this->mMemcKey, $this->mData, 0 );
33 } else {
34 Block::enumBlocks( 'wfBlockCacheInsert', '' );
38 # Load the cache from memcached or, if that's not possible, from the DB
39 function load() {
40 global $wgUseMemCached, $wgMemc;
42 if ( $this->mData === false) {
43 # Try memcached
44 if ( $wgUseMemCached ) {
45 $this->mData = $wgMemc->get( $this->mMemcKey );
48 if ( !is_array( $this->mData ) ) {
49 $this->loadFromDB();
54 # Add a block to the cache
55 function insert( &$block ) {
56 if ( $block->mUser == 0 ) {
57 $nb = $block->getNetworkBits();
58 $ipint = $block->getIntegerAddr();
59 $index = $ipint >> ( 32 - $nb );
61 if ( !array_key_exists( $nb, $this->mData ) ) {
62 $this->mData[$nb] = array();
65 $this->mData[$nb][$index] = 1;
69 # Find out if a given IP address is blocked
70 function get( $ip ) {
71 $this->load();
72 $ipint = ip2long( $ip );
73 $blocked = false;
75 foreach ( $this->mData as $networkBits => $blockInts ) {
76 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) {
77 $blocked = true;
78 break;
81 if ( $blocked ) {
82 # Clear low order bits
83 if ( $networkBits != 32 ) {
84 $ip .= '/'.$networkBits;
85 $ip = Block::normaliseRange( $ip );
87 $block = new Block();
88 $block->load( $ip );
89 } else {
90 $block = false;
93 return $block;
96 # Clear the local cache
97 # There was once a clear() to clear memcached too, but I deleted it
98 function clearLocal() {
99 $this->mData = false;
103 function wfBlockCacheInsert( $block, $tag ) {
104 global $wgBlockCache;
105 $wgBlockCache->insert( $block );