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