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' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
489 }
490
491 # Conditions to fetch oversized pages to ignore them
492 $bigConds = $conds;
493 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
494
495 # Load titles for all oversized pages in the MediaWiki namespace
496 $res = $dbr->select(
497 'page',
498 [ 'page_title', 'page_latest' ],
499 $bigConds,
500 __METHOD__ . "($code)-big"
501 );
502 foreach ( $res as $row ) {
503 $cache[$row->page_title] = '!TOO BIG';
504 // At least include revision ID so page changes are reflected in the hash
505 $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
506 }
507
508 # Conditions to load the remaining pages with their contents
509 $smallConds = $conds;
510 $smallConds[] = 'page_latest=rev_id';
511 $smallConds[] = 'rev_text_id=old_id';
512 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
513
514 $res = $dbr->select(
515 [ 'page', 'revision', 'text' ],
516 [ 'page_title', 'old_id', 'old_text', 'old_flags' ],
517 $smallConds,
518 __METHOD__ . "($code)-small"
519 );
520
521 foreach ( $res as $row ) {
522 $text = Revision::getRevisionText( $row );
523 if ( $text === false ) {
524 // Failed to fetch data; possible ES errors?
525 // Store a marker to fetch on-demand as a workaround...
526 // TODO Use a differnt marker
527 $entry = '!TOO BIG';
528 wfDebugLog(
529 'MessageCache',
530 __METHOD__
531 . ": failed to load message page text for {$row->page_title} ($code)"
532 );
533 } else {
534 $entry = ' ' . $text;
535 }
536 $cache[$row->page_title] = $entry;
537 }
538
539 $cache['VERSION'] = MSG_CACHE_VERSION;
540 ksort( $cache );
541
542 # Hash for validating local cache (APC). No need to take into account
543 # messages larger than $wgMaxMsgCacheEntrySize, since those are only
544 # stored and fetched from memcache.
545 $cache['HASH'] = md5( serialize( $cache ) );
546 $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
547
548 return $cache;
549 }
550
551 /**
552 * Updates cache as necessary when message page is changed
553 *
554 * @param string $title Message cache key with initial uppercase letter.
555 * @param string|bool $text New contents of the page (false if deleted)
556 */
557 public function replace( $title, $text ) {
558 global $wgLanguageCode;
559
560 if ( $this->mDisable ) {
561 return;
562 }
563
564 list( $msg, $code ) = $this->figureMessage( $title );
565 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
566 // Content language overrides do not use the /<code> suffix
567 return;
568 }
569
570 // (a) Update the process cache with the new message text
571 if ( $text === false ) {
572 // Page deleted
573 $this->mCache[$code][$title] = '!NONEXISTENT';
574 } else {
575 // Ignore $wgMaxMsgCacheEntrySize so the process cache is up to date
576 $this->mCache[$code][$title] = ' ' . $text;
577 }
578
579 // (b) Update the shared caches in a deferred update with a fresh DB snapshot
580 DeferredUpdates::addCallableUpdate(
581 function () use ( $title, $msg, $code ) {
582 global $wgContLang, $wgMaxMsgCacheEntrySize;
583 // Allow one caller at a time to avoid race conditions
584 $scopedLock = $this->getReentrantScopedLock( wfMemcKey( 'messages', $code ) );
585 if ( !$scopedLock ) {
586 LoggerFactory::getInstance( 'MessageCache' )->error(
587 __METHOD__ . ': could not acquire lock to update {title} ({code})',
588 [ 'title' => $title, 'code' => $code ] );
589 return;
590 }
591 // Load the messages from the master DB to avoid race conditions
592 $cache = $this->loadFromDB( $code, self::FOR_UPDATE );
593 $this->mCache[$code] = $cache;
594 // Load the process cache values and set the per-title cache keys
595 $page = WikiPage::factory( Title::makeTitle( NS_MEDIAWIKI, $title ) );
596 $page->loadPageData( $page::READ_LATEST );
597 $text = $this->getMessageTextFromContent( $page->getContent() );
598 // Check if an individual cache key should exist and update cache accordingly
599 $titleKey = $this->wanCache->makeKey(
600 'messages-big', $this->mCache[$code]['HASH'], $title );
601 if ( is_string( $text ) && strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
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 // Try the individual message cache
999 $titleKey = $this->wanCache->makeKey( 'messages-big', $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 $entry = $this->wanCache->get( $titleKey );
1009 }
1010
1011 if ( $entry !== false ) {
1012 if ( substr( $entry, 0, 1 ) === ' ' ) {
1013 $this->mCache[$code][$title] = $entry;
1014 // The message exists, so make sure a string is returned
1015 return (string)substr( $entry, 1 );
1016 } elseif ( $entry === '!NONEXISTENT' ) {
1017 $this->mCache[$code][$title] = '!NONEXISTENT';
1018
1019 return false;
1020 } else {
1021 // Corrupt/obsolete entry, delete it
1022 $this->wanCache->delete( $titleKey );
1023 }
1024 }
1025
1026 // Try loading the message from the database
1027 $dbr = wfGetDB( DB_REPLICA );
1028 $cacheOpts = Database::getCacheSetOptions( $dbr );
1029 // Use newKnownCurrent() to avoid querying revision/user tables
1030 $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
1031 if ( $titleObj->getLatestRevID() ) {
1032 $revision = Revision::newKnownCurrent(
1033 $dbr,
1034 $titleObj->getArticleID(),
1035 $titleObj->getLatestRevID()
1036 );
1037 } else {
1038 $revision = false;
1039 }
1040
1041 if ( $revision ) {
1042 $content = $revision->getContent();
1043 if ( $content ) {
1044 $message = $this->getMessageTextFromContent( $content );
1045 if ( is_string( $message ) ) {
1046 $this->mCache[$code][$title] = ' ' . $message;
1047 $this->wanCache->set( $titleKey, ' ' . $message, $this->mExpiry, $cacheOpts );
1048 }
1049 } else {
1050 // A possibly temporary loading failure
1051 LoggerFactory::getInstance( 'MessageCache' )->warning(
1052 __METHOD__ . ': failed to load message page text for \'{titleKey}\'',
1053 [ 'titleKey' => $titleKey, 'code' => $code ] );
1054 $message = null; // no negative caching
1055 }
1056 } else {
1057 $message = false; // negative caching
1058 }
1059
1060 if ( $message === false ) { // negative caching
1061 $this->mCache[$code][$title] = '!NONEXISTENT';
1062 $this->wanCache->set( $titleKey, '!NONEXISTENT', $this->mExpiry, $cacheOpts );
1063 }
1064
1065 return $message;
1066 }
1067
1068 /**
1069 * @param string $message
1070 * @param bool $interface
1071 * @param string $language Language code
1072 * @param Title $title
1073 * @return string
1074 */
1075 function transform( $message, $interface = false, $language = null, $title = null ) {
1076 // Avoid creating parser if nothing to transform
1077 if ( strpos( $message, '{{' ) === false ) {
1078 return $message;
1079 }
1080
1081 if ( $this->mInParser ) {
1082 return $message;
1083 }
1084
1085 $parser = $this->getParser();
1086 if ( $parser ) {
1087 $popts = $this->getParserOptions();
1088 $popts->setInterfaceMessage( $interface );
1089 $popts->setTargetLanguage( $language );
1090
1091 $userlang = $popts->setUserLang( $language );
1092 $this->mInParser = true;
1093 $message = $parser->transformMsg( $message, $popts, $title );
1094 $this->mInParser = false;
1095 $popts->setUserLang( $userlang );
1096 }
1097
1098 return $message;
1099 }
1100
1101 /**
1102 * @return Parser
1103 */
1104 function getParser() {
1105 global $wgParser, $wgParserConf;
1106
1107 if ( !$this->mParser && isset( $wgParser ) ) {
1108 # Do some initialisation so that we don't have to do it twice
1109 $wgParser->firstCallInit();
1110 # Clone it and store it
1111 $class = $wgParserConf['class'];
1112 if ( $class == 'ParserDiffTest' ) {
1113 # Uncloneable
1114 $this->mParser = new $class( $wgParserConf );
1115 } else {
1116 $this->mParser = clone $wgParser;
1117 }
1118 }
1119
1120 return $this->mParser;
1121 }
1122
1123 /**
1124 * @param string $text
1125 * @param Title $title
1126 * @param bool $linestart Whether or not this is at the start of a line
1127 * @param bool $interface Whether this is an interface message
1128 * @param Language|string $language Language code
1129 * @return ParserOutput|string
1130 */
1131 public function parse( $text, $title = null, $linestart = true,
1132 $interface = false, $language = null
1133 ) {
1134 global $wgTitle;
1135
1136 if ( $this->mInParser ) {
1137 return htmlspecialchars( $text );
1138 }
1139
1140 $parser = $this->getParser();
1141 $popts = $this->getParserOptions();
1142 $popts->setInterfaceMessage( $interface );
1143
1144 if ( is_string( $language ) ) {
1145 $language = Language::factory( $language );
1146 }
1147 $popts->setTargetLanguage( $language );
1148
1149 if ( !$title || !$title instanceof Title ) {
1150 wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1151 wfGetAllCallers( 6 ) . ' with no title set.' );
1152 $title = $wgTitle;
1153 }
1154 // Sometimes $wgTitle isn't set either...
1155 if ( !$title ) {
1156 # It's not uncommon having a null $wgTitle in scripts. See r80898
1157 # Create a ghost title in such case
1158 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
1159 }
1160
1161 $this->mInParser = true;
1162 $res = $parser->parse( $text, $title, $popts, $linestart );
1163 $this->mInParser = false;
1164
1165 return $res;
1166 }
1167
1168 function disable() {
1169 $this->mDisable = true;
1170 }
1171
1172 function enable() {
1173 $this->mDisable = false;
1174 }
1175
1176 /**
1177 * Whether DB/cache usage is disabled for determining messages
1178 *
1179 * If so, this typically indicates either:
1180 * - a) load() failed to find a cached copy nor query the DB
1181 * - b) we are in a special context or error mode that cannot use the DB
1182 * If the DB is ignored, any derived HTML output or cached objects may be wrong.
1183 * To avoid long-term cache pollution, TTLs can be adjusted accordingly.
1184 *
1185 * @return bool
1186 * @since 1.27
1187 */
1188 public function isDisabled() {
1189 return $this->mDisable;
1190 }
1191
1192 /**
1193 * Clear all stored messages. Mainly used after a mass rebuild.
1194 */
1195 function clear() {
1196 $langs = Language::fetchLanguageNames( null, 'mw' );
1197 foreach ( array_keys( $langs ) as $code ) {
1198 # Global and local caches
1199 $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
1200 }
1201
1202 $this->mLoadedLanguages = [];
1203 }
1204
1205 /**
1206 * @param string $key
1207 * @return array
1208 */
1209 public function figureMessage( $key ) {
1210 global $wgLanguageCode;
1211
1212 $pieces = explode( '/', $key );
1213 if ( count( $pieces ) < 2 ) {
1214 return [ $key, $wgLanguageCode ];
1215 }
1216
1217 $lang = array_pop( $pieces );
1218 if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1219 return [ $key, $wgLanguageCode ];
1220 }
1221
1222 $message = implode( '/', $pieces );
1223
1224 return [ $message, $lang ];
1225 }
1226
1227 /**
1228 * Get all message keys stored in the message cache for a given language.
1229 * If $code is the content language code, this will return all message keys
1230 * for which MediaWiki:msgkey exists. If $code is another language code, this
1231 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1232 * @param string $code Language code
1233 * @return array Array of message keys (strings)
1234 */
1235 public function getAllMessageKeys( $code ) {
1236 global $wgContLang;
1237
1238 $this->load( $code );
1239 if ( !isset( $this->mCache[$code] ) ) {
1240 // Apparently load() failed
1241 return null;
1242 }
1243 // Remove administrative keys
1244 $cache = $this->mCache[$code];
1245 unset( $cache['VERSION'] );
1246 unset( $cache['EXPIRY'] );
1247 unset( $cache['EXCESSIVE'] );
1248 // Remove any !NONEXISTENT keys
1249 $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1250
1251 // Keys may appear with a capital first letter. lcfirst them.
1252 return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
1253 }
1254
1255 /**
1256 * Purge message caches when a MediaWiki: page is created, updated, or deleted
1257 *
1258 * @param Title $title Message page title
1259 * @param Content|null $content New content for edit/create, null on deletion
1260 * @since 1.29
1261 */
1262 public function updateMessageOverride( Title $title, Content $content = null ) {
1263 global $wgContLang;
1264
1265 $msgText = $this->getMessageTextFromContent( $content );
1266 if ( $msgText === null ) {
1267 $msgText = false; // treat as not existing
1268 }
1269
1270 $this->replace( $title->getDBkey(), $msgText );
1271
1272 if ( $wgContLang->hasVariants() ) {
1273 $wgContLang->updateConversionTable( $title );
1274 }
1275 }
1276
1277 /**
1278 * @param Content|null $content Content or null if the message page does not exist
1279 * @return string|bool|null Returns false if $content is null and null on error
1280 */
1281 private function getMessageTextFromContent( Content $content = null ) {
1282 // @TODO: could skip pseudo-messages like js/css here, based on content model
1283 if ( $content ) {
1284 // Message page exists...
1285 // XXX: Is this the right way to turn a Content object into a message?
1286 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1287 // CssContent. MessageContent is *not* used for storing messages, it's
1288 // only used for wrapping them when needed.
1289 $msgText = $content->getWikitextForTransclusion();
1290 if ( $msgText === false || $msgText === null ) {
1291 // This might be due to some kind of misconfiguration...
1292 $msgText = null;
1293 LoggerFactory::getInstance( 'MessageCache' )->warning(
1294 __METHOD__ . ": message content doesn't provide wikitext "
1295 . "(content model: " . $content->getModel() . ")" );
1296 }
1297 } else {
1298 // Message page does not exist...
1299 $msgText = false;
1300 }
1301
1302 return $msgText;
1303 }
1304 }