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