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