Better guard code around wiki services, always call callbacks

This commit is contained in:
Bryan Housel
2016-09-28 14:42:30 -04:00
parent 774fe1e67a
commit 4a94474a3a
3 changed files with 47 additions and 29 deletions
+14 -1
View File
@@ -4,10 +4,17 @@ import { qsString } from '../util/index';
var wikidata = {},
endpoint = 'https://www.wikidata.org/w/api.php?';
export function init() {
// Given a Wikipedia language and article title, return an array of
// corresponding Wikidata entities.
wikidata.itemsByTitle = function(lang, title, callback) {
if (!title) {
callback('', {});
return;
}
lang = lang || 'en';
jsonpRequest(endpoint + qsString({
action: 'wbgetentities',
@@ -17,8 +24,14 @@ export function init() {
languages: 'en', // shrink response by filtering to one language
callback: '{callback}'
}), function(data) {
callback(title, data.entities || {});
if (!data || data.error) {
callback('', {});
} else {
callback(title, data.entities || {});
}
});
};
return wikidata;
}
+24 -22
View File
@@ -4,10 +4,11 @@ import { qsString } from '../util/index';
var wikipedia = {},
endpoint = 'https://en.wikipedia.org/w/api.php?';
export function init() {
wikipedia.search = function(lang, query, callback) {
if (!query) {
callback([]);
callback('', []);
return;
}
@@ -22,17 +23,17 @@ export function init() {
callback: '{callback}',
srsearch: query
}), function(data) {
if (!data || !data.query || !data.query.search) {
callback([]);
return;
if (!data || !data.query || !data.query.search || data.error) {
callback('', []);
} else {
var results = data.query.search.map(function(d) { return d.title; });
callback(query, results);
}
callback(query, data.query.search.map(function(d) {
return d.title;
}));
}
);
};
wikipedia.suggestions = function(lang, query, callback) {
if (!query) {
callback('', []);
@@ -48,16 +49,17 @@ export function init() {
format: 'json',
callback: '{callback}',
search: query
}), function(d) {
if (!d || d.error) {
}), function(data) {
if (!data || data.error) {
callback('', []);
return;
} else {
callback(data[0], data[1] || []);
}
callback(d[0], d[1]);
}
);
};
wikipedia.translations = function(lang, title, callback) {
if (!title) {
callback({});
@@ -72,23 +74,23 @@ export function init() {
callback: '{callback}',
lllimit: 500,
titles: title
}), function(d) {
if (!d || d.error) {
}), function(data) {
if (!data || !data.query || !data.query.pages || data.error) {
callback({});
return;
}
var list = d.query.pages[Object.keys(d.query.pages)[0]],
translations = {};
if (list && list.langlinks) {
list.langlinks.forEach(function(d) {
translations[d.lang] = d['*'];
});
} else {
var list = data.query.pages[Object.keys(data.query.pages)[0]],
translations = {};
if (list && list.langlinks) {
list.langlinks.forEach(function(d) {
translations[d.lang] = d['*'];
});
}
callback(translations);
}
}
);
};
return wikipedia;
}
+9 -6
View File
@@ -41,10 +41,11 @@ export function wikipedia(field, context) {
var titlecombo = d3combobox()
.fetcher(function(value, cb) {
if (!value) {
value = context.entity(entity.id).tags.name || '';
}
if (!value) value = context.entity(entity.id).tags.name || '';
var searchfn = value.length > 7 ? wikipedia.search : wikipedia.suggestions;
searchfn(language()[2], value, function(query, data) {
cb(data.map(function(d) {
return { value: d };
@@ -162,7 +163,10 @@ export function wikipedia(field, context) {
var initEntityId = entity.id,
initWikipedia = context.entity(initEntityId).tags.wikipedia;
wikidata.itemsByTitle(language()[2], value, function (title, data) {
wikidata.itemsByTitle(language()[2], value, function(title, data) {
if (!data || !Object.keys(data).length) return;
var qids = Object.keys(data);
// 1. most recent change was a tag change
var annotation = t('operations.change_tags.annotation'),
currAnnotation = context.history().undoAnnotation();
@@ -174,12 +178,11 @@ export function wikipedia(field, context) {
if (currEntityId !== initEntityId) return;
// 3. wikipedia value has not changed
var currTags = _.clone(context.entity(currEntityId).tags),
qids = data && Object.keys(data);
var currTags = _.clone(context.entity(currEntityId).tags);
if (initWikipedia !== currTags.wikipedia) return;
// ok to coalesce the update of wikidata tag into the previous tag change
currTags.wikidata = qids && _.find(qids, function (id) {
currTags.wikidata = qids && _.find(qids, function(id) {
return id.match(/^Q\d+$/);
});