mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-15 13:38:26 +02:00
committed by
Bryan Housel
parent
0cea4e9d27
commit
669cad74f6
@@ -43,6 +43,7 @@
|
||||
<script src='js/id/services/mapillary.js'></script>
|
||||
<script src='js/id/services/nominatim.js'></script>
|
||||
<script src='js/id/services/taginfo.js'></script>
|
||||
<script src='js/id/services/wikidata.js'></script>
|
||||
<script src='js/id/services/wikipedia.js'></script>
|
||||
|
||||
<script src='data/data_dev.js'></script>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
iD.services.wikidata = function() {
|
||||
var wiki = {},
|
||||
endpoint = 'https://www.wikidata.org/w/api.php?';
|
||||
|
||||
// Given a Wikipedia language and article title, return an array of
|
||||
// corresponding Wikidata entities.
|
||||
wiki.itemsByTitle = function(lang, title, callback) {
|
||||
lang = lang || 'en';
|
||||
d3.jsonp(endpoint + iD.util.qsString({
|
||||
action: 'wbgetentities',
|
||||
format: 'json',
|
||||
sites: lang.replace(/-/g, '_') + 'wiki',
|
||||
titles: title,
|
||||
languages: 'en', // shrink response by filtering to one language
|
||||
callback: '{callback}'
|
||||
}), function(data) {
|
||||
callback(title, data.entities || {});
|
||||
});
|
||||
};
|
||||
|
||||
return wiki;
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
iD.ui.preset.wikipedia = function(field, context) {
|
||||
var dispatch = d3.dispatch('change'),
|
||||
wikipedia = iD.services.wikipedia(),
|
||||
wikidata = iD.services.wikidata(),
|
||||
link, entity, lang, title;
|
||||
|
||||
function i(selection) {
|
||||
@@ -88,7 +89,8 @@ iD.ui.preset.wikipedia = function(field, context) {
|
||||
var value = title.value(),
|
||||
m = value.match(/https?:\/\/([-a-z]+)\.wikipedia\.org\/(?:wiki|\1-[-a-z]+)\/([^#]+)(?:#(.+))?/),
|
||||
l = m && _.find(iD.data.wikipedia, function(d) { return m[1] === d[2]; }),
|
||||
anchor;
|
||||
anchor,
|
||||
syncTags = {};
|
||||
|
||||
if (l) {
|
||||
// Normalize title http://www.mediawiki.org/wiki/API:Query#Title_normalization
|
||||
@@ -107,9 +109,19 @@ iD.ui.preset.wikipedia = function(field, context) {
|
||||
title.value(value);
|
||||
}
|
||||
|
||||
var t = {};
|
||||
t[field.key] = value ? language()[2] + ':' + value : undefined;
|
||||
dispatch.change(t);
|
||||
syncTags[field.key] = value ? language()[2] + ':' + value : undefined;
|
||||
dispatch.change(syncTags);
|
||||
|
||||
if (value && language()[2]) {
|
||||
wikidata.itemsByTitle(language()[2], value, function (title, entities) {
|
||||
var qids = entities && Object.keys(entities),
|
||||
asyncTags = {};
|
||||
asyncTags.wikidata = qids && _.find(qids, function (id) {
|
||||
return id.match(/^Q\d+$/);
|
||||
});
|
||||
dispatch.change(asyncTags);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
i.tags = function(tags) {
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
<script src='../js/id/services/mapillary.js'></script>
|
||||
<script src='../js/id/services/nominatim.js'></script>
|
||||
<script src='../js/id/services/taginfo.js'></script>
|
||||
<script src='../js/id/services/wikidata.js'></script>
|
||||
<script src='../js/id/services/wikipedia.js'></script>
|
||||
|
||||
<script src='../data/data_dev.js'></script>
|
||||
|
||||
@@ -27,7 +27,9 @@ describe('iD.ui.preset.wikipedia', function() {
|
||||
happen.once(selection.selectAll('.wiki-lang').node(), {type: 'change'});
|
||||
|
||||
wikipedia.on('change', function(tags) {
|
||||
expect(tags).to.eql({wikipedia: 'de:Title'});
|
||||
expect(tags).to.satisfy(function (tags) {
|
||||
return tags.wikipedia === 'de:Title' || 'wikidata' in tags;
|
||||
});
|
||||
});
|
||||
|
||||
selection.selectAll('.wiki-title').value('Title');
|
||||
|
||||
Reference in New Issue
Block a user