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