Nikerabbit has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/359378 )

Change subject: WIP: Debugging and working on fix for T167004
......................................................................

WIP: Debugging and working on fix for T167004

Change-Id: Ia02a21b23345435b8b2eef5c5333991deec1d829
---
M modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
M modules/dm/translationunits/mw.cx.dm.SectionTranslationUnit.js
M modules/dm/translationunits/mw.cx.dm.TranslationUnit.js
M modules/mw.cx.TranslationController.js
M modules/mw.cx.init.Translation.js
M modules/ui/translationunits/mw.cx.ui.SectionTranslationUnit.js
6 files changed, 86 insertions(+), 37 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/78/359378/1

diff --git a/modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js 
b/modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
index 36f1627..17b7b20 100644
--- a/modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
+++ b/modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
@@ -86,6 +86,8 @@
  * @return {jQuery.Promise}
  */
 mw.cx.dm.LinkTranslationUnit.prototype.adapt = function () {
+       debugger;
+
        if ( this.targetDocument ) {
                this.redlink = this.targetDocument.classList.contains( 'new' );
                mw.log.warn( '[CX] Adapting a link which looks already adapted: 
' + this );
@@ -134,8 +136,11 @@
        this.targetDocument.href = this.targetTitle;
        this.targetTitleMissing = false;
 
+       debugger;
+
        // As a nice gesture, when using 'source' MT, update the link texts 
when adapting.
        this.getParentSectionTranslationUnit().getMTProvider().done( function ( 
provider ) {
+               debugger;
                if ( provider === 'source' ) {
                        this.targetDocument.text = this.targetTitle;
                }
diff --git a/modules/dm/translationunits/mw.cx.dm.SectionTranslationUnit.js 
b/modules/dm/translationunits/mw.cx.dm.SectionTranslationUnit.js
index 71cba4d..ddf0fb3 100644
--- a/modules/dm/translationunits/mw.cx.dm.SectionTranslationUnit.js
+++ b/modules/dm/translationunits/mw.cx.dm.SectionTranslationUnit.js
@@ -104,6 +104,8 @@
  * @fires adapt
  */
 mw.cx.dm.SectionTranslationUnit.prototype.adapt = function ( requestedProvider 
) {
+       var document;
+
        if ( this.MTProvider && this.targetDocument ) {
                // Store the current version so that it can be restored
                this.documentsPerProvider[ this.MTProvider ] = 
this.targetDocument;
@@ -116,9 +118,9 @@
 
                // Use the cached version
                if ( this.documentsPerProvider[ requestedProvider ] ) {
-                       this.targetDocument = this.documentsPerProvider[ 
requestedProvider ];
-                       this.updateAfterTranslation( this.targetDocument, 
requestedProvider );
-                       this.emit( 'adapt', this.targetDocument, 
requestedProvider );
+                       document = this.documentsPerProvider[ requestedProvider 
];
+                       this.updateAfterTranslation( document, 
requestedProvider );
+                       this.emit( 'adapt', document, requestedProvider );
                        return;
                }
        }
@@ -132,6 +134,14 @@
        }.bind( this ) );
 };
 
+mw.cx.dm.SectionTranslationUnit.prototype.adaptWithRestoredContent = function 
( document, provider ) {
+       this.MTProvider = provider;
+       this.updateAfterTranslation( document, provider );
+       // This is actually useless, because mw.cx.ui.SectionTranslationUnit is 
not listening
+       // when mw.cx.TranslationController#restore calls this method.
+       this.emit( 'adapt', document, provider );
+};
+
 /**
  * Do any necessary updates and book keeping after section contents has been 
filled
  * with a new or cached default value when changing providers.
@@ -139,12 +149,14 @@
  * @param {Element} document Translated translation document
  */
 mw.cx.dm.SectionTranslationUnit.prototype.updateAfterTranslation = function ( 
document ) {
-       this.targetDocument = document;
+       this.setTargetDocument( document );
 };
 
 /**
  * Do any necessary changes to translated document after it has been filled 
with default
- * contents. This is not called when restoring a cached version when changing 
providers.
+ * contents. This is not called:
+ *  - when restoring a cached version when changing providers.
+ *  - when restoring from a saved draft.
  *
  * @param {Element} document Translated translation document
  */
diff --git a/modules/dm/translationunits/mw.cx.dm.TranslationUnit.js 
b/modules/dm/translationunits/mw.cx.dm.TranslationUnit.js
index 955cc9f..8bd2d25 100644
--- a/modules/dm/translationunits/mw.cx.dm.TranslationUnit.js
+++ b/modules/dm/translationunits/mw.cx.dm.TranslationUnit.js
@@ -99,6 +99,11 @@
                throw new Error( '[CX] Both source and target documents can not 
be null' );
        }
 
+       // We are going over both source and target separately, because each 
one can
+       // have units that don't have pair on the other sides (e.g. added 
links, removed
+       // links). But buildSubTranslationUnit is clever and checks the element 
ids to
+       // see if there is a match, and only constructs one unit even for 
things that
+       // exist both on the source and the target side.
        if ( sourceDocument ) {
                sourceChildren = sourceDocument.children || [];
                for ( i = 0; i < sourceChildren.length; i++ ) {
@@ -128,6 +133,8 @@
 mw.cx.dm.TranslationUnit.prototype.buildSubTranslationUnit = function ( 
source, target ) {
        var key, subTranslationUnit, model;
 
+       // The mw.cx.dm.SectionTranslationUnit keeps cache of all the subunits
+       // (recursively) keyed by the DOM id.
        key = this.getKeyForModelMap( source || target );
 
        model = mw.cx.dm.modelRegistry.matchElement( source || target );
@@ -139,21 +146,25 @@
                throw new Error( '[CX] Could not find any unique key for sub 
translation unit of: ' + this.toString() );
        }
 
+       //
+
        subTranslationUnit = this.subTranslationUnitModels[ key ];
        if ( subTranslationUnit ) {
-               subTranslationUnit.setTargetDocument( target );
-       } else {
-               if ( !source ) {
-                       mw.log( '[CX] No source element for ' + target.id );
-               }
-               subTranslationUnit = mw.cx.dm.translationUnitFactory.create(
-                       model, this.config, this.translation, source, target
-               );
-               // Keep a map of DOM ids and translation units
-               this.subTranslationUnitModels[ key ] = subTranslationUnit;
-               this.translationUnits.push( subTranslationUnit );
-               subTranslationUnit.setParentTranslationUnit( this );
+               target && ( subTranslationUnit.targetDocument = target );
+               source && ( subTranslationUnit.sourceDocument = source );
+               return;
        }
+
+       if ( !source ) {
+               mw.log( '[CX] No source element for ' + target.id );
+       }
+       subTranslationUnit = mw.cx.dm.translationUnitFactory.create(
+               model, this.config, this.translation, source, target
+       );
+       // Keep a map of DOM ids and translation units
+       this.subTranslationUnitModels[ key ] = subTranslationUnit;
+       this.translationUnits.push( subTranslationUnit );
+       subTranslationUnit.setParentTranslationUnit( this );
 };
 
 /**
@@ -184,8 +195,15 @@
        return this.sourceDocument;
 };
 
+/**
+ * This gets called from mw.cx.dm.SectionTranslationUnit whenever the target 
document
+ * is replaced with something else.
+ */
 mw.cx.dm.TranslationUnit.prototype.setTargetDocument = function ( 
targetDocument ) {
+       debugger;
        this.targetDocument = targetDocument;
+       // Clear cached units. We have new content, so let's not reuse them.
+       this.translationUnits = [];
        this.buildSubTranslationUnits( this.sourceDocument, this.targetDocument 
);
 };
 
diff --git a/modules/mw.cx.TranslationController.js 
b/modules/mw.cx.TranslationController.js
index 3a4cf90..4edeea0 100644
--- a/modules/mw.cx.TranslationController.js
+++ b/modules/mw.cx.TranslationController.js
@@ -14,6 +14,8 @@
        this.targetTitle = config.targetTitle;
        this.sourceLanguage = config.sourceLanguage;
        this.targetLanguage = config.targetLanguage;
+
+       this.MTManager = config.MTManager;
        // Mixin constructors
        OO.EventEmitter.call( this );
        // Properties
@@ -215,10 +217,12 @@
        validate = false;
 
        sequenceId = translationUnit.sourceDocument.getAttribute( 'data-seqid' 
);
-       /// XXX should use the promise, but at this point the member variable 
should always be present
+       // XXX should use the promise, but at this point the member variable 
should always be present
        translationSource = translationUnit.MTProvider;
-       if ( translationSource === 'source' ) {
+       if ( translationSource === 'source' || translationSource === 'scratch' 
) {
                origin = 'user';
+       } else {
+               origin = translationSource;
        }
 
        records.push( {
@@ -240,28 +244,35 @@
 };
 
 /**
- * Restore the translation from database to translation view to continue.
+ * Restore translations from a saved draft.
  * @param {Object} savedTranslation
  */
 mw.cx.TranslationController.prototype.restore = function ( savedTranslation ) {
-       var i, translationUnits, sectionId, savedTranslationUnits, 
savedTranslationUnit, translationContent;
+       var savedUnits = savedTranslation.translationUnits;
 
-       translationUnits = this.translation.getTranslationUnits();
-       savedTranslationUnits = savedTranslation.translationUnits;
-       for ( i = 0; i < translationUnits.length; i++ ) {
-               sectionId = translationUnits[ i ].getSectionId();
+       this.translation.getTranslationUnits().forEach( function ( unit ) {
+               var savedSection, provider, document,
+                       sectionId = unit.getSectionId();
 
-               savedTranslationUnit = savedTranslationUnits[ sectionId ];
-
-               if ( !savedTranslationUnit ) {
-                       continue;
+               if ( !savedUnits[ sectionId ] ) {
+                       return;
                }
 
-               translationContent = savedTranslationUnit.user || 
savedTranslationUnit.mt || savedTranslationUnit.source;
-               // TODO: translationContent is an object, it has MT service, 
timestamp and content.
-               // Do we need to retain all these info to the model?
-               translationUnits[ i ].setTargetDocument( $( 
translationContent.content )[ 0 ] );
-       }
+               // XXX: When whould "user" not be present?
+               savedSection = savedUnits[ sectionId ].user || savedUnits[ 
sectionId ].mt;
+
+               if ( !savedSection ) {
+                       mw.log.error( '[CX] Missing content to restore for 
section ' + sectionId );
+                       return;
+               }
+
+               // Convert HTML string to Element.
+               document = $( savedSection.content )[ 0 ];
+               // XXX: We don't really know whether it was "source" or 
"scratch" if user changed the default
+               provider = ( savedUnits[ sectionId ].mt && savedUnits[ 
sectionId ].mt.engine ) || 'source';
+
+               unit.adaptWithRestoredContent( document, provider );
+       } );
        // TODO: Find out orphan translation units and handle them.
 };
 
diff --git a/modules/mw.cx.init.Translation.js 
b/modules/mw.cx.init.Translation.js
index b9b1a5e..ff5259c 100644
--- a/modules/mw.cx.init.Translation.js
+++ b/modules/mw.cx.init.Translation.js
@@ -306,6 +306,7 @@
 };
 
 mw.cx.init.Translation.prototype.restoreDraftTranslation = function ( draft ) {
+       debugger;
        // In case there is no draft (see fetchDraft), there is nothing for us 
to do
        if ( !draft ) {
                return;
diff --git a/modules/ui/translationunits/mw.cx.ui.SectionTranslationUnit.js 
b/modules/ui/translationunits/mw.cx.ui.SectionTranslationUnit.js
index ae1ae20..aea36ad 100644
--- a/modules/ui/translationunits/mw.cx.ui.SectionTranslationUnit.js
+++ b/modules/ui/translationunits/mw.cx.ui.SectionTranslationUnit.js
@@ -113,12 +113,13 @@
 };
 
 /**
- * Fill in the contents of the target section if it does not exist at all.
+ * Replace placeholder section with some contents determined by the model.
  */
 mw.cx.ui.SectionTranslationUnit.prototype.translate = function () {
        this.removeHighlight();
        this.removePlaceholder();
 
+       // Skip adaptation if section was filled from a saved draft
        if ( !this.isTranslated() ) {
                mw.log( '[CX] Adapting to replace placeholder: ' + this, 
this.$sourceSection[ 0 ] );
                this.markMTLoading();
@@ -126,8 +127,9 @@
                // ready, which is then handled in this.onModelAdapted.
                this.model.adapt();
        } else {
-               // Restored section from stored translation
-               this.buildSubTranslationUnits( this.model );
+               // We missed an "adapt" event from restoration, do it manually
+               debugger;
+               this.onModelAdapted( this.model.getTargetDocument(), 'restoring 
draft' );
        }
 
        if ( this.isEditable() ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/359378
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia02a21b23345435b8b2eef5c5333991deec1d829
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to