mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-15 13:38:26 +02:00
@@ -5,7 +5,7 @@ import { json as d3_json } from 'd3-request';
|
||||
import { utilQsString } from '../util';
|
||||
import { currentLocale } from '../util/locale';
|
||||
|
||||
var endpoint = 'https://www.wikidata.org/w/api.php?';
|
||||
var apibase = 'https://www.wikidata.org/w/api.php?';
|
||||
var _wikidataCache = {};
|
||||
|
||||
export default {
|
||||
@@ -21,13 +21,13 @@ export default {
|
||||
// corresponding Wikidata entities.
|
||||
itemsByTitle: function(lang, title, callback) {
|
||||
if (!title) {
|
||||
callback('', {});
|
||||
callback('No title', {});
|
||||
return;
|
||||
}
|
||||
|
||||
lang = lang || 'en';
|
||||
|
||||
d3_json(endpoint + utilQsString({
|
||||
d3_json(apibase + utilQsString({
|
||||
action: 'wbgetentities',
|
||||
format: 'json',
|
||||
formatversion: 2,
|
||||
@@ -36,10 +36,13 @@ export default {
|
||||
languages: 'en', // shrink response by filtering to one language
|
||||
origin: '*'
|
||||
}), function(err, data) {
|
||||
if (err || !data || data.error) {
|
||||
callback('', {});
|
||||
if (data && data.error) {
|
||||
err = data.error;
|
||||
}
|
||||
if (err) {
|
||||
callback(err, {});
|
||||
} else {
|
||||
callback(title, data.entities || {});
|
||||
callback(null, data.entities || {});
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -47,11 +50,11 @@ export default {
|
||||
|
||||
entityByQID: function(qid, callback) {
|
||||
if (!qid) {
|
||||
callback('', {});
|
||||
callback('No qid', {});
|
||||
return;
|
||||
}
|
||||
if (_wikidataCache[qid]) {
|
||||
callback('', _wikidataCache[qid]);
|
||||
callback(null, _wikidataCache[qid]);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -61,7 +64,7 @@ export default {
|
||||
'en'
|
||||
]);
|
||||
|
||||
d3_json(endpoint + utilQsString({
|
||||
d3_json(apibase + utilQsString({
|
||||
action: 'wbgetentities',
|
||||
format: 'json',
|
||||
formatversion: 2,
|
||||
@@ -72,11 +75,14 @@ export default {
|
||||
languagefallback: 1,
|
||||
origin: '*'
|
||||
}), function(err, data) {
|
||||
if (err || !data || data.error) {
|
||||
callback('', {});
|
||||
if (data && data.error) {
|
||||
err = data.error;
|
||||
}
|
||||
if (err) {
|
||||
callback(err, {});
|
||||
} else {
|
||||
_wikidataCache[qid] = data.entities[qid];
|
||||
callback(qid, data.entities[qid] || {});
|
||||
callback(null, data.entities[qid] || {});
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -99,7 +105,7 @@ export default {
|
||||
getDocs: function(params, callback) {
|
||||
this.entityByQID(params.qid, function(err, entity) {
|
||||
if (err || !entity) {
|
||||
callback(err);
|
||||
callback(err || 'No entity');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -138,43 +138,54 @@ export function uiFieldWikidata(field) {
|
||||
|
||||
wiki.tags = function(tags) {
|
||||
var value = tags[field.key] || '';
|
||||
var matches = value.match(/^Q[0-9]*$/);
|
||||
|
||||
utilGetSetValue(title, value);
|
||||
|
||||
// value in correct format
|
||||
if (matches) {
|
||||
_wikiURL = 'https://wikidata.org/wiki/' + value;
|
||||
wikidata.entityByQID(value, function(qid, entity) {
|
||||
var label = '';
|
||||
var description = '';
|
||||
if (!/^Q[0-9]*$/.test(value)) { // not a proper QID
|
||||
unrecognized();
|
||||
return;
|
||||
}
|
||||
|
||||
if (entity.labels && Object.keys(entity.labels).length > 0) {
|
||||
label = entity.labels[Object.keys(entity.labels)[0]].value;
|
||||
}
|
||||
if (entity.descriptions && Object.keys(entity.descriptions).length > 0) {
|
||||
description = entity.descriptions[Object.keys(entity.descriptions)[0]].value;
|
||||
}
|
||||
// QID value in correct format
|
||||
_wikiURL = 'https://wikidata.org/wiki/' + value;
|
||||
wikidata.entityByQID(value, function(err, entity) {
|
||||
if (err) {
|
||||
unrecognized();
|
||||
return;
|
||||
}
|
||||
|
||||
d3_select('.preset-wikidata-label')
|
||||
.style('display', function(){
|
||||
return label.length > 0 ? 'flex' : 'none';
|
||||
})
|
||||
.select('input')
|
||||
.attr('value', label);
|
||||
var label = '';
|
||||
var description = '';
|
||||
|
||||
d3_select('.preset-wikidata-description')
|
||||
.style('display', function(){
|
||||
return description.length > 0 ? 'flex' : 'none';
|
||||
})
|
||||
.select('input')
|
||||
.attr('value', description);
|
||||
});
|
||||
if (entity.labels && Object.keys(entity.labels).length > 0) {
|
||||
label = entity.labels[Object.keys(entity.labels)[0]].value;
|
||||
}
|
||||
if (entity.descriptions && Object.keys(entity.descriptions).length > 0) {
|
||||
description = entity.descriptions[Object.keys(entity.descriptions)[0]].value;
|
||||
}
|
||||
|
||||
d3_select('.preset-wikidata-label')
|
||||
.style('display', function(){
|
||||
return label.length > 0 ? 'flex' : 'none';
|
||||
})
|
||||
.select('input')
|
||||
.attr('value', label);
|
||||
|
||||
d3_select('.preset-wikidata-description')
|
||||
.style('display', function(){
|
||||
return description.length > 0 ? 'flex' : 'none';
|
||||
})
|
||||
.select('input')
|
||||
.attr('value', description);
|
||||
});
|
||||
|
||||
|
||||
// not a proper QID
|
||||
function unrecognized() {
|
||||
d3_select('.preset-wikidata-label')
|
||||
.style('display', 'none');
|
||||
d3_select('.preset-wikidata-description')
|
||||
.style('display', 'none');
|
||||
|
||||
// unrecognized value format
|
||||
} else {
|
||||
d3_select('.preset-wikidata-label').style('display', 'none');
|
||||
d3_select('.preset-wikidata-description').style('display', 'none');
|
||||
if (value && value !== '') {
|
||||
_wikiURL = 'https://wikidata.org/wiki/Special:Search?search=' + value;
|
||||
} else {
|
||||
|
||||
@@ -200,7 +200,9 @@ export function uiFieldWikipedia(field, context) {
|
||||
var initGraph = context.graph();
|
||||
var initEntityID = _entity.id;
|
||||
|
||||
wikidata.itemsByTitle(language()[2], value, function(title, data) {
|
||||
wikidata.itemsByTitle(language()[2], value, function(err, data) {
|
||||
if (err) return;
|
||||
|
||||
// If graph has changed, we can't apply this update.
|
||||
if (context.graph() !== initGraph) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user