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
25 var $mCache, $mUseCache, $mDisable, $mExpiry;
26 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
27 var $mExtensionMessages = array();
28 var $mInitialised = false;
29 var $mDeferred = true;
31 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
32 $fname = 'MessageCache::initialise';
33 wfProfileIn( $fname );
35 $this->mUseCache
= !is_null( $memCached );
36 $this->mMemc
= &$memCached;
37 $this->mDisable
= !$useDB;
38 $this->mExpiry
= $expiry;
39 $this->mDisableTransform
= false;
40 $this->mMemcKey
= $memcPrefix.':messages';
41 $this->mKeys
= false; # initialised on demand
42 $this->mInitialised
= true;
44 wfProfileIn( $fname.'-parseropt' );
45 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
46 wfProfileOut( $fname.'-parseropt' );
47 wfProfileIn( $fname.'-parser' );
48 $this->mParser
= new Parser
;
49 wfProfileOut( $fname.'-parser' );
51 # When we first get asked for a message,
52 # then we'll fill up the cache. If we
53 # can return a cache hit, this saves
54 # some extra milliseconds
55 $this->mDeferred
= true;
57 wfProfileOut( $fname );
61 * Try to load the cache from a local file
63 function loadFromLocal( $hash ) {
64 global $wgLocalMessageCache, $wgDBname;
66 $this->mCache
= false;
67 if ( $wgLocalMessageCache === false ) {
71 $filename = "$wgLocalMessageCache/messages-$wgDBname";
73 $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, 1000000 );
83 $this->mCache
= unserialize( $serialized );
89 * Save the cache to a local file
91 function saveToLocal( $serialized, $hash ) {
92 global $wgLocalMessageCache, $wgDBname;
94 if ( $wgLocalMessageCache === false ) {
98 $filename = "$wgLocalMessageCache/messages-$wgDBname";
99 wfMkdirParents( $wgLocalMessageCache, 0777 );
101 $file = fopen( $filename, 'w' );
103 wfDebug( "Unable to open local cache file for writing\n" );
107 fwrite( $file, $hash . $serialized );
113 * Loads messages either from memcached or the database, if not disabled
114 * On error, quietly switches to a fallback mode
115 * Returns false for a reportable error, true otherwise
118 global $wgLocalMessageCache;
120 if ( $this->mDisable
) {
121 static $shownDisabled = false;
122 if ( !$shownDisabled ) {
123 wfDebug( "MessageCache::load(): disabled\n" );
124 $shownDisabled = true;
128 $fname = 'MessageCache::load';
129 wfProfileIn( $fname );
132 if ( $this->mUseCache
) {
133 $this->mCache
= false;
136 wfProfileIn( $fname.'-fromlocal' );
137 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
139 $this->loadFromLocal( $hash );
141 wfProfileOut( $fname.'-fromlocal' );
144 if ( !$this->mCache
) {
145 wfProfileIn( $fname.'-fromcache' );
146 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
148 # Save to local cache
149 if ( $wgLocalMessageCache !== false ) {
150 $serialized = serialize( $this->mCache
);
152 $hash = md5( $serialized );
153 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
155 $this->saveToLocal( $serialized, $hash );
157 wfProfileOut( $fname.'-fromcache' );
161 # If there's nothing in memcached, load all the messages from the database
162 if ( !$this->mCache
) {
163 wfDebug( "MessageCache::load(): loading all messages\n" );
165 # Other threads don't need to load the messages if another thread is doing it.
166 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
168 wfProfileIn( $fname.'-load' );
170 wfProfileOut( $fname.'-load' );
173 # Keep trying if it fails, this is kind of important
174 wfProfileIn( $fname.'-save' );
176 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
178 usleep(mt_rand(500000,1500000));
181 # Save to local cache
182 if ( $wgLocalMessageCache !== false ) {
183 $serialized = serialize( $this->mCache
);
184 $hash = md5( $serialized );
185 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
186 $this->saveToLocal( $serialized, $hash );
189 wfProfileOut( $fname.'-save' );
191 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
192 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
198 if ( !is_array( $this->mCache
) ) {
199 wfDebug( "MessageCache::load(): individual message mode\n" );
200 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
201 # Causing too much DB load, disabling -- TS
202 $this->mDisable
= true;
204 if ( $this->mCache == "loading" ) {
205 $this->mUseCache = false;
206 } elseif ( $this->mCache == "error" ) {
207 $this->mUseCache = false;
210 $this->mDisable = true;
213 $this->mCache
= false;
216 wfProfileOut( $fname );
217 $this->mDeferred
= false;
222 * Loads all or main part of cacheable messages from the database
224 function loadFromDB() {
225 global $wgAllMessagesEn, $wgLang;
227 $fname = 'MessageCache::loadFromDB';
228 $dbr =& wfGetDB( DB_SLAVE
);
230 wfDebugDieBacktrace( 'Invalid database object' );
232 $conditions = array( 'page_is_redirect' => 0,
233 'page_namespace' => NS_MEDIAWIKI
);
234 $res = $dbr->select( array( 'page', 'revision', 'text' ),
235 array( 'page_title', 'old_text', 'old_flags' ),
236 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
240 $this->mCache
= array();
241 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
242 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
246 # Go through the language array and the extension array and make a note of
247 # any keys missing from the cache
248 foreach ( $wgAllMessagesEn as $key => $value ) {
249 $uckey = $wgLang->ucfirst( $key );
250 if ( !array_key_exists( $uckey, $this->mCache
) ) {
251 $this->mCache
[$uckey] = false;
254 foreach ( $this->mExtensionMessages
as $key => $value ) {
255 $uckey = $wgLang->ucfirst( $key );
256 if ( !array_key_exists( $uckey, $this->mCache
) ) {
257 $this->mCache
[$uckey] = false;
261 $dbr->freeResult( $res );
265 * Not really needed anymore
268 global $wgAllMessagesEn, $wgContLang;
269 if ( !$this->mKeys
) {
270 $this->mKeys
= array();
271 foreach ( $wgAllMessagesEn as $key => $value ) {
272 $title = $wgContLang->ucfirst( $key );
273 array_push( $this->mKeys
, $title );
282 function isCacheable( $key ) {
286 function replace( $title, $text ) {
287 global $wgLocalMessageCache;
291 if ( is_array( $this->mCache
) ) {
292 $this->mCache
[$title] = $text;
293 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
295 # Save to local cache
296 if ( $wgLocalMessageCache !== false ) {
297 $serialized = serialize( $this->mCache
);
298 $hash = md5( $serialized );
299 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
300 $this->saveToLocal( $serialized, $hash );
310 * Represents a write lock on the messages key
313 if ( !$this->mUseCache
) {
317 $lockKey = $this->mMemcKey
. 'lock';
318 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
322 return $i >= MSG_WAIT_TIMEOUT
;
326 if ( !$this->mUseCache
) {
330 $lockKey = $this->mMemcKey
. 'lock';
331 $this->mMemc
->delete( $lockKey );
334 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
335 global $wgContLanguageCode;
338 $lang =& $wgContLang;
339 $langcode = $wgContLanguageCode;
341 global $wgLang, $wgLanguageCode;
343 $langcode = $wgLanguageCode;
345 # If uninitialised, someone is trying to call this halfway through Setup.php
346 if( !$this->mInitialised
) {
347 return '<' . htmlspecialchars($key) . '>';
349 # If cache initialization was deferred, start it now.
350 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
355 if( !$this->mDisable
&& $useDB ) {
356 $title = $lang->ucfirst( $key );
357 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
358 $title .= '/' . $langcode;
360 $message = $this->getFromCache( $title );
362 # Try the extension array
363 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
364 $message = $this->mExtensionMessages
[$key];
367 # Try the array in the language object
368 if( $message === false ) {
369 wfSuppressWarnings();
370 $message = $lang->getMessage( $key );
372 if ( is_null( $message ) ) {
377 # Try the English array
378 if( $message === false && $langcode != 'en' ) {
379 wfSuppressWarnings();
380 $message = Language
::getMessage( $key );
382 if ( is_null( $message ) ) {
387 # Is this a custom message? Try the default language in the db...
388 if( $message === false &&
389 !$this->mDisable
&& $useDB &&
390 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
391 $message = $this->getFromCache( $lang->ucfirst( $key ) );
395 if( $message === false ) {
396 return '<' . htmlspecialchars($key) . '>';
400 $message = $this->transform( $message );
404 function getFromCache( $title ) {
408 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
409 return $this->mCache
[$title];
412 # Try individual message cache
413 if ( $this->mUseCache
) {
414 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
415 if ( $message == '###NONEXISTENT###' ) {
417 } elseif( !is_null( $message ) ) {
418 $this->mCache
[$title] = $message;
425 # If it wasn't in the cache, load each message from the DB individually
426 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
428 $message = $revision->getText();
429 if ($this->mUseCache
) {
430 $this->mCache
[$title]=$message;
431 /* individual messages may be often
432 recached until proper purge code exists
434 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
438 # Use some special text instead of false, because false gets converted to '' somewhere
439 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
445 function transform( $message ) {
446 if( !$this->mDisableTransform
) {
447 if( strpos( $message, '{{' ) !== false ) {
448 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
454 function disable() { $this->mDisable
= true; }
455 function enable() { $this->mDisable
= false; }
456 function disableTransform() { $this->mDisableTransform
= true; }
457 function enableTransform() { $this->mDisableTransform
= false; }
458 function setTransform( $x ) { $this->mDisableTransform
= $x; }
459 function getTransform() { return $this->mDisableTransform
; }
462 * Add a message to the cache
465 * @param mixed $value
467 function addMessage( $key, $value ) {
468 $this->mExtensionMessages
[$key] = $value;
472 * Add an associative array of message to the cache
474 * @param array $messages An associative array of key => values to be added
476 function addMessages( $messages ) {
477 foreach ( $messages as $key => $value ) {
478 $this->addMessage( $key, $value );
483 * Clear all stored messages. Mainly used after a mass rebuild.
486 if( $this->mUseCache
) {
487 $this->mMemc
->delete( $this->mMemcKey
);