* Removed messages in English, they'll be inherited from the parent.
[lhc/web/wiklou.git] / includes / BlockCache.php
1 <?php
2 /**
3 * Contain the blockcache class
4 * @package MediaWiki
5 * @package Cache
6 */
7
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
12 */
13 class BlockCache
14 {
15 var $mData = false, $mMemcKey;
16
17 /**
18 * Constructor
19 * Create a new BlockCache object
20 *
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.
23 */
24 function BlockCache( $deferLoad = false, $dbName = '' ) {
25 global $wgDBname;
26
27 if ( $dbName == '' ) {
28 $dbName = $wgDBname;
29 }
30
31 $this->mMemcKey = $dbName.':ipblocks';
32
33 if ( !$deferLoad ) {
34 $this->load();
35 }
36 }
37
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
41 */
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', '' );
52 }
53 }
54
55 /**
56 * Load the cache from memcached or, if that's not possible, from the DB
57 */
58 function load( $bFromSlave ) {
59 global $wgUseMemCached, $wgMemc;
60
61 if ( $this->mData === false) {
62 $this->loadFromDB( $bFromSlave );
63 /*
64 // Memcache disabled for performance issues.
65 # Try memcached
66 if ( $wgUseMemCached ) {
67 $this->mData = $wgMemc->get( $this->mMemcKey );
68 }
69
70 if ( !is_array( $this->mData ) ) {
71 $this->loadFromDB( $bFromSlave );
72 }*/
73 }
74 }
75
76 /**
77 * Add a block to the cache
78 *
79 * @param Object &$block Reference to a "Block" object.
80 */
81 function insert( &$block ) {
82 if ( $block->mUser == 0 ) {
83 $nb = $block->getNetworkBits();
84 $ipint = $block->getIntegerAddr();
85 $index = $ipint >> ( 32 - $nb );
86
87 if ( !array_key_exists( $nb, $this->mData ) ) {
88 $this->mData[$nb] = array();
89 }
90
91 $this->mData[$nb][$index] = 1;
92 }
93 }
94
95 /**
96 * Find out if a given IP address is blocked
97 *
98 * @param String $ip IP address
99 * @param bool $bFromSlave True means to load check against slave, else check against master.
100 */
101 function get( $ip, $bFromSlave ) {
102 $this->load( $bFromSlave );
103 $ipint = ip2long( $ip );
104 $blocked = false;
105
106 foreach ( $this->mData as $networkBits => $blockInts ) {
107 if ( array_key_exists( $ipint >> ( 32 - $networkBits ), $blockInts ) ) {
108 $blocked = true;
109 break;
110 }
111 }
112 if ( $blocked ) {
113 # Clear low order bits
114 if ( $networkBits != 32 ) {
115 $ip .= '/'.$networkBits;
116 $ip = Block::normaliseRange( $ip );
117 }
118 $block = new Block();
119 $block->forUpdate( $bFromSlave );
120 $block->load( $ip );
121 } else {
122 $block = false;
123 }
124
125 return $block;
126 }
127
128 /**
129 * Clear the local cache
130 * There was once a clear() to clear memcached too, but I deleted it
131 */
132 function clearLocal() {
133 $this->mData = false;
134 }
135 }
136
137 /**
138 * Add a block to the global $wgBlockCache
139 *
140 * @param Object $block A "Block"-object
141 * @param Any $tag unused
142 */
143 function wfBlockCacheInsert( $block, $tag ) {
144 global $wgBlockCache;
145 $wgBlockCache->insert( $block );
146 }