Use Revision for individual message loads; not using it for bulk load just now; need...
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 */
6
7 /** */
8 require_once( 'Revision.php' );
9
10 /**
11 *
12 */
13 define( 'MSG_LOAD_TIMEOUT', 60);
14 define( 'MSG_LOCK_TIMEOUT', 10);
15 define( 'MSG_WAIT_TIMEOUT', 10);
16
17 /**
18 * Message cache
19 * Performs various useful MediaWiki namespace-related functions
20 *
21 * @package MediaWiki
22 */
23 class MessageCache
24 {
25 var $mCache, $mUseCache, $mDisable, $mExpiry;
26 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
27 var $mExtensionMessages;
28 var $mInitialised = false;
29 var $mDeferred = true;
30
31 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
32 $fname = 'MessageCache::initialise';
33 wfProfileIn( $fname );
34
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;
43
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' );
50
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;
56
57 wfProfileOut( $fname );
58 }
59
60 /**
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
64 */
65 function load() {
66 global $wgAllMessagesEn;
67
68 if ( $this->mDisable ) {
69 wfDebug( "MessageCache::load(): disabled\n" );
70 return true;
71 }
72 $fname = 'MessageCache::load';
73 wfProfileIn( $fname );
74 $success = true;
75
76 if ( $this->mUseCache ) {
77 wfProfileIn( $fname.'-fromcache' );
78 $this->mCache = $this->mMemc->get( $this->mMemcKey );
79 wfProfileOut( $fname.'-fromcache' );
80
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" );
84 $this->lock();
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 );
87 if ( $success ) {
88 wfProfileIn( $fname.'-load' );
89 $this->loadFromDB();
90 wfProfileOut( $fname.'-load' );
91 # Save in memcached
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));
96 }
97 wfProfileOut( $fname.'-save' );
98 if ( $i == 20 ) {
99 $this->mMemc->set( $this->mMemcKey, 'error', 60*5 );
100 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
101 }
102 }
103 $this->unlock();
104 }
105
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;
111 /*
112 if ( $this->mCache == "loading" ) {
113 $this->mUseCache = false;
114 } elseif ( $this->mCache == "error" ) {
115 $this->mUseCache = false;
116 $success = false;
117 } else {
118 $this->mDisable = true;
119 $success = false;
120 }*/
121 $this->mCache = false;
122 }
123 }
124 wfProfileOut( $fname );
125 $this->mDeferred = false;
126 return $success;
127 }
128
129 /**
130 * Loads all or main part of cacheable messages from the database
131 */
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;
142 } else {
143 require_once("MessageCacheHints.php");
144 $conditions['page_title']=MessageCacheHints::get();
145 }
146 }
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',
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 * Obsolete
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;$key&gt;";
247 }
248 # If cache initialization was deferred, start it now.
249 if( $this->mDeferred ) {
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 $message = "&lt;$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 function addMessage( $key, $value ) {
345 $this->mExtensionMessages[$key] = $value;
346 }
347
348 function addMessages( $messages ) {
349 foreach ( $messages as $key => $value ) {
350 $this->mExtensionMessages[$key] = $value;
351 }
352 }
353
354 /**
355 * Clear all stored messages. Mainly used after a mass rebuild.
356 */
357 function clear() {
358 if( $this->mUseCache ) {
359 $this->mMemc->delete( $this->mMemcKey );
360 }
361 }
362 }
363 ?>