Merge "Added Linker::getInvalidTitleDescription() to display invalid title entries...
[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 *
26 */
27 define( 'MSG_LOAD_TIMEOUT', 60 );
28 define( 'MSG_LOCK_TIMEOUT', 10 );
29 define( 'MSG_WAIT_TIMEOUT', 10 );
30 define( 'MSG_CACHE_VERSION', 1 );
31
32 /**
33 * Message cache
34 * Performs various MediaWiki namespace-related functions
35 * @ingroup Cache
36 */
37 class MessageCache {
38 /**
39 * Process local cache of loaded messages that are defined in
40 * MediaWiki namespace. First array level is a language code,
41 * second level is message key and the values are either message
42 * content prefixed with space, or !NONEXISTENT for negative
43 * caching.
44 */
45 protected $mCache;
46
47 // Should mean that database cannot be used, but check
48 protected $mDisable;
49
50 /// Lifetime for cache, used by object caching
51 protected $mExpiry;
52
53 /**
54 * Message cache has it's own parser which it uses to transform
55 * messages.
56 */
57 protected $mParserOptions, $mParser;
58
59 /// Variable for tracking which variables are already loaded
60 protected $mLoadedLanguages = array();
61
62 /**
63 * Used for automatic detection of most used messages.
64 */
65 protected $mRequestedMessages = array();
66
67 /**
68 * How long the message request counts are stored. Longer period gives
69 * better sample, but also takes longer to adapt changes. The counts
70 * are aggregrated per day, regardless of the value of this variable.
71 */
72 protected static $mAdaptiveDataAge = 604800; // Is 7*24*3600
73
74 /**
75 * Filter the tail of less used messages that are requested more seldom
76 * than this factor times the number of request of most requested message.
77 * These messages are not loaded in the default set, but are still cached
78 * individually on demand with the normal cache expiry time.
79 */
80 protected static $mAdaptiveInclusionThreshold = 0.05;
81
82 /**
83 * Singleton instance
84 *
85 * @var MessageCache
86 */
87 private static $instance;
88
89 /**
90 * @var bool
91 */
92 protected $mInParser = false;
93
94 /**
95 * Get the signleton instance of this class
96 *
97 * @since 1.18
98 * @return MessageCache object
99 */
100 public static function singleton() {
101 if ( is_null( self::$instance ) ) {
102 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
103 self::$instance = new self( wfGetMessageCacheStorage(), $wgUseDatabaseMessages, $wgMsgCacheExpiry );
104 }
105 return self::$instance;
106 }
107
108 /**
109 * Destroy the singleton instance
110 *
111 * @since 1.18
112 */
113 public static function destroyInstance() {
114 self::$instance = null;
115 }
116
117 function __construct( $memCached, $useDB, $expiry ) {
118 if ( !$memCached ) {
119 $memCached = wfGetCache( CACHE_NONE );
120 }
121
122 $this->mMemc = $memCached;
123 $this->mDisable = !$useDB;
124 $this->mExpiry = $expiry;
125 }
126
127 /**
128 * ParserOptions is lazy initialised.
129 *
130 * @return ParserOptions
131 */
132 function getParserOptions() {
133 if ( !$this->mParserOptions ) {
134 $this->mParserOptions = new ParserOptions;
135 }
136 return $this->mParserOptions;
137 }
138
139 /**
140 * Try to load the cache from a local file.
141 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
142 * setting.
143 *
144 * @param $hash String: the hash of contents, to check validity.
145 * @param $code Mixed: Optional language code, see documenation of load().
146 * @return bool on failure.
147 */
148 function loadFromLocal( $hash, $code ) {
149 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
150
151 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
152
153 # Check file existence
154 wfSuppressWarnings();
155 $file = fopen( $filename, 'r' );
156 wfRestoreWarnings();
157 if ( !$file ) {
158 return false; // No cache file
159 }
160
161 if ( $wgLocalMessageCacheSerialized ) {
162 // Check to see if the file has the hash specified
163 $localHash = fread( $file, 32 );
164 if ( $hash === $localHash ) {
165 // All good, get the rest of it
166 $serialized = '';
167 while ( !feof( $file ) ) {
168 $serialized .= fread( $file, 100000 );
169 }
170 fclose( $file );
171 return $this->setCache( unserialize( $serialized ), $code );
172 } else {
173 fclose( $file );
174 return false; // Wrong hash
175 }
176 } else {
177 $localHash = substr( fread( $file, 40 ), 8 );
178 fclose( $file );
179 if ( $hash != $localHash ) {
180 return false; // Wrong hash
181 }
182
183 # Require overwrites the member variable or just shadows it?
184 require( $filename );
185 return $this->setCache( $this->mCache, $code );
186 }
187 }
188
189 /**
190 * Save the cache to a local file.
191 */
192 function saveToLocal( $serialized, $hash, $code ) {
193 global $wgCacheDirectory;
194
195 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
196 wfMkdirParents( $wgCacheDirectory, null, __METHOD__ ); // might fail
197
198 wfSuppressWarnings();
199 $file = fopen( $filename, 'w' );
200 wfRestoreWarnings();
201
202 if ( !$file ) {
203 wfDebug( "Unable to open local cache file for writing\n" );
204 return;
205 }
206
207 fwrite( $file, $hash . $serialized );
208 fclose( $file );
209 wfSuppressWarnings();
210 chmod( $filename, 0666 );
211 wfRestoreWarnings();
212 }
213
214 function saveToScript( $array, $hash, $code ) {
215 global $wgCacheDirectory;
216
217 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
218 $tempFilename = $filename . '.tmp';
219 wfMkdirParents( $wgCacheDirectory, null, __METHOD__ ); // might fail
220
221 wfSuppressWarnings();
222 $file = fopen( $tempFilename, 'w' );
223 wfRestoreWarnings();
224
225 if ( !$file ) {
226 wfDebug( "Unable to open local cache file for writing\n" );
227 return;
228 }
229
230 fwrite( $file, "<?php\n//$hash\n\n \$this->mCache = array(" );
231
232 foreach ( $array as $key => $message ) {
233 $key = $this->escapeForScript( $key );
234 $message = $this->escapeForScript( $message );
235 fwrite( $file, "'$key' => '$message',\n" );
236 }
237
238 fwrite( $file, ");\n?>" );
239 fclose( $file);
240 rename( $tempFilename, $filename );
241 }
242
243 function escapeForScript( $string ) {
244 $string = str_replace( '\\', '\\\\', $string );
245 $string = str_replace( '\'', '\\\'', $string );
246 return $string;
247 }
248
249 /**
250 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
251 *
252 * @return bool
253 */
254 function setCache( $cache, $code ) {
255 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
256 $this->mCache[$code] = $cache;
257 return true;
258 } else {
259 return false;
260 }
261 }
262
263 /**
264 * Loads messages from caches or from database in this order:
265 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
266 * (2) memcached
267 * (3) from the database.
268 *
269 * When succesfully loading from (2) or (3), all higher level caches are
270 * updated for the newest version.
271 *
272 * Nothing is loaded if member variable mDisable is true, either manually
273 * set by calling code or if message loading fails (is this possible?).
274 *
275 * Returns true if cache is already populated or it was succesfully populated,
276 * or false if populating empty cache fails. Also returns true if MessageCache
277 * is disabled.
278 *
279 * @param $code String: language to which load messages
280 * @return bool
281 */
282 function load( $code = false ) {
283 global $wgUseLocalMessageCache;
284
285 if( !is_string( $code ) ) {
286 # This isn't really nice, so at least make a note about it and try to
287 # fall back
288 wfDebug( __METHOD__ . " called without providing a language code\n" );
289 $code = 'en';
290 }
291
292 # Don't do double loading...
293 if ( isset( $this->mLoadedLanguages[$code] ) ) {
294 return true;
295 }
296
297 # 8 lines of code just to say (once) that message cache is disabled
298 if ( $this->mDisable ) {
299 static $shownDisabled = false;
300 if ( !$shownDisabled ) {
301 wfDebug( __METHOD__ . ": disabled\n" );
302 $shownDisabled = true;
303 }
304 return true;
305 }
306
307 # Loading code starts
308 wfProfileIn( __METHOD__ );
309 $success = false; # Keep track of success
310 $where = array(); # Debug info, delayed to avoid spamming debug log too much
311 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
312
313 # (1) local cache
314 # Hash of the contents is stored in memcache, to detect if local cache goes
315 # out of date (due to update in other thread?)
316 if ( $wgUseLocalMessageCache ) {
317 wfProfileIn( __METHOD__ . '-fromlocal' );
318
319 $hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
320 if ( $hash ) {
321 $success = $this->loadFromLocal( $hash, $code );
322 if ( $success ) $where[] = 'got from local cache';
323 }
324 wfProfileOut( __METHOD__ . '-fromlocal' );
325 }
326
327 # (2) memcache
328 # Fails if nothing in cache, or in the wrong version.
329 if ( !$success ) {
330 wfProfileIn( __METHOD__ . '-fromcache' );
331 $cache = $this->mMemc->get( $cacheKey );
332 $success = $this->setCache( $cache, $code );
333 if ( $success ) {
334 $where[] = 'got from global cache';
335 $this->saveToCaches( $cache, false, $code );
336 }
337 wfProfileOut( __METHOD__ . '-fromcache' );
338 }
339
340 # (3)
341 # Nothing in caches... so we need create one and store it in caches
342 if ( !$success ) {
343 $where[] = 'cache is empty';
344 $where[] = 'loading from database';
345
346 $this->lock( $cacheKey );
347
348 # Limit the concurrency of loadFromDB to a single process
349 # This prevents the site from going down when the cache expires
350 $statusKey = wfMemcKey( 'messages', $code, 'status' );
351 $success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
352 if ( $success ) {
353 $cache = $this->loadFromDB( $code );
354 $success = $this->setCache( $cache, $code );
355 }
356 if ( $success ) {
357 $success = $this->saveToCaches( $cache, true, $code );
358 if ( $success ) {
359 $this->mMemc->delete( $statusKey );
360 } else {
361 $this->mMemc->set( $statusKey, 'error', 60 * 5 );
362 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
363 }
364 }
365 $this->unlock($cacheKey);
366 }
367
368 if ( !$success ) {
369 # Bad luck... this should not happen
370 $where[] = 'loading FAILED - cache is disabled';
371 $info = implode( ', ', $where );
372 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
373 $this->mDisable = true;
374 $this->mCache = false;
375 } else {
376 # All good, just record the success
377 $info = implode( ', ', $where );
378 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
379 $this->mLoadedLanguages[$code] = true;
380 }
381 wfProfileOut( __METHOD__ );
382 return $success;
383 }
384
385 /**
386 * Loads cacheable messages from the database. Messages bigger than
387 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
388 * on-demand from the database later.
389 *
390 * @param $code String: language code.
391 * @return Array: loaded messages for storing in caches.
392 */
393 function loadFromDB( $code ) {
394 wfProfileIn( __METHOD__ );
395 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
396 $dbr = wfGetDB( DB_SLAVE );
397 $cache = array();
398
399 # Common conditions
400 $conds = array(
401 'page_is_redirect' => 0,
402 'page_namespace' => NS_MEDIAWIKI,
403 );
404
405 $mostused = array();
406 if ( $wgAdaptiveMessageCache ) {
407 $mostused = $this->getMostUsedMessages();
408 if ( $code !== $wgLanguageCode ) {
409 foreach ( $mostused as $key => $value ) {
410 $mostused[$key] = "$value/$code";
411 }
412 }
413 }
414
415 if ( count( $mostused ) ) {
416 $conds['page_title'] = $mostused;
417 } elseif ( $code !== $wgLanguageCode ) {
418 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
419 } else {
420 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
421 # other than language code.
422 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
423 }
424
425 # Conditions to fetch oversized pages to ignore them
426 $bigConds = $conds;
427 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
428
429 # Load titles for all oversized pages in the MediaWiki namespace
430 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ . "($code)-big" );
431 foreach ( $res as $row ) {
432 $cache[$row->page_title] = '!TOO BIG';
433 }
434
435 # Conditions to load the remaining pages with their contents
436 $smallConds = $conds;
437 $smallConds[] = 'page_latest=rev_id';
438 $smallConds[] = 'rev_text_id=old_id';
439 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
440
441 $res = $dbr->select(
442 array( 'page', 'revision', 'text' ),
443 array( 'page_title', 'old_text', 'old_flags' ),
444 $smallConds,
445 __METHOD__ . "($code)-small"
446 );
447
448 foreach ( $res as $row ) {
449 $text = Revision::getRevisionText( $row );
450 if( $text === false ) {
451 // Failed to fetch data; possible ES errors?
452 // Store a marker to fetch on-demand as a workaround...
453 $entry = '!TOO BIG';
454 wfDebugLog( 'MessageCache', __METHOD__ . ": failed to load message page text for {$row->page_title} ($code)" );
455 } else {
456 $entry = ' ' . $text;
457 }
458 $cache[$row->page_title] = $entry;
459 }
460
461 foreach ( $mostused as $key ) {
462 if ( !isset( $cache[$key] ) ) {
463 $cache[$key] = '!NONEXISTENT';
464 }
465 }
466
467 $cache['VERSION'] = MSG_CACHE_VERSION;
468 wfProfileOut( __METHOD__ );
469 return $cache;
470 }
471
472 /**
473 * Updates cache as necessary when message page is changed
474 *
475 * @param $title String: name of the page changed.
476 * @param $text Mixed: new contents of the page.
477 */
478 public function replace( $title, $text ) {
479 global $wgMaxMsgCacheEntrySize;
480 wfProfileIn( __METHOD__ );
481
482 if ( $this->mDisable ) {
483 wfProfileOut( __METHOD__ );
484 return;
485 }
486
487 list( $msg, $code ) = $this->figureMessage( $title );
488
489 $cacheKey = wfMemcKey( 'messages', $code );
490 $this->load( $code );
491 $this->lock( $cacheKey );
492
493 $titleKey = wfMemcKey( 'messages', 'individual', $title );
494
495 if ( $text === false ) {
496 # Article was deleted
497 $this->mCache[$code][$title] = '!NONEXISTENT';
498 $this->mMemc->delete( $titleKey );
499 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
500 # Check for size
501 $this->mCache[$code][$title] = '!TOO BIG';
502 $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
503 } else {
504 $this->mCache[$code][$title] = ' ' . $text;
505 $this->mMemc->delete( $titleKey );
506 }
507
508 # Update caches
509 $this->saveToCaches( $this->mCache[$code], true, $code );
510 $this->unlock( $cacheKey );
511
512 // Also delete cached sidebar... just in case it is affected
513 $codes = array( $code );
514 if ( $code === 'en' ) {
515 // Delete all sidebars, like for example on action=purge on the
516 // sidebar messages
517 $codes = array_keys( Language::fetchLanguageNames() );
518 }
519
520 global $wgMemc;
521 foreach ( $codes as $code ) {
522 $sidebarKey = wfMemcKey( 'sidebar', $code );
523 $wgMemc->delete( $sidebarKey );
524 }
525
526 // Update the message in the message blob store
527 global $wgContLang;
528 MessageBlobStore::updateMessage( $wgContLang->lcfirst( $msg ) );
529
530 wfRunHooks( 'MessageCacheReplace', array( $title, $text ) );
531
532 wfProfileOut( __METHOD__ );
533 }
534
535 /**
536 * Shortcut to update caches.
537 *
538 * @param $cache Array: cached messages with a version.
539 * @param $memc Bool: Wether to update or not memcache.
540 * @param $code String: Language code.
541 * @return bool on somekind of error.
542 */
543 protected function saveToCaches( $cache, $memc = true, $code = false ) {
544 wfProfileIn( __METHOD__ );
545 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
546
547 $cacheKey = wfMemcKey( 'messages', $code );
548
549 if ( $memc ) {
550 $success = $this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
551 } else {
552 $success = true;
553 }
554
555 # Save to local cache
556 if ( $wgUseLocalMessageCache ) {
557 $serialized = serialize( $cache );
558 $hash = md5( $serialized );
559 $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry );
560 if ($wgLocalMessageCacheSerialized) {
561 $this->saveToLocal( $serialized, $hash, $code );
562 } else {
563 $this->saveToScript( $cache, $hash, $code );
564 }
565 }
566
567 wfProfileOut( __METHOD__ );
568 return $success;
569 }
570
571 /**
572 * Represents a write lock on the messages key
573 *
574 * @param $key string
575 *
576 * @return Boolean: success
577 */
578 function lock( $key ) {
579 $lockKey = $key . ':lock';
580 for ( $i = 0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
581 sleep( 1 );
582 }
583
584 return $i >= MSG_WAIT_TIMEOUT;
585 }
586
587 function unlock( $key ) {
588 $lockKey = $key . ':lock';
589 $this->mMemc->delete( $lockKey );
590 }
591
592 /**
593 * Get a message from either the content language or the user language.
594 *
595 * @param $key String: the message cache key
596 * @param $useDB Boolean: get the message from the DB, false to use only
597 * the localisation
598 * @param $langcode String: code of the language to get the message for, if
599 * it is a valid code create a language for that language,
600 * if it is a string but not a valid code then make a basic
601 * language object, if it is a false boolean then use the
602 * current users language (as a fallback for the old
603 * parameter functionality), or if it is a true boolean
604 * then use the wikis content language (also as a
605 * fallback).
606 * @param $isFullKey Boolean: specifies whether $key is a two part key
607 * "msg/lang".
608 *
609 * @return string|bool
610 */
611 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
612 global $wgLanguageCode, $wgContLang;
613
614 if ( is_int( $key ) ) {
615 // "Non-string key given" exception sometimes happens for numerical strings that become ints somewhere on their way here
616 $key = strval( $key );
617 }
618
619 if ( !is_string( $key ) ) {
620 throw new MWException( 'Non-string key given' );
621 }
622
623 if ( strval( $key ) === '' ) {
624 # Shortcut: the empty key is always missing
625 return false;
626 }
627
628 $lang = wfGetLangObj( $langcode );
629 if ( !$lang ) {
630 throw new MWException( "Bad lang code $langcode given" );
631 }
632
633 $langcode = $lang->getCode();
634
635 $message = false;
636
637 # Normalise title-case input (with some inlining)
638 $lckey = str_replace( ' ', '_', $key );
639 if ( ord( $key ) < 128 ) {
640 $lckey[0] = strtolower( $lckey[0] );
641 $uckey = ucfirst( $lckey );
642 } else {
643 $lckey = $wgContLang->lcfirst( $lckey );
644 $uckey = $wgContLang->ucfirst( $lckey );
645 }
646
647 /**
648 * Record each message request, but only once per request.
649 * This information is not used unless $wgAdaptiveMessageCache
650 * is enabled.
651 */
652 $this->mRequestedMessages[$uckey] = true;
653
654 # Try the MediaWiki namespace
655 if( !$this->mDisable && $useDB ) {
656 $title = $uckey;
657 if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
658 $title .= '/' . $langcode;
659 }
660 $message = $this->getMsgFromNamespace( $title, $langcode );
661 }
662
663 # Try the array in the language object
664 if ( $message === false ) {
665 $message = $lang->getMessage( $lckey );
666 if ( is_null( $message ) ) {
667 $message = false;
668 }
669 }
670
671 # Try the array of another language
672 if( $message === false ) {
673 $parts = explode( '/', $lckey );
674 # We may get calls for things that are http-urls from sidebar
675 # Let's not load nonexistent languages for those
676 # They usually have more than one slash.
677 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
678 $message = Language::getMessageFor( $parts[0], $parts[1] );
679 if ( is_null( $message ) ) {
680 $message = false;
681 }
682 }
683 }
684
685 # Is this a custom message? Try the default language in the db...
686 if( ( $message === false || $message === '-' ) &&
687 !$this->mDisable && $useDB &&
688 !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
689 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
690 }
691
692 # Final fallback
693 if( $message === false ) {
694 return false;
695 }
696
697 # Fix whitespace
698 $message = strtr( $message,
699 array(
700 # Fix for trailing whitespace, removed by textarea
701 '&#32;' => ' ',
702 # Fix for NBSP, converted to space by firefox
703 '&nbsp;' => "\xc2\xa0",
704 '&#160;' => "\xc2\xa0",
705 ) );
706
707 return $message;
708 }
709
710 /**
711 * Get a message from the MediaWiki namespace, with caching. The key must
712 * first be converted to two-part lang/msg form if necessary.
713 *
714 * @param $title String: Message cache key with initial uppercase letter.
715 * @param $code String: code denoting the language to try.
716 *
717 * @return string|bool False on failure
718 */
719 function getMsgFromNamespace( $title, $code ) {
720 global $wgAdaptiveMessageCache;
721
722 $this->load( $code );
723 if ( isset( $this->mCache[$code][$title] ) ) {
724 $entry = $this->mCache[$code][$title];
725 if ( substr( $entry, 0, 1 ) === ' ' ) {
726 return substr( $entry, 1 );
727 } elseif ( $entry === '!NONEXISTENT' ) {
728 return false;
729 } elseif( $entry === '!TOO BIG' ) {
730 // Fall through and try invididual message cache below
731 }
732 } else {
733 // XXX: This is not cached in process cache, should it?
734 $message = false;
735 wfRunHooks( 'MessagesPreLoad', array( $title, &$message ) );
736 if ( $message !== false ) {
737 return $message;
738 }
739
740 /**
741 * If message cache is in normal mode, it is guaranteed
742 * (except bugs) that there is always entry (or placeholder)
743 * in the cache if message exists. Thus we can do minor
744 * performance improvement and return false early.
745 */
746 if ( !$wgAdaptiveMessageCache ) {
747 return false;
748 }
749 }
750
751 # Try the individual message cache
752 $titleKey = wfMemcKey( 'messages', 'individual', $title );
753 $entry = $this->mMemc->get( $titleKey );
754 if ( $entry ) {
755 if ( substr( $entry, 0, 1 ) === ' ' ) {
756 $this->mCache[$code][$title] = $entry;
757 return substr( $entry, 1 );
758 } elseif ( $entry === '!NONEXISTENT' ) {
759 $this->mCache[$code][$title] = '!NONEXISTENT';
760 return false;
761 } else {
762 # Corrupt/obsolete entry, delete it
763 $this->mMemc->delete( $titleKey );
764 }
765 }
766
767 # Try loading it from the database
768 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
769 if ( $revision ) {
770 $message = $revision->getText();
771 if ($message === false) {
772 // A possibly temporary loading failure.
773 wfDebugLog( 'MessageCache', __METHOD__ . ": failed to load message page text for {$title} ($code)" );
774 } else {
775 $this->mCache[$code][$title] = ' ' . $message;
776 $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
777 }
778 } else {
779 $message = false;
780 $this->mCache[$code][$title] = '!NONEXISTENT';
781 $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
782 }
783
784 return $message;
785 }
786
787 /**
788 * @param $message string
789 * @param $interface bool
790 * @param $language
791 * @param $title Title
792 * @return string
793 */
794 function transform( $message, $interface = false, $language = null, $title = null ) {
795 // Avoid creating parser if nothing to transform
796 if( strpos( $message, '{{' ) === false ) {
797 return $message;
798 }
799
800 if ( $this->mInParser ) {
801 return $message;
802 }
803
804 $parser = $this->getParser();
805 if ( $parser ) {
806 $popts = $this->getParserOptions();
807 $popts->setInterfaceMessage( $interface );
808 $popts->setTargetLanguage( $language );
809
810 $userlang = $popts->setUserLang( $language );
811 $this->mInParser = true;
812 $message = $parser->transformMsg( $message, $popts, $title );
813 $this->mInParser = false;
814 $popts->setUserLang( $userlang );
815 }
816 return $message;
817 }
818
819 /**
820 * @return Parser
821 */
822 function getParser() {
823 global $wgParser, $wgParserConf;
824 if ( !$this->mParser && isset( $wgParser ) ) {
825 # Do some initialisation so that we don't have to do it twice
826 $wgParser->firstCallInit();
827 # Clone it and store it
828 $class = $wgParserConf['class'];
829 if ( $class == 'Parser_DiffTest' ) {
830 # Uncloneable
831 $this->mParser = new $class( $wgParserConf );
832 } else {
833 $this->mParser = clone $wgParser;
834 }
835 }
836 return $this->mParser;
837 }
838
839 /**
840 * @param $text string
841 * @param $title Title
842 * @param $linestart bool
843 * @param $interface bool
844 * @param $language
845 * @return ParserOutput
846 */
847 public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
848 if ( $this->mInParser ) {
849 return htmlspecialchars( $text );
850 }
851
852 $parser = $this->getParser();
853 $popts = $this->getParserOptions();
854 $popts->setInterfaceMessage( $interface );
855 $popts->setTargetLanguage( $language );
856
857 wfProfileIn( __METHOD__ );
858 if ( !$title || !$title instanceof Title ) {
859 global $wgTitle;
860 $title = $wgTitle;
861 }
862 // Sometimes $wgTitle isn't set either...
863 if ( !$title ) {
864 # It's not uncommon having a null $wgTitle in scripts. See r80898
865 # Create a ghost title in such case
866 $title = Title::newFromText( 'Dwimmerlaik' );
867 }
868
869 $this->mInParser = true;
870 $res = $parser->parse( $text, $title, $popts, $linestart );
871 $this->mInParser = false;
872
873 wfProfileOut( __METHOD__ );
874 return $res;
875 }
876
877 function disable() {
878 $this->mDisable = true;
879 }
880
881 function enable() {
882 $this->mDisable = false;
883 }
884
885 /**
886 * Clear all stored messages. Mainly used after a mass rebuild.
887 */
888 function clear() {
889 $langs = Language::fetchLanguageNames( null, 'mw' );
890 foreach ( array_keys($langs) as $code ) {
891 # Global cache
892 $this->mMemc->delete( wfMemcKey( 'messages', $code ) );
893 # Invalidate all local caches
894 $this->mMemc->delete( wfMemcKey( 'messages', $code, 'hash' ) );
895 }
896 $this->mLoadedLanguages = array();
897 }
898
899 /**
900 * @param $key
901 * @return array
902 */
903 public function figureMessage( $key ) {
904 global $wgLanguageCode;
905 $pieces = explode( '/', $key );
906 if( count( $pieces ) < 2 ) {
907 return array( $key, $wgLanguageCode );
908 }
909
910 $lang = array_pop( $pieces );
911 if( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
912 return array( $key, $wgLanguageCode );
913 }
914
915 $message = implode( '/', $pieces );
916 return array( $message, $lang );
917 }
918
919 public static function logMessages() {
920 wfProfileIn( __METHOD__ );
921 global $wgAdaptiveMessageCache;
922 if ( !$wgAdaptiveMessageCache || !self::$instance instanceof MessageCache ) {
923 wfProfileOut( __METHOD__ );
924 return;
925 }
926
927 $cachekey = wfMemckey( 'message-profiling' );
928 $cache = wfGetCache( CACHE_DB );
929 $data = $cache->get( $cachekey );
930
931 if ( !$data ) {
932 $data = array();
933 }
934
935 $age = self::$mAdaptiveDataAge;
936 $filterDate = substr( wfTimestamp( TS_MW, time() - $age ), 0, 8 );
937 foreach ( array_keys( $data ) as $key ) {
938 if ( $key < $filterDate ) {
939 unset( $data[$key] );
940 }
941 }
942
943 $index = substr( wfTimestampNow(), 0, 8 );
944 if ( !isset( $data[$index] ) ) {
945 $data[$index] = array();
946 }
947
948 foreach ( self::$instance->mRequestedMessages as $message => $_ ) {
949 if ( !isset( $data[$index][$message] ) ) {
950 $data[$index][$message] = 0;
951 }
952 $data[$index][$message]++;
953 }
954
955 $cache->set( $cachekey, $data );
956 wfProfileOut( __METHOD__ );
957 }
958
959 /**
960 * @return array
961 */
962 public function getMostUsedMessages() {
963 wfProfileIn( __METHOD__ );
964 $cachekey = wfMemcKey( 'message-profiling' );
965 $cache = wfGetCache( CACHE_DB );
966 $data = $cache->get( $cachekey );
967 if ( !$data ) {
968 wfProfileOut( __METHOD__ );
969 return array();
970 }
971
972 $list = array();
973
974 foreach( $data as $messages ) {
975 foreach( $messages as $message => $count ) {
976 $key = $message;
977 if ( !isset( $list[$key] ) ) {
978 $list[$key] = 0;
979 }
980 $list[$key] += $count;
981 }
982 }
983
984 $max = max( $list );
985 foreach ( $list as $message => $count ) {
986 if ( $count < intval( $max * self::$mAdaptiveInclusionThreshold ) ) {
987 unset( $list[$message] );
988 }
989 }
990
991 wfProfileOut( __METHOD__ );
992 return array_keys( $list );
993 }
994
995 /**
996 * Get all message keys stored in the message cache for a given language.
997 * If $code is the content language code, this will return all message keys
998 * for which MediaWiki:msgkey exists. If $code is another language code, this
999 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1000 * @param $code string
1001 * @return array of message keys (strings)
1002 */
1003 public function getAllMessageKeys( $code ) {
1004 global $wgContLang;
1005 $this->load( $code );
1006 if ( !isset( $this->mCache[$code] ) ) {
1007 // Apparently load() failed
1008 return null;
1009 }
1010 $cache = $this->mCache[$code]; // Copy the cache
1011 unset( $cache['VERSION'] ); // Remove the VERSION key
1012 $cache = array_diff( $cache, array( '!NONEXISTENT' ) ); // Remove any !NONEXISTENT keys
1013 // Keys may appear with a capital first letter. lcfirst them.
1014 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );
1015 }
1016 }