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');
+                assert.deepEqual(lead.displaytitle, 'San Francisco');
                 assert.deepEqual(lead.protection, {
-                    "edit": [
-                        "sysop"
-                    ],
                     "move": [
                         "sysop"
                     ]
                 });
-                assert.deepEqual(lead.editable, false);
-                assert.deepEqual(lead.image, {});
+                assert.deepEqual(lead.editable, true);
+                assert.deepEqual(lead.image.file, "Golden Gate Bridge, SF 
(cropped).jpg");
+                assert.deepEqual(Object.keys(lead.image.urls).length, 3);
                 assert.ok(lead.sections.length > 0, 'Expected at least one 
section element');
                 assert.deepEqual(lead.sections[0].id, 0);
                 assert.ok(lead.sections[0].text.length > 0, 'Expected text to 
be non-empty');
+                assert.deepEqual(lead.geo.latitude, 37.78333333333333);
+                assert.deepEqual(lead.geo.longitude, -122.41666666666667);
             });
     });
     it('Obama (redirect) should have a lead image', function() {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I799ae9859ca1c78eceeb0df09355931022f36ad0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to