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 $fname = 'MessageCache::loadFromDB';
135 $dbr =& wfGetDB( DB_SLAVE
);
136 $conditions = array( 'page_is_redirect' => 0,
137 'page_namespace' => NS_MEDIAWIKI
);
138 $res = $dbr->select( array( 'page', 'revision', 'text' ),
139 array( 'page_title', 'old_text', 'old_flags' ),
140 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
144 $this->mCache
= array();
145 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
146 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
149 $dbr->freeResult( $res );
151 # FIXME: This is too slow currently.
152 # We need to bulk-fetch revisions, but in a portable way...
153 $resultSet = Revision::fetchFromConds( $dbr, array(
154 'page_namespace' => NS_MEDIAWIKI,
155 'page_is_redirect' => 0,
156 'page_id=rev_page' ) );
157 while( $row = $resultSet->fetchObject() ) {
158 $revision = new Revision( $row );
159 $title = $revision->getTitle();
160 $this->mCache[$title->getDBkey()] = $revision->getText();
167 * Not really needed anymore
170 global $wgAllMessagesEn, $wgContLang;
171 if ( !$this->mKeys
) {
172 $this->mKeys
= array();
173 foreach ( $wgAllMessagesEn as $key => $value ) {
174 $title = $wgContLang->ucfirst( $key );
175 array_push( $this->mKeys
, $title );
184 function isCacheable( $key ) {
188 function replace( $title, $text ) {
191 if ( is_array( $this->mCache
) ) {
192 $this->mCache
[$title] = $text;
193 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
200 * Represents a write lock on the messages key
203 if ( !$this->mUseCache
) {
207 $lockKey = $this->mMemcKey
. 'lock';
208 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
212 return $i >= MSG_WAIT_TIMEOUT
;
216 if ( !$this->mUseCache
) {
220 $lockKey = $this->mMemcKey
. 'lock';
221 $this->mMemc
->delete( $lockKey );
224 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
225 global $wgContLanguageCode;
228 $lang =& $wgContLang;
229 $langcode = $wgContLanguageCode;
231 global $wgLang, $wgLanguageCode;
233 $langcode = $wgLanguageCode;
235 # If uninitialised, someone is trying to call this halfway through Setup.php
236 if( !$this->mInitialised
) {
237 return "<$key>";
239 # If cache initialization was deferred, start it now.
240 if( $this->mDeferred
) {
245 if( !$this->mDisable
&& $useDB ) {
246 $title = $lang->ucfirst( $key );
247 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
248 $title .= '/' . $langcode;
250 $message = $this->getFromCache( $title );
252 # Try the extension array
254 $message = @$this->mExtensionMessages
[$key];
257 # Try the array in the language object
259 wfSuppressWarnings();
260 $message = $lang->getMessage( $key );
264 # Try the English array
265 if( !$message && $langcode != 'en' ) {
266 wfSuppressWarnings();
267 $message = Language
::getMessage( $key );
271 # Is this a custom message? Try the default language in the db...
273 !$this->mDisable
&& $useDB &&
274 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
275 $message = $this->getFromCache( $lang->ucfirst( $key ) );
280 $message = "<$key>";
284 $message = $this->transform( $message );
288 function getFromCache( $title ) {
292 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
293 $message = $this->mCache
[$title];
296 if ( !$message && $this->mUseCache
) {
297 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
299 $this->mCache
[$title] = $message;
303 # If it wasn't in the cache, load each message from the DB individually
305 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
307 $message = $revision->getText();
308 if ($this->mUseCache
) {
309 $this->mCache
[$title]=$message;
310 /* individual messages may be often
311 recached until proper purge code exists
313 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
321 function transform( $message ) {
322 if( !$this->mDisableTransform
) {
323 if( strpos( $message, '{{' ) !== false ) {
324 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
330 function disable() { $this->mDisable
= true; }
331 function enable() { $this->mDisable
= false; }
332 function disableTransform() { $this->mDisableTransform
= true; }
333 function enableTransform() { $this->mDisableTransform
= false; }
335 function addMessage( $key, $value ) {
336 $this->mExtensionMessages
[$key] = $value;
339 function addMessages( $messages ) {
340 foreach ( $messages as $key => $value ) {
341 $this->mExtensionMessages
[$key] = $value;
346 * Clear all stored messages. Mainly used after a mass rebuild.
349 if( $this->mUseCache
) {
350 $this->mMemc
->delete( $this->mMemcKey
);