Merge "Revert "Accept BCP 47 codes as aliases for nonstandard variants""
[lhc/web/wiklou.git] / includes / cache / MessageCache.php
1 <?php
2 /**
3 * Localisation messages cache.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23 use MediaWiki\MediaWikiServices;
24 use Wikimedia\ScopedCallback;
25 use MediaWiki\Logger\LoggerFactory;
26 use Wikimedia\Rdbms\Database;
27
28 /**
29 * MediaWiki message cache structure version.
30 * Bump this whenever the message cache format has changed.
31 */
32 define( 'MSG_CACHE_VERSION', 2 );
33
34 /**
35 * Cache of messages that are defined by MediaWiki namespace pages or by hooks
36 *
37 * Performs various MediaWiki namespace-related functions
38 * @ingroup Cache
39 */
40 class MessageCache {
41 const FOR_UPDATE = 1; // force message reload
42
43 /** How long to wait for memcached locks */
44 const WAIT_SEC = 15;
45 /** How long memcached locks last */
46 const LOCK_TTL = 30;
47
48 /**
49 * Process cache of loaded messages that are defined in MediaWiki namespace
50 *
51 * @var MapCacheLRU Map of (language code => key => " <MESSAGE>" or "!TOO BIG")
52 */
53 protected $cache;
54
55 /**
56 * @var bool[] Map of (language code => boolean)
57 */
58 protected $cacheVolatile = [];
59
60 /**
61 * Should mean that database cannot be used, but check
62 * @var bool $mDisable
63 */
64 protected $mDisable;
65
66 /**
67 * Lifetime for cache, used by object caching.
68 * Set on construction, see __construct().
69 */
70 protected $mExpiry;
71
72 /**
73 * Message cache has its own parser which it uses to transform messages
74 * @var ParserOptions
75 */
76 protected $mParserOptions;
77 /** @var Parser */
78 protected $mParser;
79
80 /**
81 * @var bool $mInParser
82 */
83 protected $mInParser = false;
84
85 /** @var WANObjectCache */
86 protected $wanCache;
87 /** @var BagOStuff */
88 protected $clusterCache;
89 /** @var BagOStuff */
90 protected $srvCache;
91
92 /**
93 * Singleton instance
94 *
95 * @var MessageCache $instance
96 */
97 private static $instance;
98
99 /**
100 * Get the signleton instance of this class
101 *
102 * @since 1.18
103 * @return MessageCache
104 */
105 public static function singleton() {
106 if ( self::$instance === null ) {
107 global $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgUseLocalMessageCache;
108 self::$instance = new self(
109 MediaWikiServices::getInstance()->getMainWANObjectCache(),
110 wfGetMessageCacheStorage(),
111 $wgUseLocalMessageCache
112 ? MediaWikiServices::getInstance()->getLocalServerObjectCache()
113 : new EmptyBagOStuff(),
114 $wgUseDatabaseMessages,
115 $wgMsgCacheExpiry
116 );
117 }
118
119 return self::$instance;
120 }
121
122 /**
123 * Destroy the singleton instance
124 *
125 * @since 1.18
126 */
127 public static function destroyInstance() {
128 self::$instance = null;
129 }
130
131 /**
132 * Normalize message key input
133 *
134 * @param string $key Input message key to be normalized
135 * @return string Normalized message key
136 */
137 public static function normalizeKey( $key ) {
138 global $wgContLang;
139
140 $lckey = strtr( $key, ' ', '_' );
141 if ( ord( $lckey ) < 128 ) {
142 $lckey[0] = strtolower( $lckey[0] );
143 } else {
144 $lckey = $wgContLang->lcfirst( $lckey );
145 }
146
147 return $lckey;
148 }
149
150 /**
151 * @param WANObjectCache $wanCache
152 * @param BagOStuff $clusterCache
153 * @param BagOStuff $serverCache
154 * @param bool $useDB Whether to look for message overrides (e.g. MediaWiki: pages)
155 * @param int $expiry Lifetime for cache. @see $mExpiry.
156 */
157 public function __construct(
158 WANObjectCache $wanCache,
159 BagOStuff $clusterCache,
160 BagOStuff $serverCache,
161 $useDB,
162 $expiry
163 ) {
164 $this->wanCache = $wanCache;
165 $this->clusterCache = $clusterCache;
166 $this->srvCache = $serverCache;
167
168 $this->cache = new MapCacheLRU( 5 ); // limit size for sanity
169
170 $this->mDisable = !$useDB;
171 $this->mExpiry = $expiry;
172 }
173
174 /**
175 * ParserOptions is lazy initialised.
176 *
177 * @return ParserOptions
178 */
179 function getParserOptions() {
180 global $wgUser;
181
182 if ( !$this->mParserOptions ) {
183 if ( !$wgUser->isSafeToLoad() ) {
184 // $wgUser isn't unstubbable yet, so don't try to get a
185 // ParserOptions for it. And don't cache this ParserOptions
186 // either.
187 $po = ParserOptions::newFromAnon();
188 $po->setAllowUnsafeRawHtml( false );
189 return $po;
190 }
191
192 $this->mParserOptions = new ParserOptions;
193 // Messages may take parameters that could come
194 // from malicious sources. As a precaution, disable
195 // the <html> parser tag when parsing messages.
196 $this->mParserOptions->setAllowUnsafeRawHtml( false );
197 }
198
199 return $this->mParserOptions;
200 }
201
202 /**
203 * Try to load the cache from APC.
204 *
205 * @param string $code Optional language code, see documenation of load().
206 * @return array|bool The cache array, or false if not in cache.
207 */
208 protected function getLocalCache( $code ) {
209 $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
210
211 return $this->srvCache->get( $cacheKey );
212 }
213
214 /**
215 * Save the cache to APC.
216 *
217 * @param string $code
218 * @param array $cache The cache array
219 */
220 protected function saveToLocalCache( $code, $cache ) {
221 $cacheKey = $this->srvCache->makeKey( __CLASS__, $code );
222 $this->srvCache->set( $cacheKey, $cache );
223 }
224
225 /**
226 * Loads messages from caches or from database in this order:
227 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
228 * (2) memcached
229 * (3) from the database.
230 *
231 * When succesfully loading from (2) or (3), all higher level caches are
232 * updated for the newest version.
233 *
234 * Nothing is loaded if member variable mDisable is true, either manually
235 * set by calling code or if message loading fails (is this possible?).
236 *
237 * Returns true if cache is already populated or it was succesfully populated,
238 * or false if populating empty cache fails. Also returns true if MessageCache
239 * is disabled.
240 *
241 * @param string $code Language to which load messages
242 * @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
243 * @throws InvalidArgumentException
244 * @return bool
245 */
246 protected function load( $code, $mode = null ) {
247 if ( !is_string( $code ) ) {
248 throw new InvalidArgumentException( "Missing language code" );
249 }
250
251 # Don't do double loading...
252 if ( $this->cache->has( $code ) && $mode != self::FOR_UPDATE ) {
253 return true;
254 }
255
256 # 8 lines of code just to say (once) that message cache is disabled
257 if ( $this->mDisable ) {
258 static $shownDisabled = false;
259 if ( !$shownDisabled ) {
260 wfDebug( __METHOD__ . ": disabled\n" );
261 $shownDisabled = true;
262 }
263
264 return true;
265 }
266
267 # Loading code starts
268 $success = false; # Keep track of success
269 $staleCache = false; # a cache array with expired data, or false if none has been loaded
270 $where = []; # Debug info, delayed to avoid spamming debug log too much
271
272 # Hash of the contents is stored in memcache, to detect if data-center cache
273 # or local cache goes out of date (e.g. due to replace() on some other server)
274 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
275 $this->cacheVolatile[$code] = $hashVolatile;
276
277 # Try the local cache and check against the cluster hash key...
278 $cache = $this->getLocalCache( $code );
279 if ( !$cache ) {
280 $where[] = 'local cache is empty';
281 } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
282 $where[] = 'local cache has the wrong hash';
283 $staleCache = $cache;
284 } elseif ( $this->isCacheExpired( $cache ) ) {
285 $where[] = 'local cache is expired';
286 $staleCache = $cache;
287 } elseif ( $hashVolatile ) {
288 $where[] = 'local cache validation key is expired/volatile';
289 $staleCache = $cache;
290 } else {
291 $where[] = 'got from local cache';
292 $this->cache->set( $code, $cache );
293 $success = true;
294 }
295
296 if ( !$success ) {
297 $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
298 # Try the global cache. If it is empty, try to acquire a lock. If
299 # the lock can't be acquired, wait for the other thread to finish
300 # and then try the global cache a second time.
301 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
302 if ( $hashVolatile && $staleCache ) {
303 # Do not bother fetching the whole cache blob to avoid I/O.
304 # Instead, just try to get the non-blocking $statusKey lock
305 # below, and use the local stale value if it was not acquired.
306 $where[] = 'global cache is presumed expired';
307 } else {
308 $cache = $this->clusterCache->get( $cacheKey );
309 if ( !$cache ) {
310 $where[] = 'global cache is empty';
311 } elseif ( $this->isCacheExpired( $cache ) ) {
312 $where[] = 'global cache is expired';
313 $staleCache = $cache;
314 } elseif ( $hashVolatile ) {
315 # DB results are replica DB lag prone until the holdoff TTL passes.
316 # By then, updates should be reflected in loadFromDBWithLock().
317 # One thread renerates the cache while others use old values.
318 $where[] = 'global cache is expired/volatile';
319 $staleCache = $cache;
320 } else {
321 $where[] = 'got from global cache';
322 $this->cache->set( $code, $cache );
323 $this->saveToCaches( $cache, 'local-only', $code );
324 $success = true;
325 }
326 }
327
328 if ( $success ) {
329 # Done, no need to retry
330 break;
331 }
332
333 # We need to call loadFromDB. Limit the concurrency to one process.
334 # This prevents the site from going down when the cache expires.
335 # Note that the DB slam protection lock here is non-blocking.
336 $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
337 if ( $loadStatus === true ) {
338 $success = true;
339 break;
340 } elseif ( $staleCache ) {
341 # Use the stale cache while some other thread constructs the new one
342 $where[] = 'using stale cache';
343 $this->cache->set( $code, $staleCache );
344 $success = true;
345 break;
346 } elseif ( $failedAttempts > 0 ) {
347 # Already blocked once, so avoid another lock/unlock cycle.
348 # This case will typically be hit if memcached is down, or if
349 # loadFromDB() takes longer than LOCK_WAIT.
350 $where[] = "could not acquire status key.";
351 break;
352 } elseif ( $loadStatus === 'cantacquire' ) {
353 # Wait for the other thread to finish, then retry. Normally,
354 # the memcached get() will then yeild the other thread's result.
355 $where[] = 'waited for other thread to complete';
356 $this->getReentrantScopedLock( $cacheKey );
357 } else {
358 # Disable cache; $loadStatus is 'disabled'
359 break;
360 }
361 }
362 }
363
364 if ( !$success ) {
365 $where[] = 'loading FAILED - cache is disabled';
366 $this->mDisable = true;
367 $this->cache->set( $code, null );
368 wfDebugLog( 'MessageCacheError', __METHOD__ . ": Failed to load $code\n" );
369 # This used to throw an exception, but that led to nasty side effects like
370 # the whole wiki being instantly down if the memcached server died
371 }
372
373 if ( !$this->cache->has( $code ) ) { // sanity
374 throw new LogicException( "Process cache for '$code' should be set by now." );
375 }
376
377 $info = implode( ', ', $where );
378 wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
379
380 return $success;
381 }
382
383 /**
384 * @param string $code
385 * @param array &$where List of wfDebug() comments
386 * @param int|null $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
387 * @return bool|string True on success or one of ("cantacquire", "disabled")
388 */
389 protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
390 # If cache updates on all levels fail, give up on message overrides.
391 # This is to avoid easy site outages; see $saveSuccess comments below.
392 $statusKey = $this->clusterCache->makeKey( 'messages', $code, 'status' );
393 $status = $this->clusterCache->get( $statusKey );
394 if ( $status === 'error' ) {
395 $where[] = "could not load; method is still globally disabled";
396 return 'disabled';
397 }
398
399 # Now let's regenerate
400 $where[] = 'loading from database';
401
402 # Lock the cache to prevent conflicting writes.
403 # This lock is non-blocking so stale cache can quickly be used.
404 # Note that load() will call a blocking getReentrantScopedLock()
405 # after this if it really need to wait for any current thread.
406 $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
407 $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
408 if ( !$scopedLock ) {
409 $where[] = 'could not acquire main lock';
410 return 'cantacquire';
411 }
412
413 $cache = $this->loadFromDB( $code, $mode );
414 $this->cache->set( $code, $cache );
415 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
416
417 if ( !$saveSuccess ) {
418 /**
419 * Cache save has failed.
420 *
421 * There are two main scenarios where this could be a problem:
422 * - The cache is more than the maximum size (typically 1MB compressed).
423 * - Memcached has no space remaining in the relevant slab class. This is
424 * unlikely with recent versions of memcached.
425 *
426 * Either way, if there is a local cache, nothing bad will happen. If there
427 * is no local cache, disabling the message cache for all requests avoids
428 * incurring a loadFromDB() overhead on every request, and thus saves the
429 * wiki from complete downtime under moderate traffic conditions.
430 */
431 if ( $this->srvCache instanceof EmptyBagOStuff ) {
432 $this->clusterCache->set( $statusKey, 'error', 60 * 5 );
433 $where[] = 'could not save cache, disabled globally for 5 minutes';
434 } else {
435 $where[] = "could not save global cache";
436 }
437 }
438
439 return true;
440 }
441
442 /**
443 * Loads cacheable messages from the database. Messages bigger than
444 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
445 * on-demand from the database later.
446 *
447 * @param string $code Language code
448 * @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache
449 * @return array Loaded messages for storing in caches
450 */
451 protected function loadFromDB( $code, $mode = null ) {
452 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
453
454 // (T164666) The query here performs really poorly on WMF's
455 // contributions replicas. We don't have a way to say "any group except
456 // contributions", so for the moment let's specify 'api'.
457 // @todo: Get rid of this hack.
458 $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_REPLICA, 'api' );
459
460 $cache = [];
461
462 # Common conditions
463 $conds = [
464 'page_is_redirect' => 0,
465 'page_namespace' => NS_MEDIAWIKI,
466 ];
467
468 $mostused = [];
469 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
470 if ( !$this->cache->has( $wgLanguageCode ) ) {
471 $this->load( $wgLanguageCode );
472 }
473 $mostused = array_keys( $this->cache->get( $wgLanguageCode ) );
474 foreach ( $mostused as $key => $value ) {
475 $mostused[$key] = "$value/$code";
476 }
477 }
478
479 if ( count( $mostused ) ) {
480 $conds['page_title'] = $mostused;
481 } elseif ( $code !== $wgLanguageCode ) {
482 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
483 } else {
484 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
485 # other than language code.
486 $conds[] = 'page_title NOT' .
487 $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
488 }
489
490 # Conditions to fetch oversized pages to ignore them
491 $bigConds = $conds;
492 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
493
494 # Load titles for all oversized pages in the MediaWiki namespace
495 $res = $dbr->select(
496 'page',
497 [ 'page_title', 'page_latest' ],
498 $bigConds,
499 __METHOD__ . "($code)-big"
500 );
501 foreach ( $res as $row ) {
502 $cache[$row->page_title] = '!TOO BIG';
503 // At least include revision ID so page changes are reflected in the hash
504 $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
505 }
506
507 # Conditions to load the remaining pages with their contents
508 $smallConds = $conds;
509 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
510
511 $res = $dbr->select(
512 [ 'page', 'revision', 'text' ],
513 [ 'page_title', 'old_id', 'old_text', 'old_flags' ],
514 $smallConds,
515 __METHOD__ . "($code)-small",
516 [],
517 [
518 'revision' => [ 'JOIN', 'page_latest=rev_id' ],
519 'text' => [ 'JOIN', 'rev_text_id=old_id' ],
520 ]
521 );
522
523 foreach ( $res as $row ) {
524 $text = Revision::getRevisionText( $row );
525 if ( $text === false ) {
526 // Failed to fetch data; possible ES errors?
527 // Store a marker to fetch on-demand as a workaround...
528 // TODO Use a differnt marker
529 $entry = '!TOO BIG';
530 wfDebugLog(
531 'MessageCache',
532 __METHOD__
533 . ": failed to load message page text for {$row->page_title} ($code)"
534 );
535 } else {
536 $entry = ' ' . $text;
537 }
538 $cache[$row->page_title] = $entry;
539 }
540
541 $cache['VERSION'] = MSG_CACHE_VERSION;
542 ksort( $cache );
543
544 # Hash for validating local cache (APC). No need to take into account
545 # messages larger than $wgMaxMsgCacheEntrySize, since those are only
546 # stored and fetched from memcache.
547 $cache['HASH'] = md5( serialize( $cache ) );
548 $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
549
550 return $cache;
551 }
552
553 /**
554 * Updates cache as necessary when message page is changed
555 *
556 * @param string $title Message cache key with initial uppercase letter
557 * @param string|bool $text New contents of the page (false if deleted)
558 */
559 public function replace( $title, $text ) {
560 global $wgLanguageCode;
561
562 if ( $this->mDisable ) {
563 return;
564 }
565
566 list( $msg, $code ) = $this->figureMessage( $title );
567 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
568 // Content language overrides do not use the /<code> suffix
569 return;
570 }
571
572 // (a) Update the process cache with the new message text
573 if ( $text === false ) {
574 // Page deleted
575 $this->cache->setField( $code, $title, '!NONEXISTENT' );
576 } else {
577 // Ignore $wgMaxMsgCacheEntrySize so the process cache is up to date
578 $this->cache->setField( $code, $title, ' ' . $text );
579 }
580
581 // (b) Update the shared caches in a deferred update with a fresh DB snapshot
582 DeferredUpdates::addCallableUpdate(
583 function () use ( $title, $msg, $code ) {
584 global $wgContLang, $wgMaxMsgCacheEntrySize;
585 // Allow one caller at a time to avoid race conditions
586 $scopedLock = $this->getReentrantScopedLock(
587 $this->clusterCache->makeKey( 'messages', $code )
588 );
589 if ( !$scopedLock ) {
590 LoggerFactory::getInstance( 'MessageCache' )->error(
591 __METHOD__ . ': could not acquire lock to update {title} ({code})',
592 [ 'title' => $title, 'code' => $code ] );
593 return;
594 }
595 // Load the messages from the master DB to avoid race conditions
596 $cache = $this->loadFromDB( $code, self::FOR_UPDATE );
597 // Check if an individual cache key should exist and update cache accordingly
598 $page = WikiPage::factory( Title::makeTitle( NS_MEDIAWIKI, $title ) );
599 $page->loadPageData( $page::READ_LATEST );
600 $text = $this->getMessageTextFromContent( $page->getContent() );
601 if ( is_string( $text ) && strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
602 $this->wanCache->set(
603 $this->bigMessageCacheKey( $cache['HASH'], $title ),
604 ' ' . $text,
605 $this->mExpiry
606 );
607 }
608 // Mark this cache as definitely being "latest" (non-volatile) so
609 // load() calls do not try to refresh the cache with replica DB data
610 $cache['LATEST'] = time();
611 // Update the process cache
612 $this->cache->set( $code, $cache );
613 // Pre-emptively update the local datacenter cache so things like edit filter and
614 // blacklist changes are reflected immediately; these often use MediaWiki: pages.
615 // The datacenter handling replace() calls should be the same one handling edits
616 // as they require HTTP POST.
617 $this->saveToCaches( $cache, 'all', $code );
618 // Release the lock now that the cache is saved
619 ScopedCallback::consume( $scopedLock );
620
621 // Relay the purge. Touching this check key expires cache contents
622 // and local cache (APC) validation hash across all datacenters.
623 $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
624
625 // Purge the message in the message blob store
626 $resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
627 $blobStore = $resourceloader->getMessageBlobStore();
628 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
629
630 Hooks::run( 'MessageCacheReplace', [ $title, $text ] );
631 },
632 DeferredUpdates::PRESEND
633 );
634 }
635
636 /**
637 * Is the given cache array expired due to time passing or a version change?
638 *
639 * @param array $cache
640 * @return bool
641 */
642 protected function isCacheExpired( $cache ) {
643 if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
644 return true;
645 }
646 if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
647 return true;
648 }
649 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
650 return true;
651 }
652
653 return false;
654 }
655
656 /**
657 * Shortcut to update caches.
658 *
659 * @param array $cache Cached messages with a version.
660 * @param string $dest Either "local-only" to save to local caches only
661 * or "all" to save to all caches.
662 * @param string|bool $code Language code (default: false)
663 * @return bool
664 */
665 protected function saveToCaches( array $cache, $dest, $code = false ) {
666 if ( $dest === 'all' ) {
667 $cacheKey = $this->clusterCache->makeKey( 'messages', $code );
668 $success = $this->clusterCache->set( $cacheKey, $cache );
669 $this->setValidationHash( $code, $cache );
670 } else {
671 $success = true;
672 }
673
674 $this->saveToLocalCache( $code, $cache );
675
676 return $success;
677 }
678
679 /**
680 * Get the md5 used to validate the local APC cache
681 *
682 * @param string $code
683 * @return array (hash or false, bool expiry/volatility status)
684 */
685 protected function getValidationHash( $code ) {
686 $curTTL = null;
687 $value = $this->wanCache->get(
688 $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
689 $curTTL,
690 [ $this->getCheckKey( $code ) ]
691 );
692
693 if ( $value ) {
694 $hash = $value['hash'];
695 if ( ( time() - $value['latest'] ) < WANObjectCache::TTL_MINUTE ) {
696 // Cache was recently updated via replace() and should be up-to-date.
697 // That method is only called in the primary datacenter and uses FOR_UPDATE.
698 // Also, it is unlikely that the current datacenter is *now* secondary one.
699 $expired = false;
700 } else {
701 // See if the "check" key was bumped after the hash was generated
702 $expired = ( $curTTL < 0 );
703 }
704 } else {
705 // No hash found at all; cache must regenerate to be safe
706 $hash = false;
707 $expired = true;
708 }
709
710 return [ $hash, $expired ];
711 }
712
713 /**
714 * Set the md5 used to validate the local disk cache
715 *
716 * If $cache has a 'LATEST' UNIX timestamp key, then the hash will not
717 * be treated as "volatile" by getValidationHash() for the next few seconds.
718 * This is triggered when $cache is generated using FOR_UPDATE mode.
719 *
720 * @param string $code
721 * @param array $cache Cached messages with a version
722 */
723 protected function setValidationHash( $code, array $cache ) {
724 $this->wanCache->set(
725 $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
726 [
727 'hash' => $cache['HASH'],
728 'latest' => $cache['LATEST'] ?? 0
729 ],
730 WANObjectCache::TTL_INDEFINITE
731 );
732 }
733
734 /**
735 * @param string $key A language message cache key that stores blobs
736 * @param int $timeout Wait timeout in seconds
737 * @return null|ScopedCallback
738 */
739 protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
740 return $this->clusterCache->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
741 }
742
743 /**
744 * Get a message from either the content language or the user language.
745 *
746 * First, assemble a list of languages to attempt getting the message from. This
747 * chain begins with the requested language and its fallbacks and then continues with
748 * the content language and its fallbacks. For each language in the chain, the following
749 * process will occur (in this order):
750 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
751 * Note: for the content language, there is no /lang subpage.
752 * 2. Fetch from the static CDB cache.
753 * 3. If available, check the database for fallback language overrides.
754 *
755 * This process provides a number of guarantees. When changing this code, make sure all
756 * of these guarantees are preserved.
757 * * If the requested language is *not* the content language, then the CDB cache for that
758 * specific language will take precedence over the root database page ([[MW:msg]]).
759 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
760 * the message is available *anywhere* in the language for which it is a fallback.
761 *
762 * @param string $key The message key
763 * @param bool $useDB If true, look for the message in the DB, false
764 * to use only the compiled l10n cache.
765 * @param bool|string|object $langcode Code of the language to get the message for.
766 * - If string and a valid code, will create a standard language object
767 * - If string but not a valid code, will create a basic language object
768 * - If boolean and false, create object from the current users language
769 * - If boolean and true, create object from the wikis content language
770 * - If language object, use it as given
771 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
772 *
773 * @throws MWException When given an invalid key
774 * @return string|bool False if the message doesn't exist, otherwise the
775 * message (which can be empty)
776 */
777 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
778 if ( is_int( $key ) ) {
779 // Fix numerical strings that somehow become ints
780 // on their way here
781 $key = (string)$key;
782 } elseif ( !is_string( $key ) ) {
783 throw new MWException( 'Non-string key given' );
784 } elseif ( $key === '' ) {
785 // Shortcut: the empty key is always missing
786 return false;
787 }
788
789 // For full keys, get the language code from the key
790 $pos = strrpos( $key, '/' );
791 if ( $isFullKey && $pos !== false ) {
792 $langcode = substr( $key, $pos + 1 );
793 $key = substr( $key, 0, $pos );
794 }
795
796 // Normalise title-case input (with some inlining)
797 $lckey = self::normalizeKey( $key );
798
799 Hooks::run( 'MessageCache::get', [ &$lckey ] );
800
801 // Loop through each language in the fallback list until we find something useful
802 $lang = wfGetLangObj( $langcode );
803 $message = $this->getMessageFromFallbackChain(
804 $lang,
805 $lckey,
806 !$this->mDisable && $useDB
807 );
808
809 // If we still have no message, maybe the key was in fact a full key so try that
810 if ( $message === false ) {
811 $parts = explode( '/', $lckey );
812 // We may get calls for things that are http-urls from sidebar
813 // Let's not load nonexistent languages for those
814 // They usually have more than one slash.
815 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
816 $message = Language::getMessageFor( $parts[0], $parts[1] );
817 if ( $message === null ) {
818 $message = false;
819 }
820 }
821 }
822
823 // Post-processing if the message exists
824 if ( $message !== false ) {
825 // Fix whitespace
826 $message = str_replace(
827 [
828 # Fix for trailing whitespace, removed by textarea
829 '&#32;',
830 # Fix for NBSP, converted to space by firefox
831 '&nbsp;',
832 '&#160;',
833 '&shy;'
834 ],
835 [
836 ' ',
837 "\u{00A0}",
838 "\u{00A0}",
839 "\u{00AD}"
840 ],
841 $message
842 );
843 }
844
845 return $message;
846 }
847
848 /**
849 * Given a language, try and fetch messages from that language.
850 *
851 * Will also consider fallbacks of that language, the site language, and fallbacks for
852 * the site language.
853 *
854 * @see MessageCache::get
855 * @param Language|StubObject $lang Preferred language
856 * @param string $lckey Lowercase key for the message (as for localisation cache)
857 * @param bool $useDB Whether to include messages from the wiki database
858 * @return string|bool The message, or false if not found
859 */
860 protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
861 global $wgContLang;
862
863 $alreadyTried = [];
864
865 // First try the requested language.
866 $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
867 if ( $message !== false ) {
868 return $message;
869 }
870
871 // Now try checking the site language.
872 $message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
873 return $message;
874 }
875
876 /**
877 * Given a language, try and fetch messages from that language and its fallbacks.
878 *
879 * @see MessageCache::get
880 * @param Language|StubObject $lang Preferred language
881 * @param string $lckey Lowercase key for the message (as for localisation cache)
882 * @param bool $useDB Whether to include messages from the wiki database
883 * @param bool[] $alreadyTried Contains true for each language that has been tried already
884 * @return string|bool The message, or false if not found
885 */
886 private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
887 global $wgContLang;
888
889 $langcode = $lang->getCode();
890
891 // Try checking the database for the requested language
892 if ( $useDB ) {
893 $uckey = $wgContLang->ucfirst( $lckey );
894
895 if ( !isset( $alreadyTried[$langcode] ) ) {
896 $message = $this->getMsgFromNamespace(
897 $this->getMessagePageName( $langcode, $uckey ),
898 $langcode
899 );
900
901 if ( $message !== false ) {
902 return $message;
903 }
904 $alreadyTried[$langcode] = true;
905 }
906 } else {
907 $uckey = null;
908 }
909
910 // Check the CDB cache
911 $message = $lang->getMessage( $lckey );
912 if ( $message !== null ) {
913 return $message;
914 }
915
916 // Try checking the database for all of the fallback languages
917 if ( $useDB ) {
918 $fallbackChain = Language::getFallbacksFor( $langcode );
919
920 foreach ( $fallbackChain as $code ) {
921 if ( isset( $alreadyTried[$code] ) ) {
922 continue;
923 }
924
925 $message = $this->getMsgFromNamespace(
926 $this->getMessagePageName( $code, $uckey ), $code );
927
928 if ( $message !== false ) {
929 return $message;
930 }
931 $alreadyTried[$code] = true;
932 }
933 }
934
935 return false;
936 }
937
938 /**
939 * Get the message page name for a given language
940 *
941 * @param string $langcode
942 * @param string $uckey Uppercase key for the message
943 * @return string The page name
944 */
945 private function getMessagePageName( $langcode, $uckey ) {
946 global $wgLanguageCode;
947
948 if ( $langcode === $wgLanguageCode ) {
949 // Messages created in the content language will not have the /lang extension
950 return $uckey;
951 } else {
952 return "$uckey/$langcode";
953 }
954 }
955
956 /**
957 * Get a message from the MediaWiki namespace, with caching. The key must
958 * first be converted to two-part lang/msg form if necessary.
959 *
960 * Unlike self::get(), this function doesn't resolve fallback chains, and
961 * some callers require this behavior. LanguageConverter::parseCachedTable()
962 * and self::get() are some examples in core.
963 *
964 * @param string $title Message cache key with initial uppercase letter
965 * @param string $code Code denoting the language to try
966 * @return string|bool The message, or false if it does not exist or on error
967 */
968 public function getMsgFromNamespace( $title, $code ) {
969 // Load all MediaWiki page definitions into cache. Note that individual keys
970 // already loaded into cache during this request remain in the cache, which
971 // includes the value of hook-defined messages.
972 $this->load( $code );
973
974 $entry = $this->cache->getField( $code, $title );
975 if ( $entry !== null ) {
976 if ( substr( $entry, 0, 1 ) === ' ' ) {
977 // The message exists and is not '!TOO BIG'
978 return (string)substr( $entry, 1 );
979 } elseif ( $entry === '!NONEXISTENT' ) {
980 return false;
981 }
982 // Fall through and try invididual message cache below
983 } else {
984 // Message does not have a MediaWiki page definition
985 $message = false;
986 Hooks::run( 'MessagesPreLoad', [ $title, &$message, $code ] );
987 if ( $message !== false ) {
988 $this->cache->setField( $code, $title, ' ' . $message );
989 } else {
990 $this->cache->setField( $code, $title, '!NONEXISTENT' );
991 }
992
993 return $message;
994 }
995
996 if ( $this->cacheVolatile[$code] ) {
997 $entry = false;
998 // Make sure that individual keys respect the WAN cache holdoff period too
999 LoggerFactory::getInstance( 'MessageCache' )->debug(
1000 __METHOD__ . ': loading volatile key \'{titleKey}\'',
1001 [ 'titleKey' => $title, 'code' => $code ] );
1002 } else {
1003 // Try the individual message cache
1004 $entry = $this->loadCachedMessagePageEntry(
1005 $title,
1006 $code,
1007 $this->cache->getField( $code, 'HASH' )
1008 );
1009 }
1010
1011 if ( $entry !== false && substr( $entry, 0, 1 ) === ' ' ) {
1012 $this->cache->setField( $code, $title, $entry );
1013 // The message exists, so make sure a string is returned
1014 return (string)substr( $entry, 1 );
1015 }
1016
1017 $this->cache->setField( $code, $title, '!NONEXISTENT' );
1018
1019 return false;
1020 }
1021
1022 /**
1023 * @param string $dbKey
1024 * @param string $code
1025 * @param string $hash
1026 * @return string Either " <MESSAGE>" or "!NONEXISTANT"
1027 */
1028 private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) {
1029 return $this->wanCache->getWithSetCallback(
1030 $this->bigMessageCacheKey( $hash, $dbKey ),
1031 $this->mExpiry,
1032 function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code ) {
1033 // Try loading the message from the database
1034 $dbr = wfGetDB( DB_REPLICA );
1035 $setOpts += Database::getCacheSetOptions( $dbr );
1036 // Use newKnownCurrent() to avoid querying revision/user tables
1037 $title = Title::makeTitle( NS_MEDIAWIKI, $dbKey );
1038 $revision = Revision::newKnownCurrent( $dbr, $title );
1039 if ( !$revision ) {
1040 return '!NONEXISTENT';
1041 }
1042 $content = $revision->getContent();
1043 if ( $content ) {
1044 $message = $this->getMessageTextFromContent( $content );
1045 } else {
1046 LoggerFactory::getInstance( 'MessageCache' )->warning(
1047 __METHOD__ . ': failed to load page text for \'{titleKey}\'',
1048 [ 'titleKey' => $dbKey, 'code' => $code ]
1049 );
1050 $message = null;
1051 }
1052
1053 if ( is_string( $message ) ) {
1054 return ' ' . $message;
1055 }
1056
1057 $ttl = 5; // possibly a temporary loading failure
1058
1059 return '!NONEXISTENT';
1060 }
1061 );
1062 }
1063
1064 /**
1065 * @param string $message
1066 * @param bool $interface
1067 * @param Language|null $language
1068 * @param Title|null $title
1069 * @return string
1070 */
1071 public function transform( $message, $interface = false, $language = null, $title = null ) {
1072 // Avoid creating parser if nothing to transform
1073 if ( strpos( $message, '{{' ) === false ) {
1074 return $message;
1075 }
1076
1077 if ( $this->mInParser ) {
1078 return $message;
1079 }
1080
1081 $parser = $this->getParser();
1082 if ( $parser ) {
1083 $popts = $this->getParserOptions();
1084 $popts->setInterfaceMessage( $interface );
1085 $popts->setTargetLanguage( $language );
1086
1087 $userlang = $popts->setUserLang( $language );
1088 $this->mInParser = true;
1089 $message = $parser->transformMsg( $message, $popts, $title );
1090 $this->mInParser = false;
1091 $popts->setUserLang( $userlang );
1092 }
1093
1094 return $message;
1095 }
1096
1097 /**
1098 * @return Parser
1099 */
1100 public function getParser() {
1101 global $wgParser, $wgParserConf;
1102
1103 if ( !$this->mParser && isset( $wgParser ) ) {
1104 # Do some initialisation so that we don't have to do it twice
1105 $wgParser->firstCallInit();
1106 # Clone it and store it
1107 $class = $wgParserConf['class'];
1108 if ( $class == ParserDiffTest::class ) {
1109 # Uncloneable
1110 $this->mParser = new $class( $wgParserConf );
1111 } else {
1112 $this->mParser = clone $wgParser;
1113 }
1114 }
1115
1116 return $this->mParser;
1117 }
1118
1119 /**
1120 * @param string $text
1121 * @param Title|null $title
1122 * @param bool $linestart Whether or not this is at the start of a line
1123 * @param bool $interface Whether this is an interface message
1124 * @param Language|string|null $language Language code
1125 * @return ParserOutput|string
1126 */
1127 public function parse( $text, $title = null, $linestart = true,
1128 $interface = false, $language = null
1129 ) {
1130 global $wgTitle;
1131
1132 if ( $this->mInParser ) {
1133 return htmlspecialchars( $text );
1134 }
1135
1136 $parser = $this->getParser();
1137 $popts = $this->getParserOptions();
1138 $popts->setInterfaceMessage( $interface );
1139
1140 if ( is_string( $language ) ) {
1141 $language = Language::factory( $language );
1142 }
1143 $popts->setTargetLanguage( $language );
1144
1145 if ( !$title || !$title instanceof Title ) {
1146 wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1147 wfGetAllCallers( 6 ) . ' with no title set.' );
1148 $title = $wgTitle;
1149 }
1150 // Sometimes $wgTitle isn't set either...
1151 if ( !$title ) {
1152 # It's not uncommon having a null $wgTitle in scripts. See r80898
1153 # Create a ghost title in such case
1154 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
1155 }
1156
1157 $this->mInParser = true;
1158 $res = $parser->parse( $text, $title, $popts, $linestart );
1159 $this->mInParser = false;
1160
1161 return $res;
1162 }
1163
1164 public function disable() {
1165 $this->mDisable = true;
1166 }
1167
1168 public function enable() {
1169 $this->mDisable = false;
1170 }
1171
1172 /**
1173 * Whether DB/cache usage is disabled for determining messages
1174 *
1175 * If so, this typically indicates either:
1176 * - a) load() failed to find a cached copy nor query the DB
1177 * - b) we are in a special context or error mode that cannot use the DB
1178 * If the DB is ignored, any derived HTML output or cached objects may be wrong.
1179 * To avoid long-term cache pollution, TTLs can be adjusted accordingly.
1180 *
1181 * @return bool
1182 * @since 1.27
1183 */
1184 public function isDisabled() {
1185 return $this->mDisable;
1186 }
1187
1188 /**
1189 * Clear all stored messages in global and local cache
1190 *
1191 * Mainly used after a mass rebuild
1192 */
1193 public function clear() {
1194 $langs = Language::fetchLanguageNames( null, 'mw' );
1195 foreach ( array_keys( $langs ) as $code ) {
1196 $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
1197 }
1198 $this->cache->clear();
1199 }
1200
1201 /**
1202 * @param string $key
1203 * @return array
1204 */
1205 public function figureMessage( $key ) {
1206 global $wgLanguageCode;
1207
1208 $pieces = explode( '/', $key );
1209 if ( count( $pieces ) < 2 ) {
1210 return [ $key, $wgLanguageCode ];
1211 }
1212
1213 $lang = array_pop( $pieces );
1214 if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1215 return [ $key, $wgLanguageCode ];
1216 }
1217
1218 $message = implode( '/', $pieces );
1219
1220 return [ $message, $lang ];
1221 }
1222
1223 /**
1224 * Get all message keys stored in the message cache for a given language.
1225 * If $code is the content language code, this will return all message keys
1226 * for which MediaWiki:msgkey exists. If $code is another language code, this
1227 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1228 * @param string $code Language code
1229 * @return array Array of message keys (strings)
1230 */
1231 public function getAllMessageKeys( $code ) {
1232 global $wgContLang;
1233
1234 $this->load( $code );
1235 if ( !$this->cache->has( $code ) ) {
1236 // Apparently load() failed
1237 return null;
1238 }
1239 // Remove administrative keys
1240 $cache = $this->cache->get( $code );
1241 unset( $cache['VERSION'] );
1242 unset( $cache['EXPIRY'] );
1243 unset( $cache['EXCESSIVE'] );
1244 // Remove any !NONEXISTENT keys
1245 $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1246
1247 // Keys may appear with a capital first letter. lcfirst them.
1248 return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
1249 }
1250
1251 /**
1252 * Purge message caches when a MediaWiki: page is created, updated, or deleted
1253 *
1254 * @param Title $title Message page title
1255 * @param Content|null $content New content for edit/create, null on deletion
1256 * @since 1.29
1257 */
1258 public function updateMessageOverride( Title $title, Content $content = null ) {
1259 global $wgContLang;
1260
1261 $msgText = $this->getMessageTextFromContent( $content );
1262 if ( $msgText === null ) {
1263 $msgText = false; // treat as not existing
1264 }
1265
1266 $this->replace( $title->getDBkey(), $msgText );
1267
1268 if ( $wgContLang->hasVariants() ) {
1269 $wgContLang->updateConversionTable( $title );
1270 }
1271 }
1272
1273 /**
1274 * @param string $code Language code
1275 * @return string WAN cache key usable as a "check key" against language page edits
1276 */
1277 public function getCheckKey( $code ) {
1278 return $this->wanCache->makeKey( 'messages', $code );
1279 }
1280
1281 /**
1282 * @param Content|null $content Content or null if the message page does not exist
1283 * @return string|bool|null Returns false if $content is null and null on error
1284 */
1285 private function getMessageTextFromContent( Content $content = null ) {
1286 // @TODO: could skip pseudo-messages like js/css here, based on content model
1287 if ( $content ) {
1288 // Message page exists...
1289 // XXX: Is this the right way to turn a Content object into a message?
1290 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1291 // CssContent. MessageContent is *not* used for storing messages, it's
1292 // only used for wrapping them when needed.
1293 $msgText = $content->getWikitextForTransclusion();
1294 if ( $msgText === false || $msgText === null ) {
1295 // This might be due to some kind of misconfiguration...
1296 $msgText = null;
1297 LoggerFactory::getInstance( 'MessageCache' )->warning(
1298 __METHOD__ . ": message content doesn't provide wikitext "
1299 . "(content model: " . $content->getModel() . ")" );
1300 }
1301 } else {
1302 // Message page does not exist...
1303 $msgText = false;
1304 }
1305
1306 return $msgText;
1307 }
1308
1309 /**
1310 * @param string $hash Hash for this version of the entire key/value overrides map
1311 * @param string $title Message cache key with initial uppercase letter
1312 * @return string
1313 */
1314 private function bigMessageCacheKey( $hash, $title ) {
1315 return $this->wanCache->makeKey( 'messages-big', $hash, $title );
1316 }
1317 }