9 define( 'MSG_LOAD_TIMEOUT', 60);
10 define( 'MSG_LOCK_TIMEOUT', 10);
11 define( 'MSG_WAIT_TIMEOUT', 10);
15 * Performs various useful MediaWiki namespace-related functions
19 var $mCache, $mUseCache, $mDisable, $mExpiry;
20 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
21 var $mExtensionMessages;
23 var $mInitialised = false;
25 function initialise( &$memCached, $useDB, $expiry, $memcPrefix ) {
26 $fname = 'MessageCache::initialise';
27 wfProfileIn( $fname );
28 $this->mUseCache
= !is_null( $memCached );
29 $this->mMemc
= &$memCached;
30 $this->mDisable
= !$useDB;
31 $this->mExpiry
= $expiry;
32 $this->mDisableTransform
= false;
33 $this->mMemcKey
= $memcPrefix.':messages';
34 $this->mKeys
= false; # initialised on demand
35 $this->mInitialised
= true;
36 wfProfileIn( $fname.'-parseropt' );
37 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
38 wfProfileOut( $fname.'-parseropt' );
39 wfProfileIn( $fname.'-parser' );
40 $this->mParser
= new Parser
;
41 wfProfileOut( $fname.'-parser' );
44 wfProfileOut( $fname );
48 * Loads messages either from memcached or the database, if not disabled
49 * On error, quietly switches to a fallback mode
50 * Returns false for a reportable error, true otherwise
53 global $wgAllMessagesEn;
55 if ( $this->mDisable
) {
56 wfDebug( "MessageCache::load(): disabled\n" );
59 $fname = 'MessageCache::load';
60 wfProfileIn( $fname );
63 if ( $this->mUseCache
) {
64 wfProfileIn( $fname.'-fromcache' );
65 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
66 wfProfileOut( $fname.'-fromcache' );
68 # If there's nothing in memcached, load all the messages from the database
69 if ( !$this->mCache
) {
70 wfDebug( "MessageCache::load(): loading all messages\n" );
72 # Other threads don't need to load the messages if another thread is doing it.
73 $success = $this->mMemc
->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
75 wfProfileIn( $fname.'-load' );
77 wfProfileOut( $fname.'-load' );
79 # Keep trying if it fails, this is kind of important
80 wfProfileIn( $fname.'-save' );
81 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
82 usleep(mt_rand(500000,1500000));
84 wfProfileOut( $fname.'-save' );
86 $this->mMemc
->set( $this->mMemcKey
, 'error', 86400 );
87 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
93 if ( !is_array( $this->mCache
) ) {
94 wfMsg( "MessageCache::load(): individual message mode\n" );
95 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
96 # Causing too much DB load, disabling -- TS
97 $this->mDisable
= true;
99 if ( $this->mCache == "loading" ) {
100 $this->mUseCache = false;
101 } elseif ( $this->mCache == "error" ) {
102 $this->mUseCache = false;
105 $this->mDisable = true;
108 $this->mCache
= false;
111 wfProfileOut( $fname );
116 * Loads all cacheable messages from the database
118 function loadFromDB() {
119 $fname = 'MessageCache::loadFromDB';
120 $dbr =& wfGetDB( DB_SLAVE
);
121 $res = $dbr->select( 'cur',
122 array( 'cur_title', 'cur_text' ),
123 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI
),
127 $this->mCache
= array();
128 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
129 $this->mCache
[$row->cur_title
] = $row->cur_text
;
132 $dbr->freeResult( $res );
136 * Not really needed anymore
139 global $wgAllMessagesEn, $wgLang;
140 if ( !$this->mKeys
) {
141 $this->mKeys
= array();
142 foreach ( $wgAllMessagesEn as $key => $value ) {
143 array_push( $this->mKeys
, $wgLang->ucfirst( $key ) );
152 function isCacheable( $key ) {
155 global $wgAllMessagesEn, $wgLang;
156 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
157 array_key_exists( $key, $wgAllMessagesEn );
161 function replace( $title, $text ) {
164 if ( is_array( $this->mCache
) ) {
165 $this->mCache
[$title] = $text;
166 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
173 * Represents a write lock on the messages key
176 if ( !$this->mUseCache
) {
180 $lockKey = $this->mMemcKey
. 'lock';
181 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
185 return $i >= MSG_WAIT_TIMEOUT
;
189 if ( !$this->mUseCache
) {
193 $lockKey = $this->mMemcKey
. 'lock';
194 $this->mMemc
->delete( $lockKey );
197 function get( $key, $useDB ) {
198 global $wgLang, $wgLanguageCode;
200 # If uninitialised, someone is trying to call this halfway through Setup.php
201 if ( !$this->mInitialised
) {
202 return "<$key>";
206 if ( !$this->mDisable
&& $useDB ) {
207 $title = $wgLang->ucfirst( $key );
211 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
212 $message = $this->mCache
[$title];
215 # If it wasn't in the cache, load each message from the DB individually
217 $dbr =& wfGetDB( DB_SLAVE
);
218 $result = $dbr->getArray( 'cur', array('cur_text'),
219 array( 'cur_namespace' => NS_MEDIAWIKI
, 'cur_title' => $title ),
220 'MessageCache::get' );
222 $message = $result->cur_text
;
226 # Try the extension array
228 $message = @$this->mExtensionMessages
[$key];
231 # Try the array in $wgLang
233 $message = $wgLang->getMessage( $key );
236 # Try the English array
237 if ( !$message && $wgLanguageCode != 'en' ) {
238 $message = Language
::getMessage( $key );
243 $message = "<$key>";
247 $message = $this->transform( $message );
251 function transform( $message ) {
252 if( !$this->mDisableTransform
) {
253 if ( strstr( $message, '{{' ) !== false ) {
254 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
260 function disable() { $this->mDisable
= true; }
261 function enable() { $this->mDisable
= false; }
262 function disableTransform() { $this->mDisableTransform
= true; }
264 function addMessage( $key, $value ) {
265 $this->mExtensionMessages
[$key] = $value;
268 function addMessages( $messages ) {
269 foreach ( $messages as $key => $value ) {
270 $this->mExtensionMessages
[$key] = $value;
275 * Clear all stored messages. Mainly used after a mass rebuild.
278 if( $this->mUseCache
) {
279 $this->mMemc
->delete( $this->mMemcKey
);