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;
26 var $mInitialised = false;
28 function initialise( &$memCached, $useDB, $expiry, $memcPrefix ) {
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 wfProfileIn( $fname.'-parseropt' );
40 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
41 wfProfileOut( $fname.'-parseropt' );
42 wfProfileIn( $fname.'-parser' );
43 $this->mParser
= new Parser
;
44 wfProfileOut( $fname.'-parser' );
47 wfProfileOut( $fname );
51 * Loads messages either from memcached or the database, if not disabled
52 * On error, quietly switches to a fallback mode
53 * Returns false for a reportable error, true otherwise
56 global $wgAllMessagesEn;
58 if ( $this->mDisable
) {
59 wfDebug( "MessageCache::load(): disabled\n" );
62 $fname = 'MessageCache::load';
63 wfProfileIn( $fname );
66 if ( $this->mUseCache
) {
67 wfProfileIn( $fname.'-fromcache' );
68 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
69 wfProfileOut( $fname.'-fromcache' );
71 # If there's nothing in memcached, load all the messages from the database
72 if ( !$this->mCache
) {
73 wfDebug( "MessageCache::load(): loading all messages\n" );
75 # Other threads don't need to load the messages if another thread is doing it.
76 $success = $this->mMemc
->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
78 wfProfileIn( $fname.'-load' );
80 wfProfileOut( $fname.'-load' );
82 # Keep trying if it fails, this is kind of important
83 wfProfileIn( $fname.'-save' );
84 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
85 usleep(mt_rand(500000,1500000));
87 wfProfileOut( $fname.'-save' );
89 $this->mMemc
->set( $this->mMemcKey
, 'error', 86400 );
90 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
96 if ( !is_array( $this->mCache
) ) {
97 wfMsg( "MessageCache::load(): individual message mode\n" );
98 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
99 # Causing too much DB load, disabling -- TS
100 $this->mDisable
= true;
102 if ( $this->mCache == "loading" ) {
103 $this->mUseCache = false;
104 } elseif ( $this->mCache == "error" ) {
105 $this->mUseCache = false;
108 $this->mDisable = true;
111 $this->mCache
= false;
114 wfProfileOut( $fname );
119 * Loads all cacheable messages from the database
121 function loadFromDB() {
122 $fname = 'MessageCache::loadFromDB';
123 $dbr =& wfGetDB( DB_SLAVE
);
124 $res = $dbr->select( 'cur',
125 array( 'cur_title', 'cur_text' ),
126 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI
),
130 $this->mCache
= array();
131 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
132 $this->mCache
[$row->cur_title
] = $row->cur_text
;
135 $dbr->freeResult( $res );
139 * Not really needed anymore
142 global $wgAllMessagesEn, $wgLang;
143 if ( !$this->mKeys
) {
144 $this->mKeys
= array();
145 foreach ( $wgAllMessagesEn as $key => $value ) {
146 array_push( $this->mKeys
, $wgLang->ucfirst( $key ) );
155 function isCacheable( $key ) {
158 global $wgAllMessagesEn, $wgLang;
159 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
160 array_key_exists( $key, $wgAllMessagesEn );
164 function replace( $title, $text ) {
167 if ( is_array( $this->mCache
) ) {
168 $this->mCache
[$title] = $text;
169 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
176 * Represents a write lock on the messages key
179 if ( !$this->mUseCache
) {
183 $lockKey = $this->mMemcKey
. 'lock';
184 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
188 return $i >= MSG_WAIT_TIMEOUT
;
192 if ( !$this->mUseCache
) {
196 $lockKey = $this->mMemcKey
. 'lock';
197 $this->mMemc
->delete( $lockKey );
200 function get( $key, $useDB ) {
201 global $wgLang, $wgLanguageCode;
203 # If uninitialised, someone is trying to call this halfway through Setup.php
204 if ( !$this->mInitialised
) {
205 return "<$key>";
209 if ( !$this->mDisable
&& $useDB ) {
210 $title = $wgLang->ucfirst( $key );
214 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
215 $message = $this->mCache
[$title];
218 # If it wasn't in the cache, load each message from the DB individually
220 $dbr =& wfGetDB( DB_SLAVE
);
221 $result = $dbr->getArray( 'cur', array('cur_text'),
222 array( 'cur_namespace' => NS_MEDIAWIKI
, 'cur_title' => $title ),
223 'MessageCache::get' );
225 $message = $result->cur_text
;
229 # Try the extension array
231 $message = @$this->mExtensionMessages
[$key];
234 # Try the array in $wgLang
236 $message = $wgLang->getMessage( $key );
239 # Try the English array
240 if ( !$message && $wgLanguageCode != 'en' ) {
241 $message = Language
::getMessage( $key );
246 $message = "<$key>";
250 $message = $this->transform( $message );
254 function transform( $message ) {
255 if( !$this->mDisableTransform
) {
256 if ( strstr( $message, '{{' ) !== false ) {
257 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
263 function disable() { $this->mDisable
= true; }
264 function enable() { $this->mDisable
= false; }
265 function disableTransform() { $this->mDisableTransform
= true; }
266 function enableTransform() { $this->mDisableTransform
= false; }
268 function addMessage( $key, $value ) {
269 $this->mExtensionMessages
[$key] = $value;
272 function addMessages( $messages ) {
273 foreach ( $messages as $key => $value ) {
274 $this->mExtensionMessages
[$key] = $value;
279 * Clear all stored messages. Mainly used after a mass rebuild.
282 if( $this->mUseCache
) {
283 $this->mMemc
->delete( $this->mMemcKey
);