Don't move user_newtalk into watchlist
[mediawiki.git] / includes / BlockCache.php
blobc64adc1094d9a03636cb33db4685d120ef2ceedf
1 <?php
2 /**
3 * Contain the blockcache class
4 * @package MediaWiki
5 * @package Cache
6 */
8 /**
9 * Object for fast lookup of IP blocks
10 * Represents a memcached value, and in some sense, the entire ipblocks table
11 * @package MediaWiki
13 class BlockCache
15 var $mData = false, $mMemcKey;
17 /**
18 * Constructor
19 * Create a new BlockCache object
21 * @param Boolean $deferLoad specifies whether to immediately load the data from memcached.
22 * @param String $dbName specifies the memcached dbName prefix to be used. Defaults to $wgDBname.
24 function BlockCache( $deferLoad = false, $dbName = '' ) {
25 global $wgDBname;
27 if ( $dbName == '' ) {
28 $dbName = $wgDBname;
31 $this->mMemcKey = $dbName.':ipblocks';
33 if ( !$deferLoad ) {
34 $this->load();
38 /**
39 * Load the blocks from the database and save them to memcached
40 * @param bool $bFromSlave Whether to load data from slaves or master
42 function loadFromDB( $bFromSlave = false ) {
43 global $wgUseMemCached, $wgMemc;
44 $this->mData = array();
45 # Selecting FOR UPDATE is a convenient way to serialise the memcached and DB operations,
46 # which is necessary even though we don't update the DB
47 if ( !$bFromSlave ) {
48 Block::enumBlocks( 'wfBlockCacheInsert', '', EB_FOR_UPDATE );
49 #$wgMemc->set( $this->mMemcKey, $this->mData, 0 );
50 } else {
51 Block::enumBlocks( 'wfBlockCacheInsert', '' );
55 /**
56 * Load the cache from memcached or, if that's not possible, from the DB
58 function load( $bFromSlave ) {
59 global $wgUseMemCached, $wgMemc;
61 if ( $this->mData === false) {
62 $this->loadFromDB( $bFromSlave );
64 // Memcache disabled for performance issues.
65 # Try memcached
66 if ( $wgUseMemCached ) {
67 $this->mData = $wgMemc->get( $this->mMemcKey );
70 if ( !is_array( $this->mData ) ) {
71 $this->loadFromDB( $bFromSlave );
72 }*/
76 /**
77 * Add a block to the cache
79 * @param Object &$block Reference to a "Block" object.
81 function insert( &$block ) {
82 if ( $block->mUser == 0 ) {
83 $nb = $block->getNetworkBits();
84 $ipint = $block->getIntegerAddr();
85 $index = $ipint >> ( 32 - $nb );
87 if ( !array_key_exists( $nb, $this->mData ) ) {
88 $this->mData[$nb] = array();
91 $this->mData[$nb][$index] = 1;
95 /**
96 * Find out if a given IP address is blocked
98 * @param String $ip IP address
99 * @param bool $bFromSlave True means to load check against slave, else check against master.
101 function get( $ip, $bFromSlave ) {
102 $this->load( $bFromSlave );
103 $ipint = ip2long( $ip );
104 $blocked = false;
106 foreach ( $this->mData as $networkBits => $blockInts ) {
107 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) {
108 $blocked = true;
109 break;
112 if ( $blocked ) {
113 # Clear low order bits
114 if ( $networkBits != 32 ) {
115 $ip .= '/'.$networkBits;
116 $ip = Block::normaliseRange( $ip );
118 $block = new Block();
119 $block->forUpdate( $bFromSlave );
120 $block->load( $ip );
121 } else {
122 $block = false;
125 return $block;
129 * Clear the local cache
130 * There was once a clear() to clear memcached too, but I deleted it
132 function clearLocal() {
133 $this->mData = false;
138 * Add a block to the global $wgBlockCache
140 * @param Object $block A "Block"-object
141 * @param Any $tag unused
143 function wfBlockCacheInsert( $block, $tag ) {
144 global $wgBlockCache;
145 $wgBlockCache->insert( $block );