Adding a wgContLang object to handle message translation for the content of the site...
[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 $mIsForContent; //whether this is for content messages or ui messages
26 var $mInitialised = false;
27
28 function initialise( &$memCached, $useDB, $expiry, $memcPrefix, $forContent=false ) {
29 $fname = 'MessageCache::initialise';
30 wfProfileIn( $fname );
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 $this->mIsForContent = $forContent;
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;
144 if($this->mIsForContent) {
145 global $wgContLang;
146 $langobj = $wgContLang;
147 }
148 else {
149 global $wgLang;
150 $langobj = $wgLang;
151 }
152
153 if ( !$this->mKeys ) {
154 $this->mKeys = array();
155 foreach ( $wgAllMessagesEn as $key => $value ) {
156 array_push( $this->mKeys, $langobj->ucfirst( $key ) );
157 }
158 }
159 return $this->mKeys;
160 }
161
162 /**
163 * Obsolete
164 */
165 function isCacheable( $key ) {
166 return true;
167 /*
168 global $wgAllMessagesEn, $wgLang;
169 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
170 array_key_exists( $key, $wgAllMessagesEn );
171 */
172 }
173
174 function replace( $title, $text ) {
175 $this->lock();
176 $this->load();
177 if ( is_array( $this->mCache ) ) {
178 $this->mCache[$title] = $text;
179 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
180 }
181 $this->unlock();
182 }
183
184 /**
185 * Returns success
186 * Represents a write lock on the messages key
187 */
188 function lock() {
189 if ( !$this->mUseCache ) {
190 return true;
191 }
192
193 $lockKey = $this->mMemcKey . 'lock';
194 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
195 sleep(1);
196 }
197
198 return $i >= MSG_WAIT_TIMEOUT;
199 }
200
201 function unlock() {
202 if ( !$this->mUseCache ) {
203 return;
204 }
205
206 $lockKey = $this->mMemcKey . 'lock';
207 $this->mMemc->delete( $lockKey );
208 }
209
210 function get( $key, $useDB ) {
211 if($this->mIsForContent) {
212 global $wgContLang, $wgContLanguageCode;
213 $langobj = $wgContLang;
214 $langcode=$wgContLanguageCode;
215 }
216 else {
217 global $wgLang, $wgLanguageCode;
218 $langobj=$wgLang;
219 $langcode = $wgLanguageCode;
220 }
221
222 # If uninitialised, someone is trying to call this halfway through Setup.php
223 if ( !$this->mInitialised ) {
224 return "&lt;$key&gt;";
225 }
226
227 $message = false;
228 if ( !$this->mDisable && $useDB ) {
229 $title = $langobj->ucfirst( $key );
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 it wasn't in the cache, load each message from the DB individually
238 if ( !$message ) {
239 $dbr =& wfGetDB( DB_SLAVE );
240 $result = $dbr->getArray( 'cur', array('cur_text'),
241 array( 'cur_namespace' => NS_MEDIAWIKI, 'cur_title' => $title ),
242 'MessageCache::get' );
243 if ( $result ) {
244 $message = $result->cur_text;
245 }
246 }
247 }
248 # Try the extension array
249 if ( !$message ) {
250 $message = @$this->mExtensionMessages[$key];
251 }
252
253 # Try the array in $wgLang
254 if ( !$message ) {
255 wfSuppressWarnings();
256 $message = $langobj->getMessage( $key );
257 wfRestoreWarnings();
258 }
259
260 # Try the English array
261 if ( !$message && $langcode != 'en' ) {
262 wfSuppressWarnings();
263 $message = Language::getMessage( $key );
264 wfRestoreWarnings();
265 }
266
267 # Final fallback
268 if ( !$message ) {
269 $message = "&lt;$key&gt;";
270 }
271
272 # Replace brace tags
273 $message = $this->transform( $message );
274 return $message;
275 }
276
277 function transform( $message ) {
278 if( !$this->mDisableTransform ) {
279 if ( strstr( $message, '{{' ) !== false ) {
280 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
281 }
282 }
283 return $message;
284 }
285
286 function disable() { $this->mDisable = true; }
287 function enable() { $this->mDisable = false; }
288 function disableTransform() { $this->mDisableTransform = true; }
289 function enableTransform() { $this->mDisableTransform = false; }
290
291 function addMessage( $key, $value ) {
292 $this->mExtensionMessages[$key] = $value;
293 }
294
295 function addMessages( $messages ) {
296 foreach ( $messages as $key => $value ) {
297 $this->mExtensionMessages[$key] = $value;
298 }
299 }
300
301 /**
302 * Clear all stored messages. Mainly used after a mass rebuild.
303 */
304 function clear() {
305 if( $this->mUseCache ) {
306 $this->mMemc->delete( $this->mMemcKey );
307 }
308 }
309 }
310 ?>