10 define( 'MSG_LOAD_TIMEOUT', 60);
11 define( 'MSG_LOCK_TIMEOUT', 10);
12 define( 'MSG_WAIT_TIMEOUT', 10);
16 * Performs various useful MediaWiki namespace-related functions
22 var $mCache, $mUseCache, $mDisable, $mExpiry;
23 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
24 var $mExtensionMessages;
25 var $mInitialised = false;
26 var $mDeferred = true;
28 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
29 $fname = 'MessageCache::initialise';
30 wfProfileIn( $fname );
32 $this->mUseCache
= !is_null( $memCached );
33 $this->mMemc
= &$memCached;
34 $this->mDisable
= !$useDB;
35 $this->mExpiry
= $expiry;
36 $this->mDisableTransform
= false;
37 $this->mMemcKey
= $memcPrefix.':messages';
38 $this->mKeys
= false; # initialised on demand
39 $this->mInitialised
= true;
41 wfProfileIn( $fname.'-parseropt' );
42 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
43 wfProfileOut( $fname.'-parseropt' );
44 wfProfileIn( $fname.'-parser' );
45 $this->mParser
= new Parser
;
46 wfProfileOut( $fname.'-parser' );
48 # When we first get asked for a message,
49 # then we'll fill up the cache. If we
50 # can return a cache hit, this saves
51 # some extra milliseconds
52 $this->mDeferred
= true;
54 wfProfileOut( $fname );
58 * Loads messages either from memcached or the database, if not disabled
59 * On error, quietly switches to a fallback mode
60 * Returns false for a reportable error, true otherwise
63 global $wgAllMessagesEn;
65 if ( $this->mDisable
) {
66 wfDebug( "MessageCache::load(): disabled\n" );
69 $fname = 'MessageCache::load';
70 wfProfileIn( $fname );
73 if ( $this->mUseCache
) {
74 wfProfileIn( $fname.'-fromcache' );
75 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
76 wfProfileOut( $fname.'-fromcache' );
78 # If there's nothing in memcached, load all the messages from the database
79 if ( !$this->mCache
) {
80 wfDebug( "MessageCache::load(): loading all messages\n" );
82 # Other threads don't need to load the messages if another thread is doing it.
83 $success = $this->mMemc
->add( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
85 wfProfileIn( $fname.'-load' );
87 wfProfileOut( $fname.'-load' );
89 # Keep trying if it fails, this is kind of important
90 wfProfileIn( $fname.'-save' );
91 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
92 usleep(mt_rand(500000,1500000));
94 wfProfileOut( $fname.'-save' );
96 $this->mMemc
->set( $this->mMemcKey
, 'error', 86400 );
97 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
103 if ( !is_array( $this->mCache
) ) {
104 wfMsg( "MessageCache::load(): individual message mode\n" );
105 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
106 # Causing too much DB load, disabling -- TS
107 $this->mDisable
= true;
109 if ( $this->mCache == "loading" ) {
110 $this->mUseCache = false;
111 } elseif ( $this->mCache == "error" ) {
112 $this->mUseCache = false;
115 $this->mDisable = true;
118 $this->mCache
= false;
121 wfProfileOut( $fname );
122 $this->mDeferred
= false;
127 * Loads all or main part of cacheable messages from the database
129 function loadFromDB() {
130 global $wgPartialMessageCache;
131 $fname = 'MessageCache::loadFromDB';
132 $dbr =& wfGetDB( DB_SLAVE
);
133 $conditions = array( 'cur_is_redirect' => 0,
134 'cur_namespace' => NS_MEDIAWIKI
);
135 if ($wgPartialMessageCache) {
136 if (is_array($wgPartialMessageCache)) {
137 $conditions['cur_title']=$wgPartialMessageCache;
139 require_once("MessageCacheHints.php");
140 $conditions['cur_title']=MessageCacheHints
::get();
143 $res = $dbr->select( 'cur',
144 array( 'cur_title', 'cur_text' ), $conditions, $fname);
146 $this->mCache
= array();
147 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
148 $this->mCache
[$row->cur_title
] = $row->cur_text
;
151 $dbr->freeResult( $res );
155 * Not really needed anymore
158 global $wgAllMessagesEn, $wgContLang;
159 if ( !$this->mKeys
) {
160 $this->mKeys
= array();
161 foreach ( $wgAllMessagesEn as $key => $value ) {
162 array_push( $this->mKeys
, $wgContLang->ucfirst( $key ) );
171 function isCacheable( $key ) {
174 global $wgAllMessagesEn, $wgLang;
175 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
176 array_key_exists( $key, $wgAllMessagesEn );
180 function replace( $title, $text ) {
183 if ( is_array( $this->mCache
) ) {
184 $this->mCache
[$title] = $text;
185 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
192 * Represents a write lock on the messages key
195 if ( !$this->mUseCache
) {
199 $lockKey = $this->mMemcKey
. 'lock';
200 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
204 return $i >= MSG_WAIT_TIMEOUT
;
208 if ( !$this->mUseCache
) {
212 $lockKey = $this->mMemcKey
. 'lock';
213 $this->mMemc
->delete( $lockKey );
216 function get( $key, $useDB, $forcontent=true ) {
217 global $wgContLanguageCode;
220 $lang =& $wgContLang;
221 $langcode = $wgContLanguageCode;
223 global $wgLang, $wgLanguageCode;
225 $langcode = $wgLanguageCode;
227 # If uninitialised, someone is trying to call this halfway through Setup.php
228 if( !$this->mInitialised
) {
229 return "<$key>";
231 # If cache initialization was deferred, start it now.
232 if( $this->mDeferred
) {
237 if( !$this->mDisable
&& $useDB ) {
238 $title = $lang->ucfirst( $key );
239 if( $langcode != $wgContLanguageCode ) {
240 $title .= '/' . $langcode;
244 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
245 $message = $this->mCache
[$title];
248 if ( !$message && $this->mUseCache
) {
249 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
251 $this->mCache
[$title] = $message;
255 # If it wasn't in the cache, load each message from the DB individually
257 $dbr =& wfGetDB( DB_SLAVE
);
258 $result = $dbr->selectRow( 'cur', array('cur_text'),
259 array( 'cur_namespace' => NS_MEDIAWIKI
, 'cur_title' => $title ),
260 'MessageCache::get' );
262 $message = $result->cur_text
;
263 if( $this->mUseCache
) {
264 $this->mCache
[$title] = $message;
265 /* individual messages may be often
266 recached until proper purge code exists
268 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
273 # Try the extension array
275 $message = @$this->mExtensionMessages
[$key];
278 # Try the array in the language object
280 wfSuppressWarnings();
281 $message = $lang->getMessage( $key );
285 # Try the English array
286 if( !$message && $langcode != 'en' ) {
287 wfSuppressWarnings();
288 $message = Language
::getMessage( $key );
294 $message = "<$key>";
298 $message = $this->transform( $message );
302 function transform( $message ) {
303 if( !$this->mDisableTransform
) {
304 if( strpos( $message, '{{' ) !== false ) {
305 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
311 function disable() { $this->mDisable
= true; }
312 function enable() { $this->mDisable
= false; }
313 function disableTransform() { $this->mDisableTransform
= true; }
314 function enableTransform() { $this->mDisableTransform
= false; }
316 function addMessage( $key, $value ) {
317 $this->mExtensionMessages
[$key] = $value;
320 function addMessages( $messages ) {
321 foreach ( $messages as $key => $value ) {
322 $this->mExtensionMessages
[$key] = $value;
327 * Clear all stored messages. Mainly used after a mass rebuild.
330 if( $this->mUseCache
) {
331 $this->mMemc
->delete( $this->mMemcKey
);