[MediaWiki-commits] [Gerrit] mediawiki...mobileapps[master]: Source coordinates from GeoData

2017-08-04 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/368954 )

Change subject: Source coordinates from GeoData
..


Source coordinates from GeoData

Rather than rely on Parsoid HTML, source geodata from
the query prop API. It's more future-proof given the adoption of
MediaWiki indicators 
(https://www.mediawiki.org/wiki/Help:Page_status_indicators)
and the fact that HTML markup can change.

This will help us commence removing the mobile view API call
(T103362)

Update some existing tests to use the more accurate values.

Bug: T152441
Change-Id: Ifa8d271718475df5b950890ef3ca4c8f4e6e6794
---
M lib/mwapi.js
M lib/parseProperty.js
M lib/parsoid-access.js
M routes/mobile-sections.js
M test/features/mobile-sections-lead/pagecontent.js
5 files changed, 61 insertions(+), 43 deletions(-)

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



diff --git a/lib/mwapi.js b/lib/mwapi.js
index 4396f52..3917f6f 100644
--- a/lib/mwapi.js
+++ b/lib/mwapi.js
@@ -126,11 +126,13 @@
 
 /**
  * Builds the request to get page metadata from MW API action=mobileview.
+ * The plan is to deprecate this method and usage of the mobileview api 
(T103362).
+ * Please use getMetaData and queries that do not hit the mobileview api.
  * @param {!Object} app the application object
  * @param {!Object} req the request object
  * @return {!Promise} a promise resolving as an JSON object containing the 
response
  */
-mwapi.getMetadata = function(app, req) {
+function getMobileViewMetadata(app, req) {
 const props = ['languagecount', 'thumb', 'image', 'id', 'revision', 
'description',
 'lastmodified', 'lastmodifiedby', 'normalizedtitle', 'displaytitle', 
'protection',
 'editable', 'namespace', 'pageprops', 'contentmodel'];
@@ -147,6 +149,57 @@
 mwapi.checkForMobileviewInResponse(req.logger, response);
 return response;
 });
+}
+
+/**
+ * Builds the request to get page metadata from MW API action=query
+ * @param {!Object} app the application object
+ * @param {!Object} req the request object
+ * @return {!Promise} a promise resolving as an JSON object containing the 
response
+ */
+function getMetadataActionApi(app, req) {
+const props = ['coordinates'];
+const query = apiParams({
+action: 'query',
+titles: req.params.title,
+prop: props.join('|')
+});
+
+return api.mwApiGet(app, req.params.domain, query)
+  .then((apiResponse) => {
+  const body = apiResponse.body;
+  const page = body.query && body.query.pages
+  && body.query.pages[0];
+  const coords = page && page.coordinates && page.coordinates[0];
+  let geo;
+
+  // Extract coordinates from the API response
+  if (coords) {
+  geo = {
+  latitude: coords.lat,
+  longitude: coords.lon
+  };
+  }
+  return {
+  geo,
+  };
+  });
+}
+
+/**
+ * Builds the request to get page metadata
+ * @param {!Object} app the application object
+ * @param {!Object} req the request object
+ * @return {!Promise} a promise resolving as an JSON object containing the 
response
+ */
+mwapi.getMetadata = function(app, req) {
+return getMobileViewMetadata(app, req)
+  .then((mvResponse) => {
+  return getMetadataActionApi(app, req).then((actionResp) => {
+  Object.assign(mvResponse.body.mobileview, actionResp);
+  return mvResponse;
+  });
+  });
 };
 
 /**
diff --git a/lib/parseProperty.js b/lib/parseProperty.js
index 8f38bef..4e49ac5 100644
--- a/lib/parseProperty.js
+++ b/lib/parseProperty.js
@@ -28,38 +28,6 @@
 }
 
 /**
- * @param {?string} latLngStr a string consisting of latitude and longitude. 
The values should be
- * separated by ; or , or space.
- * @return {?Geo} if latitude or longitude is truthy, else undefined.
- */
-function latLngStrToGeo(latLngStr) {
-const latLng = latLngStr && latLngStr.split(/[;, ]+/) || [];
-const geo = latLng.length &&
-{ latitude: latLng[0] && parseFloat(latLng[0]),
-longitude: latLng[1] && parseFloat(latLng[1]) };
-return mUtil.defaultVal(mUtil.filterEmpty(geo));
-}
-
-/**
- * Searches for Geo coordinates and adds them to the given page object
- */
-function parseGeo(lead, page) {
-let lat;
-let lng;
-let coordinates = lead.querySelector('span#coordinates .geo');
-let geo = coordinates && latLngStrToGeo(coordinates.textContent);
-if (!geo) {
-coordinates = lead.querySelector('#geoCoord .geo');
-lat = coordinates && coordinates.querySelector('.latitude');
-lng = coordinates && coordinates.querySelector('.longitude');
-geo = lat && lng && 
latLngStrToGeo(`${lat.textContent};${lng.textContent}`);
-}
-if (geo) {
-page.geo = geo;
-}
-}
-
-/**
  * Searches for Spoke

[MediaWiki-commits] [Gerrit] mediawiki...mobileapps[master]: Source coordinates from GeoData

2017-07-31 Thread Jdlrobson (Code Review)
Jdlrobson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/368954 )

Change subject: Source coordinates from GeoData
..

Source coordinates from GeoData

Rather than rely on Parsoid HTML, source geodata from
the query prop API. It's more future-proof given the adoption of
MediaWiki indicators 
(https://www.mediawiki.org/wiki/Help:Page_status_indicators)
and the fact that HTML markup can change.

This will help us commence removing the mobile view API call
(T103362)

Bug: T152441
Change-Id: Ifa8d271718475df5b950890ef3ca4c8f4e6e6794
---
M lib/mwapi.js
M lib/parseProperty.js
M lib/parsoid-access.js
M lib/transforms.js
M routes/mobile-sections.js
M test/features/mobile-sections-lead/pagecontent.js
6 files changed, 45 insertions(+), 39 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps 
refs/changes/54/368954/1

diff --git a/lib/mwapi.js b/lib/mwapi.js
index 4396f52..8e363c9 100644
--- a/lib/mwapi.js
+++ b/lib/mwapi.js
@@ -126,11 +126,14 @@
 
 /**
  * Builds the request to get page metadata from MW API action=mobileview.
+ * The plan is to deprecate this method and usage of the mobileview api 
(T103362).
+ * Please use getMetaData and queries that do not hit the mobileview api.
+ *
  * @param {!Object} app the application object
  * @param {!Object} req the request object
  * @return {!Promise} a promise resolving as an JSON object containing the 
response
  */
-mwapi.getMetadata = function(app, req) {
+function getMobileViewMetadata(app, req) {
 const props = ['languagecount', 'thumb', 'image', 'id', 'revision', 
'description',
 'lastmodified', 'lastmodifiedby', 'normalizedtitle', 'displaytitle', 
'protection',
 'editable', 'namespace', 'pageprops', 'contentmodel'];
@@ -150,6 +153,43 @@
 };
 
 /**
+ * Builds the request to get page metadata from MW API action=query
+ * @param {!Object} app the application object
+ * @param {!Object} req the request object
+ * @return {!Promise} a promise resolving as an JSON object containing the 
response
+ */
+mwapi.getMetadata = function(app, req) {
+  const props = ['coordinates'];
+
+  return getMobileViewMetadata(app, req)
+.then((mvResponse) => {
+  const query = apiParams({
+  action: 'query',
+  titles: req.params.title,
+  prop: props.join('|')
+  });
+
+  return api.mwApiGet(app, req.params.domain, query)
+.then((apiResponse) => {
+  const body = apiResponse.body;
+  const page = body.query && body.query.pages
+&& body.query.pages[0];
+  const coords = page && page.coordinates && page.coordinates[0];
+
+  // Extract coordinates from the API response
+  if (coords) {
+mvResponse.body.mobileview.geo = {
+  latitude: coords.lat,
+  longitude: coords.lon
+};
+  }
+
+  return mvResponse;
+})
+});
+};
+
+/**
  * Builds the request to get all sections from MW API action=mobileview.
  * @param {!Object} app the application object
  * @param {!Object} req the request object
diff --git a/lib/parseProperty.js b/lib/parseProperty.js
index 8f38bef..4e49ac5 100644
--- a/lib/parseProperty.js
+++ b/lib/parseProperty.js
@@ -28,38 +28,6 @@
 }
 
 /**
- * @param {?string} latLngStr a string consisting of latitude and longitude. 
The values should be
- * separated by ; or , or space.
- * @return {?Geo} if latitude or longitude is truthy, else undefined.
- */
-function latLngStrToGeo(latLngStr) {
-const latLng = latLngStr && latLngStr.split(/[;, ]+/) || [];
-const geo = latLng.length &&
-{ latitude: latLng[0] && parseFloat(latLng[0]),
-longitude: latLng[1] && parseFloat(latLng[1]) };
-return mUtil.defaultVal(mUtil.filterEmpty(geo));
-}
-
-/**
- * Searches for Geo coordinates and adds them to the given page object
- */
-function parseGeo(lead, page) {
-let lat;
-let lng;
-let coordinates = lead.querySelector('span#coordinates .geo');
-let geo = coordinates && latLngStrToGeo(coordinates.textContent);
-if (!geo) {
-coordinates = lead.querySelector('#geoCoord .geo');
-lat = coordinates && coordinates.querySelector('.latitude');
-lng = coordinates && coordinates.querySelector('.longitude');
-geo = lat && lng && 
latLngStrToGeo(`${lat.textContent};${lng.textContent}`);
-}
-if (geo) {
-page.geo = geo;
-}
-}
-
-/**
  * Searches for Spoken Wikipedia audio files and adds them to the given page 
object.
  * 
https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Spoken_Wikipedia/Template_guidelines
  */
@@ -98,6 +66,5 @@
 module.exports = {
 parseInfobox,
 parsePronunciation,
-parseSpokenWikipedia,
-parseGeo
+parseSpokenWikipedia
 };
diff --git a/lib/parsoid-access.js b/lib/parsoid-access.js
index 3cc4e6b..b6df4b8 100644
--- a/lib/parsoid-access.js
+++ b/lib/pa