Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @addtogroup Cache
5 */
6
7 /**
8 *
9 */
10 define( 'MSG_LOAD_TIMEOUT', 60);
11 define( 'MSG_LOCK_TIMEOUT', 10);
12 define( 'MSG_WAIT_TIMEOUT', 10);
13 define( 'MSG_CACHE_VERSION', 1 );
14
15 /**
16 * Message cache
17 * Performs various MediaWiki namespace-related functions
18 *
19 */
20 class MessageCache {
21 var $mCache, $mUseCache, $mDisable, $mExpiry;
22 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
23 var $mExtensionMessages = array();
24 var $mInitialised = false;
25 var $mDeferred = true;
26
27 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
28 wfProfileIn( __METHOD__ );
29
30 $this->mUseCache = !is_null( $memCached );
31 $this->mMemc = &$memCached;
32 $this->mDisable = !$useDB;
33 $this->mExpiry = $expiry;
34 $this->mDisableTransform = false;
35 $this->mMemcKey = $memcPrefix.':messages';
36 $this->mKeys = false; # initialised on demand
37 $this->mInitialised = true;
38 $this->mParser = null;
39
40 # When we first get asked for a message,
41 # then we'll fill up the cache. If we
42 # can return a cache hit, this saves
43 # some extra milliseconds
44 $this->mDeferred = true;
45
46 wfProfileOut( __METHOD__ );
47 }
48
49 function getParserOptions() {
50 if ( !$this->mParserOptions ) {
51 $this->mParserOptions = new ParserOptions;
52 }
53 return $this->mParserOptions;
54 }
55
56 /**
57 * Try to load the cache from a local file
58 */
59 function loadFromLocal( $hash ) {
60 global $wgLocalMessageCache;
61
62 $this->mCache = false;
63 if ( $wgLocalMessageCache === false ) {
64 return;
65 }
66
67 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
68
69 wfSuppressWarnings();
70 $file = fopen( $filename, 'r' );
71 wfRestoreWarnings();
72 if ( !$file ) {
73 return;
74 }
75
76 // Check to see if the file has the hash specified
77 $localHash = fread( $file, 32 );
78 if ( $hash == $localHash ) {
79 // All good, get the rest of it
80 $serialized = fread( $file, 10000000 );
81 $this->setCache( unserialize( $serialized ) );
82 }
83 fclose( $file );
84 }
85
86 /**
87 * Save the cache to a local file
88 */
89 function saveToLocal( $serialized, $hash ) {
90 global $wgLocalMessageCache;
91
92 if ( $wgLocalMessageCache === false ) {
93 return;
94 }
95
96 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
97 $oldUmask = umask( 0 );
98 wfMkdirParents( $wgLocalMessageCache, 0777 );
99 umask( $oldUmask );
100
101 $file = fopen( $filename, 'w' );
102 if ( !$file ) {
103 wfDebug( "Unable to open local cache file for writing\n" );
104 return;
105 }
106
107 fwrite( $file, $hash . $serialized );
108 fclose( $file );
109 @chmod( $filename, 0666 );
110 }
111
112 function loadFromScript( $hash ) {
113 global $wgLocalMessageCache;
114 if ( $wgLocalMessageCache === false ) {
115 return;
116 }
117
118 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
119
120 wfSuppressWarnings();
121 $file = fopen( $filename, 'r' );
122 wfRestoreWarnings();
123 if ( !$file ) {
124 return;
125 }
126 $localHash=substr(fread($file,40),8);
127 fclose($file);
128 if ($hash!=$localHash) {
129 return;
130 }
131 require("$wgLocalMessageCache/messages-" . wfWikiID());
132 $this->setCache( $this->mCache);
133 }
134
135 function saveToScript($array, $hash) {
136 global $wgLocalMessageCache;
137 if ( $wgLocalMessageCache === false ) {
138 return;
139 }
140
141 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
142 $oldUmask = umask( 0 );
143 wfMkdirParents( $wgLocalMessageCache, 0777 );
144 umask( $oldUmask );
145 $file = fopen( $filename.'.tmp', 'w');
146 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
147
148 foreach ($array as $key => $message) {
149 fwrite($file, "'". $this->escapeForScript($key).
150 "' => '" . $this->escapeForScript($message).
151 "',\n");
152 }
153 fwrite($file,");\n?>");
154 fclose($file);
155 rename($filename.'.tmp',$filename);
156 }
157
158 function escapeForScript($string) {
159 $string = str_replace( '\\', '\\\\', $string );
160 $string = str_replace( '\'', '\\\'', $string );
161 return $string;
162 }
163
164 /**
165 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
166 */
167 function setCache( $cache ) {
168 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
169 $this->mCache = $cache;
170 } else {
171 $this->mCache = false;
172 }
173 }
174
175 /**
176 * Loads messages either from memcached or the database, if not disabled
177 * On error, quietly switches to a fallback mode
178 * Returns false for a reportable error, true otherwise
179 */
180 function load() {
181 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
182
183 if ( $this->mDisable ) {
184 static $shownDisabled = false;
185 if ( !$shownDisabled ) {
186 wfDebug( "MessageCache::load(): disabled\n" );
187 $shownDisabled = true;
188 }
189 return true;
190 }
191 if ( !$this->mUseCache ) {
192 $this->mDeferred = false;
193 return true;
194 }
195
196 $fname = 'MessageCache::load';
197 wfProfileIn( $fname );
198 $success = true;
199
200 $this->mCache = false;
201
202 # Try local cache
203 wfProfileIn( $fname.'-fromlocal' );
204 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
205 if ( $hash ) {
206 if ($wgLocalMessageCacheSerialized) {
207 $this->loadFromLocal( $hash );
208 } else {
209 $this->loadFromScript( $hash );
210 }
211 if ( $this->mCache ) {
212 wfDebug( "MessageCache::load(): got from local cache\n" );
213 }
214 }
215 wfProfileOut( $fname.'-fromlocal' );
216
217 # Try memcached
218 if ( !$this->mCache ) {
219 wfProfileIn( $fname.'-fromcache' );
220 $this->setCache( $this->mMemc->get( $this->mMemcKey ) );
221 if ( $this->mCache ) {
222 wfDebug( "MessageCache::load(): got from global cache\n" );
223 # Save to local cache
224 if ( $wgLocalMessageCache !== false ) {
225 $serialized = serialize( $this->mCache );
226 if ( !$hash ) {
227 $hash = md5( $serialized );
228 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
229 }
230 if ($wgLocalMessageCacheSerialized) {
231 $this->saveToLocal( $serialized,$hash );
232 } else {
233 $this->saveToScript( $this->mCache, $hash );
234 }
235 }
236 }
237 wfProfileOut( $fname.'-fromcache' );
238 }
239
240
241 # If there's nothing in memcached, load all the messages from the database
242 if ( !$this->mCache ) {
243 wfDebug( "MessageCache::load(): cache is empty\n" );
244 $this->lock();
245 # Other threads don't need to load the messages if another thread is doing it.
246 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
247 if ( $success ) {
248 wfProfileIn( $fname.'-load' );
249 wfDebug( "MessageCache::load(): loading all messages from DB\n" );
250 $this->loadFromDB();
251 wfProfileOut( $fname.'-load' );
252
253 # Save in memcached
254 # Keep trying if it fails, this is kind of important
255 wfProfileIn( $fname.'-save' );
256 for ($i=0; $i<20 &&
257 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
258 $i++ ) {
259 usleep(mt_rand(500000,1500000));
260 }
261
262 # Save to local cache
263 if ( $wgLocalMessageCache !== false ) {
264 $serialized = serialize( $this->mCache );
265 $hash = md5( $serialized );
266 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
267 if ($wgLocalMessageCacheSerialized) {
268 $this->saveToLocal( $serialized,$hash );
269 } else {
270 $this->saveToScript( $this->mCache, $hash );
271 }
272 }
273
274 wfProfileOut( $fname.'-save' );
275 if ( $i == 20 ) {
276 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
277 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
278 } else {
279 $this->mMemc->delete( $this->mMemcKey.'-status' );
280 }
281 }
282 $this->unlock();
283 }
284
285 if ( !is_array( $this->mCache ) ) {
286 wfDebug( "MessageCache::load(): unable to load cache, disabled\n" );
287 $this->mDisable = true;
288 $this->mCache = false;
289 }
290 wfProfileOut( $fname );
291 $this->mDeferred = false;
292 return $success;
293 }
294
295 /**
296 * Loads all or main part of cacheable messages from the database
297 */
298 function loadFromDB() {
299 global $wgMaxMsgCacheEntrySize;
300
301 wfProfileIn( __METHOD__ );
302 $dbr =& wfGetDB( DB_SLAVE );
303 $this->mCache = array();
304
305 # Load titles for all oversized pages in the MediaWiki namespace
306 $res = $dbr->select( 'page', 'page_title',
307 array(
308 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ),
309 'page_is_redirect' => 0,
310 'page_namespace' => NS_MEDIAWIKI,
311 ),
312 __METHOD__ );
313 while ( $row = $dbr->fetchObject( $res ) ) {
314 $this->mCache[$row->page_title] = '!TOO BIG';
315 }
316 $dbr->freeResult( $res );
317
318 # Load text for the remaining pages
319 $res = $dbr->select( array( 'page', 'revision', 'text' ),
320 array( 'page_title', 'old_text', 'old_flags' ),
321 array(
322 'page_is_redirect' => 0,
323 'page_namespace' => NS_MEDIAWIKI,
324 'page_latest=rev_id',
325 'rev_text_id=old_id',
326 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ),
327 __METHOD__ );
328
329 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
330 $this->mCache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
331 }
332 $this->mCache['VERSION'] = MSG_CACHE_VERSION;
333 $dbr->freeResult( $res );
334 wfProfileOut( __METHOD__ );
335 }
336
337 /**
338 * Not really needed anymore
339 */
340 function getKeys() {
341 global $wgContLang;
342 if ( !$this->mKeys ) {
343 $this->mKeys = array();
344 $allMessages = Language::getMessagesFor( 'en' );
345 foreach ( $allMessages as $key => $unused ) {
346 $title = $wgContLang->ucfirst( $key );
347 array_push( $this->mKeys, $title );
348 }
349 }
350 return $this->mKeys;
351 }
352
353 function replace( $title, $text ) {
354 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
355 global $wgMaxMsgCacheEntrySize;
356
357 wfProfileIn( __METHOD__ );
358 $this->lock();
359 $this->load();
360 $parserMemc->delete(wfMemcKey('sidebar'));
361 if ( is_array( $this->mCache ) ) {
362 if ( $text === false ) {
363 # Article was deleted
364 unset( $this->mCache[$title] );
365 $this->mMemc->delete( "$this->mMemcKey:{$title}" );
366 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
367 $this->mCache[$title] = '!TOO BIG';
368 $this->mMemc->set( "$this->mMemcKey:{$title}", ' '.$text, $this->mExpiry );
369 } else {
370 $this->mCache[$title] = ' ' . $text;
371 $this->mMemc->delete( "$this->mMemcKey:{$title}" );
372 }
373 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
374
375 # Save to local cache
376 if ( $wgLocalMessageCache !== false ) {
377 $serialized = serialize( $this->mCache );
378 $hash = md5( $serialized );
379 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
380 if ($wgLocalMessageCacheSerialized) {
381 $this->saveToLocal( $serialized,$hash );
382 } else {
383 $this->saveToScript( $this->mCache, $hash );
384 }
385 }
386 }
387 $this->unlock();
388 wfProfileOut( __METHOD__ );
389 }
390
391 /**
392 * Returns success
393 * Represents a write lock on the messages key
394 */
395 function lock() {
396 if ( !$this->mUseCache ) {
397 return true;
398 }
399
400 $lockKey = $this->mMemcKey . 'lock';
401 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
402 sleep(1);
403 }
404
405 return $i >= MSG_WAIT_TIMEOUT;
406 }
407
408 function unlock() {
409 if ( !$this->mUseCache ) {
410 return;
411 }
412
413 $lockKey = $this->mMemcKey . 'lock';
414 $this->mMemc->delete( $lockKey );
415 }
416
417 /**
418 * Get a message from either the content language or the user language.
419 *
420 * @param string $key The message cache key
421 * @param bool $useDB Get the message from the DB, false to use only the localisation
422 * @param bool $forContent Get the message from the content language rather than the
423 * user language
424 * @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
425 */
426 function get( $key, $useDB = true, $forContent = true, $isFullKey = false ) {
427 global $wgContLanguageCode, $wgContLang, $wgLang;
428 if( $forContent ) {
429 $lang =& $wgContLang;
430 } else {
431 $lang =& $wgLang;
432 }
433 $langcode = $lang->getCode();
434 # If uninitialised, someone is trying to call this halfway through Setup.php
435 if( !$this->mInitialised ) {
436 return '&lt;' . htmlspecialchars($key) . '&gt;';
437 }
438 # If cache initialization was deferred, start it now.
439 if( $this->mDeferred && !$this->mDisable && $useDB ) {
440 $this->load();
441 }
442
443 $message = false;
444
445 # Normalise title-case input
446 $lckey = $wgContLang->lcfirst( $key );
447 $lckey = str_replace( ' ', '_', $lckey );
448
449 # Try the MediaWiki namespace
450 if( !$this->mDisable && $useDB ) {
451 $title = $wgContLang->ucfirst( $lckey );
452 if(!$isFullKey && ($langcode != $wgContLanguageCode) ) {
453 $title .= '/' . $langcode;
454 }
455 $message = $this->getMsgFromNamespace( $title );
456 }
457 # Try the extension array
458 if( $message === false && isset( $this->mExtensionMessages[$langcode][$lckey] ) ) {
459 $message = $this->mExtensionMessages[$langcode][$lckey];
460 }
461 if ( $message === false && isset( $this->mExtensionMessages['en'][$lckey] ) ) {
462 $message = $this->mExtensionMessages['en'][$lckey];
463 }
464
465 # Try the array in the language object
466 if( $message === false ) {
467 #wfDebug( "Trying language object for message $key\n" );
468 wfSuppressWarnings();
469 $message = $lang->getMessage( $lckey );
470 wfRestoreWarnings();
471 if ( is_null( $message ) ) {
472 $message = false;
473 }
474 }
475
476 # Try the array of another language
477 if( $message === false && strpos( $lckey, '/' ) ) {
478 $message = explode( '/', $lckey );
479 if ( $message[1] ) {
480 wfSuppressWarnings();
481 $message = Language::getMessageFor( $message[0], $message[1] );
482 wfRestoreWarnings();
483 if ( is_null( $message ) ) {
484 $message = false;
485 }
486 } else {
487 $message = false;
488 }
489 }
490
491 # Is this a custom message? Try the default language in the db...
492 if( ($message === false || $message === '-' ) &&
493 !$this->mDisable && $useDB &&
494 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
495 $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $lckey ) );
496 }
497
498 # Final fallback
499 if( $message === false ) {
500 return '&lt;' . htmlspecialchars($key) . '&gt;';
501 }
502
503 # Replace brace tags
504 $message = $this->transform( $message );
505 return $message;
506 }
507
508 /**
509 * Get a message from the MediaWiki namespace, with caching. The key must
510 * first be converted to two-part lang/msg form if necessary.
511 *
512 * @param string $title Message cache key with initial uppercase letter
513 */
514 function getMsgFromNamespace( $title ) {
515 $message = false;
516 $type = false;
517
518 # Try the cache
519 if( $this->mUseCache && isset( $this->mCache[$title] ) ) {
520 $entry = $this->mCache[$title];
521 $type = substr( $entry, 0, 1 );
522 if ( $type == ' ' ) {
523 return substr( $entry, 1 );
524 }
525 }
526
527 # Call message hooks, in case they are defined
528 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
529 if ( $message !== false ) {
530 return $message;
531 }
532
533 # If there is no cache entry and no placeholder, it doesn't exist
534 if ( $type != '!' && $message === false ) {
535 return false;
536 }
537
538 $memcKey = $this->mMemcKey . ':' . $title;
539
540 # Try the individual message cache
541 if ( $this->mUseCache ) {
542 $entry = $this->mMemc->get( $memcKey );
543 if ( $entry ) {
544 $type = substr( $entry, 0, 1 );
545
546 if ( $type == ' ' ) {
547 $message = substr( $entry, 1 );
548 $this->mCache[$title] = $message;
549 return $message;
550 } elseif ( $entry == '!NONEXISTENT' ) {
551 return false;
552 } else {
553 # Corrupt/obsolete entry, delete it
554 $this->mMemc->delete( $memcKey );
555 }
556
557 }
558 }
559
560 # Try loading it from the DB
561 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
562 if( $revision ) {
563 $message = $revision->getText();
564 if ($this->mUseCache) {
565 $this->mCache[$title] = ' ' . $message;
566 $this->mMemc->set( $memcKey, $message, $this->mExpiry );
567 }
568 } else {
569 # Negative caching
570 # Use some special text instead of false, because false gets converted to '' somewhere
571 $this->mMemc->set( $memcKey, '!NONEXISTENT', $this->mExpiry );
572 $this->mCache[$title] = false;
573 }
574
575 return $message;
576 }
577
578 function transform( $message ) {
579 global $wgParser;
580 if ( !$this->mParser && isset( $wgParser ) ) {
581 # Do some initialisation so that we don't have to do it twice
582 $wgParser->firstCallInit();
583 # Clone it and store it
584 $this->mParser = clone $wgParser;
585 }
586 if ( !$this->mDisableTransform && $this->mParser ) {
587 if( strpos( $message, '{{' ) !== false ) {
588 $message = $this->mParser->transformMsg( $message, $this->getParserOptions() );
589 }
590 }
591 return $message;
592 }
593
594 function disable() { $this->mDisable = true; }
595 function enable() { $this->mDisable = false; }
596 function disableTransform() { $this->mDisableTransform = true; }
597 function enableTransform() { $this->mDisableTransform = false; }
598 function setTransform( $x ) { $this->mDisableTransform = $x; }
599 function getTransform() { return $this->mDisableTransform; }
600
601 /**
602 * Add a message to the cache
603 *
604 * @param mixed $key
605 * @param mixed $value
606 * @param string $lang The messages language, English by default
607 */
608 function addMessage( $key, $value, $lang = 'en' ) {
609 $this->mExtensionMessages[$lang][$key] = $value;
610 }
611
612 /**
613 * Add an associative array of message to the cache
614 *
615 * @param array $messages An associative array of key => values to be added
616 * @param string $lang The messages language, English by default
617 */
618 function addMessages( $messages, $lang = 'en' ) {
619 wfProfileIn( __METHOD__ );
620 if ( isset( $this->mExtensionMessages[$lang] ) ) {
621 $this->mExtensionMessages[$lang] = $messages + $this->mExtensionMessages[$lang];
622 } else {
623 $this->mExtensionMessages[$lang] = $messages;
624 }
625 wfProfileOut( __METHOD__ );
626 }
627
628 /**
629 * Add a 2-D array of messages by lang. Useful for extensions.
630 * Introduced in 1.9. Please do not use it for now, for backwards compatibility.
631 *
632 * @param array $messages The array to be added
633 */
634 function addMessagesByLang( $messages ) {
635 wfProfileIn( __METHOD__ );
636 foreach ( $messages as $key => $value ) {
637 $this->addMessages( $value, $key );
638 }
639 wfProfileOut( __METHOD__ );
640 }
641
642 /**
643 * Get the extension messages for a specific language
644 *
645 * @param string $lang The messages language, English by default
646 */
647 function getExtensionMessagesFor( $lang = 'en' ) {
648 wfProfileIn( __METHOD__ );
649 $messages = array();
650 if ( isset( $this->mExtensionMessages[$lang] ) ) {
651 $messages = $this->mExtensionMessages[$lang];
652 }
653 if ( $lang != 'en' ) {
654 $messages = $messages + $this->mExtensionMessages['en'];
655 }
656 wfProfileOut( __METHOD__ );
657 return $messages;
658 }
659
660 /**
661 * Clear all stored messages. Mainly used after a mass rebuild.
662 */
663 function clear() {
664 global $wgLocalMessageCache;
665 if( $this->mUseCache ) {
666 # Global cache
667 $this->mMemc->delete( $this->mMemcKey );
668 # Invalidate all local caches
669 $this->mMemc->delete( "{$this->mMemcKey}-hash" );
670 }
671 }
672
673 static function loadAllMessages() {
674 # Some extensions will load their messages when you load their class file
675 wfLoadAllExtensions();
676 # Others will respond to this hook
677 wfRunHooks( 'LoadAllMessages' );
678 # Still others will respond to neither, they are EVIL. We sometimes need to know!
679 }
680 }
681 ?>