[MediaWiki-commits] [Gerrit] mediawiki...cxserver[master]: Provide title info- description, image along with the adapta...

2017-08-25 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/373255 )

Change subject: Provide title info- description, image along with the adaptation
..


Provide title info- description, image along with the adaptation

Since the comparison of HTML after adaptation is very inefficient
and difficult to comprehend, I changed it to just string length
comparison. In a follow up commit, we will try to write unit test
against the TranslationUnit classes by passing the node Object
directly.

Bug: T170674
Change-Id: I22640d11baf70e5a16103df77eabb17f9290e5f7
---
M lib/mw/ApiRequestManager.js
A lib/mw/TitleInfoRequest.js
M lib/translationunits/MWLink.js
M test/adaptation/AdaptationTest.js
4 files changed, 90 insertions(+), 31 deletions(-)

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



diff --git a/lib/mw/ApiRequestManager.js b/lib/mw/ApiRequestManager.js
index dd0e799..e93246a 100644
--- a/lib/mw/ApiRequestManager.js
+++ b/lib/mw/ApiRequestManager.js
@@ -1,6 +1,7 @@
 'use strict';
 
 const TitlePairRequest = require( './TitlePairRequest.js' ),
+   TitleInfoRequest = require( './TitleInfoRequest.js' ),
ApiRequest = require( './ApiRequest.js' );
 
 class MWApiRequestManager {
@@ -55,6 +56,24 @@
return canonicalNamespace;
} );
}
+
+   /**
+* Creates a title info request for a given given language
+* @param {string} title Title for which we want to get the information
+* @param {string} language Wiki language for the title
+* @return {Promise}
+*/
+   titleInfoRequest( title, language ) {
+   if ( !MWApiRequestManager.titleInfoCache[ language ] ) {
+   MWApiRequestManager.titleInfoCache[ language ] = new 
TitleInfoRequest( {
+   sourceLanguage: language,
+   context: this.context
+   } );
+   }
+
+   return MWApiRequestManager.titleInfoCache[ language ].get( 
title );
+   }
+
 }
 
 /**
@@ -65,4 +84,10 @@
  */
 MWApiRequestManager.titlePairCache = new Map();
 
+/**
+ * Cached instances for TitleInfoRequest for each language.
+ * @type {Map}
+ */
+MWApiRequestManager.titleInfoCache = new Map();
+
 module.exports = MWApiRequestManager;
diff --git a/lib/mw/TitleInfoRequest.js b/lib/mw/TitleInfoRequest.js
new file mode 100644
index 000..242d4dc
--- /dev/null
+++ b/lib/mw/TitleInfoRequest.js
@@ -0,0 +1,38 @@
+'use strict';
+
+const BatchedAPIRequest = require( './BatchedAPIRequest.js' );
+
+/**
+ * Fetches information about titles in batches.
+ */
+class TitleInfoRequest extends BatchedAPIRequest {
+   processPage( page ) {
+   return page;
+   }
+
+   /**
+* @param {string[]} titles
+* @return {Promise}
+*/
+   getRequestPromise( titles ) {
+   let domain, query;
+
+   query = {
+   action: 'query',
+   prop: 'info|pageprops|pageimages|pageterms',
+   pithumbsize: 80,
+   pilimit: titles.length,
+   wbptterms: 'description',
+   ppprop: 'disambiguation',
+   titles: titles.join( '|' ),
+   redirects: true,
+   'continue': ''
+   };
+   domain = this.getDomain( this.sourceLanguage );
+   // We use POST here because the titles when joined will result 
in a longer query string
+   // that GET requests cannot process sometimes.
+   return this.mwPost( domain, query );
+   }
+}
+
+module.exports = TitleInfoRequest;
diff --git a/lib/translationunits/MWLink.js b/lib/translationunits/MWLink.js
index 44b65f3..c6c7bc5 100644
--- a/lib/translationunits/MWLink.js
+++ b/lib/translationunits/MWLink.js
@@ -5,33 +5,40 @@
MWApiRequestManager = require( '../mw/ApiRequestManager.js' );
 
 class MWLink extends TranslationUnit {
-   constructor( node, sourceLanguage, targetLanguage, context ) {
-   super( node, sourceLanguage, targetLanguage, context );
-   // Nothing else?
-   }
 }
 
 MWLink.matchTagNames = [ 'a' ];
 MWLink.matchRdfaTypes = [ 'mw:WikiLink' ];
 
 MWLink.prototype.adapt = cxutil.async( function* () {
-   var linkPairInfo;
+   var linkPairInfo, adaptationInfo, sourceTitleInfoRequest,
+   apiRequestManager = new MWApiRequestManager( this.context );
 
-   linkPairInfo = yield new MWApiRequestManager( this.context )
-   .titlePairRequest( this.node.attributes.href, 
this.sourceLanguage, this.targetLanguage );
+   adaptationInfo = {
+   adapted: false
+   };
+
+   // Do not wait(pause using yield) for sourceTitleInfoRequest info, but 
start the 

[MediaWiki-commits] [Gerrit] mediawiki...cxserver[master]: Provide title info- description, image along with the adapta...

2017-08-23 Thread Santhosh (Code Review)
Santhosh has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/373255 )

Change subject: Provide title info- description, image along with the adaptation
..

Provide title info- description, image along with the adaptation

Since the comparison of HTML after adaptation is very inefficient
and difficult to comprehend, I changed it to just string length
comparison. In a follow up commit, we will try to write unit test
against the TranslationUnit classes by passing the node Object
directly.

Bug: T170674
Change-Id: I22640d11baf70e5a16103df77eabb17f9290e5f7
---
M lib/mw/ApiRequestManager.js
A lib/mw/TitleInfoRequest.js
M lib/translationunits/MWLink.js
M test/adaptation/AdaptationTest.js
4 files changed, 90 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/cxserver 
refs/changes/55/373255/1

diff --git a/lib/mw/ApiRequestManager.js b/lib/mw/ApiRequestManager.js
index dd0e799..33adac2 100644
--- a/lib/mw/ApiRequestManager.js
+++ b/lib/mw/ApiRequestManager.js
@@ -1,6 +1,7 @@
 'use strict';
 
 const TitlePairRequest = require( './TitlePairRequest.js' ),
+   TitleInfoRequest = require( './TitleInfoRequest.js' ),
ApiRequest = require( './ApiRequest.js' );
 
 class MWApiRequestManager {
@@ -55,6 +56,24 @@
return canonicalNamespace;
} );
}
+
+   /**
+* Creates a title info request for a given given language
+* @param {string} title Title for which we want to get the information
+* @param {string} language Language for the title
+* @return {Promise}
+*/
+   titleInfoRequest( title, language ) {
+   if ( !MWApiRequestManager.titleInfoCache[ language ] ) {
+   MWApiRequestManager.titleInfoCache[ language ] = new 
TitleInfoRequest( {
+   sourceLanguage: language,
+   context: this.context
+   } );
+   }
+
+   return MWApiRequestManager.titleInfoCache[ language ].get( 
title );
+   }
+
 }
 
 /**
@@ -65,4 +84,10 @@
  */
 MWApiRequestManager.titlePairCache = new Map();
 
+/**
+ * Cached instances for TitleInfoRequest for each language.
+ * @type {Map}
+ */
+MWApiRequestManager.titleInfoCache = new Map();
+
 module.exports = MWApiRequestManager;
diff --git a/lib/mw/TitleInfoRequest.js b/lib/mw/TitleInfoRequest.js
new file mode 100644
index 000..af7441b
--- /dev/null
+++ b/lib/mw/TitleInfoRequest.js
@@ -0,0 +1,42 @@
+'use strict';
+
+/**
+ * ContentTranslation Title Info request
+ *
+ */
+const BatchedAPIRequest = require( './BatchedAPIRequest.js' );
+
+/**
+ * Fetches information about titles in batches.
+ *
+ * @class
+ * @extends BatchedAPIRequest
+ * @constructor
+ * @param {Object} config Configuration
+ */
+class TitleInfoRequest extends BatchedAPIRequest {
+   processPage( page ) {
+   return page;
+   }
+
+   getRequestPromise( subqueue ) {
+   var domain, query;
+   query = {
+   action: 'query',
+   prop: 'info|pageprops|pageimages|pageterms',
+   pithumbsize: 80,
+   pilimit: subqueue.length,
+   wbptterms: 'description',
+   ppprop: 'disambiguation',
+   titles: subqueue.join( '|' ),
+   redirects: true,
+   'continue': ''
+   };
+   domain = this.getDomain( this.sourceLanguage );
+   // We use POST here because the titles when joined will result 
in a longer query string
+   // that GET requests cannot process sometimes.
+   return this.mwPost( domain, query );
+   }
+}
+
+module.exports = TitleInfoRequest;
diff --git a/lib/translationunits/MWLink.js b/lib/translationunits/MWLink.js
index 44b65f3..d28269d 100644
--- a/lib/translationunits/MWLink.js
+++ b/lib/translationunits/MWLink.js
@@ -5,33 +5,40 @@
MWApiRequestManager = require( '../mw/ApiRequestManager.js' );
 
 class MWLink extends TranslationUnit {
-   constructor( node, sourceLanguage, targetLanguage, context ) {
-   super( node, sourceLanguage, targetLanguage, context );
-   // Nothing else?
-   }
 }
 
 MWLink.matchTagNames = [ 'a' ];
 MWLink.matchRdfaTypes = [ 'mw:WikiLink' ];
 
 MWLink.prototype.adapt = cxutil.async( function* () {
-   var linkPairInfo;
+   var linkPairInfo, adaptationInfo, sourceTitleInfoRequest,
+   apiRequestManager = new MWApiRequestManager( this.context );
 
-   linkPairInfo = yield new MWApiRequestManager( this.context )
-   .titlePairRequest( this.node.attributes.href, 
this.sourceLanguage, this.targetLanguage );
+   adaptationInfo = {
+   adapted: false
+   };
+
+   //