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;
40 wfProfileIn( __METHOD__
.'-parseropt' );
41 $this->mParserOptions
= new ParserOptions( $u=NULL );
42 wfProfileOut( __METHOD__
.'-parseropt' );
43 $this->mParser
= null;
45 # When we first get asked for a message,
46 # then we'll fill up the cache. If we
47 # can return a cache hit, this saves
48 # some extra milliseconds
49 $this->mDeferred
= true;
51 wfProfileOut( __METHOD__
);
55 * Try to load the cache from a local file
57 function loadFromLocal( $hash ) {
58 global $wgLocalMessageCache, $wgDBname;
60 $this->mCache
= false;
61 if ( $wgLocalMessageCache === false ) {
65 $filename = "$wgLocalMessageCache/messages-$wgDBname";
68 $file = fopen( $filename, 'r' );
74 // Check to see if the file has the hash specified
75 $localHash = fread( $file, 32 );
76 if ( $hash == $localHash ) {
77 // All good, get the rest of it
78 $serialized = fread( $file, 1000000 );
79 $this->mCache
= unserialize( $serialized );
85 * Save the cache to a local file
87 function saveToLocal( $serialized, $hash ) {
88 global $wgLocalMessageCache, $wgDBname;
90 if ( $wgLocalMessageCache === false ) {
94 $filename = "$wgLocalMessageCache/messages-$wgDBname";
95 $oldUmask = umask( 0 );
96 wfMkdirParents( $wgLocalMessageCache, 0777 );
99 $file = fopen( $filename, 'w' );
101 wfDebug( "Unable to open local cache file for writing\n" );
105 fwrite( $file, $hash . $serialized );
107 @chmod
( $filename, 0666 );
110 function loadFromScript( $hash ) {
111 global $wgLocalMessageCache, $wgDBname;
112 if ( $wgLocalMessageCache === false ) {
116 $filename = "$wgLocalMessageCache/messages-$wgDBname";
118 wfSuppressWarnings();
119 $file = fopen( $filename, 'r' );
124 $localHash=substr(fread($file,40),8);
126 if ($hash!=$localHash) {
129 require("$wgLocalMessageCache/messages-$wgDBname");
132 function saveToScript($array, $hash) {
133 global $wgLocalMessageCache, $wgDBname;
134 if ( $wgLocalMessageCache === false ) {
138 $filename = "$wgLocalMessageCache/messages-$wgDBname";
139 $oldUmask = umask( 0 );
140 wfMkdirParents( $wgLocalMessageCache, 0777 );
142 $file = fopen( $filename.'.tmp', 'w');
143 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
145 foreach ($array as $key => $message) {
146 fwrite($file, "'". $this->escapeForScript($key).
147 "' => '" . $this->escapeForScript($message).
150 fwrite($file,");\n?>");
152 rename($filename.'.tmp',$filename);
155 function escapeForScript($string) {
156 $string = str_replace( '\\', '\\\\', $string );
157 $string = str_replace( '\'', '\\\'', $string );
162 * Loads messages either from memcached or the database, if not disabled
163 * On error, quietly switches to a fallback mode
164 * Returns false for a reportable error, true otherwise
167 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
169 if ( $this->mDisable
) {
170 static $shownDisabled = false;
171 if ( !$shownDisabled ) {
172 wfDebug( "MessageCache::load(): disabled\n" );
173 $shownDisabled = true;
177 $fname = 'MessageCache::load';
178 wfProfileIn( $fname );
181 if ( $this->mUseCache
) {
182 $this->mCache
= false;
185 wfProfileIn( $fname.'-fromlocal' );
186 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
188 if ($wgLocalMessageCacheSerialized) {
189 $this->loadFromLocal( $hash );
191 $this->loadFromScript( $hash );
194 wfProfileOut( $fname.'-fromlocal' );
197 if ( !$this->mCache
) {
198 wfProfileIn( $fname.'-fromcache' );
199 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
201 # Save to local cache
202 if ( $wgLocalMessageCache !== false ) {
203 $serialized = serialize( $this->mCache
);
205 $hash = md5( $serialized );
206 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
208 if ($wgLocalMessageCacheSerialized) {
209 $this->saveToLocal( $serialized,$hash );
211 $this->saveToScript( $this->mCache
, $hash );
214 wfProfileOut( $fname.'-fromcache' );
218 # If there's nothing in memcached, load all the messages from the database
219 if ( !$this->mCache
) {
220 wfDebug( "MessageCache::load(): loading all messages\n" );
222 # Other threads don't need to load the messages if another thread is doing it.
223 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
225 wfProfileIn( $fname.'-load' );
227 wfProfileOut( $fname.'-load' );
230 # Keep trying if it fails, this is kind of important
231 wfProfileIn( $fname.'-save' );
233 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
235 usleep(mt_rand(500000,1500000));
238 # Save to local cache
239 if ( $wgLocalMessageCache !== false ) {
240 $serialized = serialize( $this->mCache
);
241 $hash = md5( $serialized );
242 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
243 if ($wgLocalMessageCacheSerialized) {
244 $this->saveToLocal( $serialized,$hash );
246 $this->saveToScript( $this->mCache
, $hash );
250 wfProfileOut( $fname.'-save' );
252 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
253 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
259 if ( !is_array( $this->mCache
) ) {
260 wfDebug( "MessageCache::load(): individual message mode\n" );
261 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
262 # Causing too much DB load, disabling -- TS
263 $this->mDisable
= true;
265 if ( $this->mCache == "loading" ) {
266 $this->mUseCache = false;
267 } elseif ( $this->mCache == "error" ) {
268 $this->mUseCache = false;
271 $this->mDisable = true;
274 $this->mCache
= false;
277 wfProfileOut( $fname );
278 $this->mDeferred
= false;
283 * Loads all or main part of cacheable messages from the database
285 function loadFromDB() {
286 global $wgAllMessagesEn, $wgLang;
288 $fname = 'MessageCache::loadFromDB';
289 $dbr =& wfGetDB( DB_SLAVE
);
291 throw new MWException( 'Invalid database object' );
293 $conditions = array( 'page_is_redirect' => 0,
294 'page_namespace' => NS_MEDIAWIKI
);
295 $res = $dbr->select( array( 'page', 'revision', 'text' ),
296 array( 'page_title', 'old_text', 'old_flags' ),
297 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
301 $this->mCache
= array();
302 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
303 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
307 # Go through the language array and the extension array and make a note of
308 # any keys missing from the cache
309 foreach ( $wgAllMessagesEn as $key => $value ) {
310 $uckey = $wgLang->ucfirst( $key );
311 if ( !array_key_exists( $uckey, $this->mCache
) ) {
312 $this->mCache
[$uckey] = false;
316 # Make sure all extension messages are available
317 wfLoadAllExtensions();
319 # Add them to the cache
320 foreach ( $this->mExtensionMessages
as $key => $value ) {
321 $uckey = $wgLang->ucfirst( $key );
322 if ( !array_key_exists( $uckey, $this->mCache
) &&
323 ( isset( $this->mExtensionMessages
[$key][$wgLang->getCode()] ) ||
isset( $this->mExtensionMessages
[$key]['en'] ) ) ) {
324 $this->mCache
[$uckey] = false;
328 $dbr->freeResult( $res );
332 * Not really needed anymore
335 global $wgAllMessagesEn, $wgContLang;
336 if ( !$this->mKeys
) {
337 $this->mKeys
= array();
338 foreach ( $wgAllMessagesEn as $key => $value ) {
339 $title = $wgContLang->ucfirst( $key );
340 array_push( $this->mKeys
, $title );
349 function isCacheable( $key ) {
353 function replace( $title, $text ) {
354 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
358 $parserMemc->delete("$wgDBname:sidebar");
359 if ( is_array( $this->mCache
) ) {
360 $this->mCache
[$title] = $text;
361 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
363 # Save to local cache
364 if ( $wgLocalMessageCache !== false ) {
365 $serialized = serialize( $this->mCache
);
366 $hash = md5( $serialized );
367 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
368 if ($wgLocalMessageCacheSerialized) {
369 $this->saveToLocal( $serialized,$hash );
371 $this->saveToScript( $this->mCache
, $hash );
382 * Represents a write lock on the messages key
385 if ( !$this->mUseCache
) {
389 $lockKey = $this->mMemcKey
. 'lock';
390 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
394 return $i >= MSG_WAIT_TIMEOUT
;
398 if ( !$this->mUseCache
) {
402 $lockKey = $this->mMemcKey
. 'lock';
403 $this->mMemc
->delete( $lockKey );
406 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
407 global $wgContLanguageCode;
410 $lang =& $wgContLang;
411 $langcode = $wgContLanguageCode;
413 global $wgLang, $wgLanguageCode;
415 $langcode = $wgLanguageCode;
417 # If uninitialised, someone is trying to call this halfway through Setup.php
418 if( !$this->mInitialised
) {
419 return '<' . htmlspecialchars($key) . '>';
421 # If cache initialization was deferred, start it now.
422 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
427 if( !$this->mDisable
&& $useDB ) {
428 $title = $lang->ucfirst( $key );
429 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
430 $title .= '/' . $langcode;
432 $message = $this->getFromCache( $title );
434 # Try the extension array
435 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
436 if ( isset( $this->mExtensionMessages
[$key][$langcode] ) ) {
437 $message = $this->mExtensionMessages
[$key][$langcode];
438 } elseif ( isset( $this->mExtensionMessages
[$key]['en'] ) ) {
439 $message = $this->mExtensionMessages
[$key]['en'];
443 # Try the array in the language object
444 if( $message === false ) {
445 wfSuppressWarnings();
446 $message = $lang->getMessage( $key );
448 if ( is_null( $message ) ) {
453 # Try the English array
454 if( $message === false && $langcode != 'en' ) {
455 wfSuppressWarnings();
456 $message = Language
::getMessage( $key );
458 if ( is_null( $message ) ) {
463 # Is this a custom message? Try the default language in the db...
464 if( ($message === false ||
$message === '-' ) &&
465 !$this->mDisable
&& $useDB &&
466 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
467 $message = $this->getFromCache( $lang->ucfirst( $key ) );
471 if( $message === false ) {
472 return '<' . htmlspecialchars($key) . '>';
476 $message = $this->transform( $message );
480 function getFromCache( $title ) {
484 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
485 return $this->mCache
[$title];
488 # Try individual message cache
489 if ( $this->mUseCache
) {
490 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
491 if ( $message == '###NONEXISTENT###' ) {
493 } elseif( !is_null( $message ) ) {
494 $this->mCache
[$title] = $message;
501 # Call message Hooks, in case they are defined
502 wfRunHooks('MessagesPreLoad',array($title,&$message));
504 # If it wasn't in the cache, load each message from the DB individually
505 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
507 $message = $revision->getText();
508 if ($this->mUseCache
) {
509 $this->mCache
[$title]=$message;
510 /* individual messages may be often
511 recached until proper purge code exists
513 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
517 # Use some special text instead of false, because false gets converted to '' somewhere
518 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
524 function transform( $message ) {
526 if ( !$this->mParser
&& isset( $wgParser ) ) {
527 # Do some initialisation so that we don't have to do it twice
528 $wgParser->firstCallInit();
529 # Clone it and store it
530 $this->mParser
= clone $wgParser;
532 if ( !$this->mDisableTransform
&& $this->mParser
) {
533 if( strpos( $message, '{{' ) !== false ) {
534 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
540 function disable() { $this->mDisable
= true; }
541 function enable() { $this->mDisable
= false; }
542 function disableTransform() { $this->mDisableTransform
= true; }
543 function enableTransform() { $this->mDisableTransform
= false; }
544 function setTransform( $x ) { $this->mDisableTransform
= $x; }
545 function getTransform() { return $this->mDisableTransform
; }
548 * Add a message to the cache
551 * @param mixed $value
552 * @param string $lang The messages language, English by default
554 function addMessage( $key, $value, $lang = 'en' ) {
555 $this->mExtensionMessages
[$key][$lang] = $value;
559 * Add an associative array of message to the cache
561 * @param array $messages An associative array of key => values to be added
562 * @param string $lang The messages language, English by default
564 function addMessages( $messages, $lang = 'en' ) {
565 wfProfileIn( __METHOD__
);
566 foreach ( $messages as $key => $value ) {
567 $this->addMessage( $key, $value, $lang );
569 wfProfileOut( __METHOD__
);
573 * Clear all stored messages. Mainly used after a mass rebuild.
576 if( $this->mUseCache
) {
577 $this->mMemc
->delete( $this->mMemcKey
);