typo
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /** */
9 require_once( 'Revision.php' );
10
11 /**
12 *
13 */
14 define( 'MSG_LOAD_TIMEOUT', 60);
15 define( 'MSG_LOCK_TIMEOUT', 10);
16 define( 'MSG_WAIT_TIMEOUT', 10);
17
18 /**
19 * Message cache
20 * Performs various useful MediaWiki namespace-related functions
21 *
22 * @package MediaWiki
23 */
24 class MessageCache
25 {
26 var $mCache, $mUseCache, $mDisable, $mExpiry;
27 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
28 var $mExtensionMessages = array();
29 var $mInitialised = false;
30 var $mDeferred = true;
31
32 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
33 $fname = 'MessageCache::initialise';
34 wfProfileIn( $fname );
35
36 $this->mUseCache = !is_null( $memCached );
37 $this->mMemc = &$memCached;
38 $this->mDisable = !$useDB;
39 $this->mExpiry = $expiry;
40 $this->mDisableTransform = false;
41 $this->mMemcKey = $memcPrefix.':messages';
42 $this->mKeys = false; # initialised on demand
43 $this->mInitialised = true;
44
45 wfProfileIn( $fname.'-parseropt' );
46 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
47 wfProfileOut( $fname.'-parseropt' );
48 wfProfileIn( $fname.'-parser' );
49 $this->mParser = new Parser;
50 wfProfileOut( $fname.'-parser' );
51
52 # When we first get asked for a message,
53 # then we'll fill up the cache. If we
54 # can return a cache hit, this saves
55 # some extra milliseconds
56 $this->mDeferred = true;
57
58 wfProfileOut( $fname );
59 }
60
61 /**
62 * Loads messages either from memcached or the database, if not disabled
63 * On error, quietly switches to a fallback mode
64 * Returns false for a reportable error, true otherwise
65 */
66 function load() {
67 global $wgAllMessagesEn;
68
69 if ( $this->mDisable ) {
70 static $shownDisabled = false;
71 if ( !$shownDisabled ) {
72 wfDebug( "MessageCache::load(): disabled\n" );
73 $shownDisabled = true;
74 }
75 return true;
76 }
77 $fname = 'MessageCache::load';
78 wfProfileIn( $fname );
79 $success = true;
80
81 if ( $this->mUseCache ) {
82 wfProfileIn( $fname.'-fromcache' );
83 $this->mCache = $this->mMemc->get( $this->mMemcKey );
84 wfProfileOut( $fname.'-fromcache' );
85
86 # If there's nothing in memcached, load all the messages from the database
87 if ( !$this->mCache ) {
88 wfDebug( "MessageCache::load(): loading all messages\n" );
89 $this->lock();
90 # Other threads don't need to load the messages if another thread is doing it.
91 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
92 if ( $success ) {
93 wfProfileIn( $fname.'-load' );
94 $this->loadFromDB();
95 wfProfileOut( $fname.'-load' );
96 # Save in memcached
97 # Keep trying if it fails, this is kind of important
98 wfProfileIn( $fname.'-save' );
99 for ($i=0; $i<20 &&
100 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
101 $i++ ) {
102 usleep(mt_rand(500000,1500000));
103 }
104 wfProfileOut( $fname.'-save' );
105 if ( $i == 20 ) {
106 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
107 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
108 }
109 }
110 $this->unlock();
111 }
112
113 if ( !is_array( $this->mCache ) ) {
114 wfDebug( "MessageCache::load(): individual message mode\n" );
115 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
116 # Causing too much DB load, disabling -- TS
117 $this->mDisable = true;
118 /*
119 if ( $this->mCache == "loading" ) {
120 $this->mUseCache = false;
121 } elseif ( $this->mCache == "error" ) {
122 $this->mUseCache = false;
123 $success = false;
124 } else {
125 $this->mDisable = true;
126 $success = false;
127 }*/
128 $this->mCache = false;
129 }
130 }
131 wfProfileOut( $fname );
132 $this->mDeferred = false;
133 return $success;
134 }
135
136 /**
137 * Loads all or main part of cacheable messages from the database
138 */
139 function loadFromDB() {
140 global $wgAllMessagesEn, $wgLang;
141
142 $fname = 'MessageCache::loadFromDB';
143 $dbr =& wfGetDB( DB_SLAVE );
144 if ( !$dbr ) {
145 wfDebugDieBacktrace( 'Invalid database object' );
146 }
147 $conditions = array( 'page_is_redirect' => 0,
148 'page_namespace' => NS_MEDIAWIKI);
149 $res = $dbr->select( array( 'page', 'revision', 'text' ),
150 array( 'page_title', 'old_text', 'old_flags' ),
151 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
152 $fname
153 );
154
155 $this->mCache = array();
156 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
157 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
158 }
159
160 # Negative caching
161 # Go through the language array and the extension array and make a note of
162 # any keys missing from the cache
163 foreach ( $wgAllMessagesEn as $key => $value ) {
164 $uckey = $wgLang->ucfirst( $key );
165 if ( !array_key_exists( $uckey, $this->mCache ) ) {
166 $this->mCache[$uckey] = false;
167 }
168 }
169 foreach ( $this->mExtensionMessages as $key => $value ) {
170 $uckey = $wgLang->ucfirst( $key );
171 if ( !array_key_exists( $uckey, $this->mCache ) ) {
172 $this->mCache[$uckey] = false;
173 }
174 }
175
176 $dbr->freeResult( $res );
177 }
178
179 /**
180 * Not really needed anymore
181 */
182 function getKeys() {
183 global $wgAllMessagesEn, $wgContLang;
184 if ( !$this->mKeys ) {
185 $this->mKeys = array();
186 foreach ( $wgAllMessagesEn as $key => $value ) {
187 $title = $wgContLang->ucfirst( $key );
188 array_push( $this->mKeys, $title );
189 }
190 }
191 return $this->mKeys;
192 }
193
194 /**
195 * @deprecated
196 */
197 function isCacheable( $key ) {
198 return true;
199 }
200
201 function replace( $title, $text ) {
202 $this->lock();
203 $this->load();
204 if ( is_array( $this->mCache ) ) {
205 $this->mCache[$title] = $text;
206 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
207 }
208 $this->unlock();
209 }
210
211 /**
212 * Returns success
213 * Represents a write lock on the messages key
214 */
215 function lock() {
216 if ( !$this->mUseCache ) {
217 return true;
218 }
219
220 $lockKey = $this->mMemcKey . 'lock';
221 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
222 sleep(1);
223 }
224
225 return $i >= MSG_WAIT_TIMEOUT;
226 }
227
228 function unlock() {
229 if ( !$this->mUseCache ) {
230 return;
231 }
232
233 $lockKey = $this->mMemcKey . 'lock';
234 $this->mMemc->delete( $lockKey );
235 }
236
237 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
238 global $wgContLanguageCode;
239 if( $forcontent ) {
240 global $wgContLang;
241 $lang =& $wgContLang;
242 $langcode = $wgContLanguageCode;
243 } else {
244 global $wgLang, $wgLanguageCode;
245 $lang =& $wgLang;
246 $langcode = $wgLanguageCode;
247 }
248 # If uninitialised, someone is trying to call this halfway through Setup.php
249 if( !$this->mInitialised ) {
250 return '&lt;' . htmlspecialchars($key) . '&gt;';
251 }
252 # If cache initialization was deferred, start it now.
253 if( $this->mDeferred && !$this->mDisable && $useDB ) {
254 $this->load();
255 }
256
257 $message = false;
258 if( !$this->mDisable && $useDB ) {
259 $title = $lang->ucfirst( $key );
260 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
261 $title .= '/' . $langcode;
262 }
263 $message = $this->getFromCache( $title );
264 }
265 # Try the extension array
266 if( $message === false && array_key_exists( $key, $this->mExtensionMessages ) ) {
267 $message = $this->mExtensionMessages[$key];
268 }
269
270 # Try the array in the language object
271 if( $message === false ) {
272 wfSuppressWarnings();
273 $message = $lang->getMessage( $key );
274 wfRestoreWarnings();
275 }
276
277 # Try the English array
278 if( $message === false && $langcode != 'en' ) {
279 wfSuppressWarnings();
280 $message = Language::getMessage( $key );
281 wfRestoreWarnings();
282 }
283
284 # Is this a custom message? Try the default language in the db...
285 if( $message === false &&
286 !$this->mDisable && $useDB &&
287 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
288 $message = $this->getFromCache( $lang->ucfirst( $key ) );
289 }
290
291 # Final fallback
292 if( $message === false ) {
293 return '&lt;' . htmlspecialchars($key) . '&gt;';
294 }
295
296 # Replace brace tags
297 $message = $this->transform( $message );
298 return $message;
299 }
300
301 function getFromCache( $title ) {
302 $message = false;
303
304 # Try the cache
305 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
306 return $this->mCache[$title];
307 }
308
309 # Try individual message cache
310 if ( $this->mUseCache ) {
311 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
312 if ( $message == '###NONEXISTENT###' ) {
313 return false;
314 } elseif( !is_null( $message ) ) {
315 $this->mCache[$title] = $message;
316 return $message;
317 } else {
318 $message = false;
319 }
320 }
321
322 # If it wasn't in the cache, load each message from the DB individually
323 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
324 if( $revision ) {
325 $message = $revision->getText();
326 if ($this->mUseCache) {
327 $this->mCache[$title]=$message;
328 /* individual messages may be often
329 recached until proper purge code exists
330 */
331 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
332 }
333 } else {
334 # Negative caching
335 # Use some special text instead of false, because false gets converted to '' somewhere
336 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
337 }
338
339 return $message;
340 }
341
342 function transform( $message ) {
343 if( !$this->mDisableTransform ) {
344 if( strpos( $message, '{{' ) !== false ) {
345 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
346 }
347 }
348 return $message;
349 }
350
351 function disable() { $this->mDisable = true; }
352 function enable() { $this->mDisable = false; }
353 function disableTransform() { $this->mDisableTransform = true; }
354 function enableTransform() { $this->mDisableTransform = false; }
355
356 /**
357 * Add a message to the cache
358 *
359 * @param mixed $key
360 * @param mixed $value
361 */
362 function addMessage( $key, $value ) {
363 $this->mExtensionMessages[$key] = $value;
364 }
365
366 /**
367 * Add an associative array of message to the cache
368 *
369 * @param array $messages An associative array of key => values to be added
370 */
371 function addMessages( $messages ) {
372 foreach ( $messages as $key => $value ) {
373 $this->addMessage( $key, $value );
374 }
375 }
376
377 /**
378 * Clear all stored messages. Mainly used after a mass rebuild.
379 */
380 function clear() {
381 if( $this->mUseCache ) {
382 $this->mMemc->delete( $this->mMemcKey );
383 }
384 }
385 }
386 ?>