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