11 define( 'MSG_LOAD_TIMEOUT', 60);
12 define( 'MSG_LOCK_TIMEOUT', 10);
13 define( 'MSG_WAIT_TIMEOUT', 10);
17 * Performs various useful MediaWiki namespace-related functions
22 var $mCache, $mUseCache, $mDisable, $mExpiry;
23 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
24 var $mExtensionMessages = array();
25 var $mInitialised = false;
26 var $mDeferred = true;
28 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
29 wfProfileIn( __METHOD__
);
31 $this->mUseCache
= !is_null( $memCached );
32 $this->mMemc
= &$memCached;
33 $this->mDisable
= !$useDB;
34 $this->mExpiry
= $expiry;
35 $this->mDisableTransform
= false;
36 $this->mMemcKey
= $memcPrefix.':messages';
37 $this->mKeys
= false; # initialised on demand
38 $this->mInitialised
= true;
39 $this->mParser
= null;
41 # When we first get asked for a message,
42 # then we'll fill up the cache. If we
43 # can return a cache hit, this saves
44 # some extra milliseconds
45 $this->mDeferred
= true;
47 wfProfileOut( __METHOD__
);
50 function getParserOptions() {
51 if ( !$this->mParserOptions
) {
52 $this->mParserOptions
= new ParserOptions
;
54 return $this->mParserOptions
;
58 * Try to load the cache from a local file
60 function loadFromLocal( $hash ) {
61 global $wgLocalMessageCache;
63 $this->mCache
= false;
64 if ( $wgLocalMessageCache === false ) {
68 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
71 $file = fopen( $filename, 'r' );
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
81 $serialized = fread( $file, 10000000 );
82 $this->mCache
= unserialize( $serialized );
88 * Save the cache to a local file
90 function saveToLocal( $serialized, $hash ) {
91 global $wgLocalMessageCache;
93 if ( $wgLocalMessageCache === false ) {
97 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
98 $oldUmask = umask( 0 );
99 wfMkdirParents( $wgLocalMessageCache, 0777 );
102 $file = fopen( $filename, 'w' );
104 wfDebug( "Unable to open local cache file for writing\n" );
108 fwrite( $file, $hash . $serialized );
110 @chmod
( $filename, 0666 );
113 function loadFromScript( $hash ) {
114 global $wgLocalMessageCache;
115 if ( $wgLocalMessageCache === false ) {
119 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
121 wfSuppressWarnings();
122 $file = fopen( $filename, 'r' );
127 $localHash=substr(fread($file,40),8);
129 if ($hash!=$localHash) {
132 require("$wgLocalMessageCache/messages-" . wfWikiID());
135 function saveToScript($array, $hash) {
136 global $wgLocalMessageCache;
137 if ( $wgLocalMessageCache === false ) {
141 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
142 $oldUmask = umask( 0 );
143 wfMkdirParents( $wgLocalMessageCache, 0777 );
145 $file = fopen( $filename.'.tmp', 'w');
146 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
148 foreach ($array as $key => $message) {
149 fwrite($file, "'". $this->escapeForScript($key).
150 "' => '" . $this->escapeForScript($message).
153 fwrite($file,");\n?>");
155 rename($filename.'.tmp',$filename);
158 function escapeForScript($string) {
159 $string = str_replace( '\\', '\\\\', $string );
160 $string = str_replace( '\'', '\\\'', $string );
165 * Loads messages either from memcached or the database, if not disabled
166 * On error, quietly switches to a fallback mode
167 * Returns false for a reportable error, true otherwise
170 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
172 if ( $this->mDisable
) {
173 static $shownDisabled = false;
174 if ( !$shownDisabled ) {
175 wfDebug( "MessageCache::load(): disabled\n" );
176 $shownDisabled = true;
180 $fname = 'MessageCache::load';
181 wfProfileIn( $fname );
184 if ( $this->mUseCache
) {
185 $this->mCache
= false;
188 wfProfileIn( $fname.'-fromlocal' );
189 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
191 if ($wgLocalMessageCacheSerialized) {
192 $this->loadFromLocal( $hash );
194 $this->loadFromScript( $hash );
196 if ( $this->mCache
) {
197 wfDebug( "MessageCache::load(): got from local cache\n" );
200 wfProfileOut( $fname.'-fromlocal' );
203 if ( !$this->mCache
) {
204 wfProfileIn( $fname.'-fromcache' );
205 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
206 if ( $this->mCache
) {
207 wfDebug( "MessageCache::load(): got from global cache\n" );
208 # Save to local cache
209 if ( $wgLocalMessageCache !== false ) {
210 $serialized = serialize( $this->mCache
);
212 $hash = md5( $serialized );
213 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
215 if ($wgLocalMessageCacheSerialized) {
216 $this->saveToLocal( $serialized,$hash );
218 $this->saveToScript( $this->mCache
, $hash );
222 wfProfileOut( $fname.'-fromcache' );
226 # If there's nothing in memcached, load all the messages from the database
227 if ( !$this->mCache
) {
228 wfDebug( "MessageCache::load(): loading all messages\n" );
230 # Other threads don't need to load the messages if another thread is doing it.
231 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
233 wfProfileIn( $fname.'-load' );
235 wfProfileOut( $fname.'-load' );
238 # Keep trying if it fails, this is kind of important
239 wfProfileIn( $fname.'-save' );
241 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
243 usleep(mt_rand(500000,1500000));
246 # Save to local cache
247 if ( $wgLocalMessageCache !== false ) {
248 $serialized = serialize( $this->mCache
);
249 $hash = md5( $serialized );
250 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
251 if ($wgLocalMessageCacheSerialized) {
252 $this->saveToLocal( $serialized,$hash );
254 $this->saveToScript( $this->mCache
, $hash );
258 wfProfileOut( $fname.'-save' );
260 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
261 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
267 if ( !is_array( $this->mCache
) ) {
268 wfDebug( "MessageCache::load(): individual message mode\n" );
269 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
270 # Causing too much DB load, disabling -- TS
271 $this->mDisable
= true;
273 if ( $this->mCache == "loading" ) {
274 $this->mUseCache = false;
275 } elseif ( $this->mCache == "error" ) {
276 $this->mUseCache = false;
279 $this->mDisable = true;
282 $this->mCache
= false;
285 wfProfileOut( $fname );
286 $this->mDeferred
= false;
291 * Loads all or main part of cacheable messages from the database
293 function loadFromDB() {
296 $fname = 'MessageCache::loadFromDB';
297 $dbr =& wfGetDB( DB_SLAVE
);
299 throw new MWException( 'Invalid database object' );
301 $res = $dbr->select( array( 'page', 'revision', 'text' ),
302 array( 'page_title', 'old_text', 'old_flags' ),
303 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
307 $this->mCache
= array();
308 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
309 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
313 # Go through the language array and the extension array and make a note of
314 # any keys missing from the cache
315 $allMessages = Language
::getMessagesFor( 'en' );
316 foreach ( $allMessages as $key => $unused ) {
317 $uckey = $wgLang->ucfirst( $key );
318 if ( !array_key_exists( $uckey, $this->mCache
) ) {
319 $this->mCache
[$uckey] = false;
323 # Make sure all extension messages are available
324 MessageCache
::loadAllMessages();
326 # Add them to the cache
327 foreach ( $this->mExtensionMessages
as $key => $unused ) {
328 $uckey = $wgLang->ucfirst( $key );
329 if ( !array_key_exists( $uckey, $this->mCache
) &&
330 ( isset( $this->mExtensionMessages
[$key][$wgLang->getCode()] ) ||
isset( $this->mExtensionMessages
[$key]['en'] ) ) ) {
331 $this->mCache
[$uckey] = false;
335 $dbr->freeResult( $res );
339 * Not really needed anymore
343 if ( !$this->mKeys
) {
344 $this->mKeys
= array();
345 $allMessages = Language
::getMessagesFor( 'en' );
346 foreach ( $allMessages as $key => $unused ) {
347 $title = $wgContLang->ucfirst( $key );
348 array_push( $this->mKeys
, $title );
357 function isCacheable( $key ) {
361 function replace( $title, $text ) {
362 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
366 $parserMemc->delete(wfMemcKey('sidebar'));
367 if ( is_array( $this->mCache
) ) {
368 $this->mCache
[$title] = $text;
369 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
371 # Save to local cache
372 if ( $wgLocalMessageCache !== false ) {
373 $serialized = serialize( $this->mCache
);
374 $hash = md5( $serialized );
375 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
376 if ($wgLocalMessageCacheSerialized) {
377 $this->saveToLocal( $serialized,$hash );
379 $this->saveToScript( $this->mCache
, $hash );
390 * Represents a write lock on the messages key
393 if ( !$this->mUseCache
) {
397 $lockKey = $this->mMemcKey
. 'lock';
398 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
402 return $i >= MSG_WAIT_TIMEOUT
;
406 if ( !$this->mUseCache
) {
410 $lockKey = $this->mMemcKey
. 'lock';
411 $this->mMemc
->delete( $lockKey );
414 function get( $key, $useDB = true, $forcontent = true, $isfullkey = false ) {
415 global $wgContLanguageCode, $wgContLang, $wgLang;
417 $lang =& $wgContLang;
421 $langcode = $lang->getCode();
422 # If uninitialised, someone is trying to call this halfway through Setup.php
423 if( !$this->mInitialised
) {
424 return '<' . htmlspecialchars($key) . '>';
426 # If cache initialization was deferred, start it now.
427 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
432 if( !$this->mDisable
&& $useDB ) {
433 $title = $wgContLang->ucfirst( $key );
434 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
435 $title .= '/' . $langcode;
437 $message = $this->getFromCache( $title );
439 # Try the extension array
440 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
441 if ( isset( $this->mExtensionMessages
[$key][$langcode] ) ) {
442 $message = $this->mExtensionMessages
[$key][$langcode];
443 } elseif ( isset( $this->mExtensionMessages
[$key]['en'] ) ) {
444 $message = $this->mExtensionMessages
[$key]['en'];
448 # Try the array in the language object
449 if( $message === false ) {
450 #wfDebug( "Trying language object for message $key\n" );
451 wfSuppressWarnings();
452 $message = $lang->getMessage( $key );
454 if ( is_null( $message ) ) {
459 # Try the array of another language
460 if( $message === false && strpos( $key, '/' ) ) {
461 $message = explode( '/', $key );
463 wfSuppressWarnings();
464 $message = Language
::getMessageFor( $message[0], $message[1] );
466 if ( is_null( $message ) ) {
474 # Is this a custom message? Try the default language in the db...
475 if( ($message === false ||
$message === '-' ) &&
476 !$this->mDisable
&& $useDB &&
477 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
478 $message = $this->getFromCache( $wgContLang->ucfirst( $key ) );
482 if( $message === false ) {
483 return '<' . htmlspecialchars($key) . '>';
487 $message = $this->transform( $message );
491 function getFromCache( $title ) {
495 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
496 return $this->mCache
[$title];
499 # Try individual message cache
500 if ( $this->mUseCache
) {
501 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
502 if ( $message == '###NONEXISTENT###' ) {
503 $this->mCache
[$title] = false;
505 } elseif( !is_null( $message ) ) {
506 $this->mCache
[$title] = $message;
513 # Call message Hooks, in case they are defined
514 wfRunHooks('MessagesPreLoad',array($title,&$message));
516 # If it wasn't in the cache, load each message from the DB individually
517 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
519 $message = $revision->getText();
520 if ($this->mUseCache
) {
521 $this->mCache
[$title]=$message;
522 /* individual messages may be often
523 recached until proper purge code exists
525 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
529 # Use some special text instead of false, because false gets converted to '' somewhere
530 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
531 $this->mCache
[$title] = false;
537 function transform( $message ) {
539 if ( !$this->mParser
&& isset( $wgParser ) ) {
540 # Do some initialisation so that we don't have to do it twice
541 $wgParser->firstCallInit();
542 # Clone it and store it
543 $this->mParser
= clone $wgParser;
545 if ( !$this->mDisableTransform
&& $this->mParser
) {
546 if( strpos( $message, '{{' ) !== false ) {
547 $message = $this->mParser
->transformMsg( $message, $this->getParserOptions() );
553 function disable() { $this->mDisable
= true; }
554 function enable() { $this->mDisable
= false; }
555 function disableTransform() { $this->mDisableTransform
= true; }
556 function enableTransform() { $this->mDisableTransform
= false; }
557 function setTransform( $x ) { $this->mDisableTransform
= $x; }
558 function getTransform() { return $this->mDisableTransform
; }
561 * Add a message to the cache
564 * @param mixed $value
565 * @param string $lang The messages language, English by default
567 function addMessage( $key, $value, $lang = 'en' ) {
568 $this->mExtensionMessages
[$key][$lang] = $value;
572 * Add an associative array of message to the cache
574 * @param array $messages An associative array of key => values to be added
575 * @param string $lang The messages language, English by default
577 function addMessages( $messages, $lang = 'en' ) {
578 wfProfileIn( __METHOD__
);
579 foreach ( $messages as $key => $value ) {
580 $this->addMessage( $key, $value, $lang );
582 wfProfileOut( __METHOD__
);
586 * Get the extension messages for a specific language
588 * @param string $lang The messages language, English by default
590 function getExtensionMessagesFor( $lang = 'en' ) {
591 wfProfileIn( __METHOD__
);
593 foreach( $this->mExtensionMessages
as $key => $message ) {
594 if ( isset( $message[$lang] ) ) {
595 $messages[$key] = $message[$lang];
596 } elseif ( isset( $message['en'] ) ) {
597 $messages[$key] = $message['en'];
600 wfProfileOut( __METHOD__
);
605 * Clear all stored messages. Mainly used after a mass rebuild.
608 global $wgLocalMessageCache;
609 if( $this->mUseCache
) {
611 $this->mMemc
->delete( $this->mMemcKey
);
612 # Invalidate all local caches
613 $this->mMemc
->delete( "{$this->mMemcKey}-hash" );
617 static function loadAllMessages() {
618 # Some extensions will load their messages when you load their class file
619 wfLoadAllExtensions();
620 # Others will respond to this hook
621 wfRunHooks( 'LoadAllMessages' );
622 # Still others will respond to neither, they are EVIL. We sometimes need to know!