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