Add an objectcache table for limited caching when memcached isn't
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2
3 # Message cache
4 # Performs various useful MediaWiki namespace-related functions
5
6 define( "MSG_LOAD_TIMEOUT", 60);
7 define( "MSG_LOCK_TIMEOUT", 10);
8 define( "MSG_WAIT_TIMEOUT", 10);
9
10 class MessageCache
11 {
12 var $mCache, $mUseCache, $mDisable, $mExpiry;
13 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
14
15 var $mInitialised = false;
16
17 function initialise( &$memCached, $useDB, $expiry, $memcPrefix ) {
18 $this->mUseCache = !is_null( $memCached );
19 $this->mMemc = &$memCached;
20 $this->mDisable = !$useDB;
21 $this->mExpiry = $expiry;
22 $this->mDisableTransform = false;
23 $this->mMemcKey = "$memcPrefix:messages";
24 $this->mKeys = false; # initialised on demand
25 $this->mInitialised = true;
26 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
27 $this->mParser = new Parser;
28
29 $this->load();
30 }
31
32 # Loads messages either from memcached or the database, if not disabled
33 # On error, quietly switches to a fallback mode
34 # Returns false for a reportable error, true otherwise
35 function load() {
36 global $wgAllMessagesEn;
37
38 if ( $this->mDisable ) {
39 return true;
40 }
41
42 $success = true;
43
44 if ( $this->mUseCache ) {
45 $this->mCache = $this->mMemc->get( $this->mMemcKey );
46
47 # If there's nothing in memcached, load all the messages from the database
48 if ( !$this->mCache ) {
49 $this->lock();
50 # Other threads don't need to load the messages if another thread is doing it.
51 $this->mMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
52 $this->loadFromDB();
53 # Save in memcached
54 if ( !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ) ) {
55 # Hack for slabs reassignment problem
56 $this->mMemc->set( $this->mMemcKey, "error" );
57 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
58 }
59 $this->unlock();
60 }
61
62 if ( !is_array( $this->mCache ) ) {
63 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
64 if ( $this->mCache == "loading" ) {
65 $this->mUseCache = false;
66 } elseif ( $this->mCache == "error" ) {
67 $this->mUseCache = false;
68 $success = false;
69 } else {
70 $this->mDisable = true;
71 $success = false;
72 }
73 $this->mCache = false;
74 }
75 }
76 return $success;
77 }
78
79 # Loads all cacheable messages from the database
80 function loadFromDB()
81 {
82 $fname = "MessageCache::loadFromDB";
83 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
84 $res = wfQuery( $sql, DB_READ, $fname );
85
86 $this->mCache = array();
87 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
88 $this->mCache[$row->cur_title] = $row->cur_text;
89 }
90
91 wfFreeResult( $res );
92 }
93
94 # Not really needed anymore
95 function getKeys() {
96 global $wgAllMessagesEn, $wgLang;
97 if ( !$this->mKeys ) {
98 $this->mKeys = array();
99 foreach ( $wgAllMessagesEn as $key => $value ) {
100 array_push( $this->mKeys, $wgLang->ucfirst( $key ) );
101 }
102 }
103 return $this->mKeys;
104 }
105
106 # Obsolete
107 function isCacheable( $key ) {
108 return true;
109 /*
110 global $wgAllMessagesEn, $wgLang;
111 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
112 array_key_exists( $key, $wgAllMessagesEn );
113 */
114 }
115
116 function replace( $title, $text ) {
117 $this->lock();
118 $this->load();
119 if ( is_array( $this->mCache ) ) {
120 $this->mCache[$title] = $text;
121 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
122 }
123 $this->unlock();
124 }
125
126 # Returns success
127 # Represents a write lock on the messages key
128 function lock() {
129 if ( !$this->mUseCache ) {
130 return true;
131 }
132
133 $lockKey = $this->mMemcKey . "lock";
134 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
135 sleep(1);
136 }
137
138 return $i >= MSG_WAIT_TIMEOUT;
139 }
140
141 function unlock() {
142 if ( !$this->mUseCache ) {
143 return;
144 }
145
146 $lockKey = $this->mMemcKey . "lock";
147 $this->mMemc->delete( $lockKey );
148 }
149
150 function get( $key, $useDB ) {
151 global $wgLang, $wgLanguageCode;
152
153 # If uninitialised, someone is trying to call this halfway through Setup.php
154 if ( !$this->mInitialised ) {
155 return "&lt;$key&gt;";
156 }
157
158 if ( $this->mDisable ) {
159 return $this->transform( $wgLang->getMessage( $key ) );
160 }
161 $title = $wgLang->ucfirst( $key );
162
163 $message = false;
164
165 # Try the cache
166 if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
167 $message = $this->mCache[$title];
168 }
169
170 # If it wasn't in the cache, load each message from the DB individually
171 if ( !$message && $useDB) {
172 $result = wfGetArray( "cur", array("cur_text"),
173 array( "cur_namespace" => NS_MEDIAWIKI, "cur_title" => $title ),
174 "MessageCache::get" );
175 if ( $result ) {
176 $message = $result->cur_text;
177 }
178 }
179
180 # Try the array in $wgLang
181 if ( !$message ) {
182 $message = $wgLang->getMessage( $key );
183 }
184
185 # Try the English array
186 if ( !$message && $wgLanguageCode != "en" ) {
187 $message = Language::getMessage( $key );
188 }
189
190 # Final fallback
191 if ( !$message ) {
192 $message = "&lt;$key&gt;";
193 }
194
195 # Replace brace tags
196 $message = $this->transform( $message );
197 return $message;
198 }
199
200 function transform( $message ) {
201 if( !$this->mDisableTransform ) {
202 if ( strstr( $message, "{{" ) !== false ) {
203 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
204 }
205 }
206 return $message;
207 }
208
209 function disable() { $this->mDisable = true; }
210 function enable() { $this->mDisable = false; }
211 function disableTransform() { $this->mDisableTransform = true; }
212
213 }
214 ?>