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 $mIsForContent; //whether this is for content messages or ui messages
26 var $mInitialised = false;
28 function initialise( &$memCached, $useDB, $expiry, $memcPrefix, $forContent=false ) {
29 $fname = 'MessageCache::initialise';
30 wfProfileIn( $fname );
31 $this->mUseCache
= !is_null( $memCached );
32 $this->mMemc
= &$memCached;
33 $this->mDisable
= !$useDB;
34 $this->mExpiry
= $expiry;
35 $this->mDisableTransform
= false;
36 $this->mMemcKey
= $memcPrefix.':messages';
37 $this->mKeys
= false; # initialised on demand
38 $this->mInitialised
= true;
39 $this->mIsForContent
= $forContent;
40 wfProfileIn( $fname.'-parseropt' );
41 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
42 wfProfileOut( $fname.'-parseropt' );
43 wfProfileIn( $fname.'-parser' );
44 $this->mParser
= new Parser
;
45 wfProfileOut( $fname.'-parser' );
48 wfProfileOut( $fname );
52 * Loads messages either from memcached or the database, if not disabled
53 * On error, quietly switches to a fallback mode
54 * Returns false for a reportable error, true otherwise
57 global $wgAllMessagesEn;
59 if ( $this->mDisable
) {
60 wfDebug( "MessageCache::load(): disabled\n" );
63 $fname = 'MessageCache::load';
64 wfProfileIn( $fname );
67 if ( $this->mUseCache
) {
68 wfProfileIn( $fname.'-fromcache' );
69 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
70 wfProfileOut( $fname.'-fromcache' );
72 # If there's nothing in memcached, load all the messages from the database
73 if ( !$this->mCache
) {
74 wfDebug( "MessageCache::load(): loading all messages\n" );
76 # Other threads don't need to load the messages if another thread is doing it.
77 $success = $this->mMemc
->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
79 wfProfileIn( $fname.'-load' );
81 wfProfileOut( $fname.'-load' );
83 # Keep trying if it fails, this is kind of important
84 wfProfileIn( $fname.'-save' );
85 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
86 usleep(mt_rand(500000,1500000));
88 wfProfileOut( $fname.'-save' );
90 $this->mMemc
->set( $this->mMemcKey
, 'error', 86400 );
91 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
97 if ( !is_array( $this->mCache
) ) {
98 wfMsg( "MessageCache::load(): individual message mode\n" );
99 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
100 # Causing too much DB load, disabling -- TS
101 $this->mDisable
= true;
103 if ( $this->mCache == "loading" ) {
104 $this->mUseCache = false;
105 } elseif ( $this->mCache == "error" ) {
106 $this->mUseCache = false;
109 $this->mDisable = true;
112 $this->mCache
= false;
115 wfProfileOut( $fname );
120 * Loads all cacheable messages from the database
122 function loadFromDB() {
123 $fname = 'MessageCache::loadFromDB';
124 $dbr =& wfGetDB( DB_SLAVE
);
125 $res = $dbr->select( 'cur',
126 array( 'cur_title', 'cur_text' ),
127 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI
),
131 $this->mCache
= array();
132 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
133 $this->mCache
[$row->cur_title
] = $row->cur_text
;
136 $dbr->freeResult( $res );
140 * Not really needed anymore
143 global $wgAllMessagesEn;
144 if($this->mIsForContent
) {
146 $langobj = $wgContLang;
153 if ( !$this->mKeys
) {
154 $this->mKeys
= array();
155 foreach ( $wgAllMessagesEn as $key => $value ) {
156 array_push( $this->mKeys
, $langobj->ucfirst( $key ) );
165 function isCacheable( $key ) {
168 global $wgAllMessagesEn, $wgLang;
169 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
170 array_key_exists( $key, $wgAllMessagesEn );
174 function replace( $title, $text ) {
177 if ( is_array( $this->mCache
) ) {
178 $this->mCache
[$title] = $text;
179 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
186 * Represents a write lock on the messages key
189 if ( !$this->mUseCache
) {
193 $lockKey = $this->mMemcKey
. 'lock';
194 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
198 return $i >= MSG_WAIT_TIMEOUT
;
202 if ( !$this->mUseCache
) {
206 $lockKey = $this->mMemcKey
. 'lock';
207 $this->mMemc
->delete( $lockKey );
210 function get( $key, $useDB ) {
211 if($this->mIsForContent
) {
212 global $wgContLang, $wgContLanguageCode;
213 $langobj = $wgContLang;
214 $langcode=$wgContLanguageCode;
217 global $wgLang, $wgLanguageCode;
219 $langcode = $wgLanguageCode;
222 # If uninitialised, someone is trying to call this halfway through Setup.php
223 if ( !$this->mInitialised
) {
224 return "<$key>";
228 if ( !$this->mDisable
&& $useDB ) {
229 $title = $langobj->ucfirst( $key );
233 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
234 $message = $this->mCache
[$title];
237 # If it wasn't in the cache, load each message from the DB individually
239 $dbr =& wfGetDB( DB_SLAVE
);
240 $result = $dbr->getArray( 'cur', array('cur_text'),
241 array( 'cur_namespace' => NS_MEDIAWIKI
, 'cur_title' => $title ),
242 'MessageCache::get' );
244 $message = $result->cur_text
;
248 # Try the extension array
250 $message = @$this->mExtensionMessages
[$key];
253 # Try the array in $wgLang
255 wfSuppressWarnings();
256 $message = $langobj->getMessage( $key );
260 # Try the English array
261 if ( !$message && $langcode != 'en' ) {
262 wfSuppressWarnings();
263 $message = Language
::getMessage( $key );
269 $message = "<$key>";
273 $message = $this->transform( $message );
277 function transform( $message ) {
278 if( !$this->mDisableTransform
) {
279 if ( strstr( $message, '{{' ) !== false ) {
280 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
286 function disable() { $this->mDisable
= true; }
287 function enable() { $this->mDisable
= false; }
288 function disableTransform() { $this->mDisableTransform
= true; }
289 function enableTransform() { $this->mDisableTransform
= false; }
291 function addMessage( $key, $value ) {
292 $this->mExtensionMessages
[$key] = $value;
295 function addMessages( $messages ) {
296 foreach ( $messages as $key => $value ) {
297 $this->mExtensionMessages
[$key] = $value;
302 * Clear all stored messages. Mainly used after a mass rebuild.
305 if( $this->mUseCache
) {
306 $this->mMemc
->delete( $this->mMemcKey
);