Nikerabbit has submitted this change and it was merged.

Change subject: Templates: Use wikidata to find template in target wiki
......................................................................


Templates: Use wikidata to find template in target wiki

The existing check was simple title search in target wiki. This
was problematic and not helpful. Replace it with langlinks api
which will resolve the titles using wikidata connection information.

Testplan:
Translate Giovan Battista Perasso from it to en. Inspect the reference
"Gattera 2007, pagg..". The translation should be using 'Harward
citation no brackets'

Bug: T126957
Bug: T125579
Bug: T112739
Change-Id: I346d5653b7f6a4756f59e99881242d1cf2d17fb6
---
M modules/tools/ext.cx.tools.template.js
1 file changed, 25 insertions(+), 28 deletions(-)

Approvals:
  Nikerabbit: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/tools/ext.cx.tools.template.js 
b/modules/tools/ext.cx.tools.template.js
index 2d86ae2..7c9f9fc 100644
--- a/modules/tools/ext.cx.tools.template.js
+++ b/modules/tools/ext.cx.tools.template.js
@@ -11,7 +11,7 @@
        'use strict';
 
        var targetTemplateNamespace = {},
-               cachedTemplates = {};
+               cachedTemplateRequests = {};
 
        /**
         * TemplateTool encapsulates the handling of templates in translation.
@@ -72,7 +72,7 @@
         */
        function getTemplateNamespaceTranslation( language ) {
                if ( targetTemplateNamespace[ language ] ) {
-                       return $.Deferred().resolve( targetTemplateNamespace[ 
language ] );
+                       return $.Deferred().resolve( targetTemplateNamespace[ 
language ] ).promise();
                }
 
                // TODO: Refactor to avoid global reference
@@ -185,8 +185,7 @@
         * @return {jQuery.Promise}
         */
        TemplateTool.prototype.process = function () {
-               var templateTool = this,
-                       deferred = $.Deferred();
+               var self = this;
 
                this.templateData = this.getTemplateData();
                this.templateMapping = this.getTemplateMapping();
@@ -197,29 +196,25 @@
                        // An example: {{Version |o |1.1}}{{efn-ua |Due to an 
incident ...<ref name="releases" />}}
                        // in enwiki:Debian, Timeline table.
                        mw.log( '[CX] Skipping template. Missing template 
data.' );
-                       return;
+                       return $.Deferred().resolve().promise();
                }
 
                this.templateTitle = this.templateData.parts[ 0 
].template.target.wt;
 
-               this.getTargetTemplate()
-                       .done( function ( targetTitleData ) {
-                               var pageId = Object.keys( targetTitleData.pages 
)[ 0 ];
-
-                               templateTool.markReadOnly();
-                               if ( templateTool.templateMapping ) {
-                                       templateTool.adapt();
+               return this.getTargetTemplate()
+                       .then( function ( targetTitle ) {
+                               self.markReadOnly();
+                               if ( self.templateMapping ) {
+                                       self.adapt();
+                               } else if ( targetTitle ) {
+                                       self.adaptTitle( targetTitle );
                                } else {
-                                       templateTool.adaptTitle( 
targetTitleData.pages[ pageId ].title );
+                                       self.deconstruct();
                                }
-                               deferred.resolve();
                        } )
                        .fail( function () {
-                               templateTool.deconstruct();
-                               deferred.resolve();
+                               self.deconstruct();
                        } );
-
-               return deferred.promise();
        };
 
        /**
@@ -233,17 +228,19 @@
                var api, request,
                        cacheKey = mw.cx.targetLanguage + ':' + 
this.templateTitle;
 
-               if ( cachedTemplates[ cacheKey ] ) {
-                       return cachedTemplates[ cacheKey ].promise();
+               if ( cachedTemplateRequests[ cacheKey ] ) {
+                       return cachedTemplateRequests[ cacheKey ];
                }
 
                // TODO: Avoid direct access to globals
-               api = mw.cx.siteMapper.getApi( mw.cx.targetLanguage );
+               api = mw.cx.siteMapper.getApi( mw.cx.sourceLanguage );
 
                // Note that we use canonical namespace 'Template' for title.
                request = api.get( {
                        action: 'query',
                        titles: 'Template:' + this.templateTitle,
+                       prop: 'langlinks',
+                       lllang: mw.cx.siteMapper.getWikiDomainCode( 
mw.cx.targetLanguage ),
                        redirects: true,
                        format: 'json'
                }, {
@@ -251,17 +248,17 @@
                        // This prevents warnings about the unrecognized 
parameter "_"
                        cache: true
                } ).then( function ( response ) {
-                       var pageId = Object.keys( response.query.pages )[ 0 ];
-
-                       if ( pageId === '-1' ) {
-                               return $.Deferred().reject().promise();
+                       var page,
+                               pageId = Object.keys( response.query.pages )[ 0 
];
+                       page = response.query.pages[ pageId ];
+                       if ( page && page.langlinks ) {
+                               return page.langlinks[ 0 ][ '*' ];
                        }
-                       return response.query;
                } );
 
-               cachedTemplates[ cacheKey ] = request;
+               cachedTemplateRequests[ cacheKey ] = request;
 
-               return request.promise();
+               return request;
        };
 
        /**

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I346d5653b7f6a4756f59e99881242d1cf2d17fb6
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

Reply via email to