jenkins-bot has submitted this change and it was merged.
Change subject: Simplistic alignment based on h2 headers for
Special:PageMigration
......................................................................
Simplistic alignment based on h2 headers for Special:PageMigration
h2 headers are aligned in the order they appear in the source and target
units. If needed, blank units are added; if there is no room for aligning,
the preceding units are merged until there is enough room for alignment.
QUnit tests have also been added to verify the same.
Bug: 66162
Change-Id: I06fea8b76b01014a83c32b972b15375aa8f44e1a
---
M resources/js/ext.translate.special.pagemigration.js
M tests/qunit/ext.translate.special.pagemigration.test.js
2 files changed, 102 insertions(+), 12 deletions(-)
Approvals:
Nikerabbit: Looks good to me, approved
Nemo bis: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/resources/js/ext.translate.special.pagemigration.js
b/resources/js/ext.translate.special.pagemigration.js
index 189ddfa..f5f2b68 100644
--- a/resources/js/ext.translate.special.pagemigration.js
+++ b/resources/js/ext.translate.special.pagemigration.js
@@ -1,8 +1,7 @@
( function ( $, mw ) {
'use strict';
var noOfSourceUnits, noOfTranslationUnits,
- pageName, langCode, sourceUnits = [],
- translationUnits;
+ pageName, langCode, sourceUnits = [];
/**
* Create translation pages using content of right hand side blocks
@@ -68,7 +67,6 @@
}
pageContent = obj.revisions[0]['*'];
oldTranslationUnits = pageContent.split( '\n\n' );
- translationUnits = oldTranslationUnits;
return oldTranslationUnits;
} ).promise();
}
@@ -151,13 +149,6 @@
return sourceUnits;
} ).promise();
}
-
- mw.translate = mw.translate || {};
- mw.translate = $.extend( mw.translate, {
- getSourceUnits: getSourceUnits,
- getFuzzyTimestamp: getFuzzyTimestamp,
- splitTranslationPage: splitTranslationPage
- } );
/**
* Shift rows up by one unit. This is called after a unit is deleted.
@@ -248,6 +239,70 @@
newUnit = createNewUnit( sourceText, targetText );
unitListing.append( newUnit );
}
+ }
+
+ /**
+ * Get the index of next translation unit containing h2 header
+ * @param {Integer} startIndex Index to start the scan from
+ * @return {Integer} i Index of the next unit found, -1 if not
+ */
+ function getHeaderUnit( startIndex, translationUnits ) {
+ var i, regex;
+ regex = new RegExp( /^==[^=]+==$/m );
+ for ( i = startIndex; i < translationUnits.length; i++ ) {
+ if ( regex.test( translationUnits[i] ) ) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Align h2 headers in the order they appear.
+ * Assumption: The source headers and translation headers appear in
+ * the same order.
+ */
+ function alignHeaders( sourceUnits, translationUnits ) {
+ var i, regex, tIndex = 0,
+ matchText, emptyCount, mergeText;
+
+ regex = new RegExp( /^==[^=]+==$/m );
+ for ( i = 0; i < sourceUnits.length; i++ ) {
+ if ( regex.test( sourceUnits[i].definition ) ) {
+ tIndex = getHeaderUnit( tIndex,
translationUnits );
+ mergeText = '';
+ // search is over
+ if ( tIndex === -1 ) {
+ break;
+ }
+ // remove the unit
+ matchText = translationUnits.splice( tIndex, 1
).toString();
+ emptyCount = i - tIndex;
+ if ( emptyCount > 0 ) {
+ // add empty units
+ while ( emptyCount !== 0 ) {
+ translationUnits.splice(
tIndex, 0, '' );
+ emptyCount -= 1;
+ }
+ } else if ( emptyCount < 0 ) {
+ // merge units until there is room for
tIndex translation unit to
+ // align with ith source unit
+ while ( emptyCount !== 0 ) {
+ mergeText +=
translationUnits.splice( i, 1 ).toString() + '\n';
+ emptyCount += 1;
+ }
+ if ( i !== 0 ) {
+ translationUnits[i - 1] += '\n'
+ mergeText;
+ } else {
+ matchText = mergeText +
matchText;
+ }
+ }
+ // add the unit back
+ translationUnits.splice( i, 0, matchText );
+ tIndex = i + 1;
+ }
+ }
+ return translationUnits;
}
/**
@@ -359,8 +414,10 @@
.then( function ( sourceUnits, fuzzyTimestamp ) {
noOfSourceUnits = sourceUnits.length;
splitTranslationPage( fuzzyTimestamp, pageTitle ).done(
function ( translations ) {
- noOfTranslationUnits = translations.length;
- displayUnits( sourceUnits, translations );
+ var translationUnits = translations;
+ translationUnits = alignHeaders( sourceUnits,
translationUnits );
+ noOfTranslationUnits = translationUnits.length;
+ displayUnits( sourceUnits, translationUnits );
$( '#action-save, #action-cancel').removeClass(
'hide' );
$( '#action-import' ).addClass( 'hide' );
} );
@@ -381,4 +438,13 @@
}
$( document ).ready( listen );
+
+ mw.translate = mw.translate || {};
+ mw.translate = $.extend( mw.translate, {
+ getSourceUnits: getSourceUnits,
+ getFuzzyTimestamp: getFuzzyTimestamp,
+ splitTranslationPage: splitTranslationPage,
+ alignHeaders: alignHeaders
+ } );
+
} ( jQuery, mediaWiki ) );
diff --git a/tests/qunit/ext.translate.special.pagemigration.test.js
b/tests/qunit/ext.translate.special.pagemigration.test.js
index ea699db..4eb5fed 100644
--- a/tests/qunit/ext.translate.special.pagemigration.test.js
+++ b/tests/qunit/ext.translate.special.pagemigration.test.js
@@ -75,4 +75,28 @@
} );
} );
+ QUnit.test( '-- Align h2 headers', function ( assert ) {
+ QUnit.expect( 2 );
+ var sourceUnits, translationUnits1, result1,
+ translationUnits2, result2;
+
+ sourceUnits = [{'identifier':'1','definition':'abc'},
{'identifier':'2','definition':'==123=='},
+ {'identifier':'3','definition':'pqr'},
{'identifier':'4','definition':'xyz'},
+ {'identifier':'5','definition':'mno'},
{'identifier':'6','definition':'==456=='}];
+
+ translationUnits1 = ['==123==', 'pqr', '==456=='];
+
+ translationUnits2 = ['abc', 'lmn', '==123==', 'pqr', '==456=='];
+
+ result1 = ['', '==123==', 'pqr', '', '', '==456=='];
+
+ result2 = ['abc\nlmn\n', '==123==', 'pqr', '', '', '==456=='];
+
+ translationUnits1 = mw.translate.alignHeaders( sourceUnits,
translationUnits1 );
+ assert.deepEqual( translationUnits1, result1, 'h2 headers
aligned without merging' );
+
+ translationUnits2 = mw.translate.alignHeaders( sourceUnits,
translationUnits2 );
+ assert.deepEqual( translationUnits2, result2, 'h2 headers
aligned with merging' );
+ } );
+
}( jQuery, mediaWiki ) );
\ No newline at end of file
--
To view, visit https://gerrit.wikimedia.org/r/138220
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I06fea8b76b01014a83c32b972b15375aa8f44e1a
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: BPositive <[email protected]>
Gerrit-Reviewer: BPositive <[email protected]>
Gerrit-Reviewer: Nemo bis <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits