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 $success = $this->mMemc
->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
58 # Keep trying if it fails, this is kind of important
59 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
60 usleep(mt_rand(500000,1500000));
63 $this->mMemc
->set( $this->mMemcKey
, "error", 86400 );
64 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
70 if ( !is_array( $this->mCache
) ) {
71 wfMsg( "MessageCache::load(): individual message mode\n" );
72 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
73 # Causing too much DB load, disabling -- TS
74 $this->mDisable
= true;
76 if ( $this->mCache == "loading" ) {
77 $this->mUseCache = false;
78 } elseif ( $this->mCache == "error" ) {
79 $this->mUseCache = false;
82 $this->mDisable = true;
85 $this->mCache
= false;
91 # Loads all cacheable messages from the database
94 $fname = "MessageCache::loadFromDB";
95 $dbr =& wfGetDB( DB_SLAVE
);
96 $res = $dbr->select( 'cur',
97 array( 'cur_title', 'cur_text' ),
98 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI
),
102 $this->mCache
= array();
103 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
104 $this->mCache
[$row->cur_title
] = $row->cur_text
;
107 $dbr->freeResult( $res );
110 # Not really needed anymore
112 global $wgAllMessagesEn, $wgLang;
113 if ( !$this->mKeys
) {
114 $this->mKeys
= array();
115 foreach ( $wgAllMessagesEn as $key => $value ) {
116 array_push( $this->mKeys
, $wgLang->ucfirst( $key ) );
123 function isCacheable( $key ) {
126 global $wgAllMessagesEn, $wgLang;
127 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
128 array_key_exists( $key, $wgAllMessagesEn );
132 function replace( $title, $text ) {
135 if ( is_array( $this->mCache
) ) {
136 $this->mCache
[$title] = $text;
137 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
143 # Represents a write lock on the messages key
145 if ( !$this->mUseCache
) {
149 $lockKey = $this->mMemcKey
. "lock";
150 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
154 return $i >= MSG_WAIT_TIMEOUT
;
158 if ( !$this->mUseCache
) {
162 $lockKey = $this->mMemcKey
. "lock";
163 $this->mMemc
->delete( $lockKey );
166 function get( $key, $useDB ) {
167 global $wgLang, $wgLanguageCode;
169 # If uninitialised, someone is trying to call this halfway through Setup.php
170 if ( !$this->mInitialised
) {
171 return "<$key>";
175 if ( !$this->mDisable
) {
176 $title = $wgLang->ucfirst( $key );
180 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
181 $message = $this->mCache
[$title];
184 # If it wasn't in the cache, load each message from the DB individually
185 if ( !$message && $useDB) {
186 $dbr =& wfGetDB( DB_SLAVE
);
187 $result = $dbr->getArray( "cur", array("cur_text"),
188 array( "cur_namespace" => NS_MEDIAWIKI
, "cur_title" => $title ),
189 "MessageCache::get" );
191 $message = $result->cur_text
;
195 # Try the extension array
197 $message = @$this->mExtensionMessages
[$key];
200 # Try the array in $wgLang
202 $message = $wgLang->getMessage( $key );
205 # Try the English array
206 if ( !$message && $wgLanguageCode != "en" ) {
207 $message = Language
::getMessage( $key );
212 $message = "<$key>";
216 $message = $this->transform( $message );
220 function transform( $message ) {
221 if( !$this->mDisableTransform
) {
222 if ( strstr( $message, "{{" ) !== false ) {
223 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
229 function disable() { $this->mDisable
= true; }
230 function enable() { $this->mDisable
= false; }
231 function disableTransform() { $this->mDisableTransform
= true; }
233 function addMessage( $key, $value ) {
234 $this->mExtensionMessages
[$key] = $value;
237 function addMessages( $messages ) {
238 foreach ( $messages as $key => $value ) {
239 $this->mExtensionMessages
[$key] = $value;