8 require_once( 'Revision.php' );
13 define( 'MSG_LOAD_TIMEOUT', 60);
14 define( 'MSG_LOCK_TIMEOUT', 10);
15 define( 'MSG_WAIT_TIMEOUT', 10);
19 * Performs various useful MediaWiki namespace-related functions
25 var $mCache, $mUseCache, $mDisable, $mExpiry;
26 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
27 var $mExtensionMessages;
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 * Loads messages either from memcached or the database, if not disabled
62 * On error, quietly switches to a fallback mode
63 * Returns false for a reportable error, true otherwise
66 global $wgAllMessagesEn;
68 if ( $this->mDisable
) {
69 wfDebug( "MessageCache::load(): disabled\n" );
72 $fname = 'MessageCache::load';
73 wfProfileIn( $fname );
76 if ( $this->mUseCache
) {
77 wfProfileIn( $fname.'-fromcache' );
78 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
79 wfProfileOut( $fname.'-fromcache' );
81 # If there's nothing in memcached, load all the messages from the database
82 if ( !$this->mCache
) {
83 wfDebug( "MessageCache::load(): loading all messages\n" );
85 # Other threads don't need to load the messages if another thread is doing it.
86 $success = $this->mMemc
->add( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
88 wfProfileIn( $fname.'-load' );
90 wfProfileOut( $fname.'-load' );
92 # Keep trying if it fails, this is kind of important
93 wfProfileIn( $fname.'-save' );
94 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
95 usleep(mt_rand(500000,1500000));
97 wfProfileOut( $fname.'-save' );
99 $this->mMemc
->set( $this->mMemcKey
, 'error', 60*5 );
100 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
106 if ( !is_array( $this->mCache
) ) {
107 wfDebug( "MessageCache::load(): individual message mode\n" );
108 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
109 # Causing too much DB load, disabling -- TS
110 $this->mDisable
= true;
112 if ( $this->mCache == "loading" ) {
113 $this->mUseCache = false;
114 } elseif ( $this->mCache == "error" ) {
115 $this->mUseCache = false;
118 $this->mDisable = true;
121 $this->mCache
= false;
124 wfProfileOut( $fname );
125 $this->mDeferred
= false;
130 * Loads all or main part of cacheable messages from the database
132 function loadFromDB() {
133 global $wgPartialMessageCache;
134 $fname = 'MessageCache::loadFromDB';
135 $dbr =& wfGetDB( DB_SLAVE
);
136 $conditions = array( 'page_is_redirect' => 0,
137 'page_namespace' => NS_MEDIAWIKI
);
138 if ($wgPartialMessageCache) {
139 wfDebugDieBacktrace( "Confused about how this works." );
140 if (is_array($wgPartialMessageCache)) {
141 $conditions['page_title']=$wgPartialMessageCache;
143 require_once("MessageCacheHints.php");
144 $conditions['page_title']=MessageCacheHints
::get();
147 $res = $dbr->select( array( 'page', 'revision', 'text' ),
148 array( 'page_title', 'old_text', 'old_flags' ),
149 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
153 $this->mCache
= array();
154 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
155 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
158 $dbr->freeResult( $res );
160 # FIXME: This is too slow currently.
161 # We need to bulk-fetch revisions, but in a portable way...
162 $resultSet = Revision::fetchFromConds( $dbr, array(
163 'page_namespace' => NS_MEDIAWIKI,
164 'page_is_redirect' => 0,
165 'page_id=rev_page' ) );
166 while( $row = $resultSet->fetchObject() ) {
167 $revision = new Revision( $row );
168 $title = $revision->getTitle();
169 $this->mCache[$title->getDBkey()] = $revision->getText();
176 * Not really needed anymore
179 global $wgAllMessagesEn, $wgContLang;
180 if ( !$this->mKeys
) {
181 $this->mKeys
= array();
182 foreach ( $wgAllMessagesEn as $key => $value ) {
183 $title = $wgContLang->ucfirst( $key );
184 array_push( $this->mKeys
, $title );
193 function isCacheable( $key ) {
197 function replace( $title, $text ) {
200 if ( is_array( $this->mCache
) ) {
201 $this->mCache
[$title] = $text;
202 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
209 * Represents a write lock on the messages key
212 if ( !$this->mUseCache
) {
216 $lockKey = $this->mMemcKey
. 'lock';
217 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
221 return $i >= MSG_WAIT_TIMEOUT
;
225 if ( !$this->mUseCache
) {
229 $lockKey = $this->mMemcKey
. 'lock';
230 $this->mMemc
->delete( $lockKey );
233 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
234 global $wgContLanguageCode;
237 $lang =& $wgContLang;
238 $langcode = $wgContLanguageCode;
240 global $wgLang, $wgLanguageCode;
242 $langcode = $wgLanguageCode;
244 # If uninitialised, someone is trying to call this halfway through Setup.php
245 if( !$this->mInitialised
) {
246 return "<$key>";
248 # If cache initialization was deferred, start it now.
249 if( $this->mDeferred
) {
254 if( !$this->mDisable
&& $useDB ) {
255 $title = $lang->ucfirst( $key );
256 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
257 $title .= '/' . $langcode;
259 $message = $this->getFromCache( $title );
261 # Try the extension array
263 $message = @$this->mExtensionMessages
[$key];
266 # Try the array in the language object
268 wfSuppressWarnings();
269 $message = $lang->getMessage( $key );
273 # Try the English array
274 if( !$message && $langcode != 'en' ) {
275 wfSuppressWarnings();
276 $message = Language
::getMessage( $key );
280 # Is this a custom message? Try the default language in the db...
282 !$this->mDisable
&& $useDB &&
283 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
284 $message = $this->getFromCache( $lang->ucfirst( $key ) );
289 $message = "<$key>";
293 $message = $this->transform( $message );
297 function getFromCache( $title ) {
301 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
302 $message = $this->mCache
[$title];
305 if ( !$message && $this->mUseCache
) {
306 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
308 $this->mCache
[$title] = $message;
312 # If it wasn't in the cache, load each message from the DB individually
314 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
316 $message = $revision->getText();
317 if ($this->mUseCache
) {
318 $this->mCache
[$title]=$message;
319 /* individual messages may be often
320 recached until proper purge code exists
322 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
330 function transform( $message ) {
331 if( !$this->mDisableTransform
) {
332 if( strpos( $message, '{{' ) !== false ) {
333 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
339 function disable() { $this->mDisable
= true; }
340 function enable() { $this->mDisable
= false; }
341 function disableTransform() { $this->mDisableTransform
= true; }
342 function enableTransform() { $this->mDisableTransform
= false; }
344 function addMessage( $key, $value ) {
345 $this->mExtensionMessages
[$key] = $value;
348 function addMessages( $messages ) {
349 foreach ( $messages as $key => $value ) {
350 $this->mExtensionMessages
[$key] = $value;
355 * Clear all stored messages. Mainly used after a mass rebuild.
358 if( $this->mUseCache
) {
359 $this->mMemc
->delete( $this->mMemcKey
);