hotfix for undefined variable
[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 $mLang, $mLangCode;
27 function initialise( &$memCached, $useDB, $expiry, $memcPrefix, $langobj, $langcode) {
28 $fname = 'MessageCache::initialise';
29 wfProfileIn( $fname );
30 $this->mLang = $langobj;
31 $this->mLangCode = $langcode;
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 if ( isset ( $forContent ) ) $this->mIsForContent = $forContent;
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 $this->load();
49 wfProfileOut( $fname );
50 }
51
52 /**
53 * Loads messages either from memcached or the database, if not disabled
54 * On error, quietly switches to a fallback mode
55 * Returns false for a reportable error, true otherwise
56 */
57 function load() {
58 global $wgAllMessagesEn;
59
60 if ( $this->mDisable ) {
61 wfDebug( "MessageCache::load(): disabled\n" );
62 return true;
63 }
64 $fname = 'MessageCache::load';
65 wfProfileIn( $fname );
66 $success = true;
67
68 if ( $this->mUseCache ) {
69 wfProfileIn( $fname.'-fromcache' );
70 $this->mCache = $this->mMemc->get( $this->mMemcKey );
71 wfProfileOut( $fname.'-fromcache' );
72
73 # If there's nothing in memcached, load all the messages from the database
74 if ( !$this->mCache ) {
75 wfDebug( "MessageCache::load(): loading all messages\n" );
76 $this->lock();
77 # Other threads don't need to load the messages if another thread is doing it.
78 $success = $this->mMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
79 if ( $success ) {
80 wfProfileIn( $fname.'-load' );
81 $this->loadFromDB();
82 wfProfileOut( $fname.'-load' );
83 # Save in memcached
84 # Keep trying if it fails, this is kind of important
85 wfProfileIn( $fname.'-save' );
86 for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
87 usleep(mt_rand(500000,1500000));
88 }
89 wfProfileOut( $fname.'-save' );
90 if ( $i == 20 ) {
91 $this->mMemc->set( $this->mMemcKey, 'error', 86400 );
92 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
93 }
94 }
95 $this->unlock();
96 }
97
98 if ( !is_array( $this->mCache ) ) {
99 wfMsg( "MessageCache::load(): individual message mode\n" );
100 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
101 # Causing too much DB load, disabling -- TS
102 $this->mDisable = true;
103 /*
104 if ( $this->mCache == "loading" ) {
105 $this->mUseCache = false;
106 } elseif ( $this->mCache == "error" ) {
107 $this->mUseCache = false;
108 $success = false;
109 } else {
110 $this->mDisable = true;
111 $success = false;
112 }*/
113 $this->mCache = false;
114 }
115 }
116 wfProfileOut( $fname );
117 return $success;
118 }
119
120 /**
121 * Loads all cacheable messages from the database
122 */
123 function loadFromDB() {
124 $fname = 'MessageCache::loadFromDB';
125 $dbr =& wfGetDB( DB_SLAVE );
126 $res = $dbr->select( 'cur',
127 array( 'cur_title', 'cur_text' ),
128 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI ),
129 $fname
130 );
131
132 $this->mCache = array();
133 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
134 $this->mCache[$row->cur_title] = $row->cur_text;
135 }
136
137 $dbr->freeResult( $res );
138 }
139
140 /**
141 * Not really needed anymore
142 */
143 function getKeys() {
144 global $wgAllMessagesEn;
145 if ( !$this->mKeys ) {
146 $this->mKeys = array();
147 foreach ( $wgAllMessagesEn as $key => $value ) {
148 array_push( $this->mKeys, $this->mLang->ucfirst( $key ) );
149 }
150 }
151 return $this->mKeys;
152 }
153
154 /**
155 * Obsolete
156 */
157 function isCacheable( $key ) {
158 return true;
159 /*
160 global $wgAllMessagesEn, $wgLang;
161 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
162 array_key_exists( $key, $wgAllMessagesEn );
163 */
164 }
165
166 function replace( $title, $text ) {
167 $this->lock();
168 $this->load();
169 if ( is_array( $this->mCache ) ) {
170 $this->mCache[$title] = $text;
171 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
172 }
173 $this->unlock();
174 }
175
176 /**
177 * Returns success
178 * Represents a write lock on the messages key
179 */
180 function lock() {
181 if ( !$this->mUseCache ) {
182 return true;
183 }
184
185 $lockKey = $this->mMemcKey . 'lock';
186 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
187 sleep(1);
188 }
189
190 return $i >= MSG_WAIT_TIMEOUT;
191 }
192
193 function unlock() {
194 if ( !$this->mUseCache ) {
195 return;
196 }
197
198 $lockKey = $this->mMemcKey . 'lock';
199 $this->mMemc->delete( $lockKey );
200 }
201
202 function get( $key, $useDB ) {
203
204 # If uninitialised, someone is trying to call this halfway through Setup.php
205 if ( !$this->mInitialised ) {
206 return "&lt;$key&gt;";
207 }
208
209 $message = false;
210 if ( !$this->mDisable && $useDB ) {
211 $title = $this->mLang->ucfirst( $key );
212
213
214 # Try the cache
215 if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
216 $message = $this->mCache[$title];
217 }
218
219 # If it wasn't in the cache, load each message from the DB individually
220 if ( !$message ) {
221 $dbr =& wfGetDB( DB_SLAVE );
222 $result = $dbr->getArray( 'cur', array('cur_text'),
223 array( 'cur_namespace' => NS_MEDIAWIKI, 'cur_title' => $title ),
224 'MessageCache::get' );
225 if ( $result ) {
226 $message = $result->cur_text;
227 }
228 }
229 }
230 # Try the extension array
231 if ( !$message ) {
232 $message = @$this->mExtensionMessages[$key];
233 }
234
235 # Try the array in the language object
236 if ( !$message ) {
237 wfSuppressWarnings();
238 $message = $this->mLang->getMessage( $key );
239 wfRestoreWarnings();
240 }
241
242 # Try the English array
243 if ( !$message && $this->mLangCode != 'en' ) {
244 wfSuppressWarnings();
245 $message = Language::getMessage( $key );
246 wfRestoreWarnings();
247 }
248
249 # Final fallback
250 if ( !$message ) {
251 $message = "&lt;$key&gt;";
252 }
253
254 # Replace brace tags
255 $message = $this->transform( $message );
256 return $message;
257 }
258
259 function transform( $message ) {
260 if( !$this->mDisableTransform ) {
261 if ( strstr( $message, '{{' ) !== false ) {
262 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
263 }
264 }
265 return $message;
266 }
267
268 function disable() { $this->mDisable = true; }
269 function enable() { $this->mDisable = false; }
270 function disableTransform() { $this->mDisableTransform = true; }
271 function enableTransform() { $this->mDisableTransform = false; }
272
273 function addMessage( $key, $value ) {
274 $this->mExtensionMessages[$key] = $value;
275 }
276
277 function addMessages( $messages ) {
278 foreach ( $messages as $key => $value ) {
279 $this->mExtensionMessages[$key] = $value;
280 }
281 }
282
283 /**
284 * Clear all stored messages. Mainly used after a mass rebuild.
285 */
286 function clear() {
287 if( $this->mUseCache ) {
288 $this->mMemc->delete( $this->mMemcKey );
289 }
290 }
291 }
292 ?>