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