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