* Removed redudant global decleration
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /** */
9 require_once( 'Revision.php' );
10
11 /**
12 *
13 */
14 define( 'MSG_LOAD_TIMEOUT', 60);
15 define( 'MSG_LOCK_TIMEOUT', 10);
16 define( 'MSG_WAIT_TIMEOUT', 10);
17
18 /**
19 * Message cache
20 * Performs various useful MediaWiki namespace-related functions
21 *
22 * @package MediaWiki
23 */
24 class MessageCache
25 {
26 var $mCache, $mUseCache, $mDisable, $mExpiry;
27 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
28 var $mExtensionMessages = array();
29 var $mInitialised = false;
30 var $mDeferred = true;
31
32 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
33 $fname = 'MessageCache::initialise';
34 wfProfileIn( $fname );
35
36 $this->mUseCache = !is_null( $memCached );
37 $this->mMemc = &$memCached;
38 $this->mDisable = !$useDB;
39 $this->mExpiry = $expiry;
40 $this->mDisableTransform = false;
41 $this->mMemcKey = $memcPrefix.':messages';
42 $this->mKeys = false; # initialised on demand
43 $this->mInitialised = true;
44
45 wfProfileIn( $fname.'-parseropt' );
46 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
47 wfProfileOut( $fname.'-parseropt' );
48 wfProfileIn( $fname.'-parser' );
49 $this->mParser = new Parser;
50 wfProfileOut( $fname.'-parser' );
51
52 # When we first get asked for a message,
53 # then we'll fill up the cache. If we
54 # can return a cache hit, this saves
55 # some extra milliseconds
56 $this->mDeferred = true;
57
58 wfProfileOut( $fname );
59 }
60
61 /**
62 * Loads messages either from memcached or the database, if not disabled
63 * On error, quietly switches to a fallback mode
64 * Returns false for a reportable error, true otherwise
65 */
66 function load() {
67 global $wgAllMessagesEn;
68
69 if ( $this->mDisable ) {
70 static $shownDisabled = false;
71 if ( !$shownDisabled ) {
72 wfDebug( "MessageCache::load(): disabled\n" );
73 $shownDisabled = true;
74 }
75 return true;
76 }
77 $fname = 'MessageCache::load';
78 wfProfileIn( $fname );
79 $success = true;
80
81 if ( $this->mUseCache ) {
82 wfProfileIn( $fname.'-fromcache' );
83 $this->mCache = $this->mMemc->get( $this->mMemcKey );
84 wfProfileOut( $fname.'-fromcache' );
85
86 # If there's nothing in memcached, load all the messages from the database
87 if ( !$this->mCache ) {
88 wfDebug( "MessageCache::load(): loading all messages\n" );
89 $this->lock();
90 # Other threads don't need to load the messages if another thread is doing it.
91 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
92 if ( $success ) {
93 wfProfileIn( $fname.'-load' );
94 $this->loadFromDB();
95 wfProfileOut( $fname.'-load' );
96 # Save in memcached
97 # Keep trying if it fails, this is kind of important
98 wfProfileIn( $fname.'-save' );
99 for ($i=0; $i<20 &&
100 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
101 $i++ ) {
102 usleep(mt_rand(500000,1500000));
103 }
104 wfProfileOut( $fname.'-save' );
105 if ( $i == 20 ) {
106 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
107 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
108 }
109 }
110 $this->unlock();
111 }
112
113 if ( !is_array( $this->mCache ) ) {
114 wfDebug( "MessageCache::load(): individual message mode\n" );
115 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
116 # Causing too much DB load, disabling -- TS
117 $this->mDisable = true;
118 /*
119 if ( $this->mCache == "loading" ) {
120 $this->mUseCache = false;
121 } elseif ( $this->mCache == "error" ) {
122 $this->mUseCache = false;
123 $success = false;
124 } else {
125 $this->mDisable = true;
126 $success = false;
127 }*/
128 $this->mCache = false;
129 }
130 }
131 wfProfileOut( $fname );
132 $this->mDeferred = false;
133 return $success;
134 }
135
136 /**
137 * Loads all or main part of cacheable messages from the database
138 */
139 function loadFromDB() {
140 $fname = 'MessageCache::loadFromDB';
141 $dbr =& wfGetDB( DB_SLAVE );
142 if ( !$dbr ) {
143 wfDebugDieBacktrace( 'Invalid database object' );
144 }
145 $conditions = array( 'page_is_redirect' => 0,
146 'page_namespace' => NS_MEDIAWIKI);
147 $res = $dbr->select( array( 'page', 'revision', 'text' ),
148 array( 'page_title', 'old_text', 'old_flags' ),
149 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
150 $fname
151 );
152
153 $this->mCache = array();
154 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
155 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
156 }
157
158 $dbr->freeResult( $res );
159 /*
160 # FIXME: This is too slow currently.
161 # We need to bulk-fetch revisions, but in a portable way...
162 $resultSet = Revision::fetchFromConds( $dbr, array(
163 'page_namespace' => NS_MEDIAWIKI,
164 'page_is_redirect' => 0,
165 'page_id=rev_page' ) );
166 while( $row = $resultSet->fetchObject() ) {
167 $revision = new Revision( $row );
168 $title = $revision->getTitle();
169 $this->mCache[$title->getDBkey()] = $revision->getText();
170 }
171 $resultSet->free();
172 */
173 }
174
175 /**
176 * Not really needed anymore
177 */
178 function getKeys() {
179 global $wgAllMessagesEn, $wgContLang;
180 if ( !$this->mKeys ) {
181 $this->mKeys = array();
182 foreach ( $wgAllMessagesEn as $key => $value ) {
183 $title = $wgContLang->ucfirst( $key );
184 array_push( $this->mKeys, $title );
185 }
186 }
187 return $this->mKeys;
188 }
189
190 /**
191 * @deprecated
192 */
193 function isCacheable( $key ) {
194 return true;
195 }
196
197 function replace( $title, $text ) {
198 $this->lock();
199 $this->load();
200 if ( is_array( $this->mCache ) ) {
201 $this->mCache[$title] = $text;
202 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
203 }
204 $this->unlock();
205 }
206
207 /**
208 * Returns success
209 * Represents a write lock on the messages key
210 */
211 function lock() {
212 if ( !$this->mUseCache ) {
213 return true;
214 }
215
216 $lockKey = $this->mMemcKey . 'lock';
217 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
218 sleep(1);
219 }
220
221 return $i >= MSG_WAIT_TIMEOUT;
222 }
223
224 function unlock() {
225 if ( !$this->mUseCache ) {
226 return;
227 }
228
229 $lockKey = $this->mMemcKey . 'lock';
230 $this->mMemc->delete( $lockKey );
231 }
232
233 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
234 global $wgContLanguageCode;
235 if( $forcontent ) {
236 global $wgContLang;
237 $lang =& $wgContLang;
238 $langcode = $wgContLanguageCode;
239 } else {
240 global $wgLang, $wgLanguageCode;
241 $lang =& $wgLang;
242 $langcode = $wgLanguageCode;
243 }
244 # If uninitialised, someone is trying to call this halfway through Setup.php
245 if( !$this->mInitialised ) {
246 return '&lt;' . htmlspecialchars($key) . '&gt;';
247 }
248 # If cache initialization was deferred, start it now.
249 if( $this->mDeferred && !$this->mDisable && $useDB ) {
250 $this->load();
251 }
252
253 $message = false;
254 if( !$this->mDisable && $useDB ) {
255 $title = $lang->ucfirst( $key );
256 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
257 $title .= '/' . $langcode;
258 }
259 $message = $this->getFromCache( $title );
260 }
261 # Try the extension array
262 if( !$message ) {
263 $message = @$this->mExtensionMessages[$key];
264 }
265
266 # Try the array in the language object
267 if( !$message ) {
268 wfSuppressWarnings();
269 $message = $lang->getMessage( $key );
270 wfRestoreWarnings();
271 }
272
273 # Try the English array
274 if( !$message && $langcode != 'en' ) {
275 wfSuppressWarnings();
276 $message = Language::getMessage( $key );
277 wfRestoreWarnings();
278 }
279
280 # Is this a custom message? Try the default language in the db...
281 if( !$message &&
282 !$this->mDisable && $useDB &&
283 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
284 $message = $this->getFromCache( $lang->ucfirst( $key ) );
285 }
286
287 # Final fallback
288 if( !$message ) {
289 return '&lt;' . htmlspecialchars($key) . '&gt;';
290 }
291
292 # Replace brace tags
293 $message = $this->transform( $message );
294 return $message;
295 }
296
297 function getFromCache( $title ) {
298 $message = false;
299
300 # Try the cache
301 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
302 $message = $this->mCache[$title];
303 }
304
305 if ( !$message && $this->mUseCache ) {
306 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
307 if( $message ) {
308 $this->mCache[$title] = $message;
309 }
310 }
311
312 # If it wasn't in the cache, load each message from the DB individually
313 if ( !$message ) {
314 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
315 if( $revision ) {
316 $message = $revision->getText();
317 if ($this->mUseCache) {
318 $this->mCache[$title]=$message;
319 /* individual messages may be often
320 recached until proper purge code exists
321 */
322 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
323 }
324 }
325 }
326
327 return $message;
328 }
329
330 function transform( $message ) {
331 if( !$this->mDisableTransform ) {
332 if( strpos( $message, '{{' ) !== false ) {
333 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
334 }
335 }
336 return $message;
337 }
338
339 function disable() { $this->mDisable = true; }
340 function enable() { $this->mDisable = false; }
341 function disableTransform() { $this->mDisableTransform = true; }
342 function enableTransform() { $this->mDisableTransform = false; }
343
344 /**
345 * Add a message to the cache
346 *
347 * @param mixed $key
348 * @param mixed $value
349 */
350 function addMessage( $key, $value ) {
351 $this->mExtensionMessages[$key] = $value;
352 }
353
354 /**
355 * Add an associative array of message to the cache
356 *
357 * @param array $messages An associative array of key => values to be added
358 */
359 function addMessages( $messages ) {
360 foreach ( $messages as $key => $value ) {
361 $this->addMessage( $key, $value );
362 }
363 }
364
365 /**
366 * Clear all stored messages. Mainly used after a mass rebuild.
367 */
368 function clear() {
369 if( $this->mUseCache ) {
370 $this->mMemc->delete( $this->mMemcKey );
371 }
372 }
373 }
374 ?>