jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/349939 )
Change subject: CX2: LinkTranslationUnit: Avoid the target document dangling
without parent
......................................................................
CX2: LinkTranslationUnit: Avoid the target document dangling without parent
The target document created by cloning source document will not be attached
to any parent node. With this state, it cannot be removed too.
This commit make sure that the target document refer to an actual node
under parent target node.
Change-Id: I8a7bd31b202b8265af1e004175f8936b9df84923
---
M modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
M modules/tests/dm/mw.cx.dm.LinkTranslationUnit.test.js
2 files changed, 94 insertions(+), 30 deletions(-)
Approvals:
jenkins-bot: Verified
Nikerabbit: Looks good to me, approved
diff --git a/modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
b/modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
index 6b113e7..4224d63 100644
--- a/modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
+++ b/modules/dm/translationunits/mw.cx.dm.LinkTranslationUnit.js
@@ -44,29 +44,89 @@
};
/**
+ * Find link target for the given source title
+ * @param {string} sourceLanguage
+ * @param {string} sourceTitle
+ * @return {jQuery.Promise}
+ */
+mw.cx.dm.LinkTranslationUnit.prototype.findLinkTarget = function (
sourceLanguage, sourceTitle ) {
+ var result = $.Deferred();
+ if ( !sourceLanguage ) {
+ mw.log.error( '[CX] Invalid source language given to link
adapt' + this );
+ result.reject();
+ }
+ if ( !sourceTitle ) {
+ mw.log.error( '[CX] Invalid source title given to link adapt' +
this );
+ result.reject();
+ }
+
+ this.requestManager.getTitlePair( this.sourceLanguage, this.sourceTitle
)
+ .done( function( pairInfo ) {
+ var targetTitle = pairInfo.targetTitle;
+ if ( !targetTitle ) {
+ result.reject();
+ } else {
+ result.resolve( targetTitle );
+ }
+ }.bind( this ) );
+
+ return result.promise();
+};
+
+/**
* Adapt a source link to target language.
* @return {jQuery.Promise}
*/
mw.cx.dm.LinkTranslationUnit.prototype.adapt = function () {
- var self = this;
- this.targetDocument = this.sourceDocument.cloneNode( true );
- this.setTargetId();
+ if ( this.targetDocument ) {
+ mw.log.warn( '[CX] Adapting a link which looks already adapted:
' + this );
+ }
- return this.requestManager.getTitlePair( this.sourceLanguage,
this.sourceTitle )
- .then( function( pairInfo ) {
- var targetTitle = pairInfo.targetTitle;
- if ( targetTitle ) {
- self.targetDocument.title = targetTitle;
- self.targetDocument.href = targetTitle;
- // TODO: This is just for testing. We should
not do this if there is MT for
- // the source-target language pair.
- self.targetDocument.text = targetTitle;
- self.targetTitleMissing = false;
- } else {
- self.targetTitleMissing = true;
- }
- } );
+ // Find the element in parent section for this link.
+ this.targetDocument = this.parentTranslationUnit.getTargetDocument()
+ .querySelector( '[id="' + this.sourceDocument.id + '"]' );
+
+ if ( !this.targetDocument ) {
+ // If this is a restored translation, the link will exist with
cx id prefix
+ this.targetDocument =
this.parentTranslationUnit.getTargetDocument()
+ .querySelector( '[id="cx' + this.sourceDocument.id +
'"]' );
+ } else {
+ // Set the id with 'cx' prefix
+ this.setTargetId();
+ }
+
+ if ( !this.targetDocument ) {
+ mw.log.error( '[CX] Could not find the target element in parent
document. ' + this );
+ // Nothing to adapt
+ return $.Deferred().reject().promise();
+ }
+
+ return this.findLinkTarget( this.sourceLanguage, this.sourceTitle
).then(
+ this.adaptSuccessHandler.bind( this ),
+ this.adaptFailureHandler.bind( this )
+ );
+};
+
+/**
+ * Link adaptation success handler
+ * @param {string} targetTitle Target title corresponding to source title
+ */
+mw.cx.dm.LinkTranslationUnit.prototype.adaptSuccessHandler = function (
targetTitle ) {
+ this.targetTitle = targetTitle;
+ this.targetDocument.title = this.targetTitle;
+ this.targetDocument.href = this.targetTitle;
+ // TODO: This is just for testing. We should not do this if there is MT
for
+ // the source-target language pair.
+ this.targetDocument.text = this.targetTitle;
+ this.targetTitleMissing = false;
+};
+
+/**
+ * Link adaptation failure handler
+ */
+mw.cx.dm.LinkTranslationUnit.prototype.adaptFailureHandler = function () {
+ this.targetTitleMissing = true;
};
/**
diff --git a/modules/tests/dm/mw.cx.dm.LinkTranslationUnit.test.js
b/modules/tests/dm/mw.cx.dm.LinkTranslationUnit.test.js
index 507d798..4f8b601 100644
--- a/modules/tests/dm/mw.cx.dm.LinkTranslationUnit.test.js
+++ b/modules/tests/dm/mw.cx.dm.LinkTranslationUnit.test.js
@@ -10,7 +10,7 @@
QUnit.test( 'Title adaptation test', function ( assert ) {
var tests, config, i, linkTranslationUnit, linkText, sourceLinkDoc,
targetTitle, requestManager, description,
- mockResponse;
+ mockResponse, done;
tests = [
{
@@ -33,10 +33,15 @@
targetLanguage: 'ta',
targetTitle: 'ஒரு குழந்தைக்கு ஒரு மடிக்கணினி',
description: 'Target page exist, source page redirects'
+ },
+ {
+ title: null,
+ sourceLanguage: 'en',
+ targetLanguage: 'ta',
+ targetTitle: null,
+ description: 'Invalid source title'
}
];
-
- QUnit.expect( tests.length );
/* eslint no-loop-func:off */
for ( i = 0; i < tests.length; i++ ) {
@@ -78,15 +83,14 @@
targetTitle = tests[ i ].targetTitle;
description = tests[ i ].description;
- QUnit.stop();
- linkTranslationUnit.adapt().then( function () {
- if ( targetTitle ) {
- assert.deepEqual(
linkTranslationUnit.getTargetTitle(), targetTitle, description );
- } else {
- assert.deepEqual(
!!linkTranslationUnit.targetTitleMissing, true, description );
- }
- } ).always( function() {
- QUnit.start();
- } );
+ done = assert.async();
+ linkTranslationUnit.findLinkTarget( tests[ i ].sourceLanguage,
tests[ i ].title )
+ .done( function ( adaptedTitle ) {
+ assert.deepEqual( adaptedTitle, targetTitle,
description );
+ } ).fail( function() {
+ assert.deepEqual( targetTitle, null,
description );
+ } ).always( function() {
+ done();
+ } );
}
} );
--
To view, visit https://gerrit.wikimedia.org/r/349939
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8a7bd31b202b8265af1e004175f8936b9df84923
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Santhosh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits