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

Change subject: Image translation unit: Adapt namespace
......................................................................

Image translation unit: Adapt namespace

Also add NamespaceCache
TODO:
1. Remove images that are not in commons.
2. Treat figure caption as first class section with MT, link and
   such adaptations

Bug: T152586
Change-Id: If925397d67ba972cad30dbbeabc3af8b5e658cd0
---
A modules/cache/mw.cx.NamespaceCache.js
M modules/dm/mw.cx.dm.Translation.js
M modules/dm/translationunits/mw.cx.dm.ImageTranslationUnit.js
M modules/mw.cx.MwApiRequestManager.js
M modules/ui/mw.cx.ui.TranslationView.js
M modules/ui/translationunits/mw.cx.ui.ImageTranslationUnit.js
6 files changed, 158 insertions(+), 6 deletions(-)


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

diff --git a/modules/cache/mw.cx.NamespaceCache.js 
b/modules/cache/mw.cx.NamespaceCache.js
new file mode 100644
index 0000000..5fe5426
--- /dev/null
+++ b/modules/cache/mw.cx.NamespaceCache.js
@@ -0,0 +1,54 @@
+/**
+ * ContentTranslation Namespace request cache
+ *
+ */
+
+'use strict';
+/**
+ * Caches namespace aliases in a language
+ *
+ * @class
+ * @extends mw.cx.ApiResponseCache
+ * @constructor
+ * @param {Object} config Configuration
+ */
+mw.cx.NamespaceCache = function CXNamespaceCache( config ) {
+       this.requestPromise = null;
+       this.language = config.language;
+       this.cacheValues = {};
+       this.siteMapper = config.siteMapper;
+};
+
+/* Methods */
+
+/**
+ * Get the value from request or cached request
+ * @param {string} canonicalNamespace
+ * @return {jQuery.Promise}
+ */
+mw.cx.NamespaceCache.prototype.get = function ( canonicalNamespace ) {
+       return this.getRequestPromise().then( function() {
+               return this.cacheValues[ canonicalNamespace ];
+       }.bind( this ) );
+};
+
+mw.cx.NamespaceCache.prototype.processResponse = function ( response ) {
+       var namespaceId, namespaceObj;
+
+       for ( namespaceId in response.query.namespaces ) {
+               namespaceObj = response.query.namespaces[ namespaceId ];
+               this.cacheValues[ namespaceObj.canonical ] = OO.getProp( 
namespaceObj, '*' );
+       }
+};
+
+mw.cx.NamespaceCache.prototype.getRequestPromise = function () {
+       this.requestPromise = this.requestPromise ||
+               this.siteMapper.getApi( this.language ).get( {
+                       action: 'query',
+                       meta: 'siteinfo',
+                       siprop: 'namespaces'
+               } ).then( function( response ) {
+                       this.processResponse( response );
+               }.bind( this ) );
+       return this.requestPromise;
+};
diff --git a/modules/dm/mw.cx.dm.Translation.js 
b/modules/dm/mw.cx.dm.Translation.js
index 7b9d412..49cf76f 100644
--- a/modules/dm/mw.cx.dm.Translation.js
+++ b/modules/dm/mw.cx.dm.Translation.js
@@ -37,14 +37,19 @@
  * It corresponds to each sections to translate.
  */
 mw.cx.dm.Translation.prototype.prepareTranslationUnits = function () {
-       var translatableSections, i;
+       var translatableSections, i, model;
+
        translatableSections = this.sourcePage.getTranslatableSections();
        // Use this for creating translation units. Once done, trigger 
translation view to
        // render them in the appropriate columns.
        // Use <section> tags to hold the sections in both columns;
        for ( i = 0; i < translatableSections.length; i++ ) {
+               model = mw.cx.dm.modelRegistry.matchElement( 
translatableSections[ i ] );
+               if ( !model ) {
+                       continue;
+               }
                this.translationUnits[ i ] = 
mw.cx.dm.translationUnitFactory.create(
-                       'section',
+                       model,
                        this.config,
                        this,
                        translatableSections[ i ] );
diff --git a/modules/dm/translationunits/mw.cx.dm.ImageTranslationUnit.js 
b/modules/dm/translationunits/mw.cx.dm.ImageTranslationUnit.js
index 04d6f2b..27e549e 100644
--- a/modules/dm/translationunits/mw.cx.dm.ImageTranslationUnit.js
+++ b/modules/dm/translationunits/mw.cx.dm.ImageTranslationUnit.js
@@ -10,13 +10,50 @@
  */
 mw.cx.dm.ImageTranslationUnit = function CXImageTranslationUnit( config, 
translation, sourceDocument, targetDocument ) {
        mw.cx.dm.ImageTranslationUnit.super.call( this, config, translation, 
sourceDocument, targetDocument );
+       this.sourceResource = null;
+       this.sourceImage = null;
+       this.targetImage = null;
+       this.targetResource = null;
 };
 
 /* Inheritance */
-OO.inheritClass( mw.cx.dm.ImageTranslationUnit, mw.cx.dm.TranslationUnit );
+OO.inheritClass( mw.cx.dm.ImageTranslationUnit, 
mw.cx.dm.SectionTranslationUnit );
 
 mw.cx.dm.ImageTranslationUnit.static.name = 'Image';
 mw.cx.dm.ImageTranslationUnit.static.tags = [ 'figure' ];
 mw.cx.dm.ImageTranslationUnit.static.matchRdfaTypes = [ 'mw:Image/Thumb' ];
 
+mw.cx.dm.ImageTranslationUnit.prototype.adapt = function () {
+       this.sourceImage = this.sourceDocument.getElementsByTagName( 'img' )[ 0 
];
+       this.sourceResource = this.sourceImage.getAttribute( 'resource' );
+       this.targetDocument = this.sourceDocument.cloneNode( true );
+       this.setTargetId();
+       this.targetImage = this.targetDocument.getElementsByTagName( 'img' )[ 0 
];
+       this.targetResource = this.targetImage.getAttribute( 'resource' );
+       return this.requestManager.getNamespaceAlias( this.targetLanguage, 
'File' )
+               .then( function( namespaceAlias ) {
+                       this.targetResource = this.sourceResource.replace(
+                               /(\.\/)*(.+)(:)/g,
+                               '$1' + namespaceAlias + '$3'
+                       );
+                       this.targetImage.setAttribute( 'resource', 
this.targetResource );
+               }.bind( this ) );
+};
+
+/**
+ * Check if an image is coming from Commons or not.
+ * Uses the URL pattern of the common file repository to determine
+ * whether the image is stored there.
+ *
+ * @param {string} imageSrc
+ * @return {boolean}
+ */
+mw.cx.dm.ImageTranslationUnit.prototype.isCommonsImage = function( imageSrc ) {
+       if ( imageSrc.indexOf( '//upload.wikimedia.org/wikipedia/commons/' ) 
=== 0 ) {
+               return true;
+       }
+
+       return false;
+};
+
 mw.cx.dm.modelRegistry.register( mw.cx.dm.ImageTranslationUnit );
diff --git a/modules/mw.cx.MwApiRequestManager.js 
b/modules/mw.cx.MwApiRequestManager.js
index 3fe41bf..5a8a3d0 100644
--- a/modules/mw.cx.MwApiRequestManager.js
+++ b/modules/mw.cx.MwApiRequestManager.js
@@ -26,6 +26,7 @@
        this.imageCache = {};
        this.titlePairCache = {};
        this.categoryCache = {};
+       this.namespaceCache = {};
 };
 
 /**
@@ -64,6 +65,14 @@
                siteMapper: this.siteMapper
        } );
        this.categoryCache[ this.targetLanguage ] = new mw.cx.CategoryCache( {
+               language: this.targetLanguage,
+               siteMapper: this.siteMapper
+       } );
+       this.namespaceCache[ this.sourceLanguage ] = new mw.cx.NamespaceCache( {
+               language: this.targetLanguage,
+               siteMapper: this.siteMapper
+       } );
+       this.namespaceCache[ this.targetLanguage ] = new mw.cx.NamespaceCache( {
                language: this.targetLanguage,
                siteMapper: this.siteMapper
        } );
@@ -117,6 +126,11 @@
        return this.titlePairCache[ language ].get( title );
 };
 
+/**
+ * @param {string} language Language code
+ * @param {string} title Title
+ * @return {jQuery.Promise} Promise that will be resolved with the data once 
it's available
+ */
 mw.cx.MwApiRequestManager.prototype.getTitlePairs = function ( language, title 
) {
        if ( !this.titlePairCache[ language ] ) {
                throw Error( '[CX] TitlePairCache not initialized for ' + 
language );
@@ -124,9 +138,26 @@
        return this.titlePairCache[ language ].get( title );
 };
 
+/**
+ * @param {string} language Language code
+ * @param {string} title Title
+ * @return {jQuery.Promise} Promise that will be resolved with the data once 
it's available
+ */
 mw.cx.MwApiRequestManager.prototype.getCategories = function ( language, title 
) {
        if ( !this.categoryCache[ language ] ) {
                throw Error( '[CX] CategoryCache not initialized for ' + 
language );
        }
        return this.categoryCache[ language ].get( title );
 };
+
+/**
+ * @param {string} language Language code
+ * @param {string} canonicalNamespace Canonical namespace
+ * @return {jQuery.Promise} Promise that will be resolved with the data once 
it's available
+ */
+mw.cx.MwApiRequestManager.prototype.getNamespaceAlias = function ( language, 
canonicalNamespace ) {
+       if ( !this.namespaceCache[ language ] ) {
+               throw Error( '[CX] namespaceCache not initialized for ' + 
language );
+       }
+       return this.namespaceCache[ language ].get( canonicalNamespace );
+};
diff --git a/modules/ui/mw.cx.ui.TranslationView.js 
b/modules/ui/mw.cx.ui.TranslationView.js
index 169ef4a..c85cf06 100644
--- a/modules/ui/mw.cx.ui.TranslationView.js
+++ b/modules/ui/mw.cx.ui.TranslationView.js
@@ -89,7 +89,9 @@
        var i, j, translationUnits, subTranslationUnits, subTranslationUnit, 
translationUnit;
        translationUnits = this.translation.getTranslationUnits();
        for ( i = 0; i < translationUnits.length; i++ ) {
-               translationUnit = mw.cx.ui.translationUnitFactory.create( 
'section', translationUnits[ i ], this, this.config );
+               translationUnit = mw.cx.ui.translationUnitFactory.create(
+                       translationUnits[ i ].constructor.static.name, 
translationUnits[ i ], this, this.config
+               );
                translationUnit.render( i );
                subTranslationUnits = translationUnits[ i 
].getTranslationUnits();
                for ( j = 0; j < subTranslationUnits.length; j++ ) {
diff --git a/modules/ui/translationunits/mw.cx.ui.ImageTranslationUnit.js 
b/modules/ui/translationunits/mw.cx.ui.ImageTranslationUnit.js
index 2632095..24a6231 100644
--- a/modules/ui/translationunits/mw.cx.ui.ImageTranslationUnit.js
+++ b/modules/ui/translationunits/mw.cx.ui.ImageTranslationUnit.js
@@ -16,13 +16,36 @@
 };
 
 /* Setup */
-OO.inheritClass( mw.cx.ui.ImageTranslationUnit, mw.cx.ui.TranslationUnit );
+OO.inheritClass( mw.cx.ui.ImageTranslationUnit, 
mw.cx.ui.SectionTranslationUnit );
 OO.mixinClass( mw.cx.ui.ImageTranslationUnit, 
mw.cx.ui.mixin.AlignableTranslationUnit );
 
 mw.cx.ui.ImageTranslationUnit.static.name = 'Image';
-
+mw.cx.ui.ImageTranslationUnit.static.tags = [ 'figure' ];
 mw.cx.ui.ImageTranslationUnit.static.matchRdfaTypes = [ 'mw:Image/Thumb' ];
 
 mw.cx.ui.ImageTranslationUnit.static.highlightClass = 'cx-image-highlight';
 
+mw.cx.ui.ImageTranslationUnit.prototype.init = function () {
+       if ( !this.translationUnitModel.sourceDocument.id ) {
+               throw Error( '[CX] Invalid source document' );
+       }
+       this.$sourceSection = $( this.translationUnitModel.sourceDocument );
+       this.$translationSection = this.getTranslationSection();
+       this.adapt();
+       this.listen();
+};
+
+mw.cx.ui.ImageTranslationUnit.prototype.adapt = function () {
+       // Adapt in general will be asynchronous operation
+       this.translationUnitModel.adapt();
+       this.setContent( this.translationUnitModel.targetDocument );
+};
+
+/**
+ * @inheritDoc
+ */
+mw.cx.ui.ImageTranslationUnit.prototype.onParentTranslationStarted = function 
() {
+       this.init();
+};
+
 mw.cx.ui.translationUnitFactory.register( mw.cx.ui.ImageTranslationUnit );

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

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

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

Reply via email to