jenkins-bot has submitted this change and it was merged.
Change subject: Fix audio file URL parsing error and add null checks
......................................................................
Fix audio file URL parsing error and add null checks
Adds several null checks to prevent TypeErrors resulting in page load
failures[0][1].
Fixes failures parsing certain pronunciation file URLs.
Adds tests for the above.
Also changed a mobile-summary test to reflect a change in
the underlying wiki content being tested against.
[0] "Cannot call method 'getAttribute' of undefined "(in
parseProperty.parseSpokenWikipedia); "Cannot read property 'parts' of
null" (in parseProperty.parsePronunciation).
Bug: T121608
Change-Id: Id5530cff14859db91329b4a45331fc3d0df98e2c
---
M lib/parseProperty.js
M test/features/mobile-sections-lead/pagecontent.js
M test/features/mobile-sections-lead/parse-pronunciation-test.js
M test/features/mobile-summary/pagecontent.js
4 files changed, 32 insertions(+), 9 deletions(-)
Approvals:
BearND: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/parseProperty.js b/lib/parseProperty.js
index 3a699a8..e35f85b 100644
--- a/lib/parseProperty.js
+++ b/lib/parseProperty.js
@@ -34,7 +34,11 @@
}
function filePageUrlToFileUrlSelector(filePageUrl) {
- return filePageUrl && 'a[href^=//][href$=/' +
filePageUrlToFilename(filePageUrl) + ']';
+ // NOTE: escape() is deprecated, but encodes certain filenames correctly,
whereas the preferred
+ // encodeURI() and encodeURIComponent() both fail. One such example is
+ // 'En-us-Yazidis_from_Iraq_pronunciation_(Voice_of_America).ogg'.
+ // global.escape is used to pass jshint, which (incorrectly) flags
'escape' as undefined.
+ return filePageUrl && 'a[href^=//][href$=' +
global.escape(filePageUrlToFilename(filePageUrl)) + ']';
}
function pickPronunciationFilePageUrl(urls, title) {
@@ -52,9 +56,10 @@
function parsePronunciation(doc, title) {
var pageUrls = parsePronunciationFilePageUrls(doc);
- var pageUrl = pickPronunciationFilePageUrl(pageUrls, title);
- var selector = filePageUrlToFileUrlSelector(pageUrl);
- var url = pageUrl && doc.querySelector(selector).getAttribute('href');
+ var pageUrl = pageUrls && pickPronunciationFilePageUrl(pageUrls, title);
+ var selector = pageUrl && filePageUrlToFileUrlSelector(pageUrl);
+ var fileAnchor = selector && doc.querySelector(selector);
+ var url = fileAnchor && fileAnchor.getAttribute('href');
return url && { url: url };
}
@@ -81,13 +86,15 @@
*
https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Spoken_Wikipedia/Template_guidelines
*/
function parseSpokenWikipedia(doc, page) {
- var dataMW, template, target, match, maxKey, key, fileName,
+ var dataMW, parsedData, firstPart, template, target, match, maxKey, key,
fileName,
spokenSectionDiv = doc.querySelector('div#section_SpokenWikipedia');
if (spokenSectionDiv) {
dataMW = spokenSectionDiv.getAttribute('data-mw');
- template = JSON.parse(dataMW).parts[0].template;
- target = template.target;
+ parsedData = dataMW && JSON.parse(dataMW);
+ firstPart = parsedData && parsedData.parts[0];
+ template = firstPart && firstPart.template;
+ target = template && template.target;
if (target && target.wt) {
if (target.wt === 'Spoken Wikipedia') {
// single audio file: use first param (2nd param is recording
date)
diff --git a/test/features/mobile-sections-lead/pagecontent.js
b/test/features/mobile-sections-lead/pagecontent.js
index 586ec75..b2a8d27 100644
--- a/test/features/mobile-sections-lead/pagecontent.js
+++ b/test/features/mobile-sections-lead/pagecontent.js
@@ -94,4 +94,20 @@
assert.deepEqual(lead.pronunciation.url,
'//upload.wikimedia.org/wikipedia/commons/8/82/En-us-Barack-Hussein-Obama.ogg');
});
});
+ it('Enwiki Uranus loads successfully (no pronunciation parsing
TypeErrors)', function() {
+ return preq.get({ uri: server.config.uri +
'en.wikipedia.org/v1/page/mobile-sections-lead/Uranus' })
+ .then(function (res) {
+ var lead = res.body;
+ assert.deepEqual(res.status, 200);
+ assert.deepEqual(lead.pronunciation.url,
'//upload.wikimedia.org/wikipedia/commons/1/1c/En-us-Uranus.ogg');
+ });
+ });
+ it('Enwiki Odisha loads successfully (no pronunciation parsing
TypeErrors)', function() {
+ return preq.get({ uri: server.config.uri +
'en.wikipedia.org/v1/page/mobile-sections-lead/Odisha' })
+ .then(function (res) {
+ var lead = res.body;
+ assert.deepEqual(res.status, 200);
+ assert.deepEqual(lead.pronunciation.url,
'//upload.wikimedia.org/wikipedia/commons/c/c2/Pronunciation_of_the_Odia_language_word_%22Odisha%22.ogg');
+ });
+ });
});
diff --git a/test/features/mobile-sections-lead/parse-pronunciation-test.js
b/test/features/mobile-sections-lead/parse-pronunciation-test.js
index 2b6bc91..92bc6da 100644
--- a/test/features/mobile-sections-lead/parse-pronunciation-test.js
+++ b/test/features/mobile-sections-lead/parse-pronunciation-test.js
@@ -24,4 +24,4 @@
var result = pickPronunciatonFilePageUrl(urls, title);
assert.deepEqual(result, expected);
});
-});
\ No newline at end of file
+});
diff --git a/test/features/mobile-summary/pagecontent.js
b/test/features/mobile-summary/pagecontent.js
index 9d62d8a..043d5e3 100644
--- a/test/features/mobile-summary/pagecontent.js
+++ b/test/features/mobile-summary/pagecontent.js
@@ -23,7 +23,7 @@
var body = res.body;
assert.deepEqual(res.status, 200);
assert.deepEqual(body.title, 'Cat');
- assert.ok(body.extract.indexOf('The domestic cat is a') === 0,
'Expected different start of extract');
+ assert.ok(body.extract.indexOf('The domestic cat, often
referred to as "Kitty" is a') === 0, 'Expected different start of extract');
assert.deepEqual(body.thumbnail, {
"source":
"https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cat_poster_1.jpg/320px-Cat_poster_1.jpg"
});
--
To view, visit https://gerrit.wikimedia.org/r/259329
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Id5530cff14859db91329b4a45331fc3d0df98e2c
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>
Gerrit-Reviewer: BearND <[email protected]>
Gerrit-Reviewer: Bgerstle <[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: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits