Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

BlockCache.php

Go to the documentation of this file.
00001 <?php 00002 00003 # Object for fast lookup of IP blocks 00004 # Represents a memcached value, and in some sense, the entire ipblocks table 00005 00006 class BlockCache 00007 { 00008 var $mData = false, $mMemcKey; 00009 00010 function BlockCache( $deferLoad = false, $dbName = '' ) 00011 { 00012 global $wgDBname; 00013 00014 if ( $dbName == '' ) { 00015 $dbName = $wgDBname; 00016 } 00017 00018 $this->mMemcKey = $dbName.':ipblocks'; 00019 00020 if ( !$deferLoad ) { 00021 $this->load(); 00022 } 00023 } 00024 00025 function load() 00026 { 00027 global $wgUseMemCached, $wgMemc; 00028 00029 if ( $this->mData === false) { 00030 $saveMemc = false; 00031 # Try memcached 00032 if ( $wgUseMemCached ) { 00033 $this->mData = $wgMemc->get( $this->mMemcKey ); 00034 if ( !$this->mData ) { 00035 $saveMemc = true; 00036 } 00037 } 00038 00039 if ( !is_array( $this->mData ) ) { 00040 # Load from DB 00041 $this->mData = array(); 00042 Block::enumBlocks( 'wfBlockCacheInsert', '' ); # Calls $this->insert() 00043 } 00044 00045 if ( $saveMemc ) { 00046 $wgMemc->set( $this->mMemcKey, $this->mData, 0 ); 00047 } 00048 } 00049 } 00050 00051 function insert( &$block ) 00052 { 00053 if ( $block->mUser == 0 ) { 00054 $nb = $block->getNetworkBits(); 00055 $ipint = $block->getIntegerAddr(); 00056 $index = $ipint >> ( 32 - $nb ); 00057 00058 if ( !array_key_exists( $nb, $this->mData ) ) { 00059 $this->mData[$nb] = array(); 00060 } 00061 00062 $this->mData[$nb][$index] = 1; 00063 } 00064 } 00065 00066 function get( $ip ) 00067 { 00068 $this->load(); 00069 $ipint = ip2long( $ip ); 00070 $blocked = false; 00071 00072 foreach ( $this->mData as $networkBits => $blockInts ) { 00073 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) { 00074 $blocked = true; 00075 break; 00076 } 00077 } 00078 if ( $blocked ) { 00079 # Clear low order bits 00080 if ( $networkBits != 32 ) { 00081 $ip .= '/'.$networkBits; 00082 $ip = Block::normaliseRange( $ip ); 00083 } 00084 $block = new Block(); 00085 $block->load( $ip ); 00086 } else { 00087 $block = false; 00088 } 00089 00090 return $block; 00091 } 00092 00093 function clear() 00094 { 00095 global $wgUseMemCached, $wgMemc; 00096 00097 $this->mData = false; 00098 if ( $wgUseMemCached ) { 00099 $wgMemc->delete( $this->mMemcKey ); 00100 } 00101 } 00102 00103 function clearLocal() 00104 { 00105 $this->mData = false; 00106 } 00107 } 00108 00109 function wfBlockCacheInsert( $block, $tag ) 00110 { 00111 global $wgBlockCache; 00112 $wgBlockCache->insert( $block ); 00113 }

Generated on Tue Jun 29 23:40:03 2004 for Mediawiki by doxygen 1.3.7