* Added a test for a link with multiple pipes
[lhc/web/wiklou.git] / includes / ObjectCache.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Cache
5 */
6
7 if (!defined('MEDIAWIKI')) die( "Not a valid entry point\n");
8
9
10 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
11 # It acts as a memcached server with no RAM, that is, all objects are
12 # cleared the moment they are set. All set operations succeed and all
13 # get operations return null.
14 class FakeMemCachedClient {
15 function add ($key, $val, $exp = 0) { return true; }
16 function decr ($key, $amt=1) { return null; }
17 function delete ($key, $time = 0) { return false; }
18 function disconnect_all () { }
19 function enable_compress ($enable) { }
20 function forget_dead_hosts () { }
21 function get ($key) { return null; }
22 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
23 function incr ($key, $amt=1) { return null; }
24 function replace ($key, $value, $exp=0) { return false; }
25 function run_command ($sock, $cmd) { return null; }
26 function set ($key, $value, $exp=0){ return true; }
27 function set_compress_threshold ($thresh){ }
28 function set_debug ($dbg) { }
29 function set_servers ($list) { }
30 }
31
32 global $wgCaches;
33 $wgCaches = array();
34
35 function &wfGetCache( $inputType ) {
36 global $wgCaches, $wgMemCachedServers, $wgMemCachedDebug;
37 $cache = false;
38
39 if ( $inputType == CACHE_ANYTHING ) {
40 reset( $wgCaches );
41 $type = key( $wgCaches );
42 if ( $type === false || $type === CACHE_NONE ) {
43 $type = CACHE_DB;
44 }
45 } else {
46 $type = $inputType;
47 }
48
49 if ( $type == CACHE_MEMCACHED ) {
50 if ( !array_key_exists( CACHE_MEMCACHED, $wgCaches ) ){
51 require_once( 'memcached-client.php' );
52
53 class MemCachedClientforWiki extends memcached {
54 function _debugprint( $text ) {
55 wfDebug( "memcached: $text\n" );
56 }
57 }
58
59 $wgCaches[CACHE_DB] = new MemCachedClientforWiki(
60 array('persistant' => true, 'compress_threshold' => 1500 ) );
61 $cache =& $wgCaches[CACHE_DB];
62 $cache->set_servers( $wgMemCachedServers );
63 $cache->set_debug( $wgMemCachedDebug );
64 }
65 } elseif ( $type == CACHE_ACCEL ) {
66 if ( !array_key_exists( CACHE_ACCEL, $wgCaches ) ) {
67 if ( function_exists( 'eaccelerator_get' ) ) {
68 require_once( 'BagOStuff.php' );
69 $wgCaches[CACHE_ACCEL] = new eAccelBagOStuff;
70 } elseif ( function_exists( 'mmcache_get' ) ) {
71 require_once( 'BagOStuff.php' );
72 $wgCaches[CACHE_ACCEL] = new TurckBagOStuff;
73 } else {
74 $wgCaches[CACHE_ACCEL] = false;
75 }
76 }
77 if ( $wgCaches[CACHE_ACCEL] !== false ) {
78 $cache =& $wgCaches[CACHE_ACCEL];
79 }
80 }
81
82 if ( $type == CACHE_DB || ( $inputType == CACHE_ANYTHING && $cache === false ) ) {
83 if ( !array_key_exists( CACHE_DB, $wgCaches ) ) {
84 require_once( 'BagOStuff.php' );
85 $wgCaches[CACHE_DB] = new MediaWikiBagOStuff('objectcache');
86 }
87 $cache =& $wgCaches[CACHE_DB];
88 }
89
90 if ( $cache === false ) {
91 if ( !array_key_exists( CACHE_NONE, $wgCaches ) ) {
92 $wgCaches[CACHE_NONE] = new FakeMemCachedClient;
93 }
94 $cache =& $wgCaches[CACHE_NONE];
95 }
96
97 return $cache;
98 }
99
100 function &wfGetMainCache() {
101 global $wgMainCacheType;
102 return wfGetCache( $wgMainCacheType );
103 }
104
105 function &wfGetMessageCacheStorage() {
106 global $wgMessageCacheType;
107 return wfGetCache( $wgMessageCacheType );
108 }
109
110 function wfGetParserCacheStorage() {
111 global $wgParserCacheType;
112 return wfGetCache( $wgParserCacheType );
113 }
114
115 ?>