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

Change subject: WIP: Working on template stuff
......................................................................

WIP: Working on template stuff

Change-Id: Ic90d984c92728bbbb1d245e863e8c94f047dfd56
---
A lib/mw/TemplateDataRequest.js
M lib/translationunits/MWTemplate.js
2 files changed, 85 insertions(+), 1 deletion(-)


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

diff --git a/lib/mw/TemplateDataRequest.js b/lib/mw/TemplateDataRequest.js
new file mode 100644
index 0000000..9a5ff61
--- /dev/null
+++ b/lib/mw/TemplateDataRequest.js
@@ -0,0 +1,29 @@
+'use strict';
+
+const BatchedAPIRequest = require( './BatchedAPIRequest' );
+
+class TemplateDataRequest extends BatchedAPIRequest {
+       processPage( page ) {
+               return page;
+       }
+
+       /**
+        * @param {string[]} titles
+        * @return {Promise}
+        */
+       getRequestPromise( titles ) {
+               let domain, query;
+
+               query = {
+                       action: 'templatedata',
+                       titles: titles.join( '|' ),
+                       redirects: true
+               };
+               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 = TemplateDataRequest;
diff --git a/lib/translationunits/MWTemplate.js 
b/lib/translationunits/MWTemplate.js
index 4540679..fbc4632 100644
--- a/lib/translationunits/MWTemplate.js
+++ b/lib/translationunits/MWTemplate.js
@@ -7,10 +7,65 @@
  * TODO: attempt adapting multipart templates as well
  * TODO: write a template data request wrapper
  * TODO: move (or copy for now) template mappings from CX to CXServer
+ * See also https://www.mediawiki.org/wiki/Specs/HTML/1.5.0#Template_markup
  */
-
+/*
+ * Simple example {{Babel|fi}} in Finnish Wikipedia results in this html in VE:
+ * <span about="#mwt3" typeof="mw:Transclusion" data-mw="..." id="mwSQ">
+ * </span><table ... about="#mwt3" id="mwSg">...</table>
+ *
+ * data-mw being:
+ * {
+ *   "parts":[
+ *    {
+ *       "template":{
+ *         "target":{
+ *           "wt":"Babel",
+ *           "href":"./Malline:Babel"
+ *         },
+ *         "params":{
+ *           "1":{
+ *             "wt":"fi"
+ *           }
+ *         },
+ *         "i":0
+ *       }
+ *     }
+ *   ]
+ * }
+ */
 class MWTemplate extends TranslationUnit {
        adapt() {
+               let data;
+
+               if ( !this.node.attributes[ 'data-mw' ] ) {
+                       this.node.attributes[ 'data-cx' ] = JSON.stringify( { 
adapted: false } );
+                       this.log( 'warn', 'Not-adapting a template node without 
data-mw: ' + this.node.attributes.id );
+                       return this.node;
+               }
+
+               try {
+                       data = JSON.parse( this.node.attributes[ 'data-mw' ] );
+               } catch ( e ) {
+                       this.node.attributes[ 'data-cx' ] = JSON.stringify( { 
adapted: false } );
+                       this.log( 'error', 'Not-adapting a template node with 
non-JSON data-mw: ' + this.node.attributes.id );
+                       return this.node;
+               }
+
+               const numberOfParts = data.parts && data.parts.length;
+               if ( numberOfParts < 1 ) {
+                       this.node.attributes[ 'data-cx' ] = JSON.stringify( { 
adapted: false } );
+                       this.log( 'error', 'Not-adapting a template node with 
empty data-mw.parts: ' + this.node.attributes.id );
+                       return this.node;
+               }
+
+               if ( numberOfParts > 1 ) {
+                       this.log( 'debug', 'Skipping a multipart template node 
for now: ' + this.node.attributes.id );
+                       this.node.attributes[ 'data-cx' ] = JSON.stringify( { 
adapted: false } );
+                       return this.node;
+               }
+
+               // let templateName = data.parts[ 0 ].target.href;
                return this.node;
        }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic90d984c92728bbbb1d245e863e8c94f047dfd56
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/cxserver
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>

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

Reply via email to