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