Index: trunk/extensions/Translate/Groups.php |
— | — | @@ -222,9 +222,9 @@ |
223 | 223 | } |
224 | 224 | |
225 | 225 | public function getMessage( $key, $code ) { |
226 | | - $cache = new MessageGroupCache( $this ); |
227 | | - if ( $cache->exists( $code ) ) { |
228 | | - $msg = $cache->get( $key, $code ); |
| 226 | + $cache = new MessageGroupCache( $this, $code ); |
| 227 | + if ( $cache->exists() ) { |
| 228 | + $msg = $cache->get( $key ); |
229 | 229 | |
230 | 230 | if ( $msg !== false ) { |
231 | 231 | return $msg; |
— | — | @@ -232,7 +232,7 @@ |
233 | 233 | |
234 | 234 | // Try harder |
235 | 235 | $nkey = str_replace( ' ', '_', strtolower( $key ) ); |
236 | | - $keys = $cache->getKeys( $code ); |
| 236 | + $keys = $cache->getKeys(); |
237 | 237 | |
238 | 238 | foreach ( $keys as $k ) { |
239 | 239 | if ( $nkey === str_replace( ' ', '_', strtolower( $k ) ) ) { |
Index: trunk/extensions/Translate/utils/MessageGroupCache.php |
— | — | @@ -11,59 +11,58 @@ |
12 | 12 | * @todo Needs documentation. |
13 | 13 | */ |
14 | 14 | class MessageGroupCache { |
| 15 | + /// \string |
15 | 16 | protected $group; |
| 17 | + /// CdbReader |
16 | 18 | protected $cache; |
17 | | - |
18 | | - // Implementation detail |
| 19 | + /// \string |
19 | 20 | protected $code; |
20 | 21 | |
21 | | - // id or instance of MessageGroup |
22 | | - public function __construct( $group ) { |
| 22 | + /** |
| 23 | + * Contructs a new cache object for given group and language code. |
| 24 | + * @param $group \types{String,FileBasedMessageGroup} Group object or id. |
| 25 | + * @param $code \string Language code. Default value 'en'. |
| 26 | + */ |
| 27 | + public function __construct( $group, $code = 'en' ) { |
23 | 28 | if ( is_object( $group ) ) { |
24 | 29 | $this->group = $group->getId(); |
25 | 30 | } else { |
26 | 31 | $this->group = $group; |
27 | 32 | } |
| 33 | + $this->code = $code; |
28 | 34 | } |
29 | 35 | |
30 | | - public function exists( $code = 'en' ) { |
31 | | - return file_exists( $this->getCacheFileName( $code ) ); |
| 36 | + public function exists() { |
| 37 | + return file_exists( $this->getCacheFileName() ); |
32 | 38 | } |
33 | 39 | |
34 | | - public function getKeys( $code = 'en' ) { |
35 | | - $cache = $this->open( $code ); |
36 | | - |
37 | | - return unserialize( $cache->get( $this->specialKey( 'keys' ) ) ); |
| 40 | + public function getKeys() { |
| 41 | + return unserialize( $this->open()->get( $this->specialKey( 'keys' ) ) ); |
38 | 42 | } |
39 | 43 | |
40 | | - public function getTimestamp( $code = 'en' ) { |
41 | | - $cache = $this->open( $code ); |
42 | | - |
43 | | - return $cache->get( $this->specialKey( 'timestamp' ) ); |
| 44 | + public function getTimestamp() { |
| 45 | + return $this->open()->get( $this->specialKey( 'timestamp' ) ); |
44 | 46 | } |
45 | 47 | |
46 | | - public function updateTimestamp( $code = 'en' ) { |
47 | | - $cache = CdbWriter::open( $this->getCacheFileName( $code ) ); |
48 | | - $cache->set( $this->specialKey( 'timestamp' ), wfTimestamp() ); |
49 | | - $cache->close(); |
| 48 | + public function getHash() { |
| 49 | + return $this->open()->get( $this->specialKey( 'hash' ) ); |
50 | 50 | } |
51 | 51 | |
52 | | - public function getHash( $code = 'en' ) { |
53 | | - $cache = $this->open( $code ); |
54 | | - return $cache->get( $this->specialKey( 'hash' ) ); |
| 52 | + public function get( $key ) { |
| 53 | + return $this->open()->get( $key ); |
55 | 54 | } |
56 | 55 | |
57 | | - public function get( $key, $code = 'en' ) { |
58 | | - $cache = $this->open( $code ); |
| 56 | + public function create() { |
| 57 | + $this->close(); // Close the reader instance just to be sure |
59 | 58 | |
60 | | - return $cache->get( $key ); |
61 | | - } |
| 59 | + $group = MessageGroups::getGroup( $this->group ); |
| 60 | + $messages = $group->load( $this->code ); |
| 61 | + if ( !count( $messages ) ) { |
| 62 | + return; // Don't create empty caches |
| 63 | + } |
| 64 | + $hash = md5( file_get_contents( $group->getSourceFilePath( $this->code ) ) ); |
62 | 65 | |
63 | | - public function create( $messages, $code = 'en', $hash = false ) { |
64 | | - $this->cache = null; // Needed? |
65 | | - |
66 | | - $cache = CdbWriter::open( $this->getCacheFileName( $code ) ); |
67 | | - |
| 66 | + $cache = CdbWriter::open( $this->getCacheFileName() ); |
68 | 67 | $keys = array_keys( $messages ); |
69 | 68 | $cache->set( $this->specialKey( 'keys' ), serialize( $keys ) ); |
70 | 69 | |
— | — | @@ -76,22 +75,24 @@ |
77 | 76 | $cache->close(); |
78 | 77 | } |
79 | 78 | |
80 | | - protected function open( $code ) { |
81 | | - if ( $code !== $this->code || !$this->cache ) { |
82 | | - if ( $this->cache ) { |
83 | | - $this->cache->close(); |
84 | | - } |
85 | | - |
86 | | - $this->cache = CdbReader::open( $this->getCacheFileName( $code ) ); |
| 79 | + protected function open() { |
| 80 | + if ( $this->cache === null ) { |
| 81 | + $this->cache = CdbReader::open( $this->getCacheFileName() ); |
87 | 82 | } |
88 | | - |
89 | 83 | return $this->cache; |
90 | 84 | } |
91 | 85 | |
92 | | - protected function getCacheFileName( $code ) { |
93 | | - return TranslateUtils::cacheFile( "translate_groupcache-{$this->group}-$code.cdb" ); |
| 86 | + protected function close() { |
| 87 | + if ( $this->cache !== null ) { |
| 88 | + $this->cache->close(); |
| 89 | + $this->cache = null; |
| 90 | + } |
94 | 91 | } |
95 | 92 | |
| 93 | + protected function getCacheFileName() { |
| 94 | + return TranslateUtils::cacheFile( "translate_groupcache-{$this->group}-{$this->code}.cdb" ); |
| 95 | + } |
| 96 | + |
96 | 97 | protected function specialKey( $key ) { |
97 | 98 | return "<|$key#>"; |
98 | 99 | } |
Index: trunk/extensions/Translate/SpecialManageGroups.php |
— | — | @@ -57,31 +57,21 @@ |
58 | 58 | $this->user->isAllowed( 'translate-manage' ) && |
59 | 59 | $this->user->matchEditToken( $wgRequest->getVal( 'token' ) ) |
60 | 60 | ) { |
61 | | - $cache = new MessageGroupCache( $group ); |
62 | 61 | $languages = explode( ',', $wgRequest->getText( 'codes' ) ); |
63 | 62 | foreach ( $languages as $code ) { |
64 | | - $messages = $group->load( $code ); |
65 | | - if ( count( $messages ) ) { |
66 | | - $filename = $group->getSourceFilePath( $code ); |
67 | | - $hash = md5( file_get_contents( $filename ) ); |
68 | | - $cache->create( $messages, $code, $hash ); |
69 | | - } else { |
70 | | - ///@todo delete stale caches? |
71 | | - } |
| 63 | + $cache = new MessageGroupCache( $group, $code ); |
| 64 | + $cache->create(); |
72 | 65 | } |
73 | 66 | } |
74 | 67 | |
75 | | - $cache = new MessageGroupCache( $group ); |
76 | 68 | $code = $wgRequest->getText( 'language', 'en' ); |
77 | | - |
78 | | - |
79 | 69 | // Go to English for undefined codes. |
80 | 70 | $codes = array_keys( Language::getLanguageNames( false ) ); |
81 | 71 | if ( !in_array( $code, $codes ) ) { |
82 | 72 | $code = 'en'; |
83 | 73 | } |
84 | 74 | |
85 | | - $this->importForm( $cache, $group, $code ); |
| 75 | + $this->importForm( $group, $code ); |
86 | 76 | } else { |
87 | 77 | global $wgLang, $wgOut; |
88 | 78 | |
— | — | @@ -99,7 +89,6 @@ |
100 | 90 | $out = $link . $separator; |
101 | 91 | |
102 | 92 | $cache = new MessageGroupCache( $group ); |
103 | | - |
104 | 93 | if ( $cache->exists() ) { |
105 | 94 | $timestamp = wfTimestamp( TS_MW, $cache->getTimestamp() ); |
106 | 95 | $out .= wfMsg( 'translate-manage-cacheat', |
— | — | @@ -144,7 +133,7 @@ |
145 | 134 | /** |
146 | 135 | * @todo Very long code block; split up. |
147 | 136 | */ |
148 | | - public function importForm( $cache, $group, $code ) { |
| 137 | + public function importForm( $group, $code ) { |
149 | 138 | $this->setSubtitle( $group, $code ); |
150 | 139 | |
151 | 140 | $formParams = array( |
— | — | @@ -174,12 +163,9 @@ |
175 | 164 | ); |
176 | 165 | |
177 | 166 | // BEGIN |
178 | | - $messages = $group->load( $code ); |
179 | | - |
| 167 | + $cache = new MessageGroupCache( $group, $code ); |
180 | 168 | if ( !$cache->exists() && $code === 'en' ) { |
181 | | - $filename = $group->getSourceFilePath( $code ); |
182 | | - $hash = md5( file_get_contents( $filename ) ); |
183 | | - $cache->create( $messages, $code, $hash ); |
| 169 | + $cache->create(); |
184 | 170 | } |
185 | 171 | |
186 | 172 | $collection = $group->initCollection( $code ); |
— | — | @@ -194,6 +180,7 @@ |
195 | 181 | $ignoredMessages = array(); |
196 | 182 | } |
197 | 183 | |
| 184 | + $messages = $group->load( $code ); |
198 | 185 | $changed = array(); |
199 | 186 | foreach ( $messages as $key => $value ) { |
200 | 187 | // ignored? ignore! |
— | — | @@ -329,9 +316,7 @@ |
330 | 317 | $changed[] = '<ul>'; |
331 | 318 | } |
332 | 319 | |
333 | | - $filename = $group->getSourceFilePath( $code ); |
334 | | - $hash = md5( file_get_contents( $filename ) ); |
335 | | - $cache->create( $messages, $code, $hash ); |
| 320 | + $cache->create(); |
336 | 321 | $message = wfMsgExt( 'translate-manage-import-rebuild', 'parseinline' ); |
337 | 322 | $changed[] = "<li>$message</li>"; |
338 | 323 | $message = wfMsgExt( 'translate-manage-import-done', 'parseinline' ); |
— | — | @@ -350,6 +335,7 @@ |
351 | 336 | $this->out->addHTML( implode( "\n", $changed ) ); |
352 | 337 | $this->out->addHTML( Xml::submitButton( wfMsg( 'translate-manage-submit' ) ) ); |
353 | 338 | } else { |
| 339 | + $cache->create(); // Update timestamp |
354 | 340 | $this->out->addWikiMsg( 'translate-manage-nochanges' ); |
355 | 341 | } |
356 | 342 | } |
— | — | @@ -357,7 +343,7 @@ |
358 | 344 | $this->out->addHTML( '</form>' ); |
359 | 345 | |
360 | 346 | if ( $code === 'en' ) { |
361 | | - $this->doModLangs( $cache, $group ); |
| 347 | + $this->doModLangs( $group ); |
362 | 348 | } else { |
363 | 349 | $this->out->addHTML( '<p>' . $this->skin->link( |
364 | 350 | $this->getTitle(), |
— | — | @@ -368,7 +354,7 @@ |
369 | 355 | } |
370 | 356 | } |
371 | 357 | |
372 | | - public function doModLangs( $cache, $group ) { |
| 358 | + public function doModLangs( $group ) { |
373 | 359 | global $wgLang; |
374 | 360 | |
375 | 361 | $languages = array_keys( Language::getLanguageNames( false ) ); |
— | — | @@ -390,12 +376,9 @@ |
391 | 377 | array( 'group' => $group->getId(), 'language' => $code ) |
392 | 378 | ); |
393 | 379 | |
394 | | - $filename = $group->getSourceFilePath( $code ); |
395 | | - $mtime = file_exists( $filename ) ? filemtime( $filename ) : false; |
396 | | - $cachetime = $cache->exists( $code ) ? $cache->getTimestamp( $code ) : false; |
397 | | - if ( $mtime === false ) { |
| 380 | + if ( !$cache->exists() ) { |
398 | 381 | $modified[] = wfMsgHtml( 'translate-manage-modlang-new', $link ); |
399 | | - } elseif ( $mtime > $cachetime ) { |
| 382 | + } else { |
400 | 383 | $modified[] = $link; |
401 | 384 | } |
402 | 385 | |
— | — | @@ -478,11 +461,11 @@ |
479 | 462 | * Uses modification timestamps and file hashes to check. |
480 | 463 | */ |
481 | 464 | protected function changedSinceCached( $group, $code = 'en' ) { |
482 | | - $cache = new MessageGroupCache( $group ); |
| 465 | + $cache = new MessageGroupCache( $group, $code ); |
483 | 466 | $filename = $group->getSourceFilePath( $code ); |
484 | 467 | |
485 | 468 | $mtime = file_exists( $filename ) ? filemtime( $filename ) : false; |
486 | | - $cachetime = $cache->exists( $code ) ? $cache->getTimestamp( $code ) : false; |
| 469 | + $cachetime = $cache->exists() ? $cache->getTimestamp() : false; |
487 | 470 | |
488 | 471 | // No such language at all, or cache is up to date |
489 | 472 | if ( $mtime <= $cachetime ) { |
— | — | @@ -490,12 +473,12 @@ |
491 | 474 | } |
492 | 475 | |
493 | 476 | // Timestamps differ (or either cache or the file does not exists) |
494 | | - $oldhash = $cache->exists( $code ) ? $cache->getHash( $code ) : false; |
| 477 | + $oldhash = $cache->exists() ? $cache->getHash() : false; |
495 | 478 | $newhash = file_exists( $filename ) ? md5( file_get_contents( $filename ) ) : false; |
496 | 479 | wfDebugLog( 'translate-manage', "$mtime === $cachetime | $code | $oldhash !== $newhash\n" ); |
497 | 480 | if ( $newhash === $oldhash ) { |
498 | 481 | // Update cache so that we don't need to compare hashes next time |
499 | | - $cache->updateTimestamp( $code ); |
| 482 | + $cache->create(); |
500 | 483 | return false; |
501 | 484 | } |
502 | 485 | |