Add wikidata asynchronously based on wikipedia

ref #2680
This commit is contained in:
Minh Nguyễn
2015-07-18 01:14:17 -05:00
committed by Bryan Housel
parent 0cea4e9d27
commit 669cad74f6
5 changed files with 43 additions and 5 deletions
+1
View File
@@ -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>
+22
View File
@@ -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;
};
+16 -4
View File
@@ -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) {
+1
View File
@@ -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>
+3 -1
View File
@@ -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');