r71936 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71935‎ | r71936 | r71937 >
Date:10:10, 30 August 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Made manage-groups more useful.

* Hash file contents in addition of using timestamps to detect changes (like LU)
* Avoid fuzzy queries when not importing
* Highlight groups which have definitions changed
Modified paths:
  • /trunk/extensions/Translate/SpecialManageGroups.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageGroupCache.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/SpecialManageGroups.php
@@ -62,7 +62,11 @@
6363 foreach ( $languages as $code ) {
6464 $messages = $group->load( $code );
6565 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?
6771 }
6872 }
6973 }
@@ -102,6 +106,11 @@
103107 $wgLang->date( $timestamp ),
104108 $wgLang->time( $timestamp )
105109 );
 110+
 111+ if ( $this->changedSinceCached( $group ) ) {
 112+ $out = '<span style="color:red">!!</span> ' . $out;
 113+ }
 114+
106115 } else {
107116 $out .= wfMsg( 'translate-manage-newgroup' );
108117 }
@@ -168,7 +177,9 @@
169178 $messages = $group->load( $code );
170179
171180 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 );
173184 }
174185
175186 $collection = $group->initCollection( $code );
@@ -194,13 +205,10 @@
195206
196207 if ( isset( $collection[$key] ) ) {
197208 $old = $collection[$key]->translation();
198 - $fuzzy = TranslateEditAddons::hasFuzzyString( $old ) ||
199 - TranslateEditAddons::isFuzzy( MessageWebImporter::makeTranslationTitle( $group, $key, $code ) );
200 - $old = str_replace( TRANSLATE_FUZZY, '', $old );
201209 }
202210
203211 // No changes at all, ignore.
204 - if ( $old === $value ) {
 212+ if ( str_replace( TRANSLATE_FUZZY, '', $old ) === $value ) {
205213 continue;
206214 }
207215
@@ -213,8 +221,13 @@
214222
215223 $changed[] = MessageWebImporter::makeSectionElement( $name, 'new', $text );
216224 } 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+ }
219232 }
220233
221234 $diff->setText( $old, $value );
@@ -316,7 +329,9 @@
317330 $changed[] = '<ul>';
318331 }
319332
320 - $cache->create( $messages, $code );
 333+ $filename = $group->getSourceFilePath( $code );
 334+ $hash = md5( file_get_contents( $filename ) );
 335+ $cache->create( $messages, $code, $hash );
321336 $message = wfMsgExt( 'translate-manage-import-rebuild', 'parseinline' );
322337 $changed[] = "<li>$message</li>";
323338 $message = wfMsgExt( 'translate-manage-import-done', 'parseinline' );
@@ -364,11 +379,7 @@
365380 continue;
366381 }
367382
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 ) ) {
373384 continue;
374385 }
375386
@@ -379,6 +390,9 @@
380391 array( 'group' => $group->getId(), 'language' => $code )
381392 );
382393
 394+ $filename = $group->getSourceFilePath( $code );
 395+ $mtime = file_exists( $filename ) ? filemtime( $filename ) : false;
 396+ $cachetime = $cache->exists( $code ) ? $cache->getTimestamp( $code ) : false;
383397 if ( $mtime === false ) {
384398 $modified[] = wfMsgHtml( 'translate-manage-modlang-new', $link );
385399 } elseif ( $mtime > $cachetime ) {
@@ -458,4 +472,33 @@
459473
460474 $this->out->setSubtitle( implode( ' > ', $links ) );
461475 }
 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+ }
462505 }
Index: trunk/extensions/Translate/utils/MessageGroupCache.php
@@ -42,13 +42,24 @@
4343 return $cache->get( $this->specialKey( 'timestamp' ) );
4444 }
4545
 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+
4657 public function get( $key, $code = 'en' ) {
4758 $cache = $this->open( $code );
4859
4960 return $cache->get( $key );
5061 }
5162
52 - public function create( $messages, $code = 'en' ) {
 63+ public function create( $messages, $code = 'en', $hash = false ) {
5364 $this->cache = null; // Needed?
5465
5566 $cache = CdbWriter::open( $this->getCacheFileName( $code ) );
@@ -61,6 +72,7 @@
6273 }
6374
6475 $cache->set( $this->specialKey( 'timestamp' ), wfTimestamp() );
 76+ $cache->set( $this->specialKey( 'hash' ), $hash );
6577 $cache->close();
6678 }
6779

Status & tagging log