4 # Performs various useful MediaWiki namespace-related functions
6 define( "MSG_LOAD_TIMEOUT", 60);
7 define( "MSG_LOCK_TIMEOUT", 10);
8 define( "MSG_WAIT_TIMEOUT", 10);
12 var $mCache, $mUseCache, $mDisable, $mExpiry;
13 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
14 var $mExtensionMessages;
16 var $mInitialised = false;
18 function initialise( &$memCached, $useDB, $expiry, $memcPrefix ) {
19 $this->mUseCache
= !is_null( $memCached );
20 $this->mMemc
= &$memCached;
21 $this->mDisable
= !$useDB;
22 $this->mExpiry
= $expiry;
23 $this->mDisableTransform
= false;
24 $this->mMemcKey
= "$memcPrefix:messages";
25 $this->mKeys
= false; # initialised on demand
26 $this->mInitialised
= true;
27 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
28 $this->mParser
= new Parser
;
33 # Loads messages either from memcached or the database, if not disabled
34 # On error, quietly switches to a fallback mode
35 # Returns false for a reportable error, true otherwise
37 global $wgAllMessagesEn;
39 if ( $this->mDisable
) {
40 wfDebug( "MessageCache::load(): disabled\n" );
46 if ( $this->mUseCache
) {
47 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
49 # If there's nothing in memcached, load all the messages from the database
50 if ( !$this->mCache
) {
51 wfDebug( "MessageCache::load(): loading all messages\n" );
53 # Other threads don't need to load the messages if another thread is doing it.
54 $this->mMemc
->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
57 # Keep trying if it fails, this is kind of important
58 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
59 usleep(mt_rand(500000,1500000));
62 $this->mMemc
->set( $this->mMemcKey
, "error", 86400 );
63 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
68 if ( !is_array( $this->mCache
) ) {
69 wfMsg( "MessageCache::load(): individual message mode\n" );
70 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
71 # Causing too much DB load, disabling -- TS
72 $this->mDisable
= true;
74 if ( $this->mCache == "loading" ) {
75 $this->mUseCache = false;
76 } elseif ( $this->mCache == "error" ) {
77 $this->mUseCache = false;
80 $this->mDisable = true;
83 $this->mCache
= false;
89 # Loads all cacheable messages from the database
92 $fname = "MessageCache::loadFromDB";
93 $dbr =& wfGetDB( DB_READ
);
94 $res = $dbr->select( 'cur',
95 array( 'cur_title', 'cur_text' ),
96 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI
),
100 $this->mCache
= array();
101 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
102 $this->mCache
[$row->cur_title
] = $row->cur_text
;
105 $dbr->freeResult( $res );
108 # Not really needed anymore
110 global $wgAllMessagesEn, $wgLang;
111 if ( !$this->mKeys
) {
112 $this->mKeys
= array();
113 foreach ( $wgAllMessagesEn as $key => $value ) {
114 array_push( $this->mKeys
, $wgLang->ucfirst( $key ) );
121 function isCacheable( $key ) {
124 global $wgAllMessagesEn, $wgLang;
125 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
126 array_key_exists( $key, $wgAllMessagesEn );
130 function replace( $title, $text ) {
133 if ( is_array( $this->mCache
) ) {
134 $this->mCache
[$title] = $text;
135 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
141 # Represents a write lock on the messages key
143 if ( !$this->mUseCache
) {
147 $lockKey = $this->mMemcKey
. "lock";
148 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
152 return $i >= MSG_WAIT_TIMEOUT
;
156 if ( !$this->mUseCache
) {
160 $lockKey = $this->mMemcKey
. "lock";
161 $this->mMemc
->delete( $lockKey );
164 function get( $key, $useDB ) {
165 global $wgLang, $wgLanguageCode;
167 # If uninitialised, someone is trying to call this halfway through Setup.php
168 if ( !$this->mInitialised
) {
169 return "<$key>";
173 if ( !$this->mDisable
) {
174 $title = $wgLang->ucfirst( $key );
178 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
179 $message = $this->mCache
[$title];
182 # If it wasn't in the cache, load each message from the DB individually
183 if ( !$message && $useDB) {
184 $dbr =& wfGetDB( DB_READ
);
185 $result = $dbr->getArray( "cur", array("cur_text"),
186 array( "cur_namespace" => NS_MEDIAWIKI
, "cur_title" => $title ),
187 "MessageCache::get" );
189 $message = $result->cur_text
;
193 # Try the extension array
195 $message = @$this->mExtensionMessages
[$key];
198 # Try the array in $wgLang
200 $message = $wgLang->getMessage( $key );
203 # Try the English array
204 if ( !$message && $wgLanguageCode != "en" ) {
205 $message = Language
::getMessage( $key );
210 $message = "<$key>";
214 $message = $this->transform( $message );
218 function transform( $message ) {
219 if( !$this->mDisableTransform
) {
220 if ( strstr( $message, "{{" ) !== false ) {
221 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
227 function disable() { $this->mDisable
= true; }
228 function enable() { $this->mDisable
= false; }
229 function disableTransform() { $this->mDisableTransform
= true; }
231 function addMessage( $key, $value ) {
232 $this->mExtensionMessages
[$key] = $value;
235 function addMessages( $messages ) {
236 foreach ( $messages as $key => $value ) {
237 $this->mExtensionMessages
[$key] = $value;