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
26 var $mCache, $mUseCache, $mDisable, $mExpiry;
27 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
28 var $mExtensionMessages;
29 var $mInitialised = false;
30 var $mDeferred = true;
32 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
33 $fname = 'MessageCache::initialise';
34 wfProfileIn( $fname );
36 $this->mUseCache
= !is_null( $memCached );
37 $this->mMemc
= &$memCached;
38 $this->mDisable
= !$useDB;
39 $this->mExpiry
= $expiry;
40 $this->mDisableTransform
= false;
41 $this->mMemcKey
= $memcPrefix.':messages';
42 $this->mKeys
= false; # initialised on demand
43 $this->mInitialised
= true;
45 wfProfileIn( $fname.'-parseropt' );
46 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
47 wfProfileOut( $fname.'-parseropt' );
48 wfProfileIn( $fname.'-parser' );
49 $this->mParser
= new Parser
;
50 wfProfileOut( $fname.'-parser' );
52 # When we first get asked for a message,
53 # then we'll fill up the cache. If we
54 # can return a cache hit, this saves
55 # some extra milliseconds
56 $this->mDeferred
= true;
58 wfProfileOut( $fname );
62 * Loads messages either from memcached or the database, if not disabled
63 * On error, quietly switches to a fallback mode
64 * Returns false for a reportable error, true otherwise
67 global $wgAllMessagesEn;
69 if ( $this->mDisable
) {
70 wfDebug( "MessageCache::load(): disabled\n" );
73 $fname = 'MessageCache::load';
74 wfProfileIn( $fname );
77 if ( $this->mUseCache
) {
78 wfProfileIn( $fname.'-fromcache' );
79 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
80 wfProfileOut( $fname.'-fromcache' );
82 # If there's nothing in memcached, load all the messages from the database
83 if ( !$this->mCache
) {
84 wfDebug( "MessageCache::load(): loading all messages\n" );
86 # Other threads don't need to load the messages if another thread is doing it.
87 $success = $this->mMemc
->add( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
89 wfProfileIn( $fname.'-load' );
91 wfProfileOut( $fname.'-load' );
93 # Keep trying if it fails, this is kind of important
94 wfProfileIn( $fname.'-save' );
95 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
96 usleep(mt_rand(500000,1500000));
98 wfProfileOut( $fname.'-save' );
100 $this->mMemc
->set( $this->mMemcKey
, 'error', 60*5 );
101 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
107 if ( !is_array( $this->mCache
) ) {
108 wfDebug( "MessageCache::load(): individual message mode\n" );
109 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
110 # Causing too much DB load, disabling -- TS
111 $this->mDisable
= true;
113 if ( $this->mCache == "loading" ) {
114 $this->mUseCache = false;
115 } elseif ( $this->mCache == "error" ) {
116 $this->mUseCache = false;
119 $this->mDisable = true;
122 $this->mCache
= false;
125 wfProfileOut( $fname );
126 $this->mDeferred
= false;
131 * Loads all or main part of cacheable messages from the database
133 function loadFromDB() {
134 global $wgPartialMessageCache;
135 $fname = 'MessageCache::loadFromDB';
136 $dbr =& wfGetDB( DB_SLAVE
);
137 $conditions = array( 'page_is_redirect' => 0,
138 'page_namespace' => NS_MEDIAWIKI
);
139 if ($wgPartialMessageCache) {
140 wfDebugDieBacktrace( "Confused about how this works." );
141 if (is_array($wgPartialMessageCache)) {
142 $conditions['page_title']=$wgPartialMessageCache;
144 require_once("MessageCacheHints.php");
145 $conditions['page_title']=MessageCacheHints
::get();
148 $res = $dbr->select( array( 'page', 'revision', 'text' ),
149 array( 'page_title', 'old_text', 'old_flags' ),
150 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
154 $this->mCache
= array();
155 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
156 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
159 $dbr->freeResult( $res );
161 # FIXME: This is too slow currently.
162 # We need to bulk-fetch revisions, but in a portable way...
163 $resultSet = Revision::fetchFromConds( $dbr, array(
164 'page_namespace' => NS_MEDIAWIKI,
165 'page_is_redirect' => 0,
166 'page_id=rev_page' ) );
167 while( $row = $resultSet->fetchObject() ) {
168 $revision = new Revision( $row );
169 $title = $revision->getTitle();
170 $this->mCache[$title->getDBkey()] = $revision->getText();
177 * Not really needed anymore
180 global $wgAllMessagesEn, $wgContLang;
181 if ( !$this->mKeys
) {
182 $this->mKeys
= array();
183 foreach ( $wgAllMessagesEn as $key => $value ) {
184 $title = $wgContLang->ucfirst( $key );
185 array_push( $this->mKeys
, $title );
194 function isCacheable( $key ) {
198 function replace( $title, $text ) {
201 if ( is_array( $this->mCache
) ) {
202 $this->mCache
[$title] = $text;
203 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
210 * Represents a write lock on the messages key
213 if ( !$this->mUseCache
) {
217 $lockKey = $this->mMemcKey
. 'lock';
218 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
222 return $i >= MSG_WAIT_TIMEOUT
;
226 if ( !$this->mUseCache
) {
230 $lockKey = $this->mMemcKey
. 'lock';
231 $this->mMemc
->delete( $lockKey );
234 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
235 global $wgContLanguageCode;
238 $lang =& $wgContLang;
239 $langcode = $wgContLanguageCode;
241 global $wgLang, $wgLanguageCode;
243 $langcode = $wgLanguageCode;
245 # If uninitialised, someone is trying to call this halfway through Setup.php
246 if( !$this->mInitialised
) {
247 return "<$key>";
249 # If cache initialization was deferred, start it now.
250 if( $this->mDeferred
) {
255 if( !$this->mDisable
&& $useDB ) {
256 $title = $lang->ucfirst( $key );
257 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
258 $title .= '/' . $langcode;
260 $message = $this->getFromCache( $title );
262 # Try the extension array
264 $message = @$this->mExtensionMessages
[$key];
267 # Try the array in the language object
269 wfSuppressWarnings();
270 $message = $lang->getMessage( $key );
274 # Try the English array
275 if( !$message && $langcode != 'en' ) {
276 wfSuppressWarnings();
277 $message = Language
::getMessage( $key );
281 # Is this a custom message? Try the default language in the db...
283 !$this->mDisable
&& $useDB &&
284 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
285 $message = $this->getFromCache( $lang->ucfirst( $key ) );
290 $message = "<$key>";
294 $message = $this->transform( $message );
298 function getFromCache( $title ) {
302 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
303 $message = $this->mCache
[$title];
306 if ( !$message && $this->mUseCache
) {
307 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
309 $this->mCache
[$title] = $message;
313 # If it wasn't in the cache, load each message from the DB individually
315 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
317 $message = $revision->getText();
318 if ($this->mUseCache
) {
319 $this->mCache
[$title]=$message;
320 /* individual messages may be often
321 recached until proper purge code exists
323 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
331 function transform( $message ) {
332 if( !$this->mDisableTransform
) {
333 if( strpos( $message, '{{' ) !== false ) {
334 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
340 function disable() { $this->mDisable
= true; }
341 function enable() { $this->mDisable
= false; }
342 function disableTransform() { $this->mDisableTransform
= true; }
343 function enableTransform() { $this->mDisableTransform
= false; }
345 function addMessage( $key, $value ) {
346 $this->mExtensionMessages
[$key] = $value;
349 function addMessages( $messages ) {
350 foreach ( $messages as $key => $value ) {
351 $this->mExtensionMessages
[$key] = $value;
356 * Clear all stored messages. Mainly used after a mass rebuild.
359 if( $this->mUseCache
) {
360 $this->mMemc
->delete( $this->mMemcKey
);