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