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";
74 $file = fopen( $filename, 'r' );
80 // Check to see if the file has the hash specified
81 $localHash = fread( $file, 32 );
82 if ( $hash == $localHash ) {
83 // All good, get the rest of it
84 $serialized = fread( $file, 1000000 );
85 $this->mCache
= unserialize( $serialized );
91 * Save the cache to a local file
93 function saveToLocal( $serialized, $hash ) {
94 global $wgLocalMessageCache, $wgDBname;
96 if ( $wgLocalMessageCache === false ) {
100 $filename = "$wgLocalMessageCache/messages-$wgDBname";
101 $oldUmask = umask( 0 );
102 wfMkdirParents( $wgLocalMessageCache, 0777 );
105 $file = fopen( $filename, 'w' );
107 wfDebug( "Unable to open local cache file for writing\n" );
111 fwrite( $file, $hash . $serialized );
113 @chmod
( $filename, 0666 );
118 * Loads messages either from memcached or the database, if not disabled
119 * On error, quietly switches to a fallback mode
120 * Returns false for a reportable error, true otherwise
123 global $wgLocalMessageCache;
125 if ( $this->mDisable
) {
126 static $shownDisabled = false;
127 if ( !$shownDisabled ) {
128 wfDebug( "MessageCache::load(): disabled\n" );
129 $shownDisabled = true;
133 $fname = 'MessageCache::load';
134 wfProfileIn( $fname );
137 if ( $this->mUseCache
) {
138 $this->mCache
= false;
141 wfProfileIn( $fname.'-fromlocal' );
142 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
144 $this->loadFromLocal( $hash );
146 wfProfileOut( $fname.'-fromlocal' );
149 if ( !$this->mCache
) {
150 wfProfileIn( $fname.'-fromcache' );
151 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
153 # Save to local cache
154 if ( $wgLocalMessageCache !== false ) {
155 $serialized = serialize( $this->mCache
);
157 $hash = md5( $serialized );
158 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
160 $this->saveToLocal( $serialized, $hash );
162 wfProfileOut( $fname.'-fromcache' );
166 # If there's nothing in memcached, load all the messages from the database
167 if ( !$this->mCache
) {
168 wfDebug( "MessageCache::load(): loading all messages\n" );
170 # Other threads don't need to load the messages if another thread is doing it.
171 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
173 wfProfileIn( $fname.'-load' );
175 wfProfileOut( $fname.'-load' );
178 # Keep trying if it fails, this is kind of important
179 wfProfileIn( $fname.'-save' );
181 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
183 usleep(mt_rand(500000,1500000));
186 # Save to local cache
187 if ( $wgLocalMessageCache !== false ) {
188 $serialized = serialize( $this->mCache
);
189 $hash = md5( $serialized );
190 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
191 $this->saveToLocal( $serialized, $hash );
194 wfProfileOut( $fname.'-save' );
196 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
197 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
203 if ( !is_array( $this->mCache
) ) {
204 wfDebug( "MessageCache::load(): individual message mode\n" );
205 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
206 # Causing too much DB load, disabling -- TS
207 $this->mDisable
= true;
209 if ( $this->mCache == "loading" ) {
210 $this->mUseCache = false;
211 } elseif ( $this->mCache == "error" ) {
212 $this->mUseCache = false;
215 $this->mDisable = true;
218 $this->mCache
= false;
221 wfProfileOut( $fname );
222 $this->mDeferred
= false;
227 * Loads all or main part of cacheable messages from the database
229 function loadFromDB() {
230 global $wgAllMessagesEn, $wgLang;
232 $fname = 'MessageCache::loadFromDB';
233 $dbr =& wfGetDB( DB_SLAVE
);
235 wfDebugDieBacktrace( 'Invalid database object' );
237 $conditions = array( 'page_is_redirect' => 0,
238 'page_namespace' => NS_MEDIAWIKI
);
239 $res = $dbr->select( array( 'page', 'revision', 'text' ),
240 array( 'page_title', 'old_text', 'old_flags' ),
241 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
245 $this->mCache
= array();
246 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
247 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
251 # Go through the language array and the extension array and make a note of
252 # any keys missing from the cache
253 foreach ( $wgAllMessagesEn as $key => $value ) {
254 $uckey = $wgLang->ucfirst( $key );
255 if ( !array_key_exists( $uckey, $this->mCache
) ) {
256 $this->mCache
[$uckey] = false;
259 foreach ( $this->mExtensionMessages
as $key => $value ) {
260 $uckey = $wgLang->ucfirst( $key );
261 if ( !array_key_exists( $uckey, $this->mCache
) ) {
262 $this->mCache
[$uckey] = false;
266 $dbr->freeResult( $res );
270 * Not really needed anymore
273 global $wgAllMessagesEn, $wgContLang;
274 if ( !$this->mKeys
) {
275 $this->mKeys
= array();
276 foreach ( $wgAllMessagesEn as $key => $value ) {
277 $title = $wgContLang->ucfirst( $key );
278 array_push( $this->mKeys
, $title );
287 function isCacheable( $key ) {
291 function replace( $title, $text ) {
292 global $wgLocalMessageCache, $parserMemc, $wgDBname;
296 $parserMemc->delete("$wgDBname:sidebar");
297 if ( is_array( $this->mCache
) ) {
298 $this->mCache
[$title] = $text;
299 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
301 # Save to local cache
302 if ( $wgLocalMessageCache !== false ) {
303 $serialized = serialize( $this->mCache
);
304 $hash = md5( $serialized );
305 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
306 $this->saveToLocal( $serialized, $hash );
316 * Represents a write lock on the messages key
319 if ( !$this->mUseCache
) {
323 $lockKey = $this->mMemcKey
. 'lock';
324 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
328 return $i >= MSG_WAIT_TIMEOUT
;
332 if ( !$this->mUseCache
) {
336 $lockKey = $this->mMemcKey
. 'lock';
337 $this->mMemc
->delete( $lockKey );
340 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
341 global $wgContLanguageCode;
344 $lang =& $wgContLang;
345 $langcode = $wgContLanguageCode;
347 global $wgLang, $wgLanguageCode;
349 $langcode = $wgLanguageCode;
351 # If uninitialised, someone is trying to call this halfway through Setup.php
352 if( !$this->mInitialised
) {
353 return '<' . htmlspecialchars($key) . '>';
355 # If cache initialization was deferred, start it now.
356 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
361 if( !$this->mDisable
&& $useDB ) {
362 $title = $lang->ucfirst( $key );
363 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
364 $title .= '/' . $langcode;
366 $message = $this->getFromCache( $title );
368 # Try the extension array
369 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
370 $message = $this->mExtensionMessages
[$key];
373 # Try the array in the language object
374 if( $message === false ) {
375 wfSuppressWarnings();
376 $message = $lang->getMessage( $key );
378 if ( is_null( $message ) ) {
383 # Try the English array
384 if( $message === false && $langcode != 'en' ) {
385 wfSuppressWarnings();
386 $message = Language
::getMessage( $key );
388 if ( is_null( $message ) ) {
393 # Is this a custom message? Try the default language in the db...
394 if( $message === false &&
395 !$this->mDisable
&& $useDB &&
396 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
397 $message = $this->getFromCache( $lang->ucfirst( $key ) );
401 if( $message === false ) {
402 return '<' . htmlspecialchars($key) . '>';
406 $message = $this->transform( $message );
410 function getFromCache( $title ) {
414 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
415 return $this->mCache
[$title];
418 # Try individual message cache
419 if ( $this->mUseCache
) {
420 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
421 if ( $message == '###NONEXISTENT###' ) {
423 } elseif( !is_null( $message ) ) {
424 $this->mCache
[$title] = $message;
431 # Call message Hooks, in case they are defined
432 wfRunHooks('MessagesPreLoad',array($title,&$message));
434 # If it wasn't in the cache, load each message from the DB individually
435 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
437 $message = $revision->getText();
438 if ($this->mUseCache
) {
439 $this->mCache
[$title]=$message;
440 /* individual messages may be often
441 recached until proper purge code exists
443 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
447 # Use some special text instead of false, because false gets converted to '' somewhere
448 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
454 function transform( $message ) {
455 if( !$this->mDisableTransform
) {
456 if( strpos( $message, '{{' ) !== false ) {
457 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
463 function disable() { $this->mDisable
= true; }
464 function enable() { $this->mDisable
= false; }
465 function disableTransform() { $this->mDisableTransform
= true; }
466 function enableTransform() { $this->mDisableTransform
= false; }
467 function setTransform( $x ) { $this->mDisableTransform
= $x; }
468 function getTransform() { return $this->mDisableTransform
; }
471 * Add a message to the cache
474 * @param mixed $value
476 function addMessage( $key, $value ) {
477 $this->mExtensionMessages
[$key] = $value;
481 * Add an associative array of message to the cache
483 * @param array $messages An associative array of key => values to be added
485 function addMessages( $messages ) {
486 foreach ( $messages as $key => $value ) {
487 $this->addMessage( $key, $value );
492 * Clear all stored messages. Mainly used after a mass rebuild.
495 if( $this->mUseCache
) {
496 $this->mMemc
->delete( $this->mMemcKey
);