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