For custom messages, fall back to the default language's db messages
[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
161 /**
162 * Not really needed anymore
163 */
164 function getKeys() {
165 global $wgAllMessagesEn, $wgContLang;
166 if ( !$this->mKeys ) {
167 $this->mKeys = array();
168 foreach ( $wgAllMessagesEn as $key => $value ) {
169 $title = $wgContLang->ucfirst( $key );
170 array_push( $this->mKeys, $title );
171 }
172 }
173 return $this->mKeys;
174 }
175
176 /**
177 * Obsolete
178 */
179 function isCacheable( $key ) {
180 return true;
181 }
182
183 function replace( $title, $text ) {
184 $this->lock();
185 $this->load();
186 if ( is_array( $this->mCache ) ) {
187 $this->mCache[$title] = $text;
188 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
189 }
190 $this->unlock();
191 }
192
193 /**
194 * Returns success
195 * Represents a write lock on the messages key
196 */
197 function lock() {
198 if ( !$this->mUseCache ) {
199 return true;
200 }
201
202 $lockKey = $this->mMemcKey . 'lock';
203 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
204 sleep(1);
205 }
206
207 return $i >= MSG_WAIT_TIMEOUT;
208 }
209
210 function unlock() {
211 if ( !$this->mUseCache ) {
212 return;
213 }
214
215 $lockKey = $this->mMemcKey . 'lock';
216 $this->mMemc->delete( $lockKey );
217 }
218
219 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
220 global $wgContLanguageCode;
221 if( $forcontent ) {
222 global $wgContLang;
223 $lang =& $wgContLang;
224 $langcode = $wgContLanguageCode;
225 } else {
226 global $wgLang, $wgLanguageCode;
227 $lang =& $wgLang;
228 $langcode = $wgLanguageCode;
229 }
230 # If uninitialised, someone is trying to call this halfway through Setup.php
231 if( !$this->mInitialised ) {
232 return "&lt;$key&gt;";
233 }
234 # If cache initialization was deferred, start it now.
235 if( $this->mDeferred ) {
236 $this->load();
237 }
238
239 $message = false;
240 if( !$this->mDisable && $useDB ) {
241 $title = $lang->ucfirst( $key );
242 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
243 $title .= '/' . $langcode;
244 }
245 $message = $this->getFromCache( $title );
246 }
247 # Try the extension array
248 if( !$message ) {
249 $message = @$this->mExtensionMessages[$key];
250 }
251
252 # Try the array in the language object
253 if( !$message ) {
254 wfSuppressWarnings();
255 $message = $lang->getMessage( $key );
256 wfRestoreWarnings();
257 }
258
259 # Try the English array
260 if( !$message && $langcode != 'en' ) {
261 wfSuppressWarnings();
262 $message = Language::getMessage( $key );
263 wfRestoreWarnings();
264 }
265
266 # Is this a custom message? Try the default language in the db...
267 if( !$message &&
268 !$this->mDisable && $useDB &&
269 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
270 $message = $this->getFromCache( $lang->ucfirst( $key ) );
271 }
272
273 # Final fallback
274 if( !$message ) {
275 $message = "&lt;$key&gt;";
276 }
277
278 # Replace brace tags
279 $message = $this->transform( $message );
280 return $message;
281 }
282
283 function getFromCache( $title ) {
284 $message = false;
285
286 # Try the cache
287 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
288 $message = $this->mCache[$title];
289 }
290
291 if ( !$message && $this->mUseCache ) {
292 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
293 if( $message ) {
294 $this->mCache[$title] = $message;
295 }
296 }
297
298 # If it wasn't in the cache, load each message from the DB individually
299 if ( !$message ) {
300 $dbr =& wfGetDB( DB_SLAVE );
301 $result = $dbr->selectRow( array( 'page', 'text' ),
302 array( 'old_flags', 'old_text' ),
303 'page_namespace=' . NS_MEDIAWIKI .
304 ' AND page_title=' . $dbr->addQuotes( $title ) .
305 ' AND page_latest=old_id',
306 'MessageCache::get' );
307 if ( $result ) {
308 $message = Revision::getRevisionText( $result );
309 if ($this->mUseCache) {
310 $this->mCache[$title]=$message;
311 /* individual messages may be often
312 recached until proper purge code exists
313 */
314 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
315 }
316 }
317 }
318
319 return $message;
320 }
321
322 function transform( $message ) {
323 if( !$this->mDisableTransform ) {
324 if( strpos( $message, '{{' ) !== false ) {
325 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
326 }
327 }
328 return $message;
329 }
330
331 function disable() { $this->mDisable = true; }
332 function enable() { $this->mDisable = false; }
333 function disableTransform() { $this->mDisableTransform = true; }
334 function enableTransform() { $this->mDisableTransform = false; }
335
336 function addMessage( $key, $value ) {
337 $this->mExtensionMessages[$key] = $value;
338 }
339
340 function addMessages( $messages ) {
341 foreach ( $messages as $key => $value ) {
342 $this->mExtensionMessages[$key] = $value;
343 }
344 }
345
346 /**
347 * Clear all stored messages. Mainly used after a mass rebuild.
348 */
349 function clear() {
350 if( $this->mUseCache ) {
351 $this->mMemc->delete( $this->mMemcKey );
352 }
353 }
354 }
355 ?>