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 $mLang, $mLangCode;
27 function initialise( &$memCached, $useDB, $expiry, $memcPrefix, $langobj, $langcode) {
28 $fname = 'MessageCache::initialise';
29 wfProfileIn( $fname );
30 $this->mLang
= $langobj;
31 $this->mLangCode
= $langcode;
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;
40 $this->mIsForContent
= $forContent;
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' );
49 wfProfileOut( $fname );
53 * Loads messages either from memcached or the database, if not disabled
54 * On error, quietly switches to a fallback mode
55 * Returns false for a reportable error, true otherwise
58 global $wgAllMessagesEn;
60 if ( $this->mDisable
) {
61 wfDebug( "MessageCache::load(): disabled\n" );
64 $fname = 'MessageCache::load';
65 wfProfileIn( $fname );
68 if ( $this->mUseCache
) {
69 wfProfileIn( $fname.'-fromcache' );
70 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
71 wfProfileOut( $fname.'-fromcache' );
73 # If there's nothing in memcached, load all the messages from the database
74 if ( !$this->mCache
) {
75 wfDebug( "MessageCache::load(): loading all messages\n" );
77 # Other threads don't need to load the messages if another thread is doing it.
78 $success = $this->mMemc
->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
80 wfProfileIn( $fname.'-load' );
82 wfProfileOut( $fname.'-load' );
84 # Keep trying if it fails, this is kind of important
85 wfProfileIn( $fname.'-save' );
86 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
87 usleep(mt_rand(500000,1500000));
89 wfProfileOut( $fname.'-save' );
91 $this->mMemc
->set( $this->mMemcKey
, 'error', 86400 );
92 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
98 if ( !is_array( $this->mCache
) ) {
99 wfMsg( "MessageCache::load(): individual message mode\n" );
100 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
101 # Causing too much DB load, disabling -- TS
102 $this->mDisable
= true;
104 if ( $this->mCache == "loading" ) {
105 $this->mUseCache = false;
106 } elseif ( $this->mCache == "error" ) {
107 $this->mUseCache = false;
110 $this->mDisable = true;
113 $this->mCache
= false;
116 wfProfileOut( $fname );
121 * Loads all cacheable messages from the database
123 function loadFromDB() {
124 $fname = 'MessageCache::loadFromDB';
125 $dbr =& wfGetDB( DB_SLAVE
);
126 $res = $dbr->select( 'cur',
127 array( 'cur_title', 'cur_text' ),
128 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI
),
132 $this->mCache
= array();
133 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
134 $this->mCache
[$row->cur_title
] = $row->cur_text
;
137 $dbr->freeResult( $res );
141 * Not really needed anymore
144 global $wgAllMessagesEn;
145 if ( !$this->mKeys
) {
146 $this->mKeys
= array();
147 foreach ( $wgAllMessagesEn as $key => $value ) {
148 array_push( $this->mKeys
, $this->mLang
->ucfirst( $key ) );
157 function isCacheable( $key ) {
160 global $wgAllMessagesEn, $wgLang;
161 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
162 array_key_exists( $key, $wgAllMessagesEn );
166 function replace( $title, $text ) {
169 if ( is_array( $this->mCache
) ) {
170 $this->mCache
[$title] = $text;
171 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
178 * Represents a write lock on the messages key
181 if ( !$this->mUseCache
) {
185 $lockKey = $this->mMemcKey
. 'lock';
186 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
190 return $i >= MSG_WAIT_TIMEOUT
;
194 if ( !$this->mUseCache
) {
198 $lockKey = $this->mMemcKey
. 'lock';
199 $this->mMemc
->delete( $lockKey );
202 function get( $key, $useDB ) {
204 # If uninitialised, someone is trying to call this halfway through Setup.php
205 if ( !$this->mInitialised
) {
206 return "<$key>";
210 if ( !$this->mDisable
&& $useDB ) {
211 $title = $this->mLang
->ucfirst( $key );
215 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
216 $message = $this->mCache
[$title];
219 # If it wasn't in the cache, load each message from the DB individually
221 $dbr =& wfGetDB( DB_SLAVE
);
222 $result = $dbr->getArray( 'cur', array('cur_text'),
223 array( 'cur_namespace' => NS_MEDIAWIKI
, 'cur_title' => $title ),
224 'MessageCache::get' );
226 $message = $result->cur_text
;
230 # Try the extension array
232 $message = @$this->mExtensionMessages
[$key];
235 # Try the array in the language object
237 wfSuppressWarnings();
238 $message = $this->mLang
->getMessage( $key );
242 # Try the English array
243 if ( !$message && $this->mLangCode
!= 'en' ) {
244 wfSuppressWarnings();
245 $message = Language
::getMessage( $key );
251 $message = "<$key>";
255 $message = $this->transform( $message );
259 function transform( $message ) {
260 if( !$this->mDisableTransform
) {
261 if ( strstr( $message, '{{' ) !== false ) {
262 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
268 function disable() { $this->mDisable
= true; }
269 function enable() { $this->mDisable
= false; }
270 function disableTransform() { $this->mDisableTransform
= true; }
271 function enableTransform() { $this->mDisableTransform
= false; }
273 function addMessage( $key, $value ) {
274 $this->mExtensionMessages
[$key] = $value;
277 function addMessages( $messages ) {
278 foreach ( $messages as $key => $value ) {
279 $this->mExtensionMessages
[$key] = $value;
284 * Clear all stored messages. Mainly used after a mass rebuild.
287 if( $this->mUseCache
) {
288 $this->mMemc
->delete( $this->mMemcKey
);