Add sitelinks to result from wikidata getDocs

This commit is contained in:
Bryan Housel
2019-02-09 15:18:32 -05:00
parent 29ce6cb30c
commit d055f34be8
+30 -3
View File
@@ -109,6 +109,8 @@ export default {
return;
}
var i;
var description;
if (entity.descriptions && Object.keys(entity.descriptions).length > 0) {
description = entity.descriptions[Object.keys(entity.descriptions)[0]].value;
@@ -126,7 +128,7 @@ export default {
var imageroot = 'https://commons.wikimedia.org/w/index.php';
var props = ['P154','P18']; // logo image, image
var prop, image;
for (var i = 0; i < props.length; i++) {
for (i = 0; i < props.length; i++) {
prop = entity.claims[props[i]];
if (prop && Object.keys(prop).length > 0) {
image = prop[Object.keys(prop)[0]].mainsnak.datavalue.value;
@@ -141,8 +143,33 @@ export default {
}
}
// TODO add wiki sitelink
// result.wiki = ?
if (entity.sitelinks) {
// must be one of these that we requested..
var langs = _uniq([
currentLocale.toLowerCase(),
currentLocale.split('-', 2)[0].toLowerCase(),
'en'
]);
var englishLocale = (currentLocale.split('-', 2)[0].toLowerCase() === 'en');
for (i = 0; i < langs.length; i++) { // check each, in order of preference
var w = langs[i] + 'wiki';
if (entity.sitelinks[w]) {
var title = entity.sitelinks[w].title;
var tKey = 'inspector.wiki_reference';
if (!englishLocale && langs[i] === 'en') { // user's currentLocale isn't English but
tKey = 'inspector.wiki_en_reference'; // we are sending them to enwiki anyway..
}
result.wiki = {
title: title,
text: tKey,
url: 'https://' + langs[i] + '.wikipedia.org/wiki/' + title.replace(/ /g, '_')
};
break;
}
}
}
callback(null, result);
});