Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -2342,14 +2342,6 @@ |
2343 | 2343 | return $key; |
2344 | 2344 | } |
2345 | 2345 | |
2346 | | -function wfMemcKeyLang( $key, $code ) { |
2347 | | - if ( !is_string($code) ) { |
2348 | | - return $key; |
2349 | | - } else { |
2350 | | - return $key . ';L:' . $code; |
2351 | | - } |
2352 | | -} |
2353 | | - |
2354 | 2346 | /** |
2355 | 2347 | * Get a cache key for a foreign DB |
2356 | 2348 | */ |
Index: trunk/phase3/includes/MessageCache.php |
— | — | @@ -22,7 +22,8 @@ |
23 | 23 | var $mMemcKey, $mKeys, $mParserOptions, $mParser; |
24 | 24 | var $mExtensionMessages = array(); |
25 | 25 | var $mInitialised = false; |
26 | | - var $mAllMessagesLoaded; // Extension messages |
| 26 | + var $mDeferred = true; |
| 27 | + var $mAllMessagesLoaded; |
27 | 28 | |
28 | 29 | function __construct( &$memCached, $useDB, $expiry, $memcPrefix) { |
29 | 30 | wfProfileIn( __METHOD__ ); |
— | — | @@ -37,14 +38,15 @@ |
38 | 39 | $this->mInitialised = true; |
39 | 40 | $this->mParser = null; |
40 | 41 | |
| 42 | + # When we first get asked for a message, |
| 43 | + # then we'll fill up the cache. If we |
| 44 | + # can return a cache hit, this saves |
| 45 | + # some extra milliseconds |
| 46 | + $this->mDeferred = true; |
| 47 | + |
41 | 48 | wfProfileOut( __METHOD__ ); |
42 | 49 | } |
43 | 50 | |
44 | | - |
45 | | - /** |
46 | | - * ParserOptions is lazy initialised. |
47 | | - * Access should probably be protected. |
48 | | - */ |
49 | 51 | function getParserOptions() { |
50 | 52 | if ( !$this->mParserOptions ) { |
51 | 53 | $this->mParserOptions = new ParserOptions; |
— | — | @@ -53,26 +55,22 @@ |
54 | 56 | } |
55 | 57 | |
56 | 58 | /** |
57 | | - * Try to load the cache from a local file. |
58 | | - * Actual format of the file depends on the $wgLocalMessageCacheSerialized |
59 | | - * setting. |
60 | | - * |
61 | | - * @param $hash String: the hash of contents, to check validity. |
62 | | - * @param $code Mixed: Optional language code, see documenation of load(). |
63 | | - * @return false on failure. |
| 59 | + * Try to load the cache from a local file |
64 | 60 | */ |
65 | | - function loadFromLocal( $hash, $code ) { |
| 61 | + function loadFromLocal( $hash ) { |
66 | 62 | global $wgLocalMessageCache, $wgLocalMessageCacheSerialized; |
67 | 63 | |
| 64 | + if ( $wgLocalMessageCache === false ) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
68 | 68 | $filename = "$wgLocalMessageCache/messages-" . wfWikiID(); |
69 | | - if ( $code ) $filename .= '-' . $code; |
70 | 69 | |
71 | | - # Check file existence |
72 | 70 | wfSuppressWarnings(); |
73 | 71 | $file = fopen( $filename, 'r' ); |
74 | 72 | wfRestoreWarnings(); |
75 | 73 | if ( !$file ) { |
76 | | - return false; // No cache file |
| 74 | + return; |
77 | 75 | } |
78 | 76 | |
79 | 77 | if ( $wgLocalMessageCacheSerialized ) { |
— | — | @@ -84,36 +82,36 @@ |
85 | 83 | while ( !feof( $file ) ) { |
86 | 84 | $serialized .= fread( $file, 100000 ); |
87 | 85 | } |
88 | | - fclose( $file ); |
89 | | - return $this->setCache( unserialize( $serialized ) ); |
90 | | - } else { |
91 | | - fclose( $file ); |
92 | | - return false; // Wrong hash |
| 86 | + $this->setCache( unserialize( $serialized ) ); |
93 | 87 | } |
| 88 | + fclose( $file ); |
94 | 89 | } else { |
95 | 90 | $localHash=substr(fread($file,40),8); |
96 | 91 | fclose($file); |
97 | 92 | if ($hash!=$localHash) { |
98 | | - return false; // Wrong hash |
| 93 | + return; |
99 | 94 | } |
100 | 95 | |
101 | | - require( $filename ); |
102 | | - return $this->setCache( $this->mCache ); |
| 96 | + require("$wgLocalMessageCache/messages-" . wfWikiID()); |
| 97 | + $this->setCache( $this->mCache); |
103 | 98 | } |
104 | 99 | } |
105 | 100 | |
106 | 101 | /** |
107 | 102 | * Save the cache to a local file |
108 | 103 | */ |
109 | | - function saveToLocal( $serialized, $hash, $code ) { |
110 | | - global $wgLocalMessageCache; |
| 104 | + function saveToLocal( $serialized, $hash ) { |
| 105 | + global $wgLocalMessageCache, $wgLocalMessageCacheSerialized; |
111 | 106 | |
| 107 | + if ( $wgLocalMessageCache === false ) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
112 | 111 | $filename = "$wgLocalMessageCache/messages-" . wfWikiID(); |
113 | | - if ( $code ) $filename .= '-' . $code; |
| 112 | + $oldUmask = umask( 0 ); |
| 113 | + wfMkdirParents( $wgLocalMessageCache, 0777 ); |
| 114 | + umask( $oldUmask ); |
114 | 115 | |
115 | | - # Make sure the directories exist |
116 | | - $this->createCacheDir( $wgLocalMessageCache ); |
117 | | - |
118 | 116 | $file = fopen( $filename, 'w' ); |
119 | 117 | if ( !$file ) { |
120 | 118 | wfDebug( "Unable to open local cache file for writing\n" ); |
— | — | @@ -125,41 +123,34 @@ |
126 | 124 | @chmod( $filename, 0666 ); |
127 | 125 | } |
128 | 126 | |
129 | | - function saveToScript( $array, $hash, $code ) { |
| 127 | + function loadFromScript( $hash ) { |
| 128 | + wfDeprecated( __METHOD__ ); |
| 129 | + $this->loadFromLocal( $hash ); |
| 130 | + } |
| 131 | + |
| 132 | + function saveToScript($array, $hash) { |
130 | 133 | global $wgLocalMessageCache; |
| 134 | + if ( $wgLocalMessageCache === false ) { |
| 135 | + return; |
| 136 | + } |
131 | 137 | |
132 | 138 | $filename = "$wgLocalMessageCache/messages-" . wfWikiID(); |
133 | | - if ( $code ) $filename .= '-' . $code; |
134 | | - $tempFilename = $filename . '.tmp'; |
135 | | - |
136 | | - # Make sure the directories exist |
137 | | - $this->createCacheDir( $wgLocalMessageCache ); |
138 | | - |
139 | | - $file = fopen( $tempFilename, 'w'); |
| 139 | + $oldUmask = umask( 0 ); |
| 140 | + wfMkdirParents( $wgLocalMessageCache, 0777 ); |
| 141 | + umask( $oldUmask ); |
| 142 | + $file = fopen( $filename.'.tmp', 'w'); |
140 | 143 | fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array("); |
141 | 144 | |
142 | 145 | foreach ($array as $key => $message) { |
143 | | - $key = $this->escapeForScript($key); |
144 | | - $messages = $this->escapeForScript($message); |
145 | | - fwrite($file, "'$key' => '$message',\n"); |
| 146 | + fwrite($file, "'". $this->escapeForScript($key). |
| 147 | + "' => '" . $this->escapeForScript($message). |
| 148 | + "',\n"); |
146 | 149 | } |
147 | | - |
148 | 150 | fwrite($file,");\n?>"); |
149 | 151 | fclose($file); |
150 | | - rename($tempFilename, $filename); |
| 152 | + rename($filename.'.tmp',$filename); |
151 | 153 | } |
152 | 154 | |
153 | | - /** |
154 | | - * Creates directory with correct permissions. |
155 | | - * |
156 | | - * @param $path String: path that is created if it does not exist. |
157 | | - */ |
158 | | - protected function createCacheDir( $path ) { |
159 | | - $oldUmask = umask( 0 ); |
160 | | - wfMkdirParents( $path, 0777 ); |
161 | | - umask( $oldUmask ); |
162 | | - } |
163 | | - |
164 | 155 | function escapeForScript($string) { |
165 | 156 | $string = str_replace( '\\', '\\\\', $string ); |
166 | 157 | $string = str_replace( '\'', '\\\'', $string ); |
— | — | @@ -171,321 +162,220 @@ |
172 | 163 | */ |
173 | 164 | function setCache( $cache ) { |
174 | 165 | if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) { |
175 | | - if ( $this->mCache ) { |
176 | | - $this->mCache = $cache + $this->mCache; |
177 | | - } else { |
178 | | - $this->mCache = $cache; |
179 | | - } |
180 | | - return true; |
| 166 | + $this->mCache = $cache; |
181 | 167 | } else { |
182 | | - return false; |
| 168 | + $this->mCache = false; |
183 | 169 | } |
184 | 170 | } |
185 | 171 | |
186 | 172 | /** |
187 | | - * Loads messages from caches or from database in this order: |
188 | | - * (1) local message cache (if $wgLocalMessageCache is enabled) |
189 | | - * (2) memcached |
190 | | - * (3) from the database. |
191 | | - * |
192 | | - * When succesfully loading from (2) or (3), all higher level caches are |
193 | | - * updated for the newest version. |
194 | | - * |
195 | | - * Nothing is loaded if member variable mDisabled is true, either manually |
196 | | - * set by calling code or if message loading fails (is this possible?). |
197 | | - * |
198 | | - * Returns true if cache is already populated or it was succesfully populated, |
199 | | - * or false if populating empty cache fails. Also returns true if MessageCache |
200 | | - * is disabled. |
201 | | - * |
202 | | - * @param $code Mixed: if evaluates to false, messages for all languages is |
203 | | - * loaded and stored in cache as one object. If code is provided |
204 | | - * and $wgPerLanguageCaching is true, messages are loaded per |
205 | | - * language and stored individually in caches 1 to 2. |
| 173 | + * Loads messages either from memcached or the database, if not disabled |
| 174 | + * On error, quietly switches to a fallback mode |
| 175 | + * Returns false for a reportable error, true otherwise |
206 | 176 | */ |
207 | | - function load( $code = false ) { |
208 | | - global $wgLocalMessageCache, $wgPerLanguageCaching; |
| 177 | + function load() { |
| 178 | + global $wgLocalMessageCache, $wgLocalMessageCacheSerialized; |
209 | 179 | |
210 | | - if ( !$this->mUseCache ) { |
211 | | - return true; |
212 | | - } |
213 | | - |
214 | | - # Keep track of loaded messages. Either array of loaded codes as keys, |
215 | | - # or just true if all languages are loaded. |
216 | | - static $loaded = false; |
217 | | - |
218 | | - if ( !$wgPerLanguageCaching ) { |
219 | | - // Disable language separation for normal use case |
220 | | - $code = false; |
221 | | - } elseif( !is_string( $code ) ) { |
222 | | - # This isn't really nice, so at least make a note about it |
223 | | - wfDebug( __METHOD__ . " called without providing a language code\n" ); |
224 | | - } |
225 | | - |
226 | | - # Don't do double loading... |
227 | | - if ( $loaded === true || ($code && isset($loaded[$code])) ) return true; |
228 | | - |
229 | | - # Adjust cache key if we are handling only single language |
230 | | - $cacheKey = wfMemcKeyLang( $this->mMemcKey, $code ); |
231 | | - |
232 | | - # 8 lines of code just to say (once) that message cache is disabled |
233 | 180 | if ( $this->mDisable ) { |
234 | 181 | static $shownDisabled = false; |
235 | 182 | if ( !$shownDisabled ) { |
236 | | - wfDebug( __METHOD__ . ": disabled\n" ); |
| 183 | + wfDebug( "MessageCache::load(): disabled\n" ); |
237 | 184 | $shownDisabled = true; |
238 | 185 | } |
239 | 186 | return true; |
240 | 187 | } |
| 188 | + if ( !$this->mUseCache ) { |
| 189 | + $this->mDeferred = false; |
| 190 | + return true; |
| 191 | + } |
241 | 192 | |
242 | | - # Loading code starts |
243 | | - wfProfileIn( __METHOD__ ); |
244 | | - $success = false; # Keep track of success |
245 | | - $where = array(); # Debug info, delayed to avoid spamming debug log too much |
| 193 | + $fname = 'MessageCache::load'; |
| 194 | + wfProfileIn( $fname ); |
| 195 | + $success = true; |
246 | 196 | |
247 | | - # (1) local cache |
248 | | - # Hash of the contents is stored in memcache, to detect if local cache goes |
249 | | - # out of date (due to update in other thread?) |
| 197 | + $this->mCache = false; |
| 198 | + |
| 199 | + # Try local cache |
250 | 200 | if ( $wgLocalMessageCache !== false ) { |
251 | | - wfProfileIn( __METHOD__ . '-fromlocal' ); |
252 | | - $hash = $this->mMemc->get( "$cacheKey-hash" ); |
| 201 | + wfProfileIn( $fname.'-fromlocal' ); |
| 202 | + $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" ); |
253 | 203 | if ( $hash ) { |
254 | | - $success = $this->loadFromLocal( $hash, $code ); |
255 | | - if ( $success ) $where[] = 'got from local cache'; |
| 204 | + $this->loadFromLocal( $hash ); |
| 205 | + if ( $this->mCache ) { |
| 206 | + wfDebug( "MessageCache::load(): got from local cache\n" ); |
| 207 | + } |
256 | 208 | } |
257 | | - wfProfileOut( __METHOD__ . '-fromlocal' ); |
| 209 | + wfProfileOut( $fname.'-fromlocal' ); |
258 | 210 | } |
259 | 211 | |
260 | | - # (2) memcache |
261 | | - # Fails if nothing in cache, or in the wrong version. |
262 | | - if ( !$success ) { |
263 | | - wfProfileIn( __METHOD__ . '-fromcache' ); |
264 | | - $cache = $this->mMemc->get( $cacheKey ); |
265 | | - $success = $this->setCache( $cache ); |
266 | | - if ( $success ) { |
267 | | - $where[] = 'got from global cache'; |
268 | | - $this->saveToCaches( $cache, $cacheKey, false, $code ); |
| 212 | + # Try memcached |
| 213 | + if ( !$this->mCache ) { |
| 214 | + wfProfileIn( $fname.'-fromcache' ); |
| 215 | + $this->setCache( $this->mMemc->get( $this->mMemcKey ) ); |
| 216 | + if ( $this->mCache ) { |
| 217 | + wfDebug( "MessageCache::load(): got from global cache\n" ); |
| 218 | + # Save to local cache |
| 219 | + if ( $wgLocalMessageCache !== false ) { |
| 220 | + $serialized = serialize( $this->mCache ); |
| 221 | + if ( !$hash ) { |
| 222 | + $hash = md5( $serialized ); |
| 223 | + $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); |
| 224 | + } |
| 225 | + if ($wgLocalMessageCacheSerialized) { |
| 226 | + $this->saveToLocal( $serialized,$hash ); |
| 227 | + } else { |
| 228 | + $this->saveToScript( $this->mCache, $hash ); |
| 229 | + } |
| 230 | + } |
269 | 231 | } |
270 | | - wfProfileOut( __METHOD__ . '-fromcache' ); |
| 232 | + wfProfileOut( $fname.'-fromcache' ); |
271 | 233 | } |
272 | 234 | |
273 | 235 | |
274 | | - # (3) |
275 | | - # Nothing in caches... so we need create one and store it in caches |
276 | | - if ( !$success ) { |
277 | | - $where[] = 'cache is empty'; |
278 | | - $this->lock($cacheKey); |
279 | | - wfProfileIn( __METHOD__ . '-load' ); |
280 | | - $where[] = 'loading from database'; |
281 | | - # We want to store messages of particular language here, not everything |
282 | | - # from mCache. |
283 | | - $cache = $this->loadFromDB( $code ); |
284 | | - $success = $this->setCache( $cache ); |
285 | | - wfProfileOut( __METHOD__ . '-load' ); |
| 236 | + # If there's nothing in memcached, load all the messages from the database |
| 237 | + if ( !$this->mCache ) { |
| 238 | + wfDebug( "MessageCache::load(): cache is empty\n" ); |
| 239 | + $this->lock(); |
| 240 | + # Other threads don't need to load the messages if another thread is doing it. |
| 241 | + $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT ); |
286 | 242 | if ( $success ) { |
287 | | - $this->saveToCaches( $cache, $cacheKey, true, $code ); |
| 243 | + wfProfileIn( $fname.'-load' ); |
| 244 | + wfDebug( "MessageCache::load(): loading all messages from DB\n" ); |
| 245 | + $this->loadFromDB(); |
| 246 | + wfProfileOut( $fname.'-load' ); |
| 247 | + |
| 248 | + # Save in memcached |
| 249 | + # Keep trying if it fails, this is kind of important |
| 250 | + wfProfileIn( $fname.'-save' ); |
| 251 | + for ($i=0; $i<20 && |
| 252 | + !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); |
| 253 | + $i++ ) { |
| 254 | + usleep(mt_rand(500000,1500000)); |
| 255 | + } |
| 256 | + |
| 257 | + # Save to local cache |
| 258 | + if ( $wgLocalMessageCache !== false ) { |
| 259 | + $serialized = serialize( $this->mCache ); |
| 260 | + $hash = md5( $serialized ); |
| 261 | + $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); |
| 262 | + if ($wgLocalMessageCacheSerialized) { |
| 263 | + $this->saveToLocal( $serialized,$hash ); |
| 264 | + } else { |
| 265 | + $this->saveToScript( $this->mCache, $hash ); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + wfProfileOut( $fname.'-save' ); |
| 270 | + if ( $i == 20 ) { |
| 271 | + $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 ); |
| 272 | + wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" ); |
| 273 | + } else { |
| 274 | + $this->mMemc->delete( $this->mMemcKey.'-status' ); |
| 275 | + } |
288 | 276 | } |
289 | | - $this->unlock($cacheKey); |
| 277 | + $this->unlock(); |
290 | 278 | } |
291 | 279 | |
292 | | - if ( !$success ) { |
293 | | - # Bad luck... this should not happen |
294 | | - $where[] = 'loading FAILED - cache is disabled'; |
295 | | - $info = implode( ', ', $where ); |
296 | | - wfDebug( __METHOD__ . ": Loading $code... $info\n" ); |
| 280 | + if ( !is_array( $this->mCache ) ) { |
| 281 | + wfDebug( "MessageCache::load(): unable to load cache, disabled\n" ); |
297 | 282 | $this->mDisable = true; |
298 | 283 | $this->mCache = false; |
299 | | - } else { |
300 | | - # All good, just record the success |
301 | | - $info = implode( ', ', $where ); |
302 | | - wfDebug( __METHOD__ . ": Loading $code... $info\n" ); |
303 | | - if ( $code ) { |
304 | | - $loaded[$code] = true; |
305 | | - } else { |
306 | | - $loaded = true; |
307 | | - } |
308 | 284 | } |
309 | | - wfProfileOut( __METHOD__ ); |
| 285 | + wfProfileOut( $fname ); |
| 286 | + $this->mDeferred = false; |
310 | 287 | return $success; |
311 | 288 | } |
312 | 289 | |
313 | 290 | /** |
314 | | - * Loads cacheable messages from the database. Messages bigger than |
315 | | - * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded |
316 | | - * on-demand from the database later. |
317 | | - * |
318 | | - * @param $code Optional language code, see documenation of load(). |
319 | | - * @return Array: Loaded messages for storing in caches. |
| 291 | + * Loads all or main part of cacheable messages from the database |
320 | 292 | */ |
321 | | - function loadFromDB( $code = false ) { |
| 293 | + function loadFromDB() { |
| 294 | + global $wgMaxMsgCacheEntrySize; |
| 295 | + |
322 | 296 | wfProfileIn( __METHOD__ ); |
323 | | - global $wgMaxMsgCacheEntrySize, $wgContLanguageCode; |
324 | 297 | $dbr = wfGetDB( DB_SLAVE ); |
325 | | - $cache = array(); |
| 298 | + $this->mCache = array(); |
326 | 299 | |
327 | | - # Common conditions |
328 | | - $conds = array( |
329 | | - 'page_is_redirect' => 0, |
330 | | - 'page_namespace' => NS_MEDIAWIKI, |
331 | | - ); |
332 | | - |
333 | | - if ( $code ) { |
334 | | - # Should $code be escaped? |
335 | | - # Is this fast enough. Should not matter if the filtering is done in the |
336 | | - # database or in code. |
337 | | - if ( $code !== $wgContLanguageCode ) { |
338 | | - # Messages for particular language |
339 | | - $conds[] = "page_title like '%%/$code'"; |
340 | | - } else { |
341 | | - # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses |
342 | | - # other than language code. |
343 | | - $conds[] = "page_title not like '%%/%%'"; |
344 | | - } |
345 | | - } |
346 | | - |
347 | | - # Conditions to fetch oversized pages to ignore them |
348 | | - $bigConds = $conds; |
349 | | - $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ); |
350 | | - |
351 | 300 | # Load titles for all oversized pages in the MediaWiki namespace |
352 | | - $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ ); |
| 301 | + $res = $dbr->select( 'page', 'page_title', |
| 302 | + array( |
| 303 | + 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ), |
| 304 | + 'page_is_redirect' => 0, |
| 305 | + 'page_namespace' => NS_MEDIAWIKI, |
| 306 | + ), |
| 307 | + __METHOD__ ); |
353 | 308 | while ( $row = $dbr->fetchObject( $res ) ) { |
354 | | - $cache[$row->page_title] = '!TOO BIG'; |
| 309 | + $this->mCache[$row->page_title] = '!TOO BIG'; |
355 | 310 | } |
356 | 311 | $dbr->freeResult( $res ); |
357 | 312 | |
358 | | - # Conditions to load the remaining pages with their contents |
359 | | - $smallConds = $conds; |
360 | | - $smallConds[] = 'page_latest=rev_id'; |
361 | | - $smallConds[] = 'rev_text_id=old_id'; |
362 | | - $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ); |
363 | | - |
| 313 | + # Load text for the remaining pages |
364 | 314 | $res = $dbr->select( array( 'page', 'revision', 'text' ), |
365 | 315 | array( 'page_title', 'old_text', 'old_flags' ), |
366 | | - $smallConds, __METHOD__ ); |
| 316 | + array( |
| 317 | + 'page_is_redirect' => 0, |
| 318 | + 'page_namespace' => NS_MEDIAWIKI, |
| 319 | + 'page_latest=rev_id', |
| 320 | + 'rev_text_id=old_id', |
| 321 | + 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ), |
| 322 | + __METHOD__ ); |
367 | 323 | |
368 | 324 | for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) { |
369 | | - $cache[$row->page_title] = ' ' . Revision::getRevisionText( $row ); |
| 325 | + $this->mCache[$row->page_title] = ' ' . Revision::getRevisionText( $row ); |
370 | 326 | } |
| 327 | + $this->mCache['VERSION'] = MSG_CACHE_VERSION; |
371 | 328 | $dbr->freeResult( $res ); |
372 | | - |
373 | | - $cache['VERSION'] = MSG_CACHE_VERSION; |
374 | 329 | wfProfileOut( __METHOD__ ); |
375 | | - return $cache; |
376 | 330 | } |
377 | 331 | |
378 | | - /** |
379 | | - * Updates cache as necessary when message page is changed |
380 | | - * |
381 | | - * @param $title String: name of the page changed. |
382 | | - * @param $text Mixed: new contents of the page. |
383 | | - */ |
384 | | - public function replace( $title, $text ) { |
385 | | - global $wgMaxMsgCacheEntrySize, $wgPerLanguageCaching; |
| 332 | + function replace( $title, $text ) { |
| 333 | + global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc; |
| 334 | + global $wgMaxMsgCacheEntrySize; |
386 | 335 | |
387 | | - list( $message, $code ) = $this->figureMessage( $title ); |
388 | | - |
389 | | - # TODO: Should we define the sidebar key name somewhere? |
390 | | - $sidebarKey = wfMemcKeyLang( 'sidebar' , $code ); |
391 | | - |
392 | | - # Load only messages for affected language, if possible |
393 | | - if ( !$wgPerLanguageCaching ) $code = false; |
394 | | - $cacheKey = wfMemcKeyLang( $this->mMemcKey, $code ); |
395 | | - |
396 | 336 | wfProfileIn( __METHOD__ ); |
397 | | - $this->lock($cacheKey); |
398 | | - |
399 | | - $cache = $this->mMemc->get( $cacheKey ); |
400 | | - if ( is_array( $cache ) ) { |
| 337 | + $this->lock(); |
| 338 | + $this->load(); |
| 339 | + if ( is_array( $this->mCache ) ) { |
401 | 340 | if ( $text === false ) { |
402 | 341 | # Article was deleted |
403 | 342 | unset( $this->mCache[$title] ); |
404 | 343 | $this->mMemc->delete( "$this->mMemcKey:{$title}" ); |
405 | 344 | } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) { |
406 | | - $cache[$title] = $this->mCache[$title] = '!TOO BIG'; |
| 345 | + $this->mCache[$title] = '!TOO BIG'; |
407 | 346 | $this->mMemc->set( "$this->mMemcKey:{$title}", ' '.$text, $this->mExpiry ); |
408 | 347 | } else { |
409 | | - $cache[$title] = $this->mCache[$title] = ' ' . $text; |
| 348 | + $this->mCache[$title] = ' ' . $text; |
410 | 349 | $this->mMemc->delete( "$this->mMemcKey:{$title}" ); |
411 | 350 | } |
| 351 | + $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); |
412 | 352 | |
413 | | - # Update per-request cache |
414 | | - $success = $this->setCache( $cache, $cacheKey ); |
415 | | - if ( $success ) { |
416 | | - # And then all caches |
417 | | - $this->saveToCaches( $cache, $cacheKey, true, $code ); |
| 353 | + # Save to local cache |
| 354 | + if ( $wgLocalMessageCache !== false ) { |
| 355 | + $serialized = serialize( $this->mCache ); |
| 356 | + $hash = md5( $serialized ); |
| 357 | + $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry ); |
| 358 | + if ($wgLocalMessageCacheSerialized) { |
| 359 | + $this->saveToLocal( $serialized,$hash ); |
| 360 | + } else { |
| 361 | + $this->saveToScript( $this->mCache, $hash ); |
| 362 | + } |
418 | 363 | } |
419 | 364 | } |
420 | | - $this->unlock($cacheKey); |
421 | | - |
422 | | - // Also delete cached sidebar... just in case it is affected |
423 | | - global $parserMemc; |
424 | | - $parserMemc->delete( $sidebarKey ); |
| 365 | + $this->unlock(); |
| 366 | + $parserMemc->delete(wfMemcKey('sidebar')); |
425 | 367 | wfProfileOut( __METHOD__ ); |
426 | 368 | } |
427 | 369 | |
428 | 370 | /** |
429 | | - * Shortcut to update caches. |
430 | | - * |
431 | | - * @param $cache Array: cached messages with a version. |
432 | | - * @param $cacheKey String: Identifier for the cache. |
433 | | - * @param $memc Bool: Wether to update or not memcache. |
434 | | - * @param $code String: Language code. |
435 | | - * @return False on somekind of error. |
436 | | - */ |
437 | | - protected function saveToCaches( $cache, $cacheKey, $memc = true, $code = false ) { |
438 | | - wfProfileIn( __METHOD__ ); |
439 | | - global $wgLocalMessageCache, $wgLocalMessageCacheSerialized; |
440 | | - |
441 | | - $success = $this->mMemc->add( $cacheKey.'-status', "loading", MSG_LOAD_TIMEOUT ); |
442 | | - if ( !$success ) return true; # Other process should be updating them now |
443 | | - |
444 | | - $i = 0; |
445 | | - if ( $memc ) { |
446 | | - # Save in memcached |
447 | | - # Keep trying if it fails, this is kind of important |
448 | | - |
449 | | - for ($i=0; $i<20 && |
450 | | - !$this->mMemc->set( $cacheKey, $cache, $this->mExpiry ); |
451 | | - $i++ ) { |
452 | | - usleep(mt_rand(500000,1500000)); |
453 | | - } |
454 | | - } |
455 | | - |
456 | | - # Save to local cache |
457 | | - if ( $wgLocalMessageCache !== false ) { |
458 | | - $serialized = serialize( $cache ); |
459 | | - $hash = md5( $serialized ); |
460 | | - $this->mMemc->set( "$cacheKey-hash", $hash, $this->mExpiry ); |
461 | | - if ($wgLocalMessageCacheSerialized) { |
462 | | - $this->saveToLocal( $serialized, $hash, $code ); |
463 | | - } else { |
464 | | - $this->saveToScript( $cache, $hash, $code ); |
465 | | - } |
466 | | - } |
467 | | - |
468 | | - if ( $i == 20 ) { |
469 | | - $this->mMemc->set( $cacheKey.'-status', 'error', 60*5 ); |
470 | | - wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" ); |
471 | | - $success = false; |
472 | | - } else { |
473 | | - $this->mMemc->delete( $cacheKey.'-status' ); |
474 | | - $success = true; |
475 | | - } |
476 | | - wfProfileOut( __METHOD__ ); |
477 | | - return $success; |
478 | | - } |
479 | | - |
480 | | - /** |
481 | 371 | * Returns success |
482 | 372 | * Represents a write lock on the messages key |
483 | 373 | */ |
484 | | - function lock($key) { |
| 374 | + function lock() { |
485 | 375 | if ( !$this->mUseCache ) { |
486 | 376 | return true; |
487 | 377 | } |
488 | 378 | |
489 | | - $lockKey = $key . 'lock'; |
| 379 | + $lockKey = $this->mMemcKey . 'lock'; |
490 | 380 | for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) { |
491 | 381 | sleep(1); |
492 | 382 | } |
— | — | @@ -493,12 +383,12 @@ |
494 | 384 | return $i >= MSG_WAIT_TIMEOUT; |
495 | 385 | } |
496 | 386 | |
497 | | - function unlock($key) { |
| 387 | + function unlock() { |
498 | 388 | if ( !$this->mUseCache ) { |
499 | 389 | return; |
500 | 390 | } |
501 | 391 | |
502 | | - $lockKey = $key . 'lock'; |
| 392 | + $lockKey = $this->mMemcKey . 'lock'; |
503 | 393 | $this->mMemc->delete( $lockKey ); |
504 | 394 | } |
505 | 395 | |
— | — | @@ -548,6 +438,10 @@ |
549 | 439 | if( !$this->mInitialised ) { |
550 | 440 | return '<' . htmlspecialchars($key) . '>'; |
551 | 441 | } |
| 442 | + # If cache initialization was deferred, start it now. |
| 443 | + if( $this->mDeferred && !$this->mDisable && $useDB ) { |
| 444 | + $this->load(); |
| 445 | + } |
552 | 446 | |
553 | 447 | $message = false; |
554 | 448 | |
— | — | @@ -561,11 +455,10 @@ |
562 | 456 | if(!$isFullKey && ($langcode != $wgContLanguageCode) ) { |
563 | 457 | $title .= '/' . $langcode; |
564 | 458 | } |
565 | | - $message = $this->getMsgFromNamespace( $title, $langcode ); |
| 459 | + $message = $this->getMsgFromNamespace( $title ); |
566 | 460 | } |
567 | | - |
568 | 461 | # Try the extension array |
569 | | - if ( $message === false && isset( $this->mExtensionMessages[$langcode][$lckey] ) ) { |
| 462 | + if( $message === false && isset( $this->mExtensionMessages[$langcode][$lckey] ) ) { |
570 | 463 | $message = $this->mExtensionMessages[$langcode][$lckey]; |
571 | 464 | } |
572 | 465 | if ( $message === false && isset( $this->mExtensionMessages['en'][$lckey] ) ) { |
— | — | @@ -573,8 +466,11 @@ |
574 | 467 | } |
575 | 468 | |
576 | 469 | # Try the array in the language object |
577 | | - if ( $message === false ) { |
| 470 | + if( $message === false ) { |
| 471 | + #wfDebug( "Trying language object for message $key\n" ); |
| 472 | + wfSuppressWarnings(); |
578 | 473 | $message = $lang->getMessage( $lckey ); |
| 474 | + wfRestoreWarnings(); |
579 | 475 | if ( is_null( $message ) ) { |
580 | 476 | $message = false; |
581 | 477 | } |
— | — | @@ -602,7 +498,7 @@ |
603 | 499 | if( ($message === false || $message === '-' ) && |
604 | 500 | !$this->mDisable && $useDB && |
605 | 501 | !$isFullKey && ($langcode != $wgContLanguageCode) ) { |
606 | | - $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $lckey ), $wgContLanguageCode ); |
| 502 | + $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $lckey ) ); |
607 | 503 | } |
608 | 504 | |
609 | 505 | # Final fallback |
— | — | @@ -616,26 +512,19 @@ |
617 | 513 | * Get a message from the MediaWiki namespace, with caching. The key must |
618 | 514 | * first be converted to two-part lang/msg form if necessary. |
619 | 515 | * |
620 | | - * @param $title String: Message cache key with initial uppercase letter. |
621 | | - * @param $code String: code denoting the language to try. |
| 516 | + * @param string $title Message cache key with initial uppercase letter |
622 | 517 | */ |
623 | | - function getMsgFromNamespace( $title, $code ) { |
| 518 | + function getMsgFromNamespace( $title ) { |
624 | 519 | $message = false; |
625 | 520 | $type = false; |
626 | 521 | |
627 | | - if ( $this->mUseCache ) { |
628 | | - if ( !isset($this->mCache[$title]) ) { |
629 | | - # Delay loading as far as possible, and only load messages for requested |
630 | | - # language. |
631 | | - $this->load( $code ); |
| 522 | + # Try the cache |
| 523 | + if( $this->mUseCache && isset( $this->mCache[$title] ) ) { |
| 524 | + $entry = $this->mCache[$title]; |
| 525 | + $type = substr( $entry, 0, 1 ); |
| 526 | + if ( $type == ' ' ) { |
| 527 | + return substr( $entry, 1 ); |
632 | 528 | } |
633 | | - if (isset( $this->mCache[$title] ) ) { |
634 | | - $entry = $this->mCache[$title]; |
635 | | - $type = substr( $entry, 0, 1 ); |
636 | | - if ( $type == ' ' ) { |
637 | | - return substr( $entry, 1 ); |
638 | | - } |
639 | | - } |
640 | 529 | } |
641 | 530 | |
642 | 531 | # Call message hooks, in case they are defined |
— | — | @@ -658,7 +547,6 @@ |
659 | 548 | $type = substr( $entry, 0, 1 ); |
660 | 549 | |
661 | 550 | if ( $type == ' ' ) { |
662 | | - # Ok! |
663 | 551 | $message = substr( $entry, 1 ); |
664 | 552 | $this->mCache[$title] = $entry; |
665 | 553 | return $message; |
— | — | @@ -686,6 +574,7 @@ |
687 | 575 | $this->mMemc->set( $memcKey, '!NONEXISTENT', $this->mExpiry ); |
688 | 576 | $this->mCache[$title] = false; |
689 | 577 | } |
| 578 | + |
690 | 579 | return $message; |
691 | 580 | } |
692 | 581 | |
— | — | @@ -709,7 +598,7 @@ |
710 | 599 | |
711 | 600 | function disable() { $this->mDisable = true; } |
712 | 601 | function enable() { $this->mDisable = false; } |
713 | | - |
| 602 | + |
714 | 603 | /** @deprecated */ |
715 | 604 | function disableTransform(){ |
716 | 605 | wfDeprecated( __METHOD__ ); |
— | — | @@ -872,15 +761,4 @@ |
873 | 762 | $this->addMessages( $mergedMessages, $langcode ); |
874 | 763 | } |
875 | 764 | |
876 | | - public function figureMessage( $key ) { |
877 | | - global $wgContLanguageCode; |
878 | | - $pieces = explode('/', $key, 2); |
879 | | - |
880 | | - $key = $pieces[0]; |
881 | | - |
882 | | - # Language the user is translating to |
883 | | - $langCode = isset($pieces[1]) ? $pieces[1] : $wgContLanguageCode; |
884 | | - return array( $key, $langCode ); |
885 | | - } |
886 | | - |
887 | 765 | } |
Index: trunk/phase3/includes/DefaultSettings.php |
— | — | @@ -721,13 +721,6 @@ |
722 | 722 | $wgLocalMessageCacheSerialized = true; |
723 | 723 | |
724 | 724 | /** |
725 | | - * Cache messages in MesssageCache per language, instead of all them together. |
726 | | - * Enable this if you have thousands of messages in MediaWiki namespace in many |
727 | | - * different languages. |
728 | | - */ |
729 | | -$wgPerLanguageCaching = false; |
730 | | - |
731 | | -/** |
732 | 725 | * Directory for compiled constant message array databases |
733 | 726 | * WARNING: turning anything on will just break things, aaaaaah!!!! |
734 | 727 | */ |
Index: trunk/phase3/includes/Skin.php |
— | — | @@ -1650,18 +1650,24 @@ |
1651 | 1651 | * Build an array that represents the sidebar(s), the navigation bar among them |
1652 | 1652 | * |
1653 | 1653 | * @return array |
| 1654 | + * @private |
1654 | 1655 | */ |
1655 | 1656 | function buildSidebar() { |
1656 | 1657 | global $parserMemc, $wgEnableSidebarCache, $wgSidebarCacheExpiry; |
1657 | | - global $wgLang; |
1658 | | - wfProfileIn( __METHOD__ ); |
| 1658 | + global $wgLang, $wgContLang; |
1659 | 1659 | |
1660 | | - $key = wfMemcKeyLang( wfMemcKey( 'sidebar' ), $wgLang->getCode() ); |
| 1660 | + $fname = 'SkinTemplate::buildSidebar'; |
1661 | 1661 | |
1662 | | - if ( $wgEnableSidebarCache ) { |
| 1662 | + wfProfileIn( $fname ); |
| 1663 | + |
| 1664 | + $key = wfMemcKey( 'sidebar' ); |
| 1665 | + $cacheSidebar = $wgEnableSidebarCache && |
| 1666 | + ($wgLang->getCode() == $wgContLang->getCode()); |
| 1667 | + |
| 1668 | + if ($cacheSidebar) { |
1663 | 1669 | $cachedsidebar = $parserMemc->get( $key ); |
1664 | | - if ( $cachedsidebar ) { |
1665 | | - wfProfileOut( __METHOD__ ); |
| 1670 | + if ($cachedsidebar!="") { |
| 1671 | + wfProfileOut($fname); |
1666 | 1672 | return $cachedsidebar; |
1667 | 1673 | } |
1668 | 1674 | } |
— | — | @@ -1677,7 +1683,7 @@ |
1678 | 1684 | $heading = $line; |
1679 | 1685 | } else { |
1680 | 1686 | if (strpos($line, '|') !== false) { // sanity check |
1681 | | - $line = array_map('trim', explode( '|' , trim($line, '* '), 2 ) ); |
| 1687 | + $line = explode( '|' , trim($line, '* '), 2 ); |
1682 | 1688 | $link = wfMsgForContent( $line[0] ); |
1683 | 1689 | if ($link == '-') |
1684 | 1690 | continue; |
— | — | @@ -1707,9 +1713,9 @@ |
1708 | 1714 | } else { continue; } |
1709 | 1715 | } |
1710 | 1716 | } |
1711 | | - if ( $wgEnableSidebarCache ) $parserMemc->set( $key, $bar, $wgSidebarCacheExpiry ); |
1712 | | - wfProfileOut( __METHOD__ ); |
| 1717 | + if ($cacheSidebar) |
| 1718 | + $parserMemc->set( $key, $bar, $wgSidebarCacheExpiry ); |
| 1719 | + wfProfileOut( $fname ); |
1713 | 1720 | return $bar; |
1714 | 1721 | } |
1715 | 1722 | } |
1716 | | - |
Index: trunk/phase3/languages/Language.php |
— | — | @@ -2093,9 +2093,8 @@ |
2094 | 2094 | } |
2095 | 2095 | |
2096 | 2096 | # Try the global cache |
2097 | | - |
2098 | | - $memcKey = wfMemcKeyLang( wfMemcKey('localisation'), $code ); |
2099 | | - $fbMemcKey = wfMemcKeyLang( wfMemcKey('fallback'), $cache['fallback'] ); |
| 2097 | + $memcKey = wfMemcKey('localisation', $code ); |
| 2098 | + $fbMemcKey = wfMemcKey('fallback', $cache['fallback'] ); |
2100 | 2099 | $cache = $wgMemc->get( $memcKey ); |
2101 | 2100 | if ( $cache ) { |
2102 | 2101 | if ( self::isLocalisationOutOfDate( $cache ) ) { |
— | — | @@ -2233,7 +2232,7 @@ |
2234 | 2233 | |
2235 | 2234 | // Try memcache |
2236 | 2235 | global $wgMemc; |
2237 | | - $memcKey = wfMemcKeyLang( wfMemcKey('fallback'), $code ); |
| 2236 | + $memcKey = wfMemcKey( 'fallback', $code ); |
2238 | 2237 | $fbcode = $wgMemc->get( $memcKey ); |
2239 | 2238 | |
2240 | 2239 | if ( is_string($fbcode) ) { |
Index: trunk/phase3/RELEASE-NOTES |
— | — | @@ -47,8 +47,6 @@ |
48 | 48 | image page already exists |
49 | 49 | * $wgMaximumMovedPages restricts the number of pages that can be moved at once |
50 | 50 | (default 100) with the new subpage-move functionality of Special:Movepage |
51 | | -* New option $wgPerLanguageCaching, for wikies with many translated system |
52 | | - messages in MediaWiki namespace |
53 | 51 | |
54 | 52 | === New features in 1.13 === |
55 | 53 | |
— | — | @@ -126,7 +124,6 @@ |
127 | 125 | * (bug 13232) importScript(), importStylesheet() funcs available to custom JS |
128 | 126 | * (bug 13095) Search by first letters or digits in [[Special:Categories]] |
129 | 127 | * Users moving a page can now move all subpages automatically as well |
130 | | -* Sidebar is now cached for all languages |
131 | 128 | * (bug 14259) Localisation message for upload button on Special:Import is now |
132 | 129 | 'import-upload' instead of 'upload' |
133 | 130 | * Add information about user group membership to Special:Preferences |