Skip to content

Commit

Permalink
Add wikidata asynchronously based on wikipedia
Browse files Browse the repository at this point in the history
ref #2680
  • Loading branch information
1ec5 authored and bhousel committed May 23, 2016
1 parent 0cea4e9 commit 669cad7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
22 changes: 22 additions & 0 deletions js/id/services/wikidata.js
Original file line number Diff line number Diff line change
@@ -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;
};
20 changes: 16 additions & 4 deletions js/id/ui/preset/wikipedia.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 3 additions & 1 deletion test/spec/ui/preset/wikipedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 669cad7

Please sign in to comment.