Code style
[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
27 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
28 $fname = 'MessageCache::initialise';
29 wfProfileIn( $fname );
30
31 $this->mUseCache = !is_null( $memCached );
32 $this->mMemc = &$memCached;
33 $this->mDisable = !$useDB;
34 $this->mExpiry = $expiry;
35 $this->mDisableTransform = false;
36 $this->mMemcKey = $memcPrefix.':messages';
37 $this->mKeys = false; # initialised on demand
38 $this->mInitialised = true;
39
40 wfProfileIn( $fname.'-parseropt' );
41 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
42 wfProfileOut( $fname.'-parseropt' );
43 wfProfileIn( $fname.'-parser' );
44 $this->mParser = new Parser;
45 wfProfileOut( $fname.'-parser' );
46
47 $this->load();
48 wfProfileOut( $fname );
49 }
50
51 /**
52 * Loads messages either from memcached or the database, if not disabled
53 * On error, quietly switches to a fallback mode
54 * Returns false for a reportable error, true otherwise
55 */
56 function load() {
57 global $wgAllMessagesEn;
58
59 if ( $this->mDisable ) {
60 wfDebug( "MessageCache::load(): disabled\n" );
61 return true;
62 }
63 $fname = 'MessageCache::load';
64 wfProfileIn( $fname );
65 $success = true;
66
67 if ( $this->mUseCache ) {
68 wfProfileIn( $fname.'-fromcache' );
69 $this->mCache = $this->mMemc->get( $this->mMemcKey );
70 wfProfileOut( $fname.'-fromcache' );
71
72 # If there's nothing in memcached, load all the messages from the database
73 if ( !$this->mCache ) {
74 wfDebug( "MessageCache::load(): loading all messages\n" );
75 $this->lock();
76 # Other threads don't need to load the messages if another thread is doing it.
77 $success = $this->mMemc->add( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
78 if ( $success ) {
79 wfProfileIn( $fname.'-load' );
80 $this->loadFromDB();
81 wfProfileOut( $fname.'-load' );
82 # Save in memcached
83 # Keep trying if it fails, this is kind of important
84 wfProfileIn( $fname.'-save' );
85 for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
86 usleep(mt_rand(500000,1500000));
87 }
88 wfProfileOut( $fname.'-save' );
89 if ( $i == 20 ) {
90 $this->mMemc->set( $this->mMemcKey, 'error', 86400 );
91 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
92 }
93 }
94 $this->unlock();
95 }
96
97 if ( !is_array( $this->mCache ) ) {
98 wfMsg( "MessageCache::load(): individual message mode\n" );
99 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
100 # Causing too much DB load, disabling -- TS
101 $this->mDisable = true;
102 /*
103 if ( $this->mCache == "loading" ) {
104 $this->mUseCache = false;
105 } elseif ( $this->mCache == "error" ) {
106 $this->mUseCache = false;
107 $success = false;
108 } else {
109 $this->mDisable = true;
110 $success = false;
111 }*/
112 $this->mCache = false;
113 }
114 }
115 wfProfileOut( $fname );
116 return $success;
117 }
118
119 /**
120 * Loads all or main part of cacheable messages from the database
121 */
122 function loadFromDB() {
123 global $wgPartialMessageCache;
124 $fname = 'MessageCache::loadFromDB';
125 $dbr =& wfGetDB( DB_SLAVE );
126 $conditions = array( 'cur_is_redirect' => 0,
127 'cur_namespace' => NS_MEDIAWIKI);
128 if ($wgPartialMessageCache) {
129 if (is_array($wgPartialMessageCache)) {
130 $conditions['cur_title']=$wgPartialMessageCache;
131 } else {
132 require_once("MessageCacheHints.php");
133 $conditions['cur_title']=MessageCacheHints::get();
134 }
135 }
136 $res = $dbr->select( 'cur',
137 array( 'cur_title', 'cur_text' ), $conditions, $fname);
138
139 $this->mCache = array();
140 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
141 $this->mCache[$row->cur_title] = $row->cur_text;
142 }
143
144 $dbr->freeResult( $res );
145 }
146
147 /**
148 * Not really needed anymore
149 */
150 function getKeys() {
151 global $wgAllMessagesEn, $wgContLang;
152 if ( !$this->mKeys ) {
153 $this->mKeys = array();
154 foreach ( $wgAllMessagesEn as $key => $value ) {
155 array_push( $this->mKeys, $wgContLang->ucfirst( $key ) );
156 }
157 }
158 return $this->mKeys;
159 }
160
161 /**
162 * Obsolete
163 */
164 function isCacheable( $key ) {
165 return true;
166 /*
167 global $wgAllMessagesEn, $wgLang;
168 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
169 array_key_exists( $key, $wgAllMessagesEn );
170 */
171 }
172
173 function replace( $title, $text ) {
174 $this->lock();
175 $this->load();
176 if ( is_array( $this->mCache ) ) {
177 $this->mCache[$title] = $text;
178 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
179 }
180 $this->unlock();
181 }
182
183 /**
184 * Returns success
185 * Represents a write lock on the messages key
186 */
187 function lock() {
188 if ( !$this->mUseCache ) {
189 return true;
190 }
191
192 $lockKey = $this->mMemcKey . 'lock';
193 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
194 sleep(1);
195 }
196
197 return $i >= MSG_WAIT_TIMEOUT;
198 }
199
200 function unlock() {
201 if ( !$this->mUseCache ) {
202 return;
203 }
204
205 $lockKey = $this->mMemcKey . 'lock';
206 $this->mMemc->delete( $lockKey );
207 }
208
209 function get( $key, $useDB, $forcontent=true ) {
210 global $wgContLanguageCode;
211 if( $forcontent ) {
212 global $wgContLang;
213 $lang =& $wgContLang;
214 $langcode = $wgContLanguageCode;
215 } else {
216 global $wgLang, $wgLanguageCode;
217 $lang =& $wgLang;
218 $langcode = $wgLanguageCode;
219 }
220 # If uninitialised, someone is trying to call this halfway through Setup.php
221 if( !$this->mInitialised ) {
222 return "&lt;$key&gt;";
223 }
224
225 $message = false;
226 if( !$this->mDisable && $useDB ) {
227 $title = $lang->ucfirst( $key );
228 if( $langcode != $wgContLanguageCode ) {
229 $title .= '/' . $langcode;
230 }
231
232 # Try the cache
233 if( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
234 $message = $this->mCache[$title];
235 }
236
237 if ( !$message && $this->mUseCache ) {
238 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
239 if( $message ) {
240 $this->mCache[$title] = $message;
241 }
242 }
243
244 # If it wasn't in the cache, load each message from the DB individually
245 if ( !$message ) {
246 $dbr =& wfGetDB( DB_SLAVE );
247 $result = $dbr->selectRow( 'cur', array('cur_text'),
248 array( 'cur_namespace' => NS_MEDIAWIKI, 'cur_title' => $title ),
249 'MessageCache::get' );
250 if ( $result ) {
251 $message = $result->cur_text;
252 if( $this->mUseCache ) {
253 $this->mCache[$title] = $message;
254 /* individual messages may be often
255 recached until proper purge code exists
256 */
257 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
258 }
259 }
260 }
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 # Final fallback
282 if( !$message ) {
283 $message = "&lt;$key&gt;";
284 }
285
286 # Replace brace tags
287 $message = $this->transform( $message );
288 return $message;
289 }
290
291 function transform( $message ) {
292 if( !$this->mDisableTransform ) {
293 if( strpos( $message, '{{' ) !== false ) {
294 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
295 }
296 }
297 return $message;
298 }
299
300 function disable() { $this->mDisable = true; }
301 function enable() { $this->mDisable = false; }
302 function disableTransform() { $this->mDisableTransform = true; }
303 function enableTransform() { $this->mDisableTransform = false; }
304
305 function addMessage( $key, $value ) {
306 $this->mExtensionMessages[$key] = $value;
307 }
308
309 function addMessages( $messages ) {
310 foreach ( $messages as $key => $value ) {
311 $this->mExtensionMessages[$key] = $value;
312 }
313 }
314
315 /**
316 * Clear all stored messages. Mainly used after a mass rebuild.
317 */
318 function clear() {
319 if( $this->mUseCache ) {
320 $this->mMemc->delete( $this->mMemcKey );
321 }
322 }
323 }
324 ?>