first attempt to support multiple language variants in the MediaWiki: namespace.
[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->set( $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 cacheable messages from the database
121 */
122 function loadFromDB() {
123 $fname = 'MessageCache::loadFromDB';
124 $dbr =& wfGetDB( DB_SLAVE );
125 $res = $dbr->select( 'cur',
126 array( 'cur_title', 'cur_text' ),
127 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI ),
128 $fname
129 );
130
131 $this->mCache = array();
132 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
133 $this->mCache[$row->cur_title] = $row->cur_text;
134 }
135
136 $dbr->freeResult( $res );
137 }
138
139 /**
140 * Not really needed anymore
141 */
142 function getKeys() {
143 global $wgAllMessagesEn, $wgContLang;
144 if ( !$this->mKeys ) {
145 $this->mKeys = array();
146 foreach ( $wgAllMessagesEn as $key => $value ) {
147 array_push( $this->mKeys, $wgContLang->ucfirst( $key ) );
148 }
149 }
150 return $this->mKeys;
151 }
152
153 /**
154 * Obsolete
155 */
156 function isCacheable( $key ) {
157 return true;
158 /*
159 global $wgAllMessagesEn, $wgLang;
160 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
161 array_key_exists( $key, $wgAllMessagesEn );
162 */
163 }
164
165 function replace( $title, $text ) {
166 $this->lock();
167 $this->load();
168 if ( is_array( $this->mCache ) ) {
169 $this->mCache[$title] = $text;
170 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
171 }
172 $this->unlock();
173 }
174
175 /**
176 * Returns success
177 * Represents a write lock on the messages key
178 */
179 function lock() {
180 if ( !$this->mUseCache ) {
181 return true;
182 }
183
184 $lockKey = $this->mMemcKey . 'lock';
185 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
186 sleep(1);
187 }
188
189 return $i >= MSG_WAIT_TIMEOUT;
190 }
191
192 function unlock() {
193 if ( !$this->mUseCache ) {
194 return;
195 }
196
197 $lockKey = $this->mMemcKey . 'lock';
198 $this->mMemc->delete( $lockKey );
199 }
200
201 function get( $key, $useDB, $forcontent=true ) {
202 if($forcontent) {
203 global $wgContLang, $wgContLanguageCode;
204 $lang = $wgContLang;
205 $langcode = $wgContLanguageCode;
206 }
207 else {
208 global $wgLang, $wgLanguageCode;
209 $lang = $wgLang;
210 $langcode = $wgLanguageCode;
211 }
212 # If uninitialised, someone is trying to call this halfway through Setup.php
213 if ( !$this->mInitialised ) {
214 return "&lt;$key&gt;";
215 }
216
217 $message = false;
218 if ( !$this->mDisable && $useDB ) {
219 $title = $lang->ucfirst( $key )."/$langcode";
220
221
222 # Try the cache
223 if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
224 $message = $this->mCache[$title];
225 }
226
227 # If it wasn't in the cache, load each message from the DB individually
228 if ( !$message ) {
229 $dbr =& wfGetDB( DB_SLAVE );
230 $result = $dbr->getArray( 'cur', array('cur_text'),
231 array( 'cur_namespace' => NS_MEDIAWIKI, 'cur_title' => $title ),
232 'MessageCache::get' );
233 if ( $result ) {
234 $message = $result->cur_text;
235 }
236 }
237 }
238 # Try the extension array
239 if ( !$message ) {
240 $message = @$this->mExtensionMessages[$key];
241 }
242
243 # Try the array in the language object
244 if ( !$message ) {
245 wfSuppressWarnings();
246 $message = $lang->getMessage( $key );
247 wfRestoreWarnings();
248 }
249
250 # Try the English array
251 if ( !$message && $langcode != 'en' ) {
252 wfSuppressWarnings();
253 $message = Language::getMessage( $key );
254 wfRestoreWarnings();
255 }
256
257 # Final fallback
258 if ( !$message ) {
259 $message = "&lt;$key&gt;";
260 }
261
262 # Replace brace tags
263 $message = $this->transform( $message );
264 return $message;
265 }
266
267 function transform( $message ) {
268 if( !$this->mDisableTransform ) {
269 if ( strstr( $message, '{{' ) !== false ) {
270 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
271 }
272 }
273 return $message;
274 }
275
276 function disable() { $this->mDisable = true; }
277 function enable() { $this->mDisable = false; }
278 function disableTransform() { $this->mDisableTransform = true; }
279 function enableTransform() { $this->mDisableTransform = false; }
280
281 function addMessage( $key, $value ) {
282 $this->mExtensionMessages[$key] = $value;
283 }
284
285 function addMessages( $messages ) {
286 foreach ( $messages as $key => $value ) {
287 $this->mExtensionMessages[$key] = $value;
288 }
289 }
290
291 /**
292 * Clear all stored messages. Mainly used after a mass rebuild.
293 */
294 function clear() {
295 if( $this->mUseCache ) {
296 $this->mMemc->delete( $this->mMemcKey );
297 }
298 }
299 }
300 ?>