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