9 require_once( 'Revision.php' );
14 define( 'MSG_LOAD_TIMEOUT', 60);
15 define( 'MSG_LOCK_TIMEOUT', 10);
16 define( 'MSG_WAIT_TIMEOUT', 10);
20 * Performs various useful MediaWiki namespace-related functions
30 $mExtensionMessages = array(),
31 $mInitialised = false,
38 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
39 $fname = 'MessageCache::initialise';
40 wfProfileIn( $fname );
42 $this->mUseCache
= !is_null( $memCached );
43 $this->mMemc
= &$memCached;
44 $this->mDisable
= !$useDB;
45 $this->mExpiry
= $expiry;
46 $this->mDisableTransform
= false;
47 $this->mMemcKey
= $memcPrefix.':messages';
48 $this->mKeys
= false; # initialised on demand
49 $this->mInitialised
= true;
51 wfProfileIn( $fname.'-parseropt' );
52 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
53 wfProfileOut( $fname.'-parseropt' );
54 wfProfileIn( $fname.'-parser' );
55 $this->mParser
= new Parser
;
56 wfProfileOut( $fname.'-parser' );
58 # When we first get asked for a message,
59 # then we'll fill up the cache. If we
60 # can return a cache hit, this saves
61 # some extra milliseconds
62 $this->mDeferred
= true;
64 wfProfileOut( $fname );
68 * Try to load the cache from a local file
70 function loadFromLocal( $hash ) {
71 global $wgLocalMessageCache, $wgDBname;
73 $this->mCache
= false;
74 if ( $wgLocalMessageCache === false ) {
78 $filename = "$wgLocalMessageCache/messages-$wgDBname";
81 $file = fopen( $filename, 'r' );
87 // Check to see if the file has the hash specified
88 $localHash = fread( $file, 32 );
89 if ( $hash == $localHash ) {
90 // All good, get the rest of it
91 $serialized = fread( $file, 1000000 );
92 $this->mCache
= unserialize( $serialized );
98 * Save the cache to a local file
100 function saveToLocal( $serialized, $hash ) {
101 global $wgLocalMessageCache, $wgDBname;
103 if ( $wgLocalMessageCache === false ) {
107 $filename = "$wgLocalMessageCache/messages-$wgDBname";
108 $oldUmask = umask( 0 );
109 wfMkdirParents( $wgLocalMessageCache, 0777 );
112 $file = fopen( $filename, 'w' );
114 wfDebug( "Unable to open local cache file for writing\n" );
118 fwrite( $file, $hash . $serialized );
120 @chmod
( $filename, 0666 );
125 * Loads messages either from memcached or the database, if not disabled
126 * On error, quietly switches to a fallback mode
127 * Returns false for a reportable error, true otherwise
130 global $wgLocalMessageCache;
132 if ( $this->mDisable
) {
133 static $shownDisabled = false;
134 if ( !$shownDisabled ) {
135 wfDebug( "MessageCache::load(): disabled\n" );
136 $shownDisabled = true;
140 $fname = 'MessageCache::load';
141 wfProfileIn( $fname );
144 if ( $this->mUseCache
) {
145 $this->mCache
= false;
148 wfProfileIn( $fname.'-fromlocal' );
149 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
151 $this->loadFromLocal( $hash );
153 wfProfileOut( $fname.'-fromlocal' );
156 if ( !$this->mCache
) {
157 wfProfileIn( $fname.'-fromcache' );
158 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
160 # Save to local cache
161 if ( $wgLocalMessageCache !== false ) {
162 $serialized = serialize( $this->mCache
);
164 $hash = md5( $serialized );
165 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
167 $this->saveToLocal( $serialized, $hash );
169 wfProfileOut( $fname.'-fromcache' );
173 # If there's nothing in memcached, load all the messages from the database
174 if ( !$this->mCache
) {
175 wfDebug( "MessageCache::load(): loading all messages\n" );
177 # Other threads don't need to load the messages if another thread is doing it.
178 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
180 wfProfileIn( $fname.'-load' );
182 wfProfileOut( $fname.'-load' );
185 # Keep trying if it fails, this is kind of important
186 wfProfileIn( $fname.'-save' );
188 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
190 usleep(mt_rand(500000,1500000));
193 # Save to local cache
194 if ( $wgLocalMessageCache !== false ) {
195 $serialized = serialize( $this->mCache
);
196 $hash = md5( $serialized );
197 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
198 $this->saveToLocal( $serialized, $hash );
201 wfProfileOut( $fname.'-save' );
203 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
204 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
210 if ( !is_array( $this->mCache
) ) {
211 wfDebug( "MessageCache::load(): individual message mode\n" );
212 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
213 # Causing too much DB load, disabling -- TS
214 $this->mDisable
= true;
216 if ( $this->mCache == "loading" ) {
217 $this->mUseCache = false;
218 } elseif ( $this->mCache == "error" ) {
219 $this->mUseCache = false;
222 $this->mDisable = true;
225 $this->mCache
= false;
228 wfProfileOut( $fname );
229 $this->mDeferred
= false;
234 * Loads all or main part of cacheable messages from the database
236 function loadFromDB() {
237 global $wgAllMessagesEn, $wgLang;
239 $fname = 'MessageCache::loadFromDB';
240 $dbr =& wfGetDB( DB_SLAVE
);
242 wfDebugDieBacktrace( 'Invalid database object' );
244 $conditions = array( 'page_is_redirect' => 0,
245 'page_namespace' => NS_MEDIAWIKI
);
246 $res = $dbr->select( array( 'page', 'revision', 'text' ),
247 array( 'page_title', 'old_text', 'old_flags' ),
248 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
252 $this->mCache
= array();
253 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
254 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
258 # Go through the language array and the extension array and make a note of
259 # any keys missing from the cache
260 foreach ( $wgAllMessagesEn as $key => $value ) {
261 $uckey = $wgLang->ucfirst( $key );
262 if ( !array_key_exists( $uckey, $this->mCache
) ) {
263 $this->mCache
[$uckey] = false;
266 foreach ( $this->mExtensionMessages
as $key => $value ) {
267 $uckey = $wgLang->ucfirst( $key );
268 if ( !array_key_exists( $uckey, $this->mCache
) ) {
269 $this->mCache
[$uckey] = false;
273 $dbr->freeResult( $res );
277 * Not really needed anymore
280 global $wgAllMessagesEn, $wgContLang;
281 if ( !$this->mKeys
) {
282 $this->mKeys
= array();
283 foreach ( $wgAllMessagesEn as $key => $value ) {
284 $title = $wgContLang->ucfirst( $key );
285 array_push( $this->mKeys
, $title );
294 function isCacheable( $key ) {
298 function replace( $title, $text ) {
299 global $wgLocalMessageCache, $parserMemc, $wgDBname;
303 $parserMemc->delete("$wgDBname:sidebar");
304 if ( is_array( $this->mCache
) ) {
305 $this->mCache
[$title] = $text;
306 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
308 # Save to local cache
309 if ( $wgLocalMessageCache !== false ) {
310 $serialized = serialize( $this->mCache
);
311 $hash = md5( $serialized );
312 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
313 $this->saveToLocal( $serialized, $hash );
323 * Represents a write lock on the messages key
326 if ( !$this->mUseCache
) {
330 $lockKey = $this->mMemcKey
. 'lock';
331 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
335 return $i >= MSG_WAIT_TIMEOUT
;
339 if ( !$this->mUseCache
) {
343 $lockKey = $this->mMemcKey
. 'lock';
344 $this->mMemc
->delete( $lockKey );
347 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
348 global $wgContLanguageCode;
351 $lang =& $wgContLang;
352 $langcode = $wgContLanguageCode;
354 global $wgLang, $wgLanguageCode;
356 $langcode = $wgLanguageCode;
358 # If uninitialised, someone is trying to call this halfway through Setup.php
359 if( !$this->mInitialised
) {
360 return '<' . htmlspecialchars($key) . '>';
362 # If cache initialization was deferred, start it now.
363 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
368 if( !$this->mDisable
&& $useDB ) {
369 $title = $lang->ucfirst( $key );
370 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
371 $title .= '/' . $langcode;
373 $message = $this->getFromCache( $title );
375 # Try the extension array
376 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
377 $message = $this->mExtensionMessages
[$key];
380 # Try the array in the language object
381 if( $message === false ) {
382 wfSuppressWarnings();
383 $message = $lang->getMessage( $key );
385 if ( is_null( $message ) ) {
390 # Try the English array
391 if( $message === false && $langcode != 'en' ) {
392 wfSuppressWarnings();
393 $message = Language
::getMessage( $key );
395 if ( is_null( $message ) ) {
400 # Is this a custom message? Try the default language in the db...
401 if( $message === false &&
402 !$this->mDisable
&& $useDB &&
403 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
404 $message = $this->getFromCache( $lang->ucfirst( $key ) );
408 if( $message === false ) {
409 return '<' . htmlspecialchars($key) . '>';
413 $message = $this->transform( $message );
417 function getFromCache( $title ) {
421 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
422 return $this->mCache
[$title];
425 # Try individual message cache
426 if ( $this->mUseCache
) {
427 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
428 if ( $message == '###NONEXISTENT###' ) {
430 } elseif( !is_null( $message ) ) {
431 $this->mCache
[$title] = $message;
438 # Call message Hooks, in case they are defined
439 wfRunHooks('MessagesPreLoad',array($title,&$message));
441 # If it wasn't in the cache, load each message from the DB individually
442 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
444 $message = $revision->getText();
445 if ($this->mUseCache
) {
446 $this->mCache
[$title]=$message;
447 /* individual messages may be often
448 recached until proper purge code exists
450 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
454 # Use some special text instead of false, because false gets converted to '' somewhere
455 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
461 function transform( $message ) {
462 if( !$this->mDisableTransform
) {
463 if( strpos( $message, '{{' ) !== false ) {
464 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
470 function disable() { $this->mDisable
= true; }
471 function enable() { $this->mDisable
= false; }
472 function disableTransform() { $this->mDisableTransform
= true; }
473 function enableTransform() { $this->mDisableTransform
= false; }
474 function setTransform( $x ) { $this->mDisableTransform
= $x; }
475 function getTransform() { return $this->mDisableTransform
; }
478 * Add a message to the cache
481 * @param mixed $value
483 function addMessage( $key, $value ) {
484 $this->mExtensionMessages
[$key] = $value;
488 * Add an associative array of message to the cache
490 * @param array $messages An associative array of key => values to be added
492 function addMessages( $messages ) {
493 foreach ( $messages as $key => $value ) {
494 $this->addMessage( $key, $value );
499 * Clear all stored messages. Mainly used after a mass rebuild.
502 if( $this->mUseCache
) {
503 $this->mMemc
->delete( $this->mMemcKey
);