[MediaWiki-commits] [Gerrit] Add Geo to lead response - change (mediawiki...mobileapps)
jenkins-bot has submitted this change and it was merged. Change subject: Add Geo to lead response .. Add Geo to lead response * Add geo coordinates to lead response. * Convert from degrees, minutes, seconds to decimal coordinates to meet JSON Schema number requirements. For articles containing a coordinates ID, supply a Geo as defined by JSON Schema[0]. [0] http://json-schema.org/geo Bug: T108370 Change-Id: I799ae9859ca1c78eceeb0df09355931022f36ad0 --- M lib/transforms.js M routes/mobile-html-sections.js M spec.yaml M test/features/mobile-html-sections-lead/pagecontent.js 4 files changed, 33 insertions(+), 2 deletions(-) Approvals: BearND: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/transforms.js b/lib/transforms.js index 3311096..4e1659c 100644 --- a/lib/transforms.js +++ b/lib/transforms.js @@ -59,6 +59,24 @@ return anchor && { 'url': anchor.href }; } +/** + * @returns {Geo} if latitude or longitude is truthy, else undefined. +*/ +function latLngStrToGeo(latLngStr) { +var latLng = latLngStr && latLngStr.split('; ') || []; +return latLng.length +&& { "latitude": latLng[0] && parseFloat(latLng[0]), + "longitude": latLng[1] && parseFloat(latLng[1]) }; +} + +/** + * @returns {Geo} if latitude or longitude is found, else undefined. +*/ +function parseGeo(lead) { +var coordinates = lead.querySelector('span#coordinates .geo'); +return coordinates && latLngStrToGeo(coordinates.innerHTML); +} + function moveFirstParagraphUpInLeadSection(text) { var doc = domino.createDocument(text); // TODO: mhurd: feel free to add your lead section magic here @@ -101,6 +119,7 @@ moveFirstParagraphUpInLeadSection: moveFirstParagraphUpInLeadSection, parseInfobox: parseInfobox, parsePronunciation: parsePronunciation, +parseGeo: parseGeo, // visible for testing only: _rmBracketSpans: rmBracketSpans diff --git a/routes/mobile-html-sections.js b/routes/mobile-html-sections.js index e0f8ebe..73ada98 100644 --- a/routes/mobile-html-sections.js +++ b/routes/mobile-html-sections.js @@ -99,6 +99,7 @@ media: input.media, infobox: transforms.parseInfobox(lead), pronunciation: transforms.parsePronunciation(lead), +geo: transforms.parseGeo(lead), sections: buildLeadSections(input.page.sections) }; } diff --git a/spec.yaml b/spec.yaml index 372dc20..82c796c 100644 --- a/spec.yaml +++ b/spec.yaml @@ -131,10 +131,10 @@ media: items: [ /.+/ ] sections: [ /.+/ ] -- title: retrieve lead section of en.wp cat page via mobile-html-sections-lead +- title: retrieve lead section of en.wp San Francisco page via mobile-html-sections-lead request: params: - title: Cat + title: San_Francisco response: status: 200 headers: @@ -150,6 +150,9 @@ media: items: [ /.+/ ] infobox: [ [ /.+/ ] ] + geo: +latitude: /.+/ +longitude: /.+/ sections: [ /.+/ ] - title: retrieve lead section of en.wp Barack Obama page via mobile-html-sections-lead request: diff --git a/test/features/mobile-html-sections-lead/pagecontent.js b/test/features/mobile-html-sections-lead/pagecontent.js index 876ef73..fd6485a 100644 --- a/test/features/mobile-html-sections-lead/pagecontent.js +++ b/test/features/mobile-html-sections-lead/pagecontent.js @@ -63,6 +63,14 @@ assert.ok(lead.sections[0].text.length > 0, 'Expected text to be non-empty'); }); }); +it('en San Francisco should have a lead object with a geo property', function() { +return preq.get({ uri: server.config.uri + 'en.wikipedia.org/v1/page/mobile-html-sections-lead/San_Francisco' }) +.then(function(res) { +var lead = res.body; +assert.deepEqual(lead.geo.latitude, 37.783); +assert.deepEqual(lead.geo.longitude, -122.417); +}); +}); it('Obama (redirect) should have a lead image', function() { return preq.get({ uri: server.config.uri + 'en.wikipedia.org/v1/page/mobile-html-sections-lead/Obama' }) .then(function(res) { -- To view, visit https://gerrit.wikimedia.org/r/233907 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I799ae9859ca1c78eceeb0df09355931022f36ad0 Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/services/mobileapps Gerrit-Branch: master Gerrit-Owner: NiedzielskiGerrit-Reviewer: BearND Gerrit-Reviewer: Bgerstle Gerrit-Reviewer: Dbrant Gerrit-Reviewer: Fjalapeno Gerrit-Reviewer:
[MediaWiki-commits] [Gerrit] Add Geo to lead response - change (mediawiki...mobileapps)
Niedzielski has uploaded a new change for review. https://gerrit.wikimedia.org/r/233907 Change subject: Add Geo to lead response .. Add Geo to lead response For articles containing a coordinates ID, supply a Geo as defined by JSON Schema[0] and Schema.org[1]. [0] http://json-schema.org/geo [1] https://schema.org/GeoCoordinates Bug: T108370 Change-Id: I799ae9859ca1c78eceeb0df09355931022f36ad0 --- M lib/transforms.js M package.json M routes/mobile-html-sections.js M spec.yaml M test/features/mobile-html-sections-lead/pagecontent.js 5 files changed, 43 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps refs/changes/07/233907/1 diff --git a/lib/transforms.js b/lib/transforms.js index f6bcdd1..f639103 100644 --- a/lib/transforms.js +++ b/lib/transforms.js @@ -6,6 +6,7 @@ 'use strict'; var domino = require('domino'); +var Dms = require('geodesy').Dms; function rmSelectorAll(doc, selector) { var ps = doc.querySelectorAll(selector) || []; @@ -38,6 +39,32 @@ var node = ps[idx]; node.removeAttribute(attribute); } +} + +/* + * @param {string} [latitude] in WGS84 degrees, minutes, seconds. + * @param {string} [longitude] in WGS84 degrees, minutes, seconds. + * @returns {Geo} if latitude or longitude is truthy, else undefined. +*/ +function latLngToGeo(latitude, longitude) { +if (latitude || longitude) { +return { latitude: latitude Dms.parseDMS(latitude), + longitude: longitude Dms.parseDMS(longitude) }; +} +} + +/* + * @returns {Geo} if latitude or longitude is found, else undefined. +*/ +function queryGeo(text) { +var COORDINATES_SELECTOR = 'span#coordinates'; +var LATITUDE_SELECTOR = COORDINATES_SELECTOR + ' span.latitude'; +var LONGITUDE_SELECTOR = COORDINATES_SELECTOR + ' span.longitude'; + +var doc = domino.createDocument(text); +var lat = doc.querySelector(LATITUDE_SELECTOR); +var lng = doc.querySelector(LONGITUDE_SELECTOR); +return latLngToGeo(lat lat.innerHTML, lng lng.innerHTML); } function moveFirstParagraphUpInLeadSection(text) { @@ -80,7 +107,8 @@ module.exports = { runDomTransforms: runDomTransforms, moveFirstParagraphUpInLeadSection: moveFirstParagraphUpInLeadSection, +queryGeo: queryGeo, // visible for testing only: _rmBracketSpans: rmBracketSpans -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index d58387c..71fce94 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ express: ^4.13.1, js-yaml: ^3.3.1, preq: ^0.4.4, -service-runner: ^0.2.1 +service-runner: ^0.2.1, +geodesy: ~1.0.2 }, devDependencies: { istanbul: ^0.3.17, diff --git a/routes/mobile-html-sections.js b/routes/mobile-html-sections.js index 4f06011..6e27c0e 100644 --- a/routes/mobile-html-sections.js +++ b/routes/mobile-html-sections.js @@ -93,6 +93,7 @@ file: input.page.image input.page.image.file, urls: input.page.thumb mwapi.buildLeadImageUrls(input.page.thumb.url) }, +geo: transforms.queryGeo(input.page.sections[0].text), sections: buildLeadSections(input.page.sections) }; } diff --git a/spec.yaml b/spec.yaml index c6bf53b..b98f4d7 100644 --- a/spec.yaml +++ b/spec.yaml @@ -114,7 +114,7 @@ - title: retrieve lead section of en.wp main page via mobile-html-sections-lead request: params: - title: Main_Page + title: San_Francisco response: status: 200 headers: @@ -127,6 +127,9 @@ protection: /.+/ editable: /.+/ languagecount: /.+/ + geo: +latitude: /.+/ +longitude: /.+/ sections: [ /.+/ ] /{domain}/v1/page/mobile-html-sections-remaining/{title}: get: diff --git a/test/features/mobile-html-sections-lead/pagecontent.js b/test/features/mobile-html-sections-lead/pagecontent.js index d72c4a8..2a30c52 100644 --- a/test/features/mobile-html-sections-lead/pagecontent.js +++ b/test/features/mobile-html-sections-lead/pagecontent.js @@ -33,25 +33,25 @@ }); }); it('en Main page should have a lead object with expected properties', function() { -return preq.get({ uri: server.config.uri + 'en.wikipedia.org/v1/page/mobile-html-sections-lead/Main_Page' }) +return preq.get({ uri: server.config.uri + 'en.wikipedia.org/v1/page/mobile-html-sections-lead/San_Francisco' }) .then(function(res) { var lead = res.body; assert.deepEqual(res.status, 200); assert.ok(lead.lastmodified.startsWith('201'), lead.lastmodified + ' should start with 201'); // 2015- -assert.deepEqual(lead.displaytitle, 'Main Page'); +