11 define( 'MSG_LOAD_TIMEOUT', 60);
12 define( 'MSG_LOCK_TIMEOUT', 10);
13 define( 'MSG_WAIT_TIMEOUT', 10);
14 define( 'MSG_CACHE_VERSION', 1 );
18 * Performs various MediaWiki namespace-related functions
23 var $mCache, $mUseCache, $mDisable, $mExpiry;
24 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
25 var $mExtensionMessages = array();
26 var $mInitialised = false;
27 var $mDeferred = true;
29 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
30 wfProfileIn( __METHOD__
);
32 $this->mUseCache
= !is_null( $memCached );
33 $this->mMemc
= &$memCached;
34 $this->mDisable
= !$useDB;
35 $this->mExpiry
= $expiry;
36 $this->mDisableTransform
= false;
37 $this->mMemcKey
= $memcPrefix.':messages';
38 $this->mKeys
= false; # initialised on demand
39 $this->mInitialised
= true;
40 $this->mParser
= null;
42 # When we first get asked for a message,
43 # then we'll fill up the cache. If we
44 # can return a cache hit, this saves
45 # some extra milliseconds
46 $this->mDeferred
= true;
48 wfProfileOut( __METHOD__
);
51 function getParserOptions() {
52 if ( !$this->mParserOptions
) {
53 $this->mParserOptions
= new ParserOptions
;
55 return $this->mParserOptions
;
59 * Try to load the cache from a local file
61 function loadFromLocal( $hash ) {
62 global $wgLocalMessageCache;
64 $this->mCache
= false;
65 if ( $wgLocalMessageCache === false ) {
69 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
72 $file = fopen( $filename, 'r' );
78 // Check to see if the file has the hash specified
79 $localHash = fread( $file, 32 );
80 if ( $hash == $localHash ) {
81 // All good, get the rest of it
82 $serialized = fread( $file, 10000000 );
83 $this->setCache( unserialize( $serialized ) );
89 * Save the cache to a local file
91 function saveToLocal( $serialized, $hash ) {
92 global $wgLocalMessageCache;
94 if ( $wgLocalMessageCache === false ) {
98 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
99 $oldUmask = umask( 0 );
100 wfMkdirParents( $wgLocalMessageCache, 0777 );
103 $file = fopen( $filename, 'w' );
105 wfDebug( "Unable to open local cache file for writing\n" );
109 fwrite( $file, $hash . $serialized );
111 @chmod
( $filename, 0666 );
114 function loadFromScript( $hash ) {
115 global $wgLocalMessageCache;
116 if ( $wgLocalMessageCache === false ) {
120 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
122 wfSuppressWarnings();
123 $file = fopen( $filename, 'r' );
128 $localHash=substr(fread($file,40),8);
130 if ($hash!=$localHash) {
133 require("$wgLocalMessageCache/messages-" . wfWikiID());
134 $this->setCache( $this->mCache
);
137 function saveToScript($array, $hash) {
138 global $wgLocalMessageCache;
139 if ( $wgLocalMessageCache === false ) {
143 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
144 $oldUmask = umask( 0 );
145 wfMkdirParents( $wgLocalMessageCache, 0777 );
147 $file = fopen( $filename.'.tmp', 'w');
148 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
150 foreach ($array as $key => $message) {
151 fwrite($file, "'". $this->escapeForScript($key).
152 "' => '" . $this->escapeForScript($message).
155 fwrite($file,");\n?>");
157 rename($filename.'.tmp',$filename);
160 function escapeForScript($string) {
161 $string = str_replace( '\\', '\\\\', $string );
162 $string = str_replace( '\'', '\\\'', $string );
167 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
169 function setCache( $cache ) {
170 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION
) {
171 $this->mCache
= $cache;
173 $this->mCache
= false;
178 * Loads messages either from memcached or the database, if not disabled
179 * On error, quietly switches to a fallback mode
180 * Returns false for a reportable error, true otherwise
183 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
185 if ( $this->mDisable
) {
186 static $shownDisabled = false;
187 if ( !$shownDisabled ) {
188 wfDebug( "MessageCache::load(): disabled\n" );
189 $shownDisabled = true;
193 if ( !$this->mUseCache
) {
194 $this->mDeferred
= false;
198 $fname = 'MessageCache::load';
199 wfProfileIn( $fname );
202 $this->mCache
= false;
205 wfProfileIn( $fname.'-fromlocal' );
206 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
208 if ($wgLocalMessageCacheSerialized) {
209 $this->loadFromLocal( $hash );
211 $this->loadFromScript( $hash );
213 if ( $this->mCache
) {
214 wfDebug( "MessageCache::load(): got from local cache\n" );
217 wfProfileOut( $fname.'-fromlocal' );
220 if ( !$this->mCache
) {
221 wfProfileIn( $fname.'-fromcache' );
222 $this->setCache( $this->mMemc
->get( $this->mMemcKey
) );
223 if ( $this->mCache
) {
224 wfDebug( "MessageCache::load(): got from global cache\n" );
225 # Save to local cache
226 if ( $wgLocalMessageCache !== false ) {
227 $serialized = serialize( $this->mCache
);
229 $hash = md5( $serialized );
230 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
232 if ($wgLocalMessageCacheSerialized) {
233 $this->saveToLocal( $serialized,$hash );
235 $this->saveToScript( $this->mCache
, $hash );
239 wfProfileOut( $fname.'-fromcache' );
243 # If there's nothing in memcached, load all the messages from the database
244 if ( !$this->mCache
) {
245 wfDebug( "MessageCache::load(): cache is empty\n" );
247 # Other threads don't need to load the messages if another thread is doing it.
248 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
250 wfProfileIn( $fname.'-load' );
251 wfDebug( "MessageCache::load(): loading all messages from DB\n" );
253 wfProfileOut( $fname.'-load' );
256 # Keep trying if it fails, this is kind of important
257 wfProfileIn( $fname.'-save' );
259 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
261 usleep(mt_rand(500000,1500000));
264 # Save to local cache
265 if ( $wgLocalMessageCache !== false ) {
266 $serialized = serialize( $this->mCache
);
267 $hash = md5( $serialized );
268 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
269 if ($wgLocalMessageCacheSerialized) {
270 $this->saveToLocal( $serialized,$hash );
272 $this->saveToScript( $this->mCache
, $hash );
276 wfProfileOut( $fname.'-save' );
278 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
279 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
281 $this->mMemc
->delete( $this->mMemcKey
.'-status' );
287 if ( !is_array( $this->mCache
) ) {
288 wfDebug( "MessageCache::load(): unable to load cache, disabled\n" );
289 $this->mDisable
= true;
290 $this->mCache
= false;
292 wfProfileOut( $fname );
293 $this->mDeferred
= false;
298 * Loads all or main part of cacheable messages from the database
300 function loadFromDB() {
301 global $wgLang, $wgMaxMsgCacheEntrySize;
303 wfProfileIn( __METHOD__
);
304 $dbr =& wfGetDB( DB_SLAVE
);
305 $this->mCache
= array();
307 # Load titles for all oversized pages in the MediaWiki namespace
308 $res = $dbr->select( 'page', 'page_title',
310 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ),
311 'page_is_redirect' => 0,
312 'page_namespace' => NS_MEDIAWIKI
,
315 while ( $row = $dbr->fetchObject( $res ) ) {
316 $this->mCache
[$row->page_title
] = '!TOO BIG';
318 $dbr->freeResult( $res );
320 # Load text for the remaining pages
321 $res = $dbr->select( array( 'page', 'revision', 'text' ),
322 array( 'page_title', 'old_text', 'old_flags' ),
324 'page_is_redirect' => 0,
325 'page_namespace' => NS_MEDIAWIKI
,
326 'page_latest=rev_id',
327 'rev_text_id=old_id',
328 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ),
331 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
332 $this->mCache
[$row->page_title
] = ' ' . Revision
::getRevisionText( $row );
334 $this->mCache
['VERSION'] = MSG_CACHE_VERSION
;
335 $dbr->freeResult( $res );
336 wfProfileOut( __METHOD__
);
340 * Not really needed anymore
344 if ( !$this->mKeys
) {
345 $this->mKeys
= array();
346 $allMessages = Language
::getMessagesFor( 'en' );
347 foreach ( $allMessages as $key => $unused ) {
348 $title = $wgContLang->ucfirst( $key );
349 array_push( $this->mKeys
, $title );
355 function replace( $title, $text ) {
356 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
357 global $wgMaxMsgCacheEntrySize;
359 wfProfileIn( __METHOD__
);
362 $parserMemc->delete(wfMemcKey('sidebar'));
363 if ( is_array( $this->mCache
) ) {
364 if ( $text === false ) {
365 # Article was deleted
366 unset( $this->mCache
[$title] );
367 $this->mMemc
->delete( "$this->mMemcKey:{$title}" );
368 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
369 $this->mCache
[$title] = '!TOO BIG';
370 $this->mMemc
->set( "$this->mMemcKey:{$title}", ' '.$text, $this->mExpiry
);
372 $this->mCache
[$title] = ' ' . $text;
373 $this->mMemc
->delete( "$this->mMemcKey:{$title}" );
375 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
377 # Save to local cache
378 if ( $wgLocalMessageCache !== false ) {
379 $serialized = serialize( $this->mCache
);
380 $hash = md5( $serialized );
381 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
382 if ($wgLocalMessageCacheSerialized) {
383 $this->saveToLocal( $serialized,$hash );
385 $this->saveToScript( $this->mCache
, $hash );
390 wfProfileOut( __METHOD__
);
395 * Represents a write lock on the messages key
398 if ( !$this->mUseCache
) {
402 $lockKey = $this->mMemcKey
. 'lock';
403 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
407 return $i >= MSG_WAIT_TIMEOUT
;
411 if ( !$this->mUseCache
) {
415 $lockKey = $this->mMemcKey
. 'lock';
416 $this->mMemc
->delete( $lockKey );
420 * Get a message from either the content language or the user language.
422 * @param string $key The message cache key
423 * @param bool $useDB Get the message from the DB, false to use only the localisation
424 * @param bool $forContent Get the message from the content language rather than the
426 * @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
428 function get( $key, $useDB = true, $forContent = true, $isFullKey = false ) {
429 global $wgContLanguageCode, $wgContLang, $wgLang;
431 $lang =& $wgContLang;
435 $langcode = $lang->getCode();
436 # If uninitialised, someone is trying to call this halfway through Setup.php
437 if( !$this->mInitialised
) {
438 return '<' . htmlspecialchars($key) . '>';
440 # If cache initialization was deferred, start it now.
441 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
447 # Normalise title-case input
448 $lckey = $wgContLang->lcfirst( $key );
449 $lckey = str_replace( ' ', '_', $lckey );
451 # Try the MediaWiki namespace
452 if( !$this->mDisable
&& $useDB ) {
453 $title = $wgContLang->ucfirst( $key );
454 if(!$isFullKey && ($langcode != $wgContLanguageCode) ) {
455 $title .= '/' . $langcode;
457 $message = $this->getMsgFromNamespace( $title );
459 # Try the extension array
460 if( $message === false && isset( $this->mExtensionMessages
[$langcode][$lckey] ) ) {
461 $message = $this->mExtensionMessages
[$langcode][$lckey];
463 if ( $message === false && isset( $this->mExtensionMessages
['en'][$lckey] ) ) {
464 $message = $this->mExtensionMessages
['en'][$lckey];
467 # Try the array in the language object
468 if( $message === false ) {
469 #wfDebug( "Trying language object for message $key\n" );
470 wfSuppressWarnings();
471 $message = $lang->getMessage( $lckey );
473 if ( is_null( $message ) ) {
478 # Try the array of another language
479 if( $message === false && strpos( $lckey, '/' ) ) {
480 $message = explode( '/', $lckey );
482 wfSuppressWarnings();
483 $message = Language
::getMessageFor( $message[0], $message[1] );
485 if ( is_null( $message ) ) {
493 # Is this a custom message? Try the default language in the db...
494 if( ($message === false ||
$message === '-' ) &&
495 !$this->mDisable
&& $useDB &&
496 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
497 $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $key ) );
501 if( $message === false ) {
502 return '<' . htmlspecialchars($key) . '>';
506 $message = $this->transform( $message );
511 * Get a message from the MediaWiki namespace, with caching. The key must
512 * first be converted to two-part lang/msg form if necessary.
514 * @param string $title Message cache key with initial uppercase letter
516 function getMsgFromNamespace( $title ) {
521 if( $this->mUseCache
&& isset( $this->mCache
[$title] ) ) {
522 $entry = $this->mCache
[$title];
523 $type = substr( $entry, 0, 1 );
524 if ( $type == ' ' ) {
525 return substr( $entry, 1 );
529 # Call message hooks, in case they are defined
530 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
531 if ( $message !== false ) {
535 # If there is no cache entry and no placeholder, it doesn't exist
536 if ( $type != '!' && $message === false ) {
540 $memcKey = $this->mMemcKey
. ':' . $title;
542 # Try the individual message cache
543 if ( $this->mUseCache
) {
544 $entry = $this->mMemc
->get( $memcKey );
546 $type = substr( $entry, 0, 1 );
548 if ( $type == ' ' ) {
549 $message = substr( $entry, 1 );
550 $this->mCache
[$title] = $message;
552 } elseif ( $entry == '!NONEXISTENT' ) {
555 # Corrupt/obsolete entry, delete it
556 $this->mMemc
->delete( $memcKey );
562 # Try loading it from the DB
563 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
565 $message = $revision->getText();
566 if ($this->mUseCache
) {
567 $this->mCache
[$title] = ' ' . $message;
568 $this->mMemc
->set( $memcKey, $message, $this->mExpiry
);
572 # Use some special text instead of false, because false gets converted to '' somewhere
573 $this->mMemc
->set( $memcKey, '!NONEXISTENT', $this->mExpiry
);
574 $this->mCache
[$title] = false;
580 function transform( $message ) {
582 if ( !$this->mParser
&& isset( $wgParser ) ) {
583 # Do some initialisation so that we don't have to do it twice
584 $wgParser->firstCallInit();
585 # Clone it and store it
586 $this->mParser
= clone $wgParser;
588 if ( !$this->mDisableTransform
&& $this->mParser
) {
589 if( strpos( $message, '{{' ) !== false ) {
590 $message = $this->mParser
->transformMsg( $message, $this->getParserOptions() );
596 function disable() { $this->mDisable
= true; }
597 function enable() { $this->mDisable
= false; }
598 function disableTransform() { $this->mDisableTransform
= true; }
599 function enableTransform() { $this->mDisableTransform
= false; }
600 function setTransform( $x ) { $this->mDisableTransform
= $x; }
601 function getTransform() { return $this->mDisableTransform
; }
604 * Add a message to the cache
607 * @param mixed $value
608 * @param string $lang The messages language, English by default
610 function addMessage( $key, $value, $lang = 'en' ) {
611 $this->mExtensionMessages
[$lang][$key] = $value;
615 * Add an associative array of message to the cache
617 * @param array $messages An associative array of key => values to be added
618 * @param string $lang The messages language, English by default
620 function addMessages( $messages, $lang = 'en' ) {
621 wfProfileIn( __METHOD__
);
622 if ( isset( $this->mExtensionMessages
[$lang] ) ) {
623 $this->mExtensionMessages
[$lang] = $messages +
$this->mExtensionMessages
[$lang];
625 $this->mExtensionMessages
[$lang] = $messages;
627 wfProfileOut( __METHOD__
);
631 * Add a 2-D array of messages by lang. Useful for extensions.
632 * Introduced in 1.9. Please do not use it for now, for backwards compatibility.
634 * @param array $messages The array to be added
636 function addMessagesByLang( $messages ) {
637 wfProfileIn( __METHOD__
);
638 foreach ( $messages as $key => $value ) {
639 $this->addMessages( $value, $key );
641 wfProfileOut( __METHOD__
);
645 * Get the extension messages for a specific language
647 * @param string $lang The messages language, English by default
649 function getExtensionMessagesFor( $lang = 'en' ) {
650 wfProfileIn( __METHOD__
);
652 if ( isset( $this->mExtensionMessages
[$lang] ) ) {
653 $messages = $this->mExtensionMessages
[$lang];
655 if ( $lang != 'en' ) {
656 $messages = $messages +
$this->mExtensionMessages
['en'];
658 wfProfileOut( __METHOD__
);
663 * Clear all stored messages. Mainly used after a mass rebuild.
666 global $wgLocalMessageCache;
667 if( $this->mUseCache
) {
669 $this->mMemc
->delete( $this->mMemcKey
);
670 # Invalidate all local caches
671 $this->mMemc
->delete( "{$this->mMemcKey}-hash" );
675 static function loadAllMessages() {
676 # Some extensions will load their messages when you load their class file
677 wfLoadAllExtensions();
678 # Others will respond to this hook
679 wfRunHooks( 'LoadAllMessages' );
680 # Still others will respond to neither, they are EVIL. We sometimes need to know!