8 require_once( 'Revision.php' );
13 define( 'MSG_LOAD_TIMEOUT', 60);
14 define( 'MSG_LOCK_TIMEOUT', 10);
15 define( 'MSG_WAIT_TIMEOUT', 10);
19 * Performs various useful MediaWiki namespace-related functions
25 var $mCache, $mUseCache, $mDisable, $mExpiry;
26 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
27 var $mExtensionMessages;
28 var $mInitialised = false;
29 var $mDeferred = true;
31 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
32 $fname = 'MessageCache::initialise';
33 wfProfileIn( $fname );
35 $this->mUseCache
= !is_null( $memCached );
36 $this->mMemc
= &$memCached;
37 $this->mDisable
= !$useDB;
38 $this->mExpiry
= $expiry;
39 $this->mDisableTransform
= false;
40 $this->mMemcKey
= $memcPrefix.':messages';
41 $this->mKeys
= false; # initialised on demand
42 $this->mInitialised
= true;
44 wfProfileIn( $fname.'-parseropt' );
45 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
46 wfProfileOut( $fname.'-parseropt' );
47 wfProfileIn( $fname.'-parser' );
48 $this->mParser
= new Parser
;
49 wfProfileOut( $fname.'-parser' );
51 # When we first get asked for a message,
52 # then we'll fill up the cache. If we
53 # can return a cache hit, this saves
54 # some extra milliseconds
55 $this->mDeferred
= true;
57 wfProfileOut( $fname );
61 * Loads messages either from memcached or the database, if not disabled
62 * On error, quietly switches to a fallback mode
63 * Returns false for a reportable error, true otherwise
66 global $wgAllMessagesEn;
68 if ( $this->mDisable
) {
69 wfDebug( "MessageCache::load(): disabled\n" );
72 $fname = 'MessageCache::load';
73 wfProfileIn( $fname );
76 if ( $this->mUseCache
) {
77 wfProfileIn( $fname.'-fromcache' );
78 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
79 wfProfileOut( $fname.'-fromcache' );
81 # If there's nothing in memcached, load all the messages from the database
82 if ( !$this->mCache
) {
83 wfDebug( "MessageCache::load(): loading all messages\n" );
85 # Other threads don't need to load the messages if another thread is doing it.
86 $success = $this->mMemc
->add( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
88 wfProfileIn( $fname.'-load' );
90 wfProfileOut( $fname.'-load' );
92 # Keep trying if it fails, this is kind of important
93 wfProfileIn( $fname.'-save' );
94 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
95 usleep(mt_rand(500000,1500000));
97 wfProfileOut( $fname.'-save' );
99 $this->mMemc
->set( $this->mMemcKey
, 'error', 86400 );
100 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
106 if ( !is_array( $this->mCache
) ) {
107 wfDebug( "MessageCache::load(): individual message mode\n" );
108 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
109 # Causing too much DB load, disabling -- TS
110 $this->mDisable
= true;
112 if ( $this->mCache == "loading" ) {
113 $this->mUseCache = false;
114 } elseif ( $this->mCache == "error" ) {
115 $this->mUseCache = false;
118 $this->mDisable = true;
121 $this->mCache
= false;
124 wfProfileOut( $fname );
125 $this->mDeferred
= false;
130 * Loads all or main part of cacheable messages from the database
132 function loadFromDB() {
133 global $wgPartialMessageCache;
134 $fname = 'MessageCache::loadFromDB';
135 $dbr =& wfGetDB( DB_SLAVE
);
136 $conditions = array( 'page_is_redirect' => 0,
137 'page_namespace' => NS_MEDIAWIKI
);
138 if ($wgPartialMessageCache) {
139 wfDebugDieBacktrace( "Confused about how this works." );
140 if (is_array($wgPartialMessageCache)) {
141 $conditions['page_title']=$wgPartialMessageCache;
143 require_once("MessageCacheHints.php");
144 $conditions['page_title']=MessageCacheHints
::get();
147 $res = $dbr->select( array( 'page', 'text' ),
148 array( 'page_title', 'old_text', 'old_flags' ),
149 'page_is_redirect=0 AND page_namespace = '.NS_MEDIAWIKI
.' AND page_latest = old_id',
153 $this->mCache
= array();
154 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
155 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
158 $dbr->freeResult( $res );
162 * Not really needed anymore
165 global $wgAllMessagesEn, $wgContLang;
166 if ( !$this->mKeys
) {
167 $this->mKeys
= array();
168 foreach ( $wgAllMessagesEn as $key => $value ) {
169 $title = $wgContLang->ucfirst( $key );
170 array_push( $this->mKeys
, $title );
179 function isCacheable( $key ) {
183 function replace( $title, $text ) {
186 if ( is_array( $this->mCache
) ) {
187 $this->mCache
[$title] = $text;
188 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
195 * Represents a write lock on the messages key
198 if ( !$this->mUseCache
) {
202 $lockKey = $this->mMemcKey
. 'lock';
203 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
207 return $i >= MSG_WAIT_TIMEOUT
;
211 if ( !$this->mUseCache
) {
215 $lockKey = $this->mMemcKey
. 'lock';
216 $this->mMemc
->delete( $lockKey );
219 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
220 global $wgContLanguageCode;
223 $lang =& $wgContLang;
224 $langcode = $wgContLanguageCode;
226 global $wgLang, $wgLanguageCode;
228 $langcode = $wgLanguageCode;
230 # If uninitialised, someone is trying to call this halfway through Setup.php
231 if( !$this->mInitialised
) {
232 return "<$key>";
234 # If cache initialization was deferred, start it now.
235 if( $this->mDeferred
) {
240 if( !$this->mDisable
&& $useDB ) {
241 $title = $lang->ucfirst( $key );
242 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
243 $title .= '/' . $langcode;
247 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
248 $message = $this->mCache
[$title];
251 if ( !$message && $this->mUseCache
) {
252 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
254 $this->mCache
[$title] = $message;
258 # If it wasn't in the cache, load each message from the DB individually
260 $dbr =& wfGetDB( DB_SLAVE
);
261 $result = $dbr->selectRow( array( 'page', 'text' ),
262 array( 'old_flags', 'old_text' ),
263 'page_namespace=' . NS_MEDIAWIKI
.
264 ' AND page_title=' . $dbr->addQuotes( $title ) .
265 ' AND page_latest=old_id',
266 'MessageCache::get' );
268 $message = Revision
::getRevisionText( $result );
269 if ($this->mUseCache
) {
270 $this->mCache
[$title]=$message;
271 /* individual messages may be often
272 recached until proper purge code exists
274 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
279 # Try the extension array
281 $message = @$this->mExtensionMessages
[$key];
284 # Try the array in the language object
286 wfSuppressWarnings();
287 $message = $lang->getMessage( $key );
291 # Try the English array
292 if( !$message && $langcode != 'en' ) {
293 wfSuppressWarnings();
294 $message = Language
::getMessage( $key );
300 $message = "<$key>";
304 $message = $this->transform( $message );
308 function transform( $message ) {
309 if( !$this->mDisableTransform
) {
310 if( strpos( $message, '{{' ) !== false ) {
311 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
317 function disable() { $this->mDisable
= true; }
318 function enable() { $this->mDisable
= false; }
319 function disableTransform() { $this->mDisableTransform
= true; }
320 function enableTransform() { $this->mDisableTransform
= false; }
322 function addMessage( $key, $value ) {
323 $this->mExtensionMessages
[$key] = $value;
326 function addMessages( $messages ) {
327 foreach ( $messages as $key => $value ) {
328 $this->mExtensionMessages
[$key] = $value;
333 * Clear all stored messages. Mainly used after a mass rebuild.
336 if( $this->mUseCache
) {
337 $this->mMemc
->delete( $this->mMemcKey
);