Yurik has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311147

Change subject: Allow readable URLs for externaldata in geojson
......................................................................

Allow readable URLs for externaldata in geojson

Bug: T145047
Change-Id: I033a515e3561850fccaba8ab2be9457e040fcfe9
---
M modules/box/Map.js
1 file changed, 47 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Kartographer 
refs/changes/47/311147/1

diff --git a/modules/box/Map.js b/modules/box/Map.js
index 587b5b6..dea1a0a 100644
--- a/modules/box/Map.js
+++ b/modules/box/Map.js
@@ -216,15 +216,54 @@
         * For a given ExternalData object, gets it via XHR and expands it in 
place
         *
         * @param {Object} data
-        * @param {string} data.href URL to external data
+        * @param {string} [data.href] optional URL to external data
+        * @param {string} [data.service] optional name of the service (same as 
protocol without the ':')
+        * @param {string} [data.host] optional host of the service
+        * @param {string|string[]} [data.query] optional geoshape query
+        * @param {string} [data.ids] optional geoshape ids
         * @return {Promise} resolved when done with geojson expansion
         */
        function loadExternalDataAsync( data ) {
-               var uri = new mw.Uri( data.href );
-               // If url begins with   protocol:///...  mark it as having 
relative host
-               if ( /^[a-z]+:\/\/\//.test( data.href ) ) {
-                       uri.isRelativeHost = true;
+               var uri;
+               if ( data.href ) {
+                       uri = new mw.Uri( data.href );
+                       // If url begins with   protocol:///...  mark it as 
having relative host
+                       if ( /^[a-z]+:\/\/\//.test( data.href ) ) {
+                               uri.isRelativeHost = true;
+                       }
+               } else if ( data.service ) {
+                       // Construct URI out of the parameters in the 
externalData object
+                       uri = new mw.Uri( {
+                               protocol: data.service,
+                               host: data.host,
+                               path: '/'
+                       } );
+                       uri.isRelativeHost = !data.host;
+                       uri.query = {};
+                       switch ( data.service ) {
+                               case 'geoshape':
+                                       if ( data.query ) {
+                                               if ( typeof data.query === 
'string' ) {
+                                                       uri.query.query = 
data.query;
+                                               } if ( Array.isArray( 
data.query ) ) {
+                                                       uri.query.query = 
data.query.join(' ');
+                                               } else {
+                                                       throw new Error( 'Bad 
"query" param in externalData' );
+                                               }
+                                       }
+                                       if ( data.ids ) {
+                                               if ( typeof data.ids === 
'string' ) {
+                                                       uri.query.ids = 
data.ids;
+                                               } else {
+                                                       throw new Error( 'Bad 
"ids" param in externalData' );
+                                               }
+                                       }
+                                       break;
+                               default:
+                                       throw new Error( 'Unknown externalData 
protocol ' + data.service );
+                       }
                }
+
                switch ( uri.protocol ) {
                        case 'geoshape':
                                // geoshape:///?ids=Q16,Q30
@@ -232,10 +271,10 @@
                                // Get geo shapes data from OSM database by 
supplying Wikidata IDs or query
                                // https://maps.wikimedia.org/shape?ids=Q16,Q30
                                if ( !uri.query || ( !uri.query.ids && 
!uri.query.query ) ) {
-                                       throw new Error( 'geoshape: missing ids 
or query parameter in: ' + data.href );
+                                       throw new Error( 'geoshape: missing ids 
or query parameter in externalData' );
                                }
                                if ( !uri.isRelativeHost && uri.host !== 
'maps.wikimedia.org' ) {
-                                       throw new Error( 'geoshape: hostname 
must be missing or "maps.wikimedia.org": ' + data.href );
+                                       throw new Error( 'geoshape: hostname 
must be missing or "maps.wikimedia.org"' );
                                }
                                uri.protocol = 'https';
                                uri.host = 'maps.wikimedia.org';
@@ -272,7 +311,7 @@
                                } );
 
                        default:
-                               throw new Error( 'Unknown protocol ' + 
data.href );
+                               throw new Error( 'Unknown externalData protocol 
' + uri.protocol );
                }
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I033a515e3561850fccaba8ab2be9457e040fcfe9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Kartographer
Gerrit-Branch: master
Gerrit-Owner: Yurik <yu...@wikimedia.org>

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

Reply via email to