3 * Localisation messages cache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
26 * MediaWiki message cache structure version.
27 * Bump this whenever the message cache format has changed.
29 define( 'MSG_CACHE_VERSION', 2 );
33 * Performs various MediaWiki namespace-related functions
37 const FOR_UPDATE
= 1; // force message reload
39 /** How long to wait for memcached locks */
41 /** How long memcached locks last */
45 * Process local cache of loaded messages that are defined in
46 * MediaWiki namespace. First array level is a language code,
47 * second level is message key and the values are either message
48 * content prefixed with space, or !NONEXISTENT for negative
55 * Should mean that database cannot be used, but check
61 * Lifetime for cache, used by object caching.
62 * Set on construction, see __construct().
67 * Message cache has its own parser which it uses to transform
70 protected $mParserOptions, $mParser;
73 * Variable for tracking which variables are already loaded
74 * @var array $mLoadedLanguages
76 protected $mLoadedLanguages = [];
79 * @var bool $mInParser
81 protected $mInParser = false;
85 /** @var WANObjectCache */
91 * @var MessageCache $instance
93 private static $instance;
96 * Get the signleton instance of this class
99 * @return MessageCache
101 public static function singleton() {
102 if ( self
::$instance === null ) {
103 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
104 self
::$instance = new self(
105 wfGetMessageCacheStorage(),
106 $wgUseDatabaseMessages,
111 return self
::$instance;
115 * Destroy the singleton instance
119 public static function destroyInstance() {
120 self
::$instance = null;
124 * Normalize message key input
126 * @param string $key Input message key to be normalized
127 * @return string Normalized message key
129 public static function normalizeKey( $key ) {
131 $lckey = strtr( $key, ' ', '_' );
132 if ( ord( $lckey ) < 128 ) {
133 $lckey[0] = strtolower( $lckey[0] );
135 $lckey = $wgContLang->lcfirst( $lckey );
142 * @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
144 * @param int $expiry Lifetime for cache. @see $mExpiry.
146 function __construct( $memCached, $useDB, $expiry ) {
147 global $wgUseLocalMessageCache;
150 $memCached = wfGetCache( CACHE_NONE
);
153 $this->mMemc
= $memCached;
154 $this->mDisable
= !$useDB;
155 $this->mExpiry
= $expiry;
157 if ( $wgUseLocalMessageCache ) {
158 $this->localCache
= MediaWikiServices
::getInstance()->getLocalServerObjectCache();
160 $this->localCache
= new EmptyBagOStuff();
163 $this->wanCache
= ObjectCache
::getMainWANInstance();
167 * ParserOptions is lazy initialised.
169 * @return ParserOptions
171 function getParserOptions() {
174 if ( !$this->mParserOptions
) {
175 if ( !$wgUser->isSafeToLoad() ) {
176 // $wgUser isn't unstubbable yet, so don't try to get a
177 // ParserOptions for it. And don't cache this ParserOptions
179 $po = ParserOptions
::newFromAnon();
180 $po->setEditSection( false );
184 $this->mParserOptions
= new ParserOptions
;
185 $this->mParserOptions
->setEditSection( false );
188 return $this->mParserOptions
;
192 * Try to load the cache from APC.
194 * @param string $code Optional language code, see documenation of load().
195 * @return array|bool The cache array, or false if not in cache.
197 protected function getLocalCache( $code ) {
198 $cacheKey = wfMemcKey( __CLASS__
, $code );
200 return $this->localCache
->get( $cacheKey );
204 * Save the cache to APC.
206 * @param string $code
207 * @param array $cache The cache array
209 protected function saveToLocalCache( $code, $cache ) {
210 $cacheKey = wfMemcKey( __CLASS__
, $code );
211 $this->localCache
->set( $cacheKey, $cache );
215 * Loads messages from caches or from database in this order:
216 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
218 * (3) from the database.
220 * When succesfully loading from (2) or (3), all higher level caches are
221 * updated for the newest version.
223 * Nothing is loaded if member variable mDisable is true, either manually
224 * set by calling code or if message loading fails (is this possible?).
226 * Returns true if cache is already populated or it was succesfully populated,
227 * or false if populating empty cache fails. Also returns true if MessageCache
230 * @param string $code Language to which load messages
231 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
232 * @throws MWException
235 protected function load( $code, $mode = null ) {
236 if ( !is_string( $code ) ) {
237 throw new InvalidArgumentException( "Missing language code" );
240 # Don't do double loading...
241 if ( isset( $this->mLoadedLanguages
[$code] ) && $mode != self
::FOR_UPDATE
) {
245 # 8 lines of code just to say (once) that message cache is disabled
246 if ( $this->mDisable
) {
247 static $shownDisabled = false;
248 if ( !$shownDisabled ) {
249 wfDebug( __METHOD__
. ": disabled\n" );
250 $shownDisabled = true;
256 # Loading code starts
257 $success = false; # Keep track of success
258 $staleCache = false; # a cache array with expired data, or false if none has been loaded
259 $where = []; # Debug info, delayed to avoid spamming debug log too much
261 # Hash of the contents is stored in memcache, to detect if data-center cache
262 # or local cache goes out of date (e.g. due to replace() on some other server)
263 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
265 # Try the local cache and check against the cluster hash key...
266 $cache = $this->getLocalCache( $code );
268 $where[] = 'local cache is empty';
269 } elseif ( !isset( $cache['HASH'] ) ||
$cache['HASH'] !== $hash ) {
270 $where[] = 'local cache has the wrong hash';
271 $staleCache = $cache;
272 } elseif ( $this->isCacheExpired( $cache ) ) {
273 $where[] = 'local cache is expired';
274 $staleCache = $cache;
275 } elseif ( $hashVolatile ) {
276 $where[] = 'local cache validation key is expired/volatile';
277 $staleCache = $cache;
279 $where[] = 'got from local cache';
281 $this->mCache
[$code] = $cache;
285 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
286 # Try the global cache. If it is empty, try to acquire a lock. If
287 # the lock can't be acquired, wait for the other thread to finish
288 # and then try the global cache a second time.
289 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++
) {
290 if ( $hashVolatile && $staleCache ) {
291 # Do not bother fetching the whole cache blob to avoid I/O.
292 # Instead, just try to get the non-blocking $statusKey lock
293 # below, and use the local stale value if it was not acquired.
294 $where[] = 'global cache is presumed expired';
296 $cache = $this->mMemc
->get( $cacheKey );
298 $where[] = 'global cache is empty';
299 } elseif ( $this->isCacheExpired( $cache ) ) {
300 $where[] = 'global cache is expired';
301 $staleCache = $cache;
302 } elseif ( $hashVolatile ) {
303 # DB results are replica DB lag prone until the holdoff TTL passes.
304 # By then, updates should be reflected in loadFromDBWithLock().
305 # One thread renerates the cache while others use old values.
306 $where[] = 'global cache is expired/volatile';
307 $staleCache = $cache;
309 $where[] = 'got from global cache';
310 $this->mCache
[$code] = $cache;
311 $this->saveToCaches( $cache, 'local-only', $code );
317 # Done, no need to retry
321 # We need to call loadFromDB. Limit the concurrency to one process.
322 # This prevents the site from going down when the cache expires.
323 # Note that the DB slam protection lock here is non-blocking.
324 $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
325 if ( $loadStatus === true ) {
328 } elseif ( $staleCache ) {
329 # Use the stale cache while some other thread constructs the new one
330 $where[] = 'using stale cache';
331 $this->mCache
[$code] = $staleCache;
334 } elseif ( $failedAttempts > 0 ) {
335 # Already blocked once, so avoid another lock/unlock cycle.
336 # This case will typically be hit if memcached is down, or if
337 # loadFromDB() takes longer than LOCK_WAIT.
338 $where[] = "could not acquire status key.";
340 } elseif ( $loadStatus === 'cantacquire' ) {
341 # Wait for the other thread to finish, then retry. Normally,
342 # the memcached get() will then yeild the other thread's result.
343 $where[] = 'waited for other thread to complete';
344 $this->getReentrantScopedLock( $cacheKey );
346 # Disable cache; $loadStatus is 'disabled'
353 $where[] = 'loading FAILED - cache is disabled';
354 $this->mDisable
= true;
355 $this->mCache
= false;
356 wfDebugLog( 'MessageCacheError', __METHOD__
. ": Failed to load $code\n" );
357 # This used to throw an exception, but that led to nasty side effects like
358 # the whole wiki being instantly down if the memcached server died
360 # All good, just record the success
361 $this->mLoadedLanguages
[$code] = true;
364 $info = implode( ', ', $where );
365 wfDebugLog( 'MessageCache', __METHOD__
. ": Loading $code... $info\n" );
371 * @param string $code
372 * @param array $where List of wfDebug() comments
373 * @param integer $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
374 * @return bool|string True on success or one of ("cantacquire", "disabled")
376 protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
377 global $wgUseLocalMessageCache;
379 # If cache updates on all levels fail, give up on message overrides.
380 # This is to avoid easy site outages; see $saveSuccess comments below.
381 $statusKey = wfMemcKey( 'messages', $code, 'status' );
382 $status = $this->mMemc
->get( $statusKey );
383 if ( $status === 'error' ) {
384 $where[] = "could not load; method is still globally disabled";
388 # Now let's regenerate
389 $where[] = 'loading from database';
391 # Lock the cache to prevent conflicting writes.
392 # This lock is non-blocking so stale cache can quickly be used.
393 # Note that load() will call a blocking getReentrantScopedLock()
394 # after this if it really need to wait for any current thread.
395 $cacheKey = wfMemcKey( 'messages', $code );
396 $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
397 if ( !$scopedLock ) {
398 $where[] = 'could not acquire main lock';
399 return 'cantacquire';
402 $cache = $this->loadFromDB( $code, $mode );
403 $this->mCache
[$code] = $cache;
404 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
406 if ( !$saveSuccess ) {
408 * Cache save has failed.
410 * There are two main scenarios where this could be a problem:
411 * - The cache is more than the maximum size (typically 1MB compressed).
412 * - Memcached has no space remaining in the relevant slab class. This is
413 * unlikely with recent versions of memcached.
415 * Either way, if there is a local cache, nothing bad will happen. If there
416 * is no local cache, disabling the message cache for all requests avoids
417 * incurring a loadFromDB() overhead on every request, and thus saves the
418 * wiki from complete downtime under moderate traffic conditions.
420 if ( !$wgUseLocalMessageCache ) {
421 $this->mMemc
->set( $statusKey, 'error', 60 * 5 );
422 $where[] = 'could not save cache, disabled globally for 5 minutes';
424 $where[] = "could not save global cache";
432 * Loads cacheable messages from the database. Messages bigger than
433 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
434 * on-demand from the database later.
436 * @param string $code Language code
437 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
438 * @return array Loaded messages for storing in caches
440 function loadFromDB( $code, $mode = null ) {
441 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
443 $dbr = wfGetDB( ( $mode == self
::FOR_UPDATE
) ? DB_MASTER
: DB_REPLICA
);
449 'page_is_redirect' => 0,
450 'page_namespace' => NS_MEDIAWIKI
,
454 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
455 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
456 $this->load( $wgLanguageCode );
458 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
459 foreach ( $mostused as $key => $value ) {
460 $mostused[$key] = "$value/$code";
464 if ( count( $mostused ) ) {
465 $conds['page_title'] = $mostused;
466 } elseif ( $code !== $wgLanguageCode ) {
467 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
469 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
470 # other than language code.
471 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
474 # Conditions to fetch oversized pages to ignore them
476 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
478 # Load titles for all oversized pages in the MediaWiki namespace
479 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
480 foreach ( $res as $row ) {
481 $cache[$row->page_title
] = '!TOO BIG';
484 # Conditions to load the remaining pages with their contents
485 $smallConds = $conds;
486 $smallConds[] = 'page_latest=rev_id';
487 $smallConds[] = 'rev_text_id=old_id';
488 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
491 [ 'page', 'revision', 'text' ],
492 [ 'page_title', 'old_text', 'old_flags' ],
494 __METHOD__
. "($code)-small"
497 foreach ( $res as $row ) {
498 $text = Revision
::getRevisionText( $row );
499 if ( $text === false ) {
500 // Failed to fetch data; possible ES errors?
501 // Store a marker to fetch on-demand as a workaround...
506 . ": failed to load message page text for {$row->page_title} ($code)"
509 $entry = ' ' . $text;
511 $cache[$row->page_title
] = $entry;
514 $cache['VERSION'] = MSG_CACHE_VERSION
;
516 $cache['HASH'] = md5( serialize( $cache ) );
517 $cache['EXPIRY'] = wfTimestamp( TS_MW
, time() +
$this->mExpiry
);
523 * Updates cache as necessary when message page is changed
525 * @param string|bool $title Name of the page changed (false if deleted)
526 * @param mixed $text New contents of the page.
528 public function replace( $title, $text ) {
529 global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
531 if ( $this->mDisable
) {
535 list( $msg, $code ) = $this->figureMessage( $title );
536 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
537 // Content language overrides do not use the /<code> suffix
541 // Note that if the cache is volatile, load() may trigger a DB fetch.
542 // In that case we reenter/reuse the existing cache key lock to avoid
543 // a self-deadlock. This is safe as no reads happen *directly* in this
544 // method between getReentrantScopedLock() and load() below. There is
545 // no risk of data "changing under our feet" for replace().
546 $cacheKey = wfMemcKey( 'messages', $code );
547 $scopedLock = $this->getReentrantScopedLock( $cacheKey );
548 $this->load( $code, self
::FOR_UPDATE
);
550 $titleKey = wfMemcKey( 'messages', 'individual', $title );
551 if ( $text === false ) {
552 // Article was deleted
553 $this->mCache
[$code][$title] = '!NONEXISTENT';
554 $this->wanCache
->delete( $titleKey );
555 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
557 $this->mCache
[$code][$title] = '!TOO BIG';
558 $this->wanCache
->set( $titleKey, ' ' . $text, $this->mExpiry
);
560 $this->mCache
[$code][$title] = ' ' . $text;
561 $this->wanCache
->delete( $titleKey );
564 // Mark this cache as definitely "latest" (non-volatile) so
565 // load() calls do try to refresh the cache with replica DB data
566 $this->mCache
[$code]['LATEST'] = time();
568 // Update caches if the lock was acquired
570 $this->saveToCaches( $this->mCache
[$code], 'all', $code );
573 ScopedCallback
::consume( $scopedLock );
574 // Relay the purge to APC and other DCs
575 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
577 // Also delete cached sidebar... just in case it is affected
579 if ( $code === 'en' ) {
580 // Delete all sidebars, like for example on action=purge on the
582 $codes = array_keys( Language
::fetchLanguageNames() );
585 foreach ( $codes as $code ) {
586 $sidebarKey = wfMemcKey( 'sidebar', $code );
587 $this->wanCache
->delete( $sidebarKey );
590 // Update the message in the message blob store
591 $resourceloader = RequestContext
::getMain()->getOutput()->getResourceLoader();
592 $blobStore = $resourceloader->getMessageBlobStore();
593 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
595 Hooks
::run( 'MessageCacheReplace', [ $title, $text ] );
599 * Is the given cache array expired due to time passing or a version change?
601 * @param array $cache
604 protected function isCacheExpired( $cache ) {
605 if ( !isset( $cache['VERSION'] ) ||
!isset( $cache['EXPIRY'] ) ) {
608 if ( $cache['VERSION'] != MSG_CACHE_VERSION
) {
611 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
619 * Shortcut to update caches.
621 * @param array $cache Cached messages with a version.
622 * @param string $dest Either "local-only" to save to local caches only
623 * or "all" to save to all caches.
624 * @param string|bool $code Language code (default: false)
627 protected function saveToCaches( array $cache, $dest, $code = false ) {
628 if ( $dest === 'all' ) {
629 $cacheKey = wfMemcKey( 'messages', $code );
630 $success = $this->mMemc
->set( $cacheKey, $cache );
635 $this->setValidationHash( $code, $cache );
636 $this->saveToLocalCache( $code, $cache );
642 * Get the md5 used to validate the local APC cache
644 * @param string $code
645 * @return array (hash or false, bool expiry/volatility status)
647 protected function getValidationHash( $code ) {
649 $value = $this->wanCache
->get(
650 wfMemcKey( 'messages', $code, 'hash', 'v1' ),
652 [ wfMemcKey( 'messages', $code ) ]
656 // No hash found at all; cache must regenerate to be safe
660 $hash = $value['hash'];
661 if ( ( time() - $value['latest'] ) < WANObjectCache
::HOLDOFF_TTL
) {
662 // Cache was recently updated via replace() and should be up-to-date
665 // See if the "check" key was bumped after the hash was generated
666 $expired = ( $curTTL < 0 );
670 return [ $hash, $expired ];
674 * Set the md5 used to validate the local disk cache
676 * If $cache has a 'LATEST' UNIX timestamp key, then the hash will not
677 * be treated as "volatile" by getValidationHash() for the next few seconds
679 * @param string $code
680 * @param array $cache Cached messages with a version
682 protected function setValidationHash( $code, array $cache ) {
683 $this->wanCache
->set(
684 wfMemcKey( 'messages', $code, 'hash', 'v1' ),
686 'hash' => $cache['HASH'],
687 'latest' => isset( $cache['LATEST'] ) ?
$cache['LATEST'] : 0
689 WANObjectCache
::TTL_INDEFINITE
694 * @param string $key A language message cache key that stores blobs
695 * @param integer $timeout Wait timeout in seconds
696 * @return null|ScopedCallback
698 protected function getReentrantScopedLock( $key, $timeout = self
::WAIT_SEC
) {
699 return $this->mMemc
->getScopedLock( $key, $timeout, self
::LOCK_TTL
, __METHOD__
);
703 * Get a message from either the content language or the user language.
705 * First, assemble a list of languages to attempt getting the message from. This
706 * chain begins with the requested language and its fallbacks and then continues with
707 * the content language and its fallbacks. For each language in the chain, the following
708 * process will occur (in this order):
709 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
710 * Note: for the content language, there is no /lang subpage.
711 * 2. Fetch from the static CDB cache.
712 * 3. If available, check the database for fallback language overrides.
714 * This process provides a number of guarantees. When changing this code, make sure all
715 * of these guarantees are preserved.
716 * * If the requested language is *not* the content language, then the CDB cache for that
717 * specific language will take precedence over the root database page ([[MW:msg]]).
718 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
719 * the message is available *anywhere* in the language for which it is a fallback.
721 * @param string $key The message key
722 * @param bool $useDB If true, look for the message in the DB, false
723 * to use only the compiled l10n cache.
724 * @param bool|string|object $langcode Code of the language to get the message for.
725 * - If string and a valid code, will create a standard language object
726 * - If string but not a valid code, will create a basic language object
727 * - If boolean and false, create object from the current users language
728 * - If boolean and true, create object from the wikis content language
729 * - If language object, use it as given
730 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
732 * @throws MWException When given an invalid key
733 * @return string|bool False if the message doesn't exist, otherwise the
734 * message (which can be empty)
736 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
737 if ( is_int( $key ) ) {
738 // Fix numerical strings that somehow become ints
741 } elseif ( !is_string( $key ) ) {
742 throw new MWException( 'Non-string key given' );
743 } elseif ( $key === '' ) {
744 // Shortcut: the empty key is always missing
748 // For full keys, get the language code from the key
749 $pos = strrpos( $key, '/' );
750 if ( $isFullKey && $pos !== false ) {
751 $langcode = substr( $key, $pos +
1 );
752 $key = substr( $key, 0, $pos );
755 // Normalise title-case input (with some inlining)
756 $lckey = MessageCache
::normalizeKey( $key );
758 Hooks
::run( 'MessageCache::get', [ &$lckey ] );
760 // Loop through each language in the fallback list until we find something useful
761 $lang = wfGetLangObj( $langcode );
762 $message = $this->getMessageFromFallbackChain(
765 !$this->mDisable
&& $useDB
768 // If we still have no message, maybe the key was in fact a full key so try that
769 if ( $message === false ) {
770 $parts = explode( '/', $lckey );
771 // We may get calls for things that are http-urls from sidebar
772 // Let's not load nonexistent languages for those
773 // They usually have more than one slash.
774 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
775 $message = Language
::getMessageFor( $parts[0], $parts[1] );
776 if ( $message === null ) {
782 // Post-processing if the message exists
783 if ( $message !== false ) {
785 $message = str_replace(
787 # Fix for trailing whitespace, removed by textarea
789 # Fix for NBSP, converted to space by firefox
808 * Given a language, try and fetch messages from that language.
810 * Will also consider fallbacks of that language, the site language, and fallbacks for
813 * @see MessageCache::get
814 * @param Language|StubObject $lang Preferred language
815 * @param string $lckey Lowercase key for the message (as for localisation cache)
816 * @param bool $useDB Whether to include messages from the wiki database
817 * @return string|bool The message, or false if not found
819 protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
824 // First try the requested language.
825 $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
826 if ( $message !== false ) {
830 // Now try checking the site language.
831 $message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
836 * Given a language, try and fetch messages from that language and its fallbacks.
838 * @see MessageCache::get
839 * @param Language|StubObject $lang Preferred language
840 * @param string $lckey Lowercase key for the message (as for localisation cache)
841 * @param bool $useDB Whether to include messages from the wiki database
842 * @param bool[] $alreadyTried Contains true for each language that has been tried already
843 * @return string|bool The message, or false if not found
845 private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
847 $langcode = $lang->getCode();
849 // Try checking the database for the requested language
851 $uckey = $wgContLang->ucfirst( $lckey );
853 if ( !isset( $alreadyTried[ $langcode ] ) ) {
854 $message = $this->getMsgFromNamespace(
855 $this->getMessagePageName( $langcode, $uckey ),
859 if ( $message !== false ) {
862 $alreadyTried[ $langcode ] = true;
868 // Check the CDB cache
869 $message = $lang->getMessage( $lckey );
870 if ( $message !== null ) {
874 // Try checking the database for all of the fallback languages
876 $fallbackChain = Language
::getFallbacksFor( $langcode );
878 foreach ( $fallbackChain as $code ) {
879 if ( isset( $alreadyTried[ $code ] ) ) {
883 $message = $this->getMsgFromNamespace(
884 $this->getMessagePageName( $code, $uckey ), $code );
886 if ( $message !== false ) {
889 $alreadyTried[ $code ] = true;
897 * Get the message page name for a given language
899 * @param string $langcode
900 * @param string $uckey Uppercase key for the message
901 * @return string The page name
903 private function getMessagePageName( $langcode, $uckey ) {
904 global $wgLanguageCode;
905 if ( $langcode === $wgLanguageCode ) {
906 // Messages created in the content language will not have the /lang extension
909 return "$uckey/$langcode";
914 * Get a message from the MediaWiki namespace, with caching. The key must
915 * first be converted to two-part lang/msg form if necessary.
917 * Unlike self::get(), this function doesn't resolve fallback chains, and
918 * some callers require this behavior. LanguageConverter::parseCachedTable()
919 * and self::get() are some examples in core.
921 * @param string $title Message cache key with initial uppercase letter.
922 * @param string $code Code denoting the language to try.
923 * @return string|bool The message, or false if it does not exist or on error
925 public function getMsgFromNamespace( $title, $code ) {
926 $this->load( $code );
927 if ( isset( $this->mCache
[$code][$title] ) ) {
928 $entry = $this->mCache
[$code][$title];
929 if ( substr( $entry, 0, 1 ) === ' ' ) {
930 // The message exists, so make sure a string
932 return (string)substr( $entry, 1 );
933 } elseif ( $entry === '!NONEXISTENT' ) {
935 } elseif ( $entry === '!TOO BIG' ) {
936 // Fall through and try invididual message cache below
939 // XXX: This is not cached in process cache, should it?
941 Hooks
::run( 'MessagesPreLoad', [ $title, &$message ] );
942 if ( $message !== false ) {
949 # Try the individual message cache
950 $titleKey = wfMemcKey( 'messages', 'individual', $title );
951 $entry = $this->wanCache
->get( $titleKey );
953 if ( substr( $entry, 0, 1 ) === ' ' ) {
954 $this->mCache
[$code][$title] = $entry;
956 // The message exists, so make sure a string
958 return (string)substr( $entry, 1 );
959 } elseif ( $entry === '!NONEXISTENT' ) {
960 $this->mCache
[$code][$title] = '!NONEXISTENT';
964 # Corrupt/obsolete entry, delete it
965 $this->wanCache
->delete( $titleKey );
969 # Try loading it from the database
970 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
972 $content = $revision->getContent();
974 // A possibly temporary loading failure.
977 __METHOD__
. ": failed to load message page text for {$title} ($code)"
979 $message = null; // no negative caching
981 // XXX: Is this the right way to turn a Content object into a message?
982 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
983 // CssContent. MessageContent is *not* used for storing messages, it's
984 // only used for wrapping them when needed.
985 $message = $content->getWikitextForTransclusion();
987 if ( $message === false ||
$message === null ) {
990 __METHOD__
. ": message content doesn't provide wikitext "
991 . "(content model: " . $content->getModel() . ")"
994 $message = false; // negative caching
996 $this->mCache
[$code][$title] = ' ' . $message;
997 $this->wanCache
->set( $titleKey, ' ' . $message, $this->mExpiry
);
1001 $message = false; // negative caching
1004 if ( $message === false ) { // negative caching
1005 $this->mCache
[$code][$title] = '!NONEXISTENT';
1006 $this->wanCache
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
1013 * @param string $message
1014 * @param bool $interface
1015 * @param string $language Language code
1016 * @param Title $title
1019 function transform( $message, $interface = false, $language = null, $title = null ) {
1020 // Avoid creating parser if nothing to transform
1021 if ( strpos( $message, '{{' ) === false ) {
1025 if ( $this->mInParser
) {
1029 $parser = $this->getParser();
1031 $popts = $this->getParserOptions();
1032 $popts->setInterfaceMessage( $interface );
1033 $popts->setTargetLanguage( $language );
1035 $userlang = $popts->setUserLang( $language );
1036 $this->mInParser
= true;
1037 $message = $parser->transformMsg( $message, $popts, $title );
1038 $this->mInParser
= false;
1039 $popts->setUserLang( $userlang );
1048 function getParser() {
1049 global $wgParser, $wgParserConf;
1050 if ( !$this->mParser
&& isset( $wgParser ) ) {
1051 # Do some initialisation so that we don't have to do it twice
1052 $wgParser->firstCallInit();
1053 # Clone it and store it
1054 $class = $wgParserConf['class'];
1055 if ( $class == 'ParserDiffTest' ) {
1057 $this->mParser
= new $class( $wgParserConf );
1059 $this->mParser
= clone $wgParser;
1063 return $this->mParser
;
1067 * @param string $text
1068 * @param Title $title
1069 * @param bool $linestart Whether or not this is at the start of a line
1070 * @param bool $interface Whether this is an interface message
1071 * @param Language|string $language Language code
1072 * @return ParserOutput|string
1074 public function parse( $text, $title = null, $linestart = true,
1075 $interface = false, $language = null
1077 if ( $this->mInParser
) {
1078 return htmlspecialchars( $text );
1081 $parser = $this->getParser();
1082 $popts = $this->getParserOptions();
1083 $popts->setInterfaceMessage( $interface );
1085 if ( is_string( $language ) ) {
1086 $language = Language
::factory( $language );
1088 $popts->setTargetLanguage( $language );
1090 if ( !$title ||
!$title instanceof Title
) {
1092 wfDebugLog( 'GlobalTitleFail', __METHOD__
. ' called by ' .
1093 wfGetAllCallers( 6 ) . ' with no title set.' );
1096 // Sometimes $wgTitle isn't set either...
1098 # It's not uncommon having a null $wgTitle in scripts. See r80898
1099 # Create a ghost title in such case
1100 $title = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/title not set in ' . __METHOD__
);
1103 $this->mInParser
= true;
1104 $res = $parser->parse( $text, $title, $popts, $linestart );
1105 $this->mInParser
= false;
1110 function disable() {
1111 $this->mDisable
= true;
1115 $this->mDisable
= false;
1119 * Whether DB/cache usage is disabled for determining messages
1121 * If so, this typically indicates either:
1122 * - a) load() failed to find a cached copy nor query the DB
1123 * - b) we are in a special context or error mode that cannot use the DB
1124 * If the DB is ignored, any derived HTML output or cached objects may be wrong.
1125 * To avoid long-term cache pollution, TTLs can be adjusted accordingly.
1130 public function isDisabled() {
1131 return $this->mDisable
;
1135 * Clear all stored messages. Mainly used after a mass rebuild.
1138 $langs = Language
::fetchLanguageNames( null, 'mw' );
1139 foreach ( array_keys( $langs ) as $code ) {
1140 # Global and local caches
1141 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
1144 $this->mLoadedLanguages
= [];
1148 * @param string $key
1151 public function figureMessage( $key ) {
1152 global $wgLanguageCode;
1154 $pieces = explode( '/', $key );
1155 if ( count( $pieces ) < 2 ) {
1156 return [ $key, $wgLanguageCode ];
1159 $lang = array_pop( $pieces );
1160 if ( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
1161 return [ $key, $wgLanguageCode ];
1164 $message = implode( '/', $pieces );
1166 return [ $message, $lang ];
1170 * Get all message keys stored in the message cache for a given language.
1171 * If $code is the content language code, this will return all message keys
1172 * for which MediaWiki:msgkey exists. If $code is another language code, this
1173 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1174 * @param string $code Language code
1175 * @return array Array of message keys (strings)
1177 public function getAllMessageKeys( $code ) {
1179 $this->load( $code );
1180 if ( !isset( $this->mCache
[$code] ) ) {
1181 // Apparently load() failed
1184 // Remove administrative keys
1185 $cache = $this->mCache
[$code];
1186 unset( $cache['VERSION'] );
1187 unset( $cache['EXPIRY'] );
1188 // Remove any !NONEXISTENT keys
1189 $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1191 // Keys may appear with a capital first letter. lcfirst them.
1192 return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );