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