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