jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/369336 )

Change subject: Category adaptation and tests
......................................................................


Category adaptation and tests

Bug: T170674
Change-Id: Ie31bc774472c73914c50939931da19bde4e773c8
---
M lib/lineardoc/TextBlock.js
A lib/translationunits/MWCategory.js
M lib/translationunits/index.js
M test/adaptation/AdaptationTests.json
4 files changed, 71 insertions(+), 10 deletions(-)

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



diff --git a/lib/lineardoc/TextBlock.js b/lib/lineardoc/TextBlock.js
index abf417c..1c060ab 100644
--- a/lib/lineardoc/TextBlock.js
+++ b/lib/lineardoc/TextBlock.js
@@ -395,11 +395,11 @@
        // timeout. Pausing execution here will cause that debounce handler to 
be called.
        // So we avoid that pausing by just using an array of promises.
        this.textChunks.forEach( ( chunk ) => {
-               var tagPromises = [],
+               var tagPromises = [], adapter, adaptPromise,
                        tags = chunk.tags;
 
                tags.forEach( ( tag ) => {
-                       const adapter = getAdapter( tag );
+                       adapter = getAdapter( tag );
                        if ( adapter ) {
                                tagPromises.push( adapter.adapt() );
                        }
@@ -407,13 +407,27 @@
 
                textChunkPromises.push( Promise.all( tagPromises ) );
 
-               if ( chunk.inlineContent && chunk.inlineContent.adapt ) {
-                       textChunkPromises.push( ( ( chunk ) => 
chunk.inlineContent.adapt( getAdapter )
-                               .then( ( adaptedInlineContent ) => {
-                                       chunk.inlineContent = 
adaptedInlineContent;
-                               } ) )( chunk )
-                       );
+               if ( chunk.inlineContent ) {
+                       if ( chunk.inlineContent.adapt ) {
+                               // Inline content is a sub document.
+                               adaptPromise = chunk.inlineContent.adapt( 
getAdapter );
+                       } else {
+                               // Inline content is inline empty tag. Examples 
are link, meta etc.
+                               adapter = getAdapter( chunk.inlineContent );
+                               if ( adapter ) {
+                                       adaptPromise = adapter.adapt();
+                               }
+                       }
+
+                       if ( adaptPromise ) {
+                               textChunkPromises.push( ( ( chunk ) => 
adaptPromise
+                                       .then( ( adaptedInlineContent ) => {
+                                               chunk.inlineContent = 
adaptedInlineContent;
+                                       } ) )( chunk )
+                               );
+                       }
                }
+
        } );
 
        return Promise.all( textChunkPromises ).then( () => this );
diff --git a/lib/translationunits/MWCategory.js 
b/lib/translationunits/MWCategory.js
new file mode 100644
index 0000000..8ab163f
--- /dev/null
+++ b/lib/translationunits/MWCategory.js
@@ -0,0 +1,32 @@
+var cxutil = require( '../util.js' ),
+       TranslationUnit = require( './TranslationUnit.js' ),
+       MWAPIRequestManager = require( '../mw/ApiRequestManager.js' );
+
+class MWCategory extends TranslationUnit {}
+
+MWCategory.prototype.adapt = cxutil.async( function* () {
+       var namespaceAlias, adaptedTitle, titlePairInfo,
+               categoryTitle = this.node.attributes.href;
+
+       titlePairInfo = yield new MWAPIRequestManager( this.context )
+               .titlePairRequest( categoryTitle, this.sourceLanguage, 
this.targetLanguage );
+
+       if ( titlePairInfo.targetTitle ) {
+               adaptedTitle = titlePairInfo.targetTitle;
+               namespaceAlias = yield new MWAPIRequestManager( this.context )
+                       .getNamespaceAlias( 'Category', this.targetLanguage );
+               adaptedTitle = adaptedTitle.replace( /^(\.\.?\/)*([^:]+)(:)/, 
'$1' + namespaceAlias + '$3' );
+               this.node.attributes.href = adaptedTitle;
+       } else {
+               this.node.attributes[ 'data-cx' ] = JSON.stringify( {
+                       adapted: false,
+                       sourceTitle: titlePairInfo.sourceTitle
+               } );
+       }
+       return this.node;
+} );
+
+MWCategory.matchTagNames = [ 'link' ];
+MWCategory.matchRdfaTypes = [ 'mw:PageProp/Category' ];
+
+module.exports = MWCategory;
diff --git a/lib/translationunits/index.js b/lib/translationunits/index.js
index 050f7a1..cb1afb5 100644
--- a/lib/translationunits/index.js
+++ b/lib/translationunits/index.js
@@ -1,7 +1,8 @@
 'use strict';
 
 module.exports = {
-       MWLink: require( './MWLink.js' ),
+       MWCategory: require( './MWCategory.js' ),
+       MWGallery: require( './MWGallery.js' ),
        MWImage: require( './MWImage.js' ),
-       MWGallery: require( './MWGallery.js' )
+       MWLink: require( './MWLink.js' )
 };
diff --git a/test/adaptation/AdaptationTests.json 
b/test/adaptation/AdaptationTests.json
index 9fb8671..c044c5c 100644
--- a/test/adaptation/AdaptationTests.json
+++ b/test/adaptation/AdaptationTests.json
@@ -40,5 +40,19 @@
                "to": "es",
                "source": "<ul class='gallery mw-gallery-packed' 
typeof='mw:Extension/gallery' about='#mwt2' 
data-mw='{\"name\":\"gallery\",\"attrs\":{\"mode\":\"packed\"},\"body\":{\"extsrc\":\"\\r\\nFile:Nilgiri
 Tahr 1.jpg|[[Nilgiri tahr]]\\r\\nFile:Malabar Giant Squirrel77.jpg|[[Malabar 
Giant Squirrel]], [[Silent Valley National Park]]\\r\\nFile:Periyar 
Thekkady.jpg|Elephants at Thekkady\\r\\nFile:Rhacophorus 
malabaricus.jpg|[[Rhacophorus malabaricus]]\\r\\nFile:Brahminy 
kite2.jpg|[[Brahminy kite]]\\r\\nFile:Goldenbacked woodpecker.jpg|[[Dinopium 
benghalense|Lesser golden-backed woodpecker]] water\\r\\n\"}}' id='mwAQ'></ul>",
                "result": "<ul about=\"#mwt2\" class=\"gallery 
mw-gallery-packed\" 
data-mw=\"{&#34;name&#34;:&#34;gallery&#34;,&#34;attrs&#34;:{&#34;mode&#34;:&#34;packed&#34;},&#34;body&#34;:{&#34;extsrc&#34;:&#34;\\nArchivo:Nilgiri
 Tahr 1.jpg|[[Nilgiri tahr]]\\nArchivo:Malabar Giant Squirrel77.jpg|[[Malabar 
Giant Squirrel|Malabar Ardilla giganta]], [[Silent Valley National Park|Valle 
Silencioso Parque Nacional]]\\nArchivo:Periyar Thekkady.jpg|Elefantes en 
Thekkady\\nArchivo:Rhacophorus malabaricus.jpg|[[Rhacophorus 
malabaricus]]\\nArchivo:Brahminy kite2.jpg|[[Brahminy kite|Brahminy 
Cometa]]\\nArchivo:Goldenbacked woodpecker.jpg|[[Dinopium benghalense|Menor 
dorado-reculado woodpecker]] agua\\n&#34;}}\" id=\"mwAQ\" 
typeof=\"mw:Extension/gallery\"></ul>"
+       },
+       {
+               "desc": "Category adaptation",
+               "from": "en",
+               "to": "ml",
+               "source": "<p><link rel='mw:PageProp/Category' 
href='./Category:Planets'/></p>",
+               "result": "<p><link href='വർഗ്ഗം:ഗ്രഹങ്ങൾ' 
rel='mw:PageProp/Category' /></p>"
+       },
+       {
+               "desc": "Category adaptation - target category does not exist",
+               "from": "en",
+               "to": "ml",
+               "source": "<p><link rel='mw:PageProp/Category' 
href='./Category:Planets_with_life'/></p>",
+               "result": "<p><link 
data-cx='{&#34;adapted&#34;:false,&#34;sourceTitle&#34;:&#34;Category:Planets 
with life&#34;}' href='./Category:Planets_with_life' rel='mw:PageProp/Category' 
/></p>"
        }
 ]

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie31bc774472c73914c50939931da19bde4e773c8
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/services/cxserver
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>
Gerrit-Reviewer: Catrope <[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