Ppchelko has submitted this change and it was merged.

Change subject: Decode entities when following redirects
......................................................................


Decode entities when following redirects

Bug: T131406
Change-Id: I5c44849f8c0760d3bd65f6426640e03d15e620f3
---
M lib/parsoid-access.js
M package.json
M test/features/mobile-sections/pagecontent.js
M test/lib/parsoid/parsoid-access-test.js
4 files changed, 55 insertions(+), 2 deletions(-)

Approvals:
  Mholloway: Looks good to me, but someone else must approve
  Ppchelko: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/parsoid-access.js b/lib/parsoid-access.js
index 948a4fd..694b85a 100644
--- a/lib/parsoid-access.js
+++ b/lib/parsoid-access.js
@@ -7,6 +7,7 @@
 var preq = require('preq');
 var domino = require('domino');
 var a = require('./anchorencode');
+var Html5Entities = require('html-entities').Html5Entities;
 var Template = require('swagger-router').Template;
 var sUtil = require('../lib/util');
 var mwapi = require('./mwapi');
@@ -56,14 +57,16 @@
 }
 
 /**
- * Gets the redirected title from the Parsoid payload.
+ * Gets the redirected title from the Parsoid (HTML) payload.
+ * If there are any HTML entities inside the title string they get decoded.
  * Note: this assumes that hasRedirectInPayload returns true.
  *
  * @param {string} body the Parsoid response body to check for redirects
  * @return {string} the title of the redirect target or empty string.
  */
 function getRedirectTitleFromPayload(body) {
-    return REDIRECT_REGEXP.exec(body)[1];
+    var otherTitle = REDIRECT_REGEXP.exec(body)[1];
+    return decodeURIComponent(Html5Entities.decode(otherTitle));
 }
 
 /**
@@ -286,5 +289,6 @@
     _addSectionDivs: addSectionDivs,
     _getSectionsText: getSectionsText,
     _getRedirectTitleFromLocationHeader: getRedirectTitleFromLocationHeader,
+    _getRedirectTitleFromPayload: getRedirectTitleFromPayload,
     _hasRedirectInPayload: hasRedirectInPayload
 };
diff --git a/package.json b/package.json
index cab7d0e..c4d3db1 100644
--- a/package.json
+++ b/package.json
@@ -45,6 +45,7 @@
     "compression": "^1.6.1",
     "domino": "^1.0.23",
     "express": "^4.13.3",
+    "html-entities": "^1.2.0",
     "js-yaml": "^3.5.2",
     "phpjs": "^1.3.2",
     "preq": "^0.4.8",
diff --git a/test/features/mobile-sections/pagecontent.js 
b/test/features/mobile-sections/pagecontent.js
index f16084d..19c9b50 100644
--- a/test/features/mobile-sections/pagecontent.js
+++ b/test/features/mobile-sections/pagecontent.js
@@ -126,4 +126,40 @@
                 assert.deepEqual(res.body.lead.spoken.files[0], 
'File:En-Alliterative_verse-article.ogg');
             });
     });
+    it('Page with HTML entity in redirected page title should load', 
function() {
+        return preq.get({ uri: server.config.uri + 
'test.wikipedia.org/v1/page/mobile-sections/User:BSitzmann_%28WMF%29%2FMCS%2FTest%2FA%26B_redirect'
 })
+            .then(function(res) {
+                assert.deepEqual(res.status, 200);
+                assert.deepEqual(res.body.lead.normalizedtitle, 
'User:BSitzmann (WMF)/MCS/Test/A&B redirect');
+                assert.deepEqual(res.body.lead.displaytitle, 'User:BSitzmann 
(WMF)/MCS/Test/A&B');
+                assert.deepEqual(res.body.lead.redirected, 'User:BSitzmann 
(WMF)/MCS/Test/A&B');
+            });
+    });
+    it('Page with % in redirected page title should load', function() {
+        return preq.get({ uri: server.config.uri + 
'en.wikipedia.beta.wmflabs.org/v1/page/mobile-sections/User:Pchelolo%2fRedirect_Test'
 })
+            .then(function(res) {
+                assert.deepEqual(res.status, 200);
+                assert.deepEqual(res.body.lead.normalizedtitle, 
'User:Pchelolo/Redirect Test');
+                assert.deepEqual(res.body.lead.displaytitle, 
'User:Pchelolo/Redirect Target %');
+                assert.deepEqual(res.body.lead.redirected, 
'User:Pchelolo/Redirect Target %');
+            });
+    });
+    it('Page with % in redirected page title should load 2', function() {
+        return preq.get({ uri: server.config.uri + 
'test.wikipedia.org/v1/page/mobile-sections/User:BSitzmann_%28WMF%29%2FMCS%2FTest%2Fredirect_test2'
 })
+            .then(function(res) {
+                assert.deepEqual(res.status, 200);
+                assert.deepEqual(res.body.lead.normalizedtitle, 
'User:BSitzmann (WMF)/MCS/Test/redirect test2');
+                assert.deepEqual(res.body.lead.displaytitle, 'User:BSitzmann 
(WMF)/MCS/Test/redirect test2 target %');
+                assert.deepEqual(res.body.lead.redirected, 'User:BSitzmann 
(WMF)/MCS/Test/redirect test2 target %');
+            });
+    });
+    it('Page with % in section header of redirected page title should load', 
function() {
+        return preq.get({ uri: server.config.uri + 
'test.wikipedia.org/v1/page/mobile-sections/User:BSitzmann_%28WMF%29%2FMCS%2FTest%2Fredirect_test3'
 })
+            .then(function(res) {
+                assert.deepEqual(res.status, 200);
+                assert.deepEqual(res.body.lead.normalizedtitle, 
'User:BSitzmann (WMF)/MCS/Test/redirect test3');
+                assert.deepEqual(res.body.lead.displaytitle, 'User:BSitzmann 
(WMF)/MCS/Test/redirect test3 target');
+                assert.deepEqual(res.body.lead.redirected, 'User:BSitzmann 
(WMF)/MCS/Test/redirect test3 target#Section_.25');
+            });
+    });
 });
diff --git a/test/lib/parsoid/parsoid-access-test.js 
b/test/lib/parsoid/parsoid-access-test.js
index 45f84f0..8749e6b 100644
--- a/test/lib/parsoid/parsoid-access-test.js
+++ b/test/lib/parsoid/parsoid-access-test.js
@@ -80,6 +80,18 @@
         assert.ok(redirect === false);
     });
 
+    it('_getRedirectTitleFromPayload(): HTML entities in redirected title 
should be decoded', function() {
+        var body = '<link rel="mw:PageProp/redirect" href="./some foo &amp; 
bar title"';
+        var actual = parsoid._getRedirectTitleFromPayload(body);
+        assert.deepEqual(actual, 'some foo & bar title');
+    });
+
+    it('_getRedirectTitleFromPayload(): %25 in redirected title should be 
decoded', function() {
+        var body = '<link rel="mw:PageProp/redirect" href="./%25 in title"';
+        var actual = parsoid._getRedirectTitleFromPayload(body);
+        assert.deepEqual(actual, '% in title');
+    });
+
     it('_getRedirectTitleFromLocationHeader() with redirect', function() {
         var response = {
             'status': 301,

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I5c44849f8c0760d3bd65f6426640e03d15e620f3
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: BearND <[email protected]>
Gerrit-Reviewer: BearND <[email protected]>
Gerrit-Reviewer: Dbrant <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Mholloway <[email protected]>
Gerrit-Reviewer: Mhurd <[email protected]>
Gerrit-Reviewer: Mobrovac <[email protected]>
Gerrit-Reviewer: Niedzielski <[email protected]>
Gerrit-Reviewer: Ppchelko <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to