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