MediaWiki REL1_34
MessageCache.php
Go to the documentation of this file.
1<?php
24use Wikimedia\ScopedCallback;
27
32define( 'MSG_CACHE_VERSION', 2 );
33
41 const FOR_UPDATE = 1; // force message reload
42
44 const WAIT_SEC = 15;
46 const LOCK_TTL = 30;
47
52 const WAN_TTL = IExpiringStore::TTL_DAY;
53
59 protected $cache;
60
66 protected $overridable;
67
71 protected $cacheVolatile = [];
72
77 protected $mDisable;
78
83 protected $mParserOptions;
85 protected $mParser;
86
90 protected $mInParser = false;
91
93 protected $wanCache;
95 protected $clusterCache;
97 protected $srvCache;
99 protected $contLang;
100
105 private $loadedLanguages = [];
106
114 public static function singleton() {
115 return MediaWikiServices::getInstance()->getMessageCache();
116 }
117
124 public static function normalizeKey( $key ) {
125 $lckey = strtr( $key, ' ', '_' );
126 if ( ord( $lckey ) < 128 ) {
127 $lckey[0] = strtolower( $lckey[0] );
128 } else {
129 $lckey = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $lckey );
130 }
131
132 return $lckey;
133 }
134
142 public function __construct(
143 WANObjectCache $wanCache,
144 BagOStuff $clusterCache,
145 BagOStuff $serverCache,
146 $useDB,
147 Language $contLang = null
148 ) {
149 $this->wanCache = $wanCache;
150 $this->clusterCache = $clusterCache;
151 $this->srvCache = $serverCache;
152
153 $this->cache = new MapCacheLRU( 5 ); // limit size for sanity
154
155 $this->mDisable = !$useDB;
156 $this->contLang = $contLang ?? MediaWikiServices::getInstance()->getContentLanguage();
157 }
158
164 function getParserOptions() {
165 global $wgUser;
166
167 if ( !$this->mParserOptions ) {
168 if ( !$wgUser || !$wgUser->isSafeToLoad() ) {
169 // $wgUser isn't available yet, so don't try to get a
170 // ParserOptions for it. And don't cache this ParserOptions
171 // either.
172 $po = ParserOptions::newFromAnon();
173 $po->setAllowUnsafeRawHtml( false );
174 $po->setTidy( true );
175 return $po;
176 }
177
178 $this->mParserOptions = new ParserOptions;
179 // Messages may take parameters that could come
180 // from malicious sources. As a precaution, disable
181 // the <html> parser tag when parsing messages.
182 $this->mParserOptions->setAllowUnsafeRawHtml( false );
183 // For the same reason, tidy the output!
184 $this->mParserOptions->setTidy( true );
185 }
186
187 return $this->mParserOptions;
188 }
189
196 protected function getLocalCache( $code ) {
197 $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
198
199 return $this->srvCache->get( $cacheKey );
200 }
201
208 protected function saveToLocalCache( $code, $cache ) {
209 $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
210 $this->srvCache->set( $cacheKey, $cache );
211 }
212
234 protected function load( $code, $mode = null ) {
235 if ( !is_string( $code ) ) {
236 throw new InvalidArgumentException( "Missing language code" );
237 }
238
239 # Don't do double loading...
240 if ( isset( $this->loadedLanguages[$code] ) && $mode != self::FOR_UPDATE ) {
241 return true;
242 }
243
244 $this->overridable = array_flip( Language::getMessageKeysFor( $code ) );
245
246 # 8 lines of code just to say (once) that message cache is disabled
247 if ( $this->mDisable ) {
248 static $shownDisabled = false;
249 if ( !$shownDisabled ) {
250 wfDebug( __METHOD__ . ": disabled\n" );
251 $shownDisabled = true;
252 }
253
254 return true;
255 }
256
257 # Loading code starts
258 $success = false; # Keep track of success
259 $staleCache = false; # a cache array with expired data, or false if none has been loaded
260 $where = []; # Debug info, delayed to avoid spamming debug log too much
261
262 # Hash of the contents is stored in memcache, to detect if data-center cache
263 # or local cache goes out of date (e.g. due to replace() on some other server)
264 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
265 $this->cacheVolatile[$code] = $hashVolatile;
266
267 # Try the local cache and check against the cluster hash key...
268 $cache = $this->getLocalCache( $code );
269 if ( !$cache ) {
270 $where[] = 'local cache is empty';
271 } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
272 $where[] = 'local cache has the wrong hash';
273 $staleCache = $cache;
274 } elseif ( $this->isCacheExpired( $cache ) ) {
275 $where[] = 'local cache is expired';
276 $staleCache = $cache;
277 } elseif ( $hashVolatile ) {
278 $where[] = 'local cache validation key is expired/volatile';
279 $staleCache = $cache;
280 } else {
281 $where[] = 'got from local cache';
282 $this->cache->set( $code, $cache );
283 $success = true;
284 }
285
286 if ( !$success ) {
287 $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
288 # Try the global cache. If it is empty, try to acquire a lock. If
289 # the lock can't be acquired, wait for the other thread to finish
290 # and then try the global cache a second time.
291 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
292 if ( $hashVolatile && $staleCache ) {
293 # Do not bother fetching the whole cache blob to avoid I/O.
294 # Instead, just try to get the non-blocking $statusKey lock
295 # below, and use the local stale value if it was not acquired.
296 $where[] = 'global cache is presumed expired';
297 } else {
298 $cache = $this->clusterCache->get( $cacheKey );
299 if ( !$cache ) {
300 $where[] = 'global cache is empty';
301 } elseif ( $this->isCacheExpired( $cache ) ) {
302 $where[] = 'global cache is expired';
303 $staleCache = $cache;
304 } elseif ( $hashVolatile ) {
305 # DB results are replica DB lag prone until the holdoff TTL passes.
306 # By then, updates should be reflected in loadFromDBWithLock().
307 # One thread regenerates the cache while others use old values.
308 $where[] = 'global cache is expired/volatile';
309 $staleCache = $cache;
310 } else {
311 $where[] = 'got from global cache';
312 $this->cache->set( $code, $cache );
313 $this->saveToCaches( $cache, 'local-only', $code );
314 $success = true;
315 }
316 }
317
318 if ( $success ) {
319 # Done, no need to retry
320 break;
321 }
322
323 # We need to call loadFromDB. Limit the concurrency to one process.
324 # This prevents the site from going down when the cache expires.
325 # Note that the DB slam protection lock here is non-blocking.
326 $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
327 if ( $loadStatus === true ) {
328 $success = true;
329 break;
330 } elseif ( $staleCache ) {
331 # Use the stale cache while some other thread constructs the new one
332 $where[] = 'using stale cache';
333 $this->cache->set( $code, $staleCache );
334 $success = true;
335 break;
336 } elseif ( $failedAttempts > 0 ) {
337 # Already blocked once, so avoid another lock/unlock cycle.
338 # This case will typically be hit if memcached is down, or if
339 # loadFromDB() takes longer than LOCK_WAIT.
340 $where[] = "could not acquire status key.";
341 break;
342 } elseif ( $loadStatus === 'cantacquire' ) {
343 # Wait for the other thread to finish, then retry. Normally,
344 # the memcached get() will then yield the other thread's result.
345 $where[] = 'waited for other thread to complete';
346 $this->getReentrantScopedLock( $cacheKey );
347 } else {
348 # Disable cache; $loadStatus is 'disabled'
349 break;
350 }
351 }
352 }
353
354 if ( !$success ) {
355 $where[] = 'loading FAILED - cache is disabled';
356 $this->mDisable = true;
357 $this->cache->set( $code, [] );
358 wfDebugLog( 'MessageCacheError', __METHOD__ . ": Failed to load $code\n" );
359 # This used to throw an exception, but that led to nasty side effects like
360 # the whole wiki being instantly down if the memcached server died
361 } else {
362 # All good, just record the success
363 $this->loadedLanguages[$code] = true;
364 }
365
366 if ( !$this->cache->has( $code ) ) { // sanity
367 throw new LogicException( "Process cache for '$code' should be set by now." );
368 }
369
370 $info = implode( ', ', $where );
371 wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
372
373 return $success;
374 }
375
382 protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
383 # If cache updates on all levels fail, give up on message overrides.
384 # This is to avoid easy site outages; see $saveSuccess comments below.
385 $statusKey = $this->clusterCache->makeKey( 'messages', $code, 'status' );
386 $status = $this->clusterCache->get( $statusKey );
387 if ( $status === 'error' ) {
388 $where[] = "could not load; method is still globally disabled";
389 return 'disabled';
390 }
391
392 # Now let's regenerate
393 $where[] = 'loading from database';
394
395 # Lock the cache to prevent conflicting writes.
396 # This lock is non-blocking so stale cache can quickly be used.
397 # Note that load() will call a blocking getReentrantScopedLock()
398 # after this if it really need to wait for any current thread.
399 $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
400 $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
401 if ( !$scopedLock ) {
402 $where[] = 'could not acquire main lock';
403 return 'cantacquire';
404 }
405
406 $cache = $this->loadFromDB( $code, $mode );
407 $this->cache->set( $code, $cache );
408 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
409
410 if ( !$saveSuccess ) {
424 if ( $this->srvCache instanceof EmptyBagOStuff ) {
425 $this->clusterCache->set( $statusKey, 'error', 60 * 5 );
426 $where[] = 'could not save cache, disabled globally for 5 minutes';
427 } else {
428 $where[] = "could not save global cache";
429 }
430 }
431
432 return true;
433 }
434
444 protected function loadFromDB( $code, $mode = null ) {
446
447 // (T164666) The query here performs really poorly on WMF's
448 // contributions replicas. We don't have a way to say "any group except
449 // contributions", so for the moment let's specify 'api'.
450 // @todo: Get rid of this hack.
451 $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_REPLICA, 'api' );
452
453 $cache = [];
454
455 $mostused = []; // list of "<cased message key>/<code>"
456 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
457 if ( !$this->cache->has( $wgLanguageCode ) ) {
458 $this->load( $wgLanguageCode );
459 }
460 $mostused = array_keys( $this->cache->get( $wgLanguageCode ) );
461 foreach ( $mostused as $key => $value ) {
462 $mostused[$key] = "$value/$code";
463 }
464 }
465
466 // Get the list of software-defined messages in core/extensions
467 $overridable = array_flip( Language::getMessageKeysFor( $wgLanguageCode ) );
468
469 // Common conditions
470 $conds = [
471 'page_is_redirect' => 0,
472 'page_namespace' => NS_MEDIAWIKI,
473 ];
474 if ( count( $mostused ) ) {
475 $conds['page_title'] = $mostused;
476 } elseif ( $code !== $wgLanguageCode ) {
477 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
478 } else {
479 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
480 # other than language code.
481 $conds[] = 'page_title NOT' .
482 $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
483 }
484
485 // Set the stubs for oversized software-defined messages in the main cache map
486 $res = $dbr->select(
487 'page',
488 [ 'page_title', 'page_latest' ],
489 array_merge( $conds, [ 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ) ] ),
490 __METHOD__ . "($code)-big"
491 );
492 foreach ( $res as $row ) {
493 // Include entries/stubs for all keys in $mostused in adaptive mode
494 if ( $wgAdaptiveMessageCache || $this->isMainCacheable( $row->page_title, $overridable ) ) {
495 $cache[$row->page_title] = '!TOO BIG';
496 }
497 // At least include revision ID so page changes are reflected in the hash
498 $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
499 }
500
501 // Set the text for small software-defined messages in the main cache map
502 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
503 $revQuery = $revisionStore->getQueryInfo( [ 'page', 'user' ] );
504
505 // T231196: MySQL/MariaDB (10.1.37) can sometimes irrationally decide that querying `actor` then
506 // `revision` then `page` is somehow better than starting with `page`. Tell it not to reorder the
507 // query (and also reorder it ourselves because as generated by RevisionStore it'll have
508 // `revision` first rather than `page`).
509 $revQuery['joins']['revision'] = $revQuery['joins']['page'];
510 unset( $revQuery['joins']['page'] );
511 // It isn't actually necesssary to reorder $revQuery['tables'] as Database does the right thing
512 // when join conditions are given for all joins, but Gergő is wary of relying on that so pull
513 // `page` to the start.
514 $revQuery['tables'] = array_merge(
515 [ 'page' ],
516 array_diff( $revQuery['tables'], [ 'page' ] )
517 );
518
519 $res = $dbr->select(
520 $revQuery['tables'],
521 $revQuery['fields'],
522 array_merge( $conds, [
523 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ),
524 'page_latest = rev_id' // get the latest revision only
525 ] ),
526 __METHOD__ . "($code)-small",
527 [ 'STRAIGHT_JOIN' ],
528 $revQuery['joins']
529 );
530 foreach ( $res as $row ) {
531 // Include entries/stubs for all keys in $mostused in adaptive mode
532 if ( $wgAdaptiveMessageCache || $this->isMainCacheable( $row->page_title, $overridable ) ) {
533 try {
534 $rev = $revisionStore->newRevisionFromRow( $row );
535 $content = $rev->getContent( MediaWiki\Revision\SlotRecord::MAIN );
536 $text = $this->getMessageTextFromContent( $content );
537 } catch ( Exception $ex ) {
538 $text = false;
539 }
540
541 if ( !is_string( $text ) ) {
542 $entry = '!ERROR';
544 'MessageCache',
545 __METHOD__
546 . ": failed to load message page text for {$row->page_title} ($code)"
547 );
548 } else {
549 $entry = ' ' . $text;
550 }
551 $cache[$row->page_title] = $entry;
552 } else {
553 // T193271: cache object gets too big and slow to generate.
554 // At least include revision ID so page changes are reflected in the hash.
555 $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
556 }
557 }
558
559 $cache['VERSION'] = MSG_CACHE_VERSION;
560 ksort( $cache );
561
562 # Hash for validating local cache (APC). No need to take into account
563 # messages larger than $wgMaxMsgCacheEntrySize, since those are only
564 # stored and fetched from memcache.
565 $cache['HASH'] = md5( serialize( $cache ) );
566 $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + self::WAN_TTL );
567 unset( $cache['EXCESSIVE'] ); // only needed for hash
568
569 return $cache;
570 }
571
577 private function isMainCacheable( $name, array $overridable ) {
578 // Convert first letter to lowercase, and strip /code suffix
579 $name = $this->contLang->lcfirst( $name );
580 $msg = preg_replace( '/\/[a-z0-9-]{2,}$/', '', $name );
581 // Include common conversion table pages. This also avoids problems with
582 // Installer::parse() bailing out due to disallowed DB queries (T207979).
583 return ( isset( $overridable[$msg] ) || strpos( $name, 'conversiontable/' ) === 0 );
584 }
585
592 public function replace( $title, $text ) {
593 global $wgLanguageCode;
594
595 if ( $this->mDisable ) {
596 return;
597 }
598
599 list( $msg, $code ) = $this->figureMessage( $title );
600 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
601 // Content language overrides do not use the /<code> suffix
602 return;
603 }
604
605 // (a) Update the process cache with the new message text
606 if ( $text === false ) {
607 // Page deleted
608 $this->cache->setField( $code, $title, '!NONEXISTENT' );
609 } else {
610 // Ignore $wgMaxMsgCacheEntrySize so the process cache is up to date
611 $this->cache->setField( $code, $title, ' ' . $text );
612 }
613
614 // (b) Update the shared caches in a deferred update with a fresh DB snapshot
615 DeferredUpdates::addUpdate(
616 new MessageCacheUpdate( $code, $title, $msg ),
617 DeferredUpdates::PRESEND
618 );
619 }
620
626 public function refreshAndReplaceInternal( $code, array $replacements ) {
628
629 // Allow one caller at a time to avoid race conditions
630 $scopedLock = $this->getReentrantScopedLock(
631 $this->clusterCache->makeKey( 'messages', $code )
632 );
633 if ( !$scopedLock ) {
634 foreach ( $replacements as list( $title ) ) {
635 LoggerFactory::getInstance( 'MessageCache' )->error(
636 __METHOD__ . ': could not acquire lock to update {title} ({code})',
637 [ 'title' => $title, 'code' => $code ] );
638 }
639
640 return;
641 }
642
643 // Load the existing cache to update it in the local DC cache.
644 // The other DCs will see a hash mismatch.
645 if ( $this->load( $code, self::FOR_UPDATE ) ) {
646 $cache = $this->cache->get( $code );
647 } else {
648 // Err? Fall back to loading from the database.
649 $cache = $this->loadFromDB( $code, self::FOR_UPDATE );
650 }
651 // Check if individual cache keys should exist and update cache accordingly
652 $newTextByTitle = []; // map of (title => content)
653 $newBigTitles = []; // map of (title => latest revision ID), like EXCESSIVE in loadFromDB()
654 foreach ( $replacements as list( $title ) ) {
655 $page = WikiPage::factory( Title::makeTitle( NS_MEDIAWIKI, $title ) );
656 $page->loadPageData( $page::READ_LATEST );
657 $text = $this->getMessageTextFromContent( $page->getContent() );
658 // Remember the text for the blob store update later on
659 $newTextByTitle[$title] = $text;
660 // Note that if $text is false, then $cache should have a !NONEXISTANT entry
661 if ( !is_string( $text ) ) {
662 $cache[$title] = '!NONEXISTENT';
663 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
664 $cache[$title] = '!TOO BIG';
665 $newBigTitles[$title] = $page->getLatest();
666 } else {
667 $cache[$title] = ' ' . $text;
668 }
669 }
670 // Update HASH for the new key. Incorporates various administrative keys,
671 // including the old HASH (and thereby the EXCESSIVE value from loadFromDB()
672 // and previous replace() calls), but that doesn't really matter since we
673 // only ever compare it for equality with a copy saved by saveToCaches().
674 $cache['HASH'] = md5( serialize( $cache + [ 'EXCESSIVE' => $newBigTitles ] ) );
675 // Update the too-big WAN cache entries now that we have the new HASH
676 foreach ( $newBigTitles as $title => $id ) {
677 // Match logic of loadCachedMessagePageEntry()
678 $this->wanCache->set(
679 $this->bigMessageCacheKey( $cache['HASH'], $title ),
680 ' ' . $newTextByTitle[$title],
681 self::WAN_TTL
682 );
683 }
684 // Mark this cache as definitely being "latest" (non-volatile) so
685 // load() calls do not try to refresh the cache with replica DB data
686 $cache['LATEST'] = time();
687 // Update the process cache
688 $this->cache->set( $code, $cache );
689 // Pre-emptively update the local datacenter cache so things like edit filter and
690 // blacklist changes are reflected immediately; these often use MediaWiki: pages.
691 // The datacenter handling replace() calls should be the same one handling edits
692 // as they require HTTP POST.
693 $this->saveToCaches( $cache, 'all', $code );
694 // Release the lock now that the cache is saved
695 ScopedCallback::consume( $scopedLock );
696
697 // Relay the purge. Touching this check key expires cache contents
698 // and local cache (APC) validation hash across all datacenters.
699 $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
700
701 // Purge the messages in the message blob store and fire any hook handlers
702 $blobStore = MediaWikiServices::getInstance()->getResourceLoader()->getMessageBlobStore();
703 foreach ( $replacements as list( $title, $msg ) ) {
704 $blobStore->updateMessage( $this->contLang->lcfirst( $msg ) );
705 Hooks::run( 'MessageCacheReplace', [ $title, $newTextByTitle[$title] ] );
706 }
707 }
708
715 protected function isCacheExpired( $cache ) {
716 if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
717 return true;
718 }
719 if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
720 return true;
721 }
722 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
723 return true;
724 }
725
726 return false;
727 }
728
738 protected function saveToCaches( array $cache, $dest, $code = false ) {
739 if ( $dest === 'all' ) {
740 $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
741 $success = $this->clusterCache->set( $cacheKey, $cache );
742 $this->setValidationHash( $code, $cache );
743 } else {
744 $success = true;
745 }
746
747 $this->saveToLocalCache( $code, $cache );
748
749 return $success;
750 }
751
758 protected function getValidationHash( $code ) {
759 $curTTL = null;
760 $value = $this->wanCache->get(
761 $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
762 $curTTL,
763 [ $this->getCheckKey( $code ) ]
764 );
765
766 if ( $value ) {
767 $hash = $value['hash'];
768 if ( ( time() - $value['latest'] ) < WANObjectCache::TTL_MINUTE ) {
769 // Cache was recently updated via replace() and should be up-to-date.
770 // That method is only called in the primary datacenter and uses FOR_UPDATE.
771 // Also, it is unlikely that the current datacenter is *now* secondary one.
772 $expired = false;
773 } else {
774 // See if the "check" key was bumped after the hash was generated
775 $expired = ( $curTTL < 0 );
776 }
777 } else {
778 // No hash found at all; cache must regenerate to be safe
779 $hash = false;
780 $expired = true;
781 }
782
783 return [ $hash, $expired ];
784 }
785
796 protected function setValidationHash( $code, array $cache ) {
797 $this->wanCache->set(
798 $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
799 [
800 'hash' => $cache['HASH'],
801 'latest' => $cache['LATEST'] ?? 0
802 ],
803 WANObjectCache::TTL_INDEFINITE
804 );
805 }
806
812 protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
813 return $this->clusterCache->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
814 }
815
849 function get( $key, $useDB = true, $langcode = true ) {
850 if ( is_int( $key ) ) {
851 // Fix numerical strings that somehow become ints
852 // on their way here
853 $key = (string)$key;
854 } elseif ( !is_string( $key ) ) {
855 throw new MWException( 'Non-string key given' );
856 } elseif ( $key === '' ) {
857 // Shortcut: the empty key is always missing
858 return false;
859 }
860
861 // Normalise title-case input (with some inlining)
862 $lckey = self::normalizeKey( $key );
863
864 Hooks::run( 'MessageCache::get', [ &$lckey ] );
865
866 // Loop through each language in the fallback list until we find something useful
867 $message = $this->getMessageFromFallbackChain(
868 wfGetLangObj( $langcode ),
869 $lckey,
870 !$this->mDisable && $useDB
871 );
872
873 // If we still have no message, maybe the key was in fact a full key so try that
874 if ( $message === false ) {
875 $parts = explode( '/', $lckey );
876 // We may get calls for things that are http-urls from sidebar
877 // Let's not load nonexistent languages for those
878 // They usually have more than one slash.
879 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
880 $message = Language::getMessageFor( $parts[0], $parts[1] );
881 if ( $message === null ) {
882 $message = false;
883 }
884 }
885 }
886
887 // Post-processing if the message exists
888 if ( $message !== false ) {
889 // Fix whitespace
890 $message = str_replace(
891 [
892 # Fix for trailing whitespace, removed by textarea
893 '&#32;',
894 # Fix for NBSP, converted to space by firefox
895 '&nbsp;',
896 '&#160;',
897 '&shy;'
898 ],
899 [
900 ' ',
901 "\u{00A0}",
902 "\u{00A0}",
903 "\u{00AD}"
904 ],
905 $message
906 );
907 }
908
909 return $message;
910 }
911
924 protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
925 $alreadyTried = [];
926
927 // First try the requested language.
928 $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
929 if ( $message !== false ) {
930 return $message;
931 }
932
933 // Now try checking the site language.
934 $message = $this->getMessageForLang( $this->contLang, $lckey, $useDB, $alreadyTried );
935 return $message;
936 }
937
948 private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
949 $langcode = $lang->getCode();
950
951 // Try checking the database for the requested language
952 if ( $useDB ) {
953 $uckey = $this->contLang->ucfirst( $lckey );
954
955 if ( !isset( $alreadyTried[$langcode] ) ) {
956 $message = $this->getMsgFromNamespace(
957 $this->getMessagePageName( $langcode, $uckey ),
958 $langcode
959 );
960 if ( $message !== false ) {
961 return $message;
962 }
963 $alreadyTried[$langcode] = true;
964 }
965 } else {
966 $uckey = null;
967 }
968
969 // Check the CDB cache
970 $message = $lang->getMessage( $lckey );
971 if ( $message !== null ) {
972 return $message;
973 }
974
975 // Try checking the database for all of the fallback languages
976 if ( $useDB ) {
977 $fallbackChain = Language::getFallbacksFor( $langcode );
978
979 foreach ( $fallbackChain as $code ) {
980 if ( isset( $alreadyTried[$code] ) ) {
981 continue;
982 }
983
984 $message = $this->getMsgFromNamespace(
985 $this->getMessagePageName( $code, $uckey ), $code );
986
987 if ( $message !== false ) {
988 return $message;
989 }
990 $alreadyTried[$code] = true;
991 }
992 }
993
994 return false;
995 }
996
1004 private function getMessagePageName( $langcode, $uckey ) {
1005 global $wgLanguageCode;
1006
1007 if ( $langcode === $wgLanguageCode ) {
1008 // Messages created in the content language will not have the /lang extension
1009 return $uckey;
1010 } else {
1011 return "$uckey/$langcode";
1012 }
1013 }
1014
1027 public function getMsgFromNamespace( $title, $code ) {
1028 // Load all MediaWiki page definitions into cache. Note that individual keys
1029 // already loaded into cache during this request remain in the cache, which
1030 // includes the value of hook-defined messages.
1031 $this->load( $code );
1032
1033 $entry = $this->cache->getField( $code, $title );
1034
1035 if ( $entry !== null ) {
1036 // Message page exists as an override of a software messages
1037 if ( substr( $entry, 0, 1 ) === ' ' ) {
1038 // The message exists and is not '!TOO BIG' or '!ERROR'
1039 return (string)substr( $entry, 1 );
1040 } elseif ( $entry === '!NONEXISTENT' ) {
1041 // The text might be '-' or missing due to some data loss
1042 return false;
1043 }
1044 // Load the message page, utilizing the individual message cache.
1045 // If the page does not exist, there will be no hook handler fallbacks.
1046 $entry = $this->loadCachedMessagePageEntry(
1047 $title,
1048 $code,
1049 $this->cache->getField( $code, 'HASH' )
1050 );
1051 } else {
1052 // Message page either does not exist or does not override a software message
1053 if ( !$this->isMainCacheable( $title, $this->overridable ) ) {
1054 // Message page does not override any software-defined message. A custom
1055 // message might be defined to have content or settings specific to the wiki.
1056 // Load the message page, utilizing the individual message cache as needed.
1057 $entry = $this->loadCachedMessagePageEntry(
1058 $title,
1059 $code,
1060 $this->cache->getField( $code, 'HASH' )
1061 );
1062 }
1063 if ( $entry === null || substr( $entry, 0, 1 ) !== ' ' ) {
1064 // Message does not have a MediaWiki page definition; try hook handlers
1065 $message = false;
1066 Hooks::run( 'MessagesPreLoad', [ $title, &$message, $code ] );
1067 if ( $message !== false ) {
1068 $this->cache->setField( $code, $title, ' ' . $message );
1069 } else {
1070 $this->cache->setField( $code, $title, '!NONEXISTENT' );
1071 }
1072
1073 return $message;
1074 }
1075 }
1076
1077 if ( $entry !== false && substr( $entry, 0, 1 ) === ' ' ) {
1078 if ( $this->cacheVolatile[$code] ) {
1079 // Make sure that individual keys respect the WAN cache holdoff period too
1080 LoggerFactory::getInstance( 'MessageCache' )->debug(
1081 __METHOD__ . ': loading volatile key \'{titleKey}\'',
1082 [ 'titleKey' => $title, 'code' => $code ] );
1083 } else {
1084 $this->cache->setField( $code, $title, $entry );
1085 }
1086 // The message exists, so make sure a string is returned
1087 return (string)substr( $entry, 1 );
1088 }
1089
1090 $this->cache->setField( $code, $title, '!NONEXISTENT' );
1091
1092 return false;
1093 }
1094
1101 private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) {
1102 $fname = __METHOD__;
1103 return $this->srvCache->getWithSetCallback(
1104 $this->srvCache->makeKey( 'messages-big', $hash, $dbKey ),
1105 BagOStuff::TTL_HOUR,
1106 function () use ( $code, $dbKey, $hash, $fname ) {
1107 return $this->wanCache->getWithSetCallback(
1108 $this->bigMessageCacheKey( $hash, $dbKey ),
1109 self::WAN_TTL,
1110 function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code, $fname ) {
1111 // Try loading the message from the database
1112 $dbr = wfGetDB( DB_REPLICA );
1113 $setOpts += Database::getCacheSetOptions( $dbr );
1114 // Use newKnownCurrent() to avoid querying revision/user tables
1115 $title = Title::makeTitle( NS_MEDIAWIKI, $dbKey );
1116 $revision = Revision::newKnownCurrent( $dbr, $title );
1117 if ( !$revision ) {
1118 // The wiki doesn't have a local override page. Cache absence with normal TTL.
1119 // When overrides are created, self::replace() takes care of the cache.
1120 return '!NONEXISTENT';
1121 }
1122 $content = $revision->getContent();
1123 if ( $content ) {
1124 $message = $this->getMessageTextFromContent( $content );
1125 } else {
1126 LoggerFactory::getInstance( 'MessageCache' )->warning(
1127 $fname . ': failed to load page text for \'{titleKey}\'',
1128 [ 'titleKey' => $dbKey, 'code' => $code ]
1129 );
1130 $message = null;
1131 }
1132
1133 if ( !is_string( $message ) ) {
1134 // Revision failed to load Content, or Content is incompatible with wikitext.
1135 // Possibly a temporary loading failure.
1136 $ttl = 5;
1137
1138 return '!NONEXISTENT';
1139 }
1140
1141 return ' ' . $message;
1142 }
1143 );
1144 }
1145 );
1146 }
1147
1155 public function transform( $message, $interface = false, $language = null, $title = null ) {
1156 // Avoid creating parser if nothing to transform
1157 if ( strpos( $message, '{{' ) === false ) {
1158 return $message;
1159 }
1160
1161 if ( $this->mInParser ) {
1162 return $message;
1163 }
1164
1165 $parser = $this->getParser();
1166 if ( $parser ) {
1167 $popts = $this->getParserOptions();
1168 $popts->setInterfaceMessage( $interface );
1169 $popts->setTargetLanguage( $language );
1170
1171 $userlang = $popts->setUserLang( $language );
1172 $this->mInParser = true;
1173 $message = $parser->transformMsg( $message, $popts, $title );
1174 $this->mInParser = false;
1175 $popts->setUserLang( $userlang );
1176 }
1177
1178 return $message;
1179 }
1180
1184 public function getParser() {
1185 global $wgParserConf;
1186 if ( !$this->mParser ) {
1187 $parser = MediaWikiServices::getInstance()->getParser();
1188 # Do some initialisation so that we don't have to do it twice
1189 $parser->firstCallInit();
1190 # Clone it and store it
1191 $class = $wgParserConf['class'];
1192 if ( $class == ParserDiffTest::class ) {
1193 # Uncloneable
1194 // @phan-suppress-next-line PhanTypeMismatchProperty
1195 $this->mParser = new $class( $wgParserConf );
1196 } else {
1197 $this->mParser = clone $parser;
1198 }
1199 }
1200
1201 return $this->mParser;
1202 }
1203
1212 public function parse( $text, $title = null, $linestart = true,
1213 $interface = false, $language = null
1214 ) {
1215 global $wgTitle;
1216
1217 if ( $this->mInParser ) {
1218 return htmlspecialchars( $text );
1219 }
1220
1221 $parser = $this->getParser();
1222 $popts = $this->getParserOptions();
1223 $popts->setInterfaceMessage( $interface );
1224
1225 if ( is_string( $language ) ) {
1226 $language = Language::factory( $language );
1227 }
1228 $popts->setTargetLanguage( $language );
1229
1230 if ( !$title || !$title instanceof Title ) {
1231 wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1232 wfGetAllCallers( 6 ) . ' with no title set.' );
1233 $title = $wgTitle;
1234 }
1235 // Sometimes $wgTitle isn't set either...
1236 if ( !$title ) {
1237 # It's not uncommon having a null $wgTitle in scripts. See r80898
1238 # Create a ghost title in such case
1239 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
1240 }
1241
1242 $this->mInParser = true;
1243 $res = $parser->parse( $text, $title, $popts, $linestart );
1244 $this->mInParser = false;
1245
1246 return $res;
1247 }
1248
1249 public function disable() {
1250 $this->mDisable = true;
1251 }
1252
1253 public function enable() {
1254 $this->mDisable = false;
1255 }
1256
1269 public function isDisabled() {
1270 return $this->mDisable;
1271 }
1272
1278 public function clear() {
1279 $langs = Language::fetchLanguageNames( null, 'mw' );
1280 foreach ( array_keys( $langs ) as $code ) {
1281 $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
1282 }
1283 $this->cache->clear();
1284 $this->loadedLanguages = [];
1285 }
1286
1291 public function figureMessage( $key ) {
1292 global $wgLanguageCode;
1293
1294 $pieces = explode( '/', $key );
1295 if ( count( $pieces ) < 2 ) {
1296 return [ $key, $wgLanguageCode ];
1297 }
1298
1299 $lang = array_pop( $pieces );
1300 if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1301 return [ $key, $wgLanguageCode ];
1302 }
1303
1304 $message = implode( '/', $pieces );
1305
1306 return [ $message, $lang ];
1307 }
1308
1317 public function getAllMessageKeys( $code ) {
1318 $this->load( $code );
1319 if ( !$this->cache->has( $code ) ) {
1320 // Apparently load() failed
1321 return null;
1322 }
1323 // Remove administrative keys
1324 $cache = $this->cache->get( $code );
1325 unset( $cache['VERSION'] );
1326 unset( $cache['EXPIRY'] );
1327 unset( $cache['EXCESSIVE'] );
1328 // Remove any !NONEXISTENT keys
1329 $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1330
1331 // Keys may appear with a capital first letter. lcfirst them.
1332 return array_map( [ $this->contLang, 'lcfirst' ], array_keys( $cache ) );
1333 }
1334
1343 $msgText = $this->getMessageTextFromContent( $content );
1344 if ( $msgText === null ) {
1345 $msgText = false; // treat as not existing
1346 }
1347
1348 $this->replace( $title->getDBkey(), $msgText );
1349
1350 if ( $this->contLang->hasVariants() ) {
1351 $this->contLang->updateConversionTable( $title );
1352 }
1353 }
1354
1359 public function getCheckKey( $code ) {
1360 return $this->wanCache->makeKey( 'messages', $code );
1361 }
1362
1367 private function getMessageTextFromContent( Content $content = null ) {
1368 // @TODO: could skip pseudo-messages like js/css here, based on content model
1369 if ( $content ) {
1370 // Message page exists...
1371 // XXX: Is this the right way to turn a Content object into a message?
1372 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1373 // CssContent. MessageContent is *not* used for storing messages, it's
1374 // only used for wrapping them when needed.
1375 $msgText = $content->getWikitextForTransclusion();
1376 if ( $msgText === false || $msgText === null ) {
1377 // This might be due to some kind of misconfiguration...
1378 $msgText = null;
1379 LoggerFactory::getInstance( 'MessageCache' )->warning(
1380 __METHOD__ . ": message content doesn't provide wikitext "
1381 . "(content model: " . $content->getModel() . ")" );
1382 }
1383 } else {
1384 // Message page does not exist...
1385 $msgText = false;
1386 }
1387
1388 return $msgText;
1389 }
1390
1396 private function bigMessageCacheKey( $hash, $title ) {
1397 return $this->wanCache->makeKey( 'messages-big', $hash, $title );
1398 }
1399}
serialize()
$wgLanguageCode
Site language code.
$wgAdaptiveMessageCache
Instead of caching everything, only cache those messages which have been customised in the site conte...
$wgMaxMsgCacheEntrySize
Maximum entry size in the message cache, in bytes.
$wgParserConf
Parser configuration.
wfGetLangObj( $langcode=false)
Return a Language object from $langcode.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfGetAllCallers( $limit=3)
Return a string consisting of callers in the stack.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
const MSG_CACHE_VERSION
MediaWiki message cache structure version.
if(! $wgRequest->checkUrlExtension()) if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
Definition api.php:58
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:63
A BagOStuff object with no objects in it.
Internationalisation code.
Definition Language.php:37
MediaWiki exception.
Handles a simple LRU key/value map with a maximum number of entries.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Message cache purging and in-place update handler for specific message page changes.
Cache of messages that are defined by MediaWiki namespace pages or by hooks.
getValidationHash( $code)
Get the md5 used to validate the local APC cache.
loadFromDBWithLock( $code, array &$where, $mode=null)
const LOCK_TTL
How long memcached locks last.
loadFromDB( $code, $mode=null)
Loads cacheable messages from the database.
getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried)
Given a language, try and fetch messages from that language and its fallbacks.
saveToLocalCache( $code, $cache)
Save the cache to APC.
getMessagePageName( $langcode, $uckey)
Get the message page name for a given language.
Language $contLang
getCheckKey( $code)
saveToCaches(array $cache, $dest, $code=false)
Shortcut to update caches.
refreshAndReplaceInternal( $code, array $replacements)
setValidationHash( $code, array $cache)
Set the md5 used to validate the local disk cache.
isCacheExpired( $cache)
Is the given cache array expired due to time passing or a version change?
getMsgFromNamespace( $title, $code)
Get a message from the MediaWiki namespace, with caching.
getReentrantScopedLock( $key, $timeout=self::WAIT_SEC)
const WAIT_SEC
How long to wait for memcached locks.
$mDisable
Should mean that database cannot be used, but check.
getLocalCache( $code)
Try to load the cache from APC.
load( $code, $mode=null)
Loads messages from caches or from database in this order: (1) local message cache (if $wgUseLocalMes...
BagOStuff $srvCache
transform( $message, $interface=false, $language=null, $title=null)
updateMessageOverride(Title $title, Content $content=null)
Purge message caches when a MediaWiki: page is created, updated, or deleted.
array $loadedLanguages
Track which languages have been loaded by load().
getMessageFromFallbackChain( $lang, $lckey, $useDB)
Given a language, try and fetch messages from that language.
isDisabled()
Whether DB/cache usage is disabled for determining messages.
BagOStuff $clusterCache
isMainCacheable( $name, array $overridable)
loadCachedMessagePageEntry( $dbKey, $code, $hash)
getMessageTextFromContent(Content $content=null)
clear()
Clear all stored messages in global and local cache.
static singleton()
Get the singleton instance of this class.
getAllMessageKeys( $code)
Get all message keys stored in the message cache for a given language.
MapCacheLRU $cache
Process cache of loaded messages that are defined in MediaWiki namespace.
WANObjectCache $wanCache
static normalizeKey( $key)
Normalize message key input.
bool[] $cacheVolatile
Map of (language code => boolean)
__construct(WANObjectCache $wanCache, BagOStuff $clusterCache, BagOStuff $serverCache, $useDB, Language $contLang=null)
parse( $text, $title=null, $linestart=true, $interface=false, $language=null)
ParserOptions $mParserOptions
Message cache has its own parser which it uses to transform messages.
replace( $title, $text)
Updates cache as necessary when message page is changed.
getParserOptions()
ParserOptions is lazy initialised.
bigMessageCacheKey( $hash, $title)
array $overridable
Map of (lowercase message key => index) for all software defined messages.
Set options of the Parser.
setAllowUnsafeRawHtml( $x)
If the wiki is configured to allow raw html ($wgRawHtml = true) is it allowed in the specific case of...
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:74
static newKnownCurrent(IDatabase $db, $pageIdOrTitle, $revId=0)
Load a revision based on a known page ID and current revision ID from the DB.
Represents a title within MediaWiki.
Definition Title.php:42
Multi-datacenter aware caching interface.
Relational database abstraction object.
Definition Database.php:49
const NS_MEDIAWIKI
Definition Defines.php:77
const NS_SPECIAL
Definition Defines.php:58
Base interface for content objects.
Definition Content.php:34
$cache
Definition mcc.php:33
A helper class for throttling authentication attempts.
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26
$content
Definition router.php:78
return true
Definition router.php:94
if(!isset( $args[0])) $lang