Fix bug 2642 : watchdetails message using HTML instead of wiki syntax. Patch by zigge...
[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;
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, "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 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
100 usleep(mt_rand(500000,1500000));
101 }
102 wfProfileOut( $fname.'-save' );
103 if ( $i == 20 ) {
104 $this->mMemc->set( $this->mMemcKey, 'error', 60*5 );
105 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
106 }
107 }
108 $this->unlock();
109 }
110
111 if ( !is_array( $this->mCache ) ) {
112 wfDebug( "MessageCache::load(): individual message mode\n" );
113 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
114 # Causing too much DB load, disabling -- TS
115 $this->mDisable = true;
116 /*
117 if ( $this->mCache == "loading" ) {
118 $this->mUseCache = false;
119 } elseif ( $this->mCache == "error" ) {
120 $this->mUseCache = false;
121 $success = false;
122 } else {
123 $this->mDisable = true;
124 $success = false;
125 }*/
126 $this->mCache = false;
127 }
128 }
129 wfProfileOut( $fname );
130 $this->mDeferred = false;
131 return $success;
132 }
133
134 /**
135 * Loads all or main part of cacheable messages from the database
136 */
137 function loadFromDB() {
138 $fname = 'MessageCache::loadFromDB';
139 $dbr =& wfGetDB( DB_SLAVE );
140 $conditions = array( 'page_is_redirect' => 0,
141 'page_namespace' => NS_MEDIAWIKI);
142 $res = $dbr->select( array( 'page', 'revision', 'text' ),
143 array( 'page_title', 'old_text', 'old_flags' ),
144 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
145 $fname
146 );
147
148 $this->mCache = array();
149 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
150 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
151 }
152
153 $dbr->freeResult( $res );
154 /*
155 # FIXME: This is too slow currently.
156 # We need to bulk-fetch revisions, but in a portable way...
157 $resultSet = Revision::fetchFromConds( $dbr, array(
158 'page_namespace' => NS_MEDIAWIKI,
159 'page_is_redirect' => 0,
160 'page_id=rev_page' ) );
161 while( $row = $resultSet->fetchObject() ) {
162 $revision = new Revision( $row );
163 $title = $revision->getTitle();
164 $this->mCache[$title->getDBkey()] = $revision->getText();
165 }
166 $resultSet->free();
167 */
168 }
169
170 /**
171 * Not really needed anymore
172 */
173 function getKeys() {
174 global $wgAllMessagesEn, $wgContLang;
175 if ( !$this->mKeys ) {
176 $this->mKeys = array();
177 foreach ( $wgAllMessagesEn as $key => $value ) {
178 $title = $wgContLang->ucfirst( $key );
179 array_push( $this->mKeys, $title );
180 }
181 }
182 return $this->mKeys;
183 }
184
185 /**
186 * @deprecated
187 */
188 function isCacheable( $key ) {
189 return true;
190 }
191
192 function replace( $title, $text ) {
193 $this->lock();
194 $this->load();
195 if ( is_array( $this->mCache ) ) {
196 $this->mCache[$title] = $text;
197 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
198 }
199 $this->unlock();
200 }
201
202 /**
203 * Returns success
204 * Represents a write lock on the messages key
205 */
206 function lock() {
207 if ( !$this->mUseCache ) {
208 return true;
209 }
210
211 $lockKey = $this->mMemcKey . 'lock';
212 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
213 sleep(1);
214 }
215
216 return $i >= MSG_WAIT_TIMEOUT;
217 }
218
219 function unlock() {
220 if ( !$this->mUseCache ) {
221 return;
222 }
223
224 $lockKey = $this->mMemcKey . 'lock';
225 $this->mMemc->delete( $lockKey );
226 }
227
228 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
229 global $wgContLanguageCode;
230 if( $forcontent ) {
231 global $wgContLang;
232 $lang =& $wgContLang;
233 $langcode = $wgContLanguageCode;
234 } else {
235 global $wgLang, $wgLanguageCode;
236 $lang =& $wgLang;
237 $langcode = $wgLanguageCode;
238 }
239 # If uninitialised, someone is trying to call this halfway through Setup.php
240 if( !$this->mInitialised ) {
241 return '&lt;' . htmlspecialchars($key) . '&gt;';
242 }
243 # If cache initialization was deferred, start it now.
244 if( $this->mDeferred ) {
245 $this->load();
246 }
247
248 $message = false;
249 if( !$this->mDisable && $useDB ) {
250 $title = $lang->ucfirst( $key );
251 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
252 $title .= '/' . $langcode;
253 }
254 $message = $this->getFromCache( $title );
255 }
256 # Try the extension array
257 if( !$message ) {
258 $message = @$this->mExtensionMessages[$key];
259 }
260
261 # Try the array in the language object
262 if( !$message ) {
263 wfSuppressWarnings();
264 $message = $lang->getMessage( $key );
265 wfRestoreWarnings();
266 }
267
268 # Try the English array
269 if( !$message && $langcode != 'en' ) {
270 wfSuppressWarnings();
271 $message = Language::getMessage( $key );
272 wfRestoreWarnings();
273 }
274
275 # Is this a custom message? Try the default language in the db...
276 if( !$message &&
277 !$this->mDisable && $useDB &&
278 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
279 $message = $this->getFromCache( $lang->ucfirst( $key ) );
280 }
281
282 # Final fallback
283 if( !$message ) {
284 return '&lt;' . htmlspecialchars($key) . '&gt;';
285 }
286
287 # Replace brace tags
288 $message = $this->transform( $message );
289 return $message;
290 }
291
292 function getFromCache( $title ) {
293 $message = false;
294
295 # Try the cache
296 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
297 $message = $this->mCache[$title];
298 }
299
300 if ( !$message && $this->mUseCache ) {
301 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
302 if( $message ) {
303 $this->mCache[$title] = $message;
304 }
305 }
306
307 # If it wasn't in the cache, load each message from the DB individually
308 if ( !$message ) {
309 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
310 if( $revision ) {
311 $message = $revision->getText();
312 if ($this->mUseCache) {
313 $this->mCache[$title]=$message;
314 /* individual messages may be often
315 recached until proper purge code exists
316 */
317 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
318 }
319 }
320 }
321
322 return $message;
323 }
324
325 function transform( $message ) {
326 if( !$this->mDisableTransform ) {
327 if( strpos( $message, '{{' ) !== false ) {
328 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
329 }
330 }
331 return $message;
332 }
333
334 function disable() { $this->mDisable = true; }
335 function enable() { $this->mDisable = false; }
336 function disableTransform() { $this->mDisableTransform = true; }
337 function enableTransform() { $this->mDisableTransform = false; }
338
339 /**
340 * Add a message to the cache
341 *
342 * @param mixed $key
343 * @param mixed $value
344 */
345 function addMessage( $key, $value ) {
346 $this->mExtensionMessages[$key] = $value;
347 }
348
349 /**
350 * Add an associative array of message to the cache
351 *
352 * @param array $messages An associative array of key => values to be added
353 */
354 function addMessages( $messages ) {
355 foreach ( $messages as $key => $value ) {
356 $this->mExtensionMessages[$key] = $value;
357 }
358 }
359
360 /**
361 * Clear all stored messages. Mainly used after a mass rebuild.
362 */
363 function clear() {
364 if( $this->mUseCache ) {
365 $this->mMemc->delete( $this->mMemcKey );
366 }
367 }
368 }
369 ?>