* (bug 829) Fix URL-escaping on block success
[mediawiki.git] / includes / BlockCache.php
blob8f9a5a60aa10cd61759b45ab7d931ecbd1175bab
1 <?php
2 /**
3 * Contain the blockcache class
4 * @package MediaWiki
5 */
7 /**
8 * Object for fast lookup of IP blocks
9 * Represents a memcached value, and in some sense, the entire ipblocks table
10 * @package MediaWiki
12 class BlockCache
14 var $mData = false, $mMemcKey;
16 /**
17 * Constructor
18 * Create a new BlockCache object
20 * @param Boolean $deferLoad specifies whether to immediately load the data from memcached.
21 * @param String $dbName specifies the memcached dbName prefix to be used. Defaults to $wgDBname.
23 function BlockCache( $deferLoad = false, $dbName = '' ) {
24 global $wgDBname;
26 if ( $dbName == '' ) {
27 $dbName = $wgDBname;
30 $this->mMemcKey = $dbName.':ipblocks';
32 if ( !$deferLoad ) {
33 $this->load();
37 /**
38 * Load the blocks from the database and save them to memcached
40 function loadFromDB() {
41 global $wgUseMemCached, $wgMemc;
42 $this->mData = array();
43 # Selecting FOR UPDATE is a convenient way to serialise the memcached and DB operations,
44 # which is necessary even though we don't update the DB
45 if ( $wgUseMemCached ) {
46 Block::enumBlocks( 'wfBlockCacheInsert', '', EB_FOR_UPDATE );
47 $wgMemc->set( $this->mMemcKey, $this->mData, 0 );
48 } else {
49 Block::enumBlocks( 'wfBlockCacheInsert', '' );
53 /**
54 * Load the cache from memcached or, if that's not possible, from the DB
56 function load() {
57 global $wgUseMemCached, $wgMemc;
59 if ( $this->mData === false) {
60 # Try memcached
61 if ( $wgUseMemCached ) {
62 $this->mData = $wgMemc->get( $this->mMemcKey );
65 if ( !is_array( $this->mData ) ) {
66 $this->loadFromDB();
71 /**
72 * Add a block to the cache
74 * @param Object &$block Reference to a "Block" object.
76 function insert( &$block ) {
77 if ( $block->mUser == 0 ) {
78 $nb = $block->getNetworkBits();
79 $ipint = $block->getIntegerAddr();
80 $index = $ipint >> ( 32 - $nb );
82 if ( !array_key_exists( $nb, $this->mData ) ) {
83 $this->mData[$nb] = array();
86 $this->mData[$nb][$index] = 1;
90 /**
91 * Find out if a given IP address is blocked
93 * @param String $ip IP address
95 function get( $ip ) {
96 $this->load();
97 $ipint = ip2long( $ip );
98 $blocked = false;
100 foreach ( $this->mData as $networkBits => $blockInts ) {
101 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) {
102 $blocked = true;
103 break;
106 if ( $blocked ) {
107 # Clear low order bits
108 if ( $networkBits != 32 ) {
109 $ip .= '/'.$networkBits;
110 $ip = Block::normaliseRange( $ip );
112 $block = new Block();
113 $block->load( $ip );
114 } else {
115 $block = false;
118 return $block;
122 * Clear the local cache
123 * There was once a clear() to clear memcached too, but I deleted it
125 function clearLocal() {
126 $this->mData = false;
131 * Add a block to the global $wgBlockCache
133 * @param Object $block A "Block"-object
134 * @param Any $tag unused
136 function wfBlockCacheInsert( $block, $tag ) {
137 global $wgBlockCache;
138 $wgBlockCache->insert( $block );