10 define( 'MSG_LOAD_TIMEOUT', 60);
11 define( 'MSG_LOCK_TIMEOUT', 10);
12 define( 'MSG_WAIT_TIMEOUT', 10);
13 define( 'MSG_CACHE_VERSION', 1 );
17 * Performs various MediaWiki namespace-related functions
21 // Holds loaded messages that are defined in MediaWiki namespace.
24 var $mDisable, $mExpiry;
25 var $mKeys, $mParserOptions, $mParser;
27 // Variable for tracking which variables are loaded
28 var $mLoadedLanguages = array();
30 function __construct( $memCached, $useDB, $expiry, /*ignored*/ $memcPrefix ) {
32 $memCached = wfGetCache( CACHE_NONE
);
35 $this->mMemc
= $memCached;
36 $this->mDisable
= !$useDB;
37 $this->mExpiry
= $expiry;
38 $this->mDisableTransform
= false;
39 $this->mKeys
= false; # initialised on demand
40 $this->mParser
= null;
45 * ParserOptions is lazy initialised.
47 function getParserOptions() {
48 if ( !$this->mParserOptions
) {
49 $this->mParserOptions
= new ParserOptions
;
51 return $this->mParserOptions
;
55 * Try to load the cache from a local file.
56 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
59 * @param $hash String: the hash of contents, to check validity.
60 * @param $code Mixed: Optional language code, see documenation of load().
61 * @return false on failure.
63 function loadFromLocal( $hash, $code ) {
64 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
66 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
68 # Check file existence
70 $file = fopen( $filename, 'r' );
73 return false; // No cache file
76 if ( $wgLocalMessageCacheSerialized ) {
77 // Check to see if the file has the hash specified
78 $localHash = fread( $file, 32 );
79 if ( $hash === $localHash ) {
80 // All good, get the rest of it
82 while ( !feof( $file ) ) {
83 $serialized .= fread( $file, 100000 );
86 return $this->setCache( unserialize( $serialized ), $code );
89 return false; // Wrong hash
92 $localHash=substr(fread($file,40),8);
94 if ($hash!=$localHash) {
95 return false; // Wrong hash
98 # Require overwrites the member variable or just shadows it?
100 return $this->setCache( $this->mCache
, $code );
105 * Save the cache to a local file.
107 function saveToLocal( $serialized, $hash, $code ) {
108 global $wgCacheDirectory;
110 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
111 wfMkdirParents( $wgCacheDirectory ); // might fail
113 wfSuppressWarnings();
114 $file = fopen( $filename, 'w' );
118 wfDebug( "Unable to open local cache file for writing\n" );
122 fwrite( $file, $hash . $serialized );
124 @chmod
( $filename, 0666 );
127 function saveToScript( $array, $hash, $code ) {
128 global $wgCacheDirectory;
130 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
131 $tempFilename = $filename . '.tmp';
132 wfMkdirParents( $wgCacheDirectory ); // might fail
134 wfSuppressWarnings();
135 $file = fopen( $tempFilename, 'w');
139 wfDebug( "Unable to open local cache file for writing\n" );
143 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
145 foreach ($array as $key => $message) {
146 $key = $this->escapeForScript($key);
147 $messages = $this->escapeForScript($message);
148 fwrite($file, "'$key' => '$message',\n");
151 fwrite($file,");\n?>");
153 rename($tempFilename, $filename);
156 function escapeForScript($string) {
157 $string = str_replace( '\\', '\\\\', $string );
158 $string = str_replace( '\'', '\\\'', $string );
163 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
165 function setCache( $cache, $code ) {
166 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION
) {
167 $this->mCache
[$code] = $cache;
175 * Loads messages from caches or from database in this order:
176 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
178 * (3) from the database.
180 * When succesfully loading from (2) or (3), all higher level caches are
181 * updated for the newest version.
183 * Nothing is loaded if member variable mDisabled is true, either manually
184 * set by calling code or if message loading fails (is this possible?).
186 * Returns true if cache is already populated or it was succesfully populated,
187 * or false if populating empty cache fails. Also returns true if MessageCache
190 * @param $code String: language to which load messages
192 function load( $code = false ) {
193 global $wgUseLocalMessageCache;
195 if( !is_string( $code ) ) {
196 # This isn't really nice, so at least make a note about it and try to
198 wfDebug( __METHOD__
. " called without providing a language code\n" );
202 # Don't do double loading...
203 if ( isset($this->mLoadedLanguages
[$code]) ) return true;
205 # 8 lines of code just to say (once) that message cache is disabled
206 if ( $this->mDisable
) {
207 static $shownDisabled = false;
208 if ( !$shownDisabled ) {
209 wfDebug( __METHOD__
. ": disabled\n" );
210 $shownDisabled = true;
215 # Loading code starts
216 wfProfileIn( __METHOD__
);
217 $success = false; # Keep track of success
218 $where = array(); # Debug info, delayed to avoid spamming debug log too much
219 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
223 # Hash of the contents is stored in memcache, to detect if local cache goes
224 # out of date (due to update in other thread?)
225 if ( $wgUseLocalMessageCache ) {
226 wfProfileIn( __METHOD__
. '-fromlocal' );
228 $hash = $this->mMemc
->get( wfMemcKey( 'messages', $code, 'hash' ) );
230 $success = $this->loadFromLocal( $hash, $code );
231 if ( $success ) $where[] = 'got from local cache';
233 wfProfileOut( __METHOD__
. '-fromlocal' );
237 # Fails if nothing in cache, or in the wrong version.
239 wfProfileIn( __METHOD__
. '-fromcache' );
240 $cache = $this->mMemc
->get( $cacheKey );
241 $success = $this->setCache( $cache, $code );
243 $where[] = 'got from global cache';
244 $this->saveToCaches( $cache, false, $code );
246 wfProfileOut( __METHOD__
. '-fromcache' );
251 # Nothing in caches... so we need create one and store it in caches
253 $where[] = 'cache is empty';
254 $where[] = 'loading from database';
256 $this->lock($cacheKey);
258 # Limit the concurrency of loadFromDB to a single process
259 # This prevents the site from going down when the cache expires
260 $statusKey = wfMemcKey( 'messages', $code, 'status' );
261 $success = $this->mMemc
->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT
);
263 $cache = $this->loadFromDB( $code );
264 $success = $this->setCache( $cache, $code );
267 $success = $this->saveToCaches( $cache, true, $code );
269 $this->mMemc
->delete( $statusKey );
271 $this->mMemc
->set( $statusKey, 'error', 60*5 );
272 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
275 $this->unlock($cacheKey);
279 # Bad luck... this should not happen
280 $where[] = 'loading FAILED - cache is disabled';
281 $info = implode( ', ', $where );
282 wfDebug( __METHOD__
. ": Loading $code... $info\n" );
283 $this->mDisable
= true;
284 $this->mCache
= false;
286 # All good, just record the success
287 $info = implode( ', ', $where );
288 wfDebug( __METHOD__
. ": Loading $code... $info\n" );
289 $this->mLoadedLanguages
[$code] = true;
291 wfProfileOut( __METHOD__
);
296 * Loads cacheable messages from the database. Messages bigger than
297 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
298 * on-demand from the database later.
300 * @param $code Optional language code, see documenation of load().
301 * @return Array: Loaded messages for storing in caches.
303 function loadFromDB( $code = false ) {
304 wfProfileIn( __METHOD__
);
305 global $wgMaxMsgCacheEntrySize, $wgContLanguageCode;
306 $dbr = wfGetDB( DB_SLAVE
);
311 'page_is_redirect' => 0,
312 'page_namespace' => NS_MEDIAWIKI
,
316 # Is this fast enough. Should not matter if the filtering is done in the
317 # database or in code.
318 if ( $code !== $wgContLanguageCode ) {
319 # Messages for particular language
320 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
322 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
323 # other than language code.
324 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
328 # Conditions to fetch oversized pages to ignore them
330 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
332 # Load titles for all oversized pages in the MediaWiki namespace
333 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
334 while ( $row = $dbr->fetchObject( $res ) ) {
335 $cache[$row->page_title
] = '!TOO BIG';
337 $dbr->freeResult( $res );
339 # Conditions to load the remaining pages with their contents
340 $smallConds = $conds;
341 $smallConds[] = 'page_latest=rev_id';
342 $smallConds[] = 'rev_text_id=old_id';
343 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
345 $res = $dbr->select( array( 'page', 'revision', 'text' ),
346 array( 'page_title', 'old_text', 'old_flags' ),
347 $smallConds, __METHOD__
. "($code)-small" );
349 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
350 $cache[$row->page_title
] = ' ' . Revision
::getRevisionText( $row );
352 $dbr->freeResult( $res );
354 $cache['VERSION'] = MSG_CACHE_VERSION
;
355 wfProfileOut( __METHOD__
);
360 * Updates cache as necessary when message page is changed
362 * @param $title String: name of the page changed.
363 * @param $text Mixed: new contents of the page.
365 public function replace( $title, $text ) {
366 global $wgMaxMsgCacheEntrySize;
367 wfProfileIn( __METHOD__
);
370 list( , $code ) = $this->figureMessage( $title );
372 $cacheKey = wfMemcKey( 'messages', $code );
374 $this->lock($cacheKey);
376 if ( is_array($this->mCache
[$code]) ) {
377 $titleKey = wfMemcKey( 'messages', 'individual', $title );
379 if ( $text === false ) {
380 # Article was deleted
381 unset( $this->mCache
[$code][$title] );
382 $this->mMemc
->delete( $titleKey );
384 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
386 $this->mCache
[$code][$title] = '!TOO BIG';
387 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
390 $this->mCache
[$code][$title] = ' ' . $text;
391 $this->mMemc
->delete( $titleKey );
395 $this->saveToCaches( $this->mCache
[$code], true, $code );
397 $this->unlock($cacheKey);
399 // Also delete cached sidebar... just in case it is affected
401 $codes = array( $code );
402 if ( $code === 'en' ) {
403 // Delete all sidebars, like for example on action=purge on the
405 $codes = array_keys( Language
::getLanguageNames() );
408 foreach ( $codes as $code ) {
409 $sidebarKey = wfMemcKey( 'sidebar', $code );
410 $parserMemc->delete( $sidebarKey );
413 wfRunHooks( "MessageCacheReplace", array( $title, $text ) );
415 wfProfileOut( __METHOD__
);
419 * Shortcut to update caches.
421 * @param $cache Array: cached messages with a version.
422 * @param $memc Bool: Wether to update or not memcache.
423 * @param $code String: Language code.
424 * @return False on somekind of error.
426 protected function saveToCaches( $cache, $memc = true, $code = false ) {
427 wfProfileIn( __METHOD__
);
428 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
430 $cacheKey = wfMemcKey( 'messages', $code );
433 $success = $this->mMemc
->set( $cacheKey, $cache, $this->mExpiry
);
438 # Save to local cache
439 if ( $wgUseLocalMessageCache ) {
440 $serialized = serialize( $cache );
441 $hash = md5( $serialized );
442 $this->mMemc
->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry
);
443 if ($wgLocalMessageCacheSerialized) {
444 $this->saveToLocal( $serialized, $hash, $code );
446 $this->saveToScript( $cache, $hash, $code );
450 wfProfileOut( __METHOD__
);
455 * Represents a write lock on the messages key
457 * @return Boolean: success
459 function lock($key) {
460 $lockKey = $key . ':lock';
461 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
465 return $i >= MSG_WAIT_TIMEOUT
;
468 function unlock($key) {
469 $lockKey = $key . ':lock';
470 $this->mMemc
->delete( $lockKey );
474 * Get a message from either the content language or the user language.
476 * @param $key String: the message cache key
477 * @param $useDB Boolean: get the message from the DB, false to use only
479 * @param $langcode String: code of the language to get the message for, if
480 * it is a valid code create a language for that language,
481 * if it is a string but not a valid code then make a basic
482 * language object, if it is a false boolean then use the
483 * current users language (as a fallback for the old
484 * parameter functionality), or if it is a true boolean
485 * then use the wikis content language (also as a
487 * @param $isFullKey Boolean: specifies whether $key is a two part key
490 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
491 global $wgContLanguageCode, $wgContLang;
493 if ( !is_string( $key ) ) {
494 throw new MWException( "Non-string key given" );
497 if ( strval( $key ) === '' ) {
498 # Shortcut: the empty key is always missing
502 $lang = wfGetLangObj( $langcode );
503 $langcode = $lang->getCode();
507 # Normalise title-case input (with some inlining)
508 $lckey = str_replace( ' ', '_', $key );
509 if ( ord( $key ) < 128 ) {
510 $lckey[0] = strtolower( $lckey[0] );
511 $uckey = ucfirst( $lckey );
513 $lckey = $wgContLang->lcfirst( $lckey );
514 $uckey = $wgContLang->ucfirst( $lckey );
517 # Try the MediaWiki namespace
518 if( !$this->mDisable
&& $useDB ) {
520 if(!$isFullKey && ( $langcode != $wgContLanguageCode ) ) {
521 $title .= '/' . $langcode;
523 $message = $this->getMsgFromNamespace( $title, $langcode );
526 # Try the array in the language object
527 if ( $message === false ) {
528 $message = $lang->getMessage( $lckey );
529 if ( is_null( $message ) ) {
534 # Try the array of another language
535 if( $message === false ) {
536 $parts = explode( '/', $lckey );
537 # We may get calls for things that are http-urls from sidebar
538 # Let's not load nonexistent languages for those
539 # They usually have more than one slash.
540 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
541 $message = Language
::getMessageFor( $parts[0], $parts[1] );
542 if ( is_null( $message ) ) {
548 # Is this a custom message? Try the default language in the db...
549 if( ($message === false ||
$message === '-' ) &&
550 !$this->mDisable
&& $useDB &&
551 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
552 $message = $this->getMsgFromNamespace( $uckey, $wgContLanguageCode );
556 if( $message === false ) {
561 $message = strtr( $message,
563 # Fix for trailing whitespace, removed by textarea
565 # Fix for NBSP, converted to space by firefox
566 ' ' => "\xc2\xa0",
567 ' ' => "\xc2\xa0",
574 * Get a message from the MediaWiki namespace, with caching. The key must
575 * first be converted to two-part lang/msg form if necessary.
577 * @param $title String: Message cache key with initial uppercase letter.
578 * @param $code String: code denoting the language to try.
580 function getMsgFromNamespace( $title, $code ) {
584 $this->load( $code );
585 if (isset( $this->mCache
[$code][$title] ) ) {
586 $entry = $this->mCache
[$code][$title];
587 $type = substr( $entry, 0, 1 );
588 if ( $type == ' ' ) {
589 return substr( $entry, 1 );
593 # Call message hooks, in case they are defined
594 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
595 if ( $message !== false ) {
599 # If there is no cache entry and no placeholder, it doesn't exist
600 if ( $type !== '!' ) {
604 $titleKey = wfMemcKey( 'messages', 'individual', $title );
606 # Try the individual message cache
607 $entry = $this->mMemc
->get( $titleKey );
609 $type = substr( $entry, 0, 1 );
611 if ( $type === ' ' ) {
613 $message = substr( $entry, 1 );
614 $this->mCache
[$code][$title] = $entry;
616 } elseif ( $entry === '!NONEXISTENT' ) {
619 # Corrupt/obsolete entry, delete it
620 $this->mMemc
->delete( $titleKey );
624 # Try loading it from the DB
625 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
627 $message = $revision->getText();
628 $this->mCache
[$code][$title] = ' ' . $message;
629 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
632 # Use some special text instead of false, because false gets converted to '' somewhere
633 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
634 $this->mCache
[$code][$title] = false;
639 function transform( $message, $interface = false, $language = null ) {
640 // Avoid creating parser if nothing to transform
641 if( strpos( $message, '{{' ) === false ) {
645 global $wgParser, $wgParserConf;
646 if ( !$this->mParser
&& isset( $wgParser ) ) {
647 # Do some initialisation so that we don't have to do it twice
648 $wgParser->firstCallInit();
649 # Clone it and store it
650 $class = $wgParserConf['class'];
651 if ( $class == 'Parser_DiffTest' ) {
653 $this->mParser
= new $class( $wgParserConf );
655 $this->mParser
= clone $wgParser;
657 #wfDebug( __METHOD__ . ": following contents triggered transform: $message\n" );
659 if ( $this->mParser
) {
660 $popts = $this->getParserOptions();
661 $popts->setInterfaceMessage( $interface );
662 $popts->setTargetLanguage( $language );
663 $message = $this->mParser
->transformMsg( $message, $popts );
668 function disable() { $this->mDisable
= true; }
669 function enable() { $this->mDisable
= false; }
672 function disableTransform(){
673 wfDeprecated( __METHOD__
);
675 function enableTransform() {
676 wfDeprecated( __METHOD__
);
678 function setTransform( $x ) {
679 wfDeprecated( __METHOD__
);
681 function getTransform() {
682 wfDeprecated( __METHOD__
);
687 * Clear all stored messages. Mainly used after a mass rebuild.
690 $langs = Language
::getLanguageNames( false );
691 foreach ( array_keys($langs) as $code ) {
693 $this->mMemc
->delete( wfMemcKey( 'messages', $code ) );
694 # Invalidate all local caches
695 $this->mMemc
->delete( wfMemcKey( 'messages', $code, 'hash' ) );
700 * Add a message to the cache
701 * @deprecated Use $wgExtensionMessagesFiles
704 * @param $value Mixed
705 * @param $lang String: the messages language, English by default
707 function addMessage( $key, $value, $lang = 'en' ) {
708 wfDeprecated( __METHOD__
);
709 $lc = Language
::getLocalisationCache();
710 $lc->addLegacyMessages( array( $lang => array( $key => $value ) ) );
714 * Add an associative array of message to the cache
715 * @deprecated Use $wgExtensionMessagesFiles
717 * @param $messages Array: an associative array of key => values to be added
718 * @param $lang String: the messages language, English by default
720 function addMessages( $messages, $lang = 'en' ) {
721 wfDeprecated( __METHOD__
);
722 $lc = Language
::getLocalisationCache();
723 $lc->addLegacyMessages( array( $lang => $messages ) );
727 * Add a 2-D array of messages by lang. Useful for extensions.
728 * @deprecated Use $wgExtensionMessagesFiles
730 * @param $messages Array: the array to be added
732 function addMessagesByLang( $messages ) {
733 wfDeprecated( __METHOD__
);
734 $lc = Language
::getLocalisationCache();
735 $lc->addLegacyMessages( $messages );
739 * Set a hook for addMessagesByLang()
741 function setExtensionMessagesHook( $callback ) {
742 $this->mAddMessagesHook
= $callback;
748 function loadAllMessages( $lang = false ) {
754 function loadMessagesFile( $filename, $langcode = false ) {
757 public function figureMessage( $key ) {
758 global $wgContLanguageCode;
759 $pieces = explode( '/', $key );
760 if( count( $pieces ) < 2 )
761 return array( $key, $wgContLanguageCode );
763 $lang = array_pop( $pieces );
764 $validCodes = Language
::getLanguageNames();
765 if( !array_key_exists( $lang, $validCodes ) )
766 return array( $key, $wgContLanguageCode );
768 $message = implode( '/', $pieces );
769 return array( $message, $lang );