[MediaWiki-commits] [Gerrit] mediawiki...cxserver[master]: Template adaptation: Implement template param name matching

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

Change subject: Template adaptation: Implement template param name matching
..


Template adaptation: Implement template param name matching

Algorithm:
1. Get template data for both source and target
2. Get aliases array for each parameter in source template, also
   add the param name to that and form aliases+name set
3. For each param in target template data, form a similar set of
   aliases and param.
4. Find intersection of above two sets.
5. If the intersection set is not empty, the source param maps to
   the param name we located in target template data

Tests for name matching added.

TODO:
1. Use the param mapping to build target template definition.
2. Update tests for MWTemplate.js
3. If template data not found, try to extract info from source code
   of template like we do in CX1

Bug: T162114
Change-Id: I4ea9ae0dc902fecba0d300864e40cae6595c61b6
---
A lib/adaptation/TemplateParameterMapper.js
M lib/adaptation/TemplateTransclusion.js
A test/adaptation/TemplateParameterMapper.test.js
M test/translationunits/MWTemplate.test.js
M test/translationunits/MWTemplate.test.json
5 files changed, 281 insertions(+), 49 deletions(-)

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



diff --git a/lib/adaptation/TemplateParameterMapper.js 
b/lib/adaptation/TemplateParameterMapper.js
new file mode 100644
index 000..ddb05eb
--- /dev/null
+++ b/lib/adaptation/TemplateParameterMapper.js
@@ -0,0 +1,65 @@
+'use strict';
+
+class TemplateParameterMapper {
+   constructor( sourceParams, sourceTemplateData, targetTemplateData ) {
+   this.sourceParams = sourceParams;
+   this.sourceTemplateData = sourceTemplateData;
+   this.targetTemplateData = targetTemplateData;
+   this.parameterMap = {};
+   }
+
+   getAdaptedParameters() {
+   // TODO: Use parameter map to copy values of sourceParams
+   return this.sourceParams;
+   }
+
+   /**
+* Find target template parameter mapping for all the parameters in 
source template.
+* @return {Object} Object with mapping from each source param to param 
name in target template.
+*/
+   getParameterMap() {
+   for ( let name in this.sourceParams ) {
+   if ( !isNaN( name ) ) {
+   // Unnamed parameters, which are named 1, 2, 
3...
+   this.parameterMap[ name ] = name;
+   continue;
+   }
+
+   if ( !this.targetTemplateData.params ) {
+   // No params in target template data definition
+   continue;
+   }
+
+   // Try to locate this source param in the source 
template data definition
+   let normalizedKey = name.trim().toLowerCase().replace( 
/[\s+_-]+/g, '' );
+   let sourceTemplateParam = 
this.sourceTemplateData.params[ name ] || this.sourceTemplateData.params[ 
normalizedKey ];
+   let sourceAliases = sourceTemplateParam ? 
sourceTemplateParam.aliases : [];
+   let sourceKeyAndAliases = new Set( sourceAliases );
+   sourceKeyAndAliases.add( name );
+   sourceKeyAndAliases.add( normalizedKey );
+
+   // Search in the aliases for a match - case insensitive.
+   for ( let paramName in this.targetTemplateData.params ) 
{
+   const param = this.targetTemplateData.params[ 
paramName ];
+
+   let targetKeyAndAliases = new Set( 
param.aliases );
+   targetKeyAndAliases.add( paramName );
+
+   // Find intersection of sourceKeyAndAliases and 
targetKeyAndAliases
+   let intersection = new Set( [ 
...targetKeyAndAliases ].filter( key => {
+   let normalizedKey = 
key.trim().toLowerCase().replace( /[\s+_-]+/g, '' );
+   return sourceKeyAndAliases.has( 
normalizedKey );
+   } ) );
+
+   if ( intersection.size > 0 ) {
+   // Found a match
+   this.parameterMap[ name ] = paramName;
+   break;
+   }
+   }
+   }
+   return this.parameterMap;
+   }
+}
+
+module.exports = TemplateParameterMapper;
diff --git a/lib/adaptation/TemplateTransclusion.js 
b/lib/adaptation/TemplateTransclusion.js
index 5e26158..c2f39a1 100644
--- 

[MediaWiki-commits] [Gerrit] mediawiki...cxserver[master]: Template adaptation: Implement template param name matching

2017-09-29 Thread Santhosh (Code Review)
Santhosh has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/381418 )

Change subject: Template adaptation: Implement template param name matching
..

Template adaptation: Implement template param name matching

Algorithm:
1. Get template data for both source and target
2. Get aliases array for each parameter in source template, also
   add the param name to that and form aliases+name set
3. For each param in target template data, form a similar set of
   aliases and param.
4. Find intersection of above two sets.
5. If the intersection set is not empty, the source param maps to
   the param name we located in target template data

Tests for name matching added.

TODO:
1. Use the param mapping to build target template definition.
2. Update tests for MWTemplate.js

Bug: T162114
Change-Id: I4ea9ae0dc902fecba0d300864e40cae6595c61b6
---
D config.yaml
A config.yaml
A lib/adaptation/TemplateParameterMapper.js
M lib/adaptation/TemplateTransclusion.js
A test/adaptation/TemplateParameterMapper.test.js
M test/translationunits/MWTemplate.test.json
6 files changed, 357 insertions(+), 49 deletions(-)


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

diff --git a/config.yaml b/config.yaml
deleted file mode 12
index c11eec8..000
--- a/config.yaml
+++ /dev/null
@@ -1 +0,0 @@
-config.dev.yaml
\ No newline at end of file
diff --git a/config.yaml b/config.yaml
new file mode 100644
index 000..36280af
--- /dev/null
+++ b/config.yaml
@@ -0,0 +1,93 @@
+# Number of worker processes to spawn.
+# Set to 0 to run everything in a single process without clustering.
+# Use ncpu to run as many workers as there are CPU units
+num_workers: 0
+
+# Log error messages and gracefully restart a worker if v8 reports that it
+# uses more heap (note: not RSS) than this many megabytes.
+worker_heap_limit_mb: 250
+
+# Logger info
+logging:
+  level: trace
+#  streams:
+#  # Use gelf-stream -> logstash
+#- type: gelf
+#host: logstash1003.eqiad.wmnet
+#port: 12201
+
+# Statsd metrics reporter
+metrics:
+  type: log
+  #host: localhost
+  #port: 8125
+
+services:
+  - name: cxserver
+# a relative path or the name of an npm package, if different from name
+module: ./app.js
+# optionally, a version constraint of the npm package
+# version: ^0.4.0
+# per-service config
+conf:
+  port: 8080
+  # interface: localhost # uncomment to only listen on localhost
+  # More per-service config settings
+  # The location of the spec, defaults to spec.yaml if not specified
+  # spec: ./spec.yaml
+  # allow cross-domain requests to the API (default *)
+  cors: '*'
+  # to disable use:
+  # cors: false
+  # to restrict to a particular domain, use:
+  # cors: restricted.domain.org
+  # URL of the outbound proxy to use (complete with protocol)
+  # proxy: http://my.proxy.org:8080
+  # the list of domains for which not to use the proxy defined above
+  # no_proxy_list:
+  #   - domain1.com
+  #   - domain2.org
+  user_agent: cxserver
+  # Mediawiki host name. Example {lang}.wikisource.org which get expanded 
internally to
+  # es.wikisource.org in a spanish language context.
+  # Do not prefix with http or https://
+  # mw_host: '{lang}.wikipedia.org'
+  mwapi_req:
+body: "{{request.body}}"
+query: "{{ default(request.query, {}) }}"
+headers:
+  host: "{{request.params.domain}}"
+  user-agent: "{{user-agent}}"
+method: post
+uri: "https://{{domain}}/w/api.php;
+  restbase_req:
+method: '{{request.method}}'
+uri: https://{{domain}}/api/rest_v1/{+path}
+query: '{{ default(request.query, {}) }}'
+headers: '{{request.headers}}'
+body: '{{request.body}}'
+  jwt:
+secret: 'secret'
+algorithms:
+  - HS256
+  languages: config/languages.yaml
+  mt:
+Apertium:
+  api: http://apertium.wmflabs.org
+  languages: config/Apertium.yaml
+Yandex:
+  api: https://translate.yandex.net
+  key: null
+  languages: config/Yandex.yaml
+Youdao:
+  api: https://fanyi.youdao.com/paidapi/fanyiapi
+  key: null
+  languages: config/Youdao.yaml
+Matxin:
+  api: http://matxin.elhuyar.eus/API
+  languages: config/Matxin.yaml
+  dictionary:
+Dictd:
+  languages: config/Dictd.yaml
+JsonDict:
+  languages: config/JsonDict.yaml
diff --git a/lib/adaptation/TemplateParameterMapper.js 
b/lib/adaptation/TemplateParameterMapper.js
new file mode 100644
index 000..cb9a1f0
--- /dev/null
+++ b/lib/adaptation/TemplateParameterMapper.js
@@ -0,0 +1,59 @@
+class TemplateParameterMapper {
+   constructor( sourceParams, sourceTemplateData,