Index: trunk/extensions/Translate/SpecialManageGroups.php |
— | — | @@ -62,7 +62,11 @@ |
63 | 63 | foreach ( $languages as $code ) { |
64 | 64 | $messages = $group->load( $code ); |
65 | 65 | if ( count( $messages ) ) { |
66 | | - $cache->create( $messages, $code ); |
| 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? |
67 | 71 | } |
68 | 72 | } |
69 | 73 | } |
— | — | @@ -102,6 +106,11 @@ |
103 | 107 | $wgLang->date( $timestamp ), |
104 | 108 | $wgLang->time( $timestamp ) |
105 | 109 | ); |
| 110 | + |
| 111 | + if ( $this->changedSinceCached( $group ) ) { |
| 112 | + $out = '<span style="color:red">!!</span> ' . $out; |
| 113 | + } |
| 114 | + |
106 | 115 | } else { |
107 | 116 | $out .= wfMsg( 'translate-manage-newgroup' ); |
108 | 117 | } |
— | — | @@ -168,7 +177,9 @@ |
169 | 178 | $messages = $group->load( $code ); |
170 | 179 | |
171 | 180 | if ( !$cache->exists() && $code === 'en' ) { |
172 | | - $cache->create( $messages ); |
| 181 | + $filename = $group->getSourceFilePath( $code ); |
| 182 | + $hash = md5( file_get_contents( $filename ) ); |
| 183 | + $cache->create( $messages, $code, $hash ); |
173 | 184 | } |
174 | 185 | |
175 | 186 | $collection = $group->initCollection( $code ); |
— | — | @@ -194,13 +205,10 @@ |
195 | 206 | |
196 | 207 | if ( isset( $collection[$key] ) ) { |
197 | 208 | $old = $collection[$key]->translation(); |
198 | | - $fuzzy = TranslateEditAddons::hasFuzzyString( $old ) || |
199 | | - TranslateEditAddons::isFuzzy( MessageWebImporter::makeTranslationTitle( $group, $key, $code ) ); |
200 | | - $old = str_replace( TRANSLATE_FUZZY, '', $old ); |
201 | 209 | } |
202 | 210 | |
203 | 211 | // No changes at all, ignore. |
204 | | - if ( $old === $value ) { |
| 212 | + if ( str_replace( TRANSLATE_FUZZY, '', $old ) === $value ) { |
205 | 213 | continue; |
206 | 214 | } |
207 | 215 | |
— | — | @@ -213,8 +221,13 @@ |
214 | 222 | |
215 | 223 | $changed[] = MessageWebImporter::makeSectionElement( $name, 'new', $text ); |
216 | 224 | } else { |
217 | | - if ( $fuzzy ) { |
218 | | - $old = TRANSLATE_FUZZY . $old; |
| 225 | + if ( TranslateEditAddons::hasFuzzyString( $old ) ) { |
| 226 | + // NO-OP |
| 227 | + } else { |
| 228 | + $transTitle = MessageWebImporter::makeTranslationTitle( $group, $key, $code ); |
| 229 | + if ( TranslateEditAddons::isFuzzy( $transTitle ) ) { |
| 230 | + $old = TRANSLATE_FUZZY . $old; |
| 231 | + } |
219 | 232 | } |
220 | 233 | |
221 | 234 | $diff->setText( $old, $value ); |
— | — | @@ -316,7 +329,9 @@ |
317 | 330 | $changed[] = '<ul>'; |
318 | 331 | } |
319 | 332 | |
320 | | - $cache->create( $messages, $code ); |
| 333 | + $filename = $group->getSourceFilePath( $code ); |
| 334 | + $hash = md5( file_get_contents( $filename ) ); |
| 335 | + $cache->create( $messages, $code, $hash ); |
321 | 336 | $message = wfMsgExt( 'translate-manage-import-rebuild', 'parseinline' ); |
322 | 337 | $changed[] = "<li>$message</li>"; |
323 | 338 | $message = wfMsgExt( 'translate-manage-import-done', 'parseinline' ); |
— | — | @@ -364,11 +379,7 @@ |
365 | 380 | continue; |
366 | 381 | } |
367 | 382 | |
368 | | - $filename = $group->getSourceFilePath( $code ); |
369 | | - $mtime = file_exists( $filename ) ? filemtime( $filename ) : false; |
370 | | - $cachetime = $cache->exists( $code ) ? $cache->getTimestamp( $code ) : false; |
371 | | - |
372 | | - if ( $mtime === false && $cachetime === false ) { |
| 383 | + if ( !$this->changedSinceCached( $group, $code ) ) { |
373 | 384 | continue; |
374 | 385 | } |
375 | 386 | |
— | — | @@ -379,6 +390,9 @@ |
380 | 391 | array( 'group' => $group->getId(), 'language' => $code ) |
381 | 392 | ); |
382 | 393 | |
| 394 | + $filename = $group->getSourceFilePath( $code ); |
| 395 | + $mtime = file_exists( $filename ) ? filemtime( $filename ) : false; |
| 396 | + $cachetime = $cache->exists( $code ) ? $cache->getTimestamp( $code ) : false; |
383 | 397 | if ( $mtime === false ) { |
384 | 398 | $modified[] = wfMsgHtml( 'translate-manage-modlang-new', $link ); |
385 | 399 | } elseif ( $mtime > $cachetime ) { |
— | — | @@ -458,4 +472,33 @@ |
459 | 473 | |
460 | 474 | $this->out->setSubtitle( implode( ' > ', $links ) ); |
461 | 475 | } |
| 476 | + |
| 477 | + /** |
| 478 | + * Checks if the source file has changed since last check. |
| 479 | + * Uses modification timestamps and file hashes to check. |
| 480 | + */ |
| 481 | + protected function changedSinceCached( $group, $code = 'en' ) { |
| 482 | + $cache = new MessageGroupCache( $group ); |
| 483 | + $filename = $group->getSourceFilePath( $code ); |
| 484 | + |
| 485 | + $mtime = file_exists( $filename ) ? filemtime( $filename ) : false; |
| 486 | + $cachetime = $cache->exists( $code ) ? $cache->getTimestamp( $code ) : false; |
| 487 | + |
| 488 | + // No such language at all, or cache is up to date |
| 489 | + if ( $mtime <= $cachetime ) { |
| 490 | + return false; |
| 491 | + } |
| 492 | + |
| 493 | + // Timestamps differ (or either cache or the file does not exists) |
| 494 | + $oldhash = $cache->exists( $code ) ? $cache->getHash( $code ) : false; |
| 495 | + $newhash = file_exists( $filename ) ? md5( file_get_contents( $filename ) ) : false; |
| 496 | + wfDebugLog( 'translate-manage', "$mtime === $cachetime | $code | $oldhash !== $newhash\n" ); |
| 497 | + if ( $newhash === $oldhash ) { |
| 498 | + // Update cache so that we don't need to compare hashes next time |
| 499 | + $cache->updateTimestamp( $code ); |
| 500 | + return false; |
| 501 | + } |
| 502 | + |
| 503 | + return true; |
| 504 | + } |
462 | 505 | } |
Index: trunk/extensions/Translate/utils/MessageGroupCache.php |
— | — | @@ -42,13 +42,24 @@ |
43 | 43 | return $cache->get( $this->specialKey( 'timestamp' ) ); |
44 | 44 | } |
45 | 45 | |
| 46 | + public function updateTimestamp( $code = 'en' ) { |
| 47 | + $cache = CdbWriter::open( $this->getCacheFileName( $code ) ); |
| 48 | + $cache->set( $this->specialKey( 'timestamp' ), wfTimestamp() ); |
| 49 | + $cache->close(); |
| 50 | + } |
| 51 | + |
| 52 | + public function getHash( $code = 'en' ) { |
| 53 | + $cache = $this->open( $code ); |
| 54 | + return $cache->get( $this->specialKey( 'hash' ) ); |
| 55 | + } |
| 56 | + |
46 | 57 | public function get( $key, $code = 'en' ) { |
47 | 58 | $cache = $this->open( $code ); |
48 | 59 | |
49 | 60 | return $cache->get( $key ); |
50 | 61 | } |
51 | 62 | |
52 | | - public function create( $messages, $code = 'en' ) { |
| 63 | + public function create( $messages, $code = 'en', $hash = false ) { |
53 | 64 | $this->cache = null; // Needed? |
54 | 65 | |
55 | 66 | $cache = CdbWriter::open( $this->getCacheFileName( $code ) ); |
— | — | @@ -61,6 +72,7 @@ |
62 | 73 | } |
63 | 74 | |
64 | 75 | $cache->set( $this->specialKey( 'timestamp' ), wfTimestamp() ); |
| 76 | + $cache->set( $this->specialKey( 'hash' ), $hash ); |
65 | 77 | $cache->close(); |
66 | 78 | } |
67 | 79 | |