7 require_once( 'Revision.php' );
12 define( 'MSG_LOAD_TIMEOUT', 60);
13 define( 'MSG_LOCK_TIMEOUT', 10);
14 define( 'MSG_WAIT_TIMEOUT', 10);
18 * Performs various useful MediaWiki namespace-related functions
24 var $mCache, $mUseCache, $mDisable, $mExpiry;
25 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
26 var $mExtensionMessages;
27 var $mInitialised = false;
28 var $mDeferred = true;
30 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
31 $fname = 'MessageCache::initialise';
32 wfProfileIn( $fname );
34 $this->mUseCache
= !is_null( $memCached );
35 $this->mMemc
= &$memCached;
36 $this->mDisable
= !$useDB;
37 $this->mExpiry
= $expiry;
38 $this->mDisableTransform
= false;
39 $this->mMemcKey
= $memcPrefix.':messages';
40 $this->mKeys
= false; # initialised on demand
41 $this->mInitialised
= true;
43 wfProfileIn( $fname.'-parseropt' );
44 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
45 wfProfileOut( $fname.'-parseropt' );
46 wfProfileIn( $fname.'-parser' );
47 $this->mParser
= new Parser
;
48 wfProfileOut( $fname.'-parser' );
50 # When we first get asked for a message,
51 # then we'll fill up the cache. If we
52 # can return a cache hit, this saves
53 # some extra milliseconds
54 $this->mDeferred
= true;
56 wfProfileOut( $fname );
60 * Loads messages either from memcached or the database, if not disabled
61 * On error, quietly switches to a fallback mode
62 * Returns false for a reportable error, true otherwise
65 global $wgAllMessagesEn;
67 if ( $this->mDisable
) {
68 wfDebug( "MessageCache::load(): disabled\n" );
71 $fname = 'MessageCache::load';
72 wfProfileIn( $fname );
75 if ( $this->mUseCache
) {
76 wfProfileIn( $fname.'-fromcache' );
77 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
78 wfProfileOut( $fname.'-fromcache' );
80 # If there's nothing in memcached, load all the messages from the database
81 if ( !$this->mCache
) {
82 wfDebug( "MessageCache::load(): loading all messages\n" );
84 # Other threads don't need to load the messages if another thread is doing it.
85 $success = $this->mMemc
->add( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
87 wfProfileIn( $fname.'-load' );
89 wfProfileOut( $fname.'-load' );
91 # Keep trying if it fails, this is kind of important
92 wfProfileIn( $fname.'-save' );
93 for ( $i=0; $i<20 && !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
); $i++
) {
94 usleep(mt_rand(500000,1500000));
96 wfProfileOut( $fname.'-save' );
98 $this->mMemc
->set( $this->mMemcKey
, 'error', 86400 );
99 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
105 if ( !is_array( $this->mCache
) ) {
106 wfDebug( "MessageCache::load(): individual message mode\n" );
107 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
108 # Causing too much DB load, disabling -- TS
109 $this->mDisable
= true;
111 if ( $this->mCache == "loading" ) {
112 $this->mUseCache = false;
113 } elseif ( $this->mCache == "error" ) {
114 $this->mUseCache = false;
117 $this->mDisable = true;
120 $this->mCache
= false;
123 wfProfileOut( $fname );
124 $this->mDeferred
= false;
129 * Loads all or main part of cacheable messages from the database
131 function loadFromDB() {
132 global $wgPartialMessageCache;
133 $fname = 'MessageCache::loadFromDB';
134 $dbr =& wfGetDB( DB_SLAVE
);
135 $conditions = array( 'page_is_redirect' => 0,
136 'page_namespace' => NS_MEDIAWIKI
);
137 if ($wgPartialMessageCache) {
138 wfDebugDieBacktrace( "Confused about how this works." );
139 if (is_array($wgPartialMessageCache)) {
140 $conditions['page_title']=$wgPartialMessageCache;
142 require_once("MessageCacheHints.php");
143 $conditions['page_title']=MessageCacheHints
::get();
146 $res = $dbr->select( array( 'page', 'text' ),
147 array( 'page_title', 'old_text', 'old_flags' ),
148 'page_is_redirect=0 AND page_namespace = '.NS_MEDIAWIKI
.' AND page_latest = old_id',
152 $this->mCache
= array();
153 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
154 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
157 $dbr->freeResult( $res );
161 * Not really needed anymore
164 global $wgAllMessagesEn, $wgContLang;
165 if ( !$this->mKeys
) {
166 $this->mKeys
= array();
167 foreach ( $wgAllMessagesEn as $key => $value ) {
168 global $wgCapitalLinks;
169 $title = $wgCapitalLinks ?
$wgContLang->ucfirst( $key ) : $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 ) {
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 global $wgCapitalLinks;
242 $title = $wgCapitalLinks ?
$lang->ucfirst( $key ) : $key;
243 if( $langcode != $wgContLanguageCode ) {
244 $title .= '/' . $langcode;
248 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
249 $message = $this->mCache
[$title];
252 if ( !$message && $this->mUseCache
) {
253 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
255 $this->mCache
[$title] = $message;
259 # If it wasn't in the cache, load each message from the DB individually
261 $dbr =& wfGetDB( DB_SLAVE
);
262 $result = $dbr->selectRow( array( 'page', 'text' ),
263 array( 'old_flags', 'old_text' ),
264 'page_namespace=' . NS_MEDIAWIKI
.
265 ' AND page_title=' . $dbr->addQuotes( $title ) .
266 ' AND page_latest=old_id',
267 'MessageCache::get' );
269 $message = Revision
::getRevisionText( $result );
270 if ($this->mUseCache
) {
271 $this->mCache
[$title]=$message;
272 /* individual messages may be often
273 recached until proper purge code exists
275 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
280 # Try the extension array
282 $message = @$this->mExtensionMessages
[$key];
285 # Try the array in the language object
287 wfSuppressWarnings();
288 $message = $lang->getMessage( $key );
292 # Try the English array
293 if( !$message && $langcode != 'en' ) {
294 wfSuppressWarnings();
295 $message = Language
::getMessage( $key );
301 $message = "<$key>";
305 $message = $this->transform( $message );
309 function transform( $message ) {
310 if( !$this->mDisableTransform
) {
311 if( strpos( $message, '{{' ) !== false ) {
312 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
318 function disable() { $this->mDisable
= true; }
319 function enable() { $this->mDisable
= false; }
320 function disableTransform() { $this->mDisableTransform
= true; }
321 function enableTransform() { $this->mDisableTransform
= false; }
323 function addMessage( $key, $value ) {
324 $this->mExtensionMessages
[$key] = $value;
327 function addMessages( $messages ) {
328 foreach ( $messages as $key => $value ) {
329 $this->mExtensionMessages
[$key] = $value;
334 * Clear all stored messages. Mainly used after a mass rebuild.
337 if( $this->mUseCache
) {
338 $this->mMemc
->delete( $this->mMemcKey
);