From fbe584c7ec3bffdb416f1c484eff561bc8dbc2da Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 8 Feb 2019 16:27:33 -0500 Subject: [PATCH] Lookup docs for some presets on wikidata, not wikibase (re: #5823) Now `uiTagReference` can accept a `qid` param for presets where we want this (such as brands) --- modules/presets/preset.js | 7 ++ modules/services/wikidata.js | 65 ++++++++++++++++++- modules/ui/tag_reference.js | 122 +++++++++++++++++++---------------- 3 files changed, 135 insertions(+), 59 deletions(-) diff --git a/modules/presets/preset.js b/modules/presets/preset.js index 1460f4842..61e251b9d 100644 --- a/modules/presets/preset.js +++ b/modules/presets/preset.js @@ -166,6 +166,13 @@ export function presetPreset(id, preset, fields, visible, rawPresets) { var reference = preset.reference || {}; preset.reference = function(geometry) { + // Lookup documentation on Wikidata... + var qid = preset.tags.wikidata || preset.tags['brand:wikidata'] || preset.tags['operator:wikidata']; + if (qid) { + return { qid: qid }; + } + + // Lookup documentation on OSM Wikibase... var key = reference.key || Object.keys(_omit(preset.tags, 'name'))[0]; var value = reference.value || preset.tags[key]; diff --git a/modules/services/wikidata.js b/modules/services/wikidata.js index 95fbef3bd..b4790fb2e 100644 --- a/modules/services/wikidata.js +++ b/modules/services/wikidata.js @@ -66,8 +66,8 @@ export default { format: 'json', formatversion: 2, ids: qid, - props: /*sitelinks|*/'labels|descriptions', - //sitefilter: lang + 'wiki', + props: 'labels|descriptions|claims|sitelinks', + sitefilter: langs.map(function(d) { return d + 'wiki'; }).join('|'), languages: langs.join('|'), languagefallback: 1, origin: '*' @@ -79,6 +79,67 @@ export default { callback(qid, data.entities[qid] || {}); } }); + }, + + + // Pass `params` object of the form: + // { + // qid: 'string' // brand wikidata (e.g. 'Q37158') + // } + // + // Get an result object used to display tag documentation + // { + // title: 'string', + // description: 'string', + // editURL: 'string', + // imageURL: 'string', + // wiki: { title: 'string', text: 'string', url: 'string' } + // } + // + getDocs: function(params, callback) { + this.entityByQID(params.qid, function(err, entity) { + if (err || !entity) { + callback(err); + return; + } + + var description; + if (entity.descriptions && Object.keys(entity.descriptions).length > 0) { + description = entity.descriptions[Object.keys(entity.descriptions)[0]].value; + } + + // prepare result + var result = { + title: entity.id, + description: description, + editURL: 'https://www.wikidata.org/wiki/' + entity.id + }; + + // add image + if (entity.claims) { + 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++) { + prop = entity.claims[props[i]]; + if (prop && Object.keys(prop).length > 0) { + image = prop[Object.keys(prop)[0]].mainsnak.datavalue.value; + if (image) { + result.imageURL = imageroot + '?' + utilQsString({ + title: 'Special:Redirect/file/' + image, + width: 400 + }); + break; + } + } + } + } + + // TODO add wiki sitelink + // result.wiki = ? + + callback(null, result); + }); } }; diff --git a/modules/ui/tag_reference.js b/modules/ui/tag_reference.js index 84ae00197..801ba69aa 100644 --- a/modules/ui/tag_reference.js +++ b/modules/ui/tag_reference.js @@ -8,17 +8,22 @@ import { services } from '../services'; import { svgIcon } from '../svg'; -// Pass `tag` object of the form: +// Pass `which` object of the form: // { // key: 'string', // required // value: 'string' // optional // } // -or- // { -// rtype: 'rtype' // relation type (e.g. 'multipolygon') +// rtype: 'string' // relation type (e.g. 'multipolygon') // } -export function uiTagReference(tag) { - var wikibase = services.osmWikibase; +// -or- +// { +// qid: 'string' // brand wikidata (e.g. 'Q37158') +// } +// +export function uiTagReference(what) { + var wikibase = what.qid ? services.wikidata : services.osmWikibase; var tagReference = {}; var _button = d3_select(null); @@ -33,66 +38,69 @@ export function uiTagReference(tag) { _button .classed('tag-reference-loading', true); - wikibase.getDocs(tag, function(err, docs) { - _body.html(''); + wikibase.getDocs(what, gotDocs); + } - if (!docs || !docs.title) { - _body - .append('p') - .attr('class', 'tag-reference-description') - .text(t('inspector.no_documentation_key')); - done(); - return; - } - if (docs.imageURL) { - _body - .append('img') - .attr('class', 'tag-reference-wiki-image') - .attr('src', docs.imageURL) - .on('load', function() { done(); }) - .on('error', function() { d3_select(this).remove(); done(); }); - } else { - done(); - } + function gotDocs(err, docs) { + _body.html(''); + if (!docs || !docs.title) { _body .append('p') .attr('class', 'tag-reference-description') - .text(docs.description || t('inspector.no_documentation_key')) + .text(t('inspector.no_documentation_key')); + done(); + return; + } + + if (docs.imageURL) { + _body + .append('img') + .attr('class', 'tag-reference-wiki-image') + .attr('src', docs.imageURL) + .on('load', function() { done(); }) + .on('error', function() { d3_select(this).remove(); done(); }); + } else { + done(); + } + + _body + .append('p') + .attr('class', 'tag-reference-description') + .text(docs.description || t('inspector.no_documentation_key')) + .append('a') + .attr('class', 'tag-reference-edit') + .attr('target', '_blank') + .attr('tabindex', -1) + .attr('title', t('inspector.edit_reference')) + .attr('href', docs.editURL) + .call(svgIcon('#iD-icon-edit', 'inline')); + + if (docs.wiki) { + _body + .append('a') + .attr('class', 'tag-reference-link') + .attr('target', '_blank') + .attr('tabindex', -1) + .attr('href', docs.wiki.url) + .call(svgIcon('#iD-icon-out-link', 'inline')) + .append('span') + .text(t(docs.wiki.text)); + } + + // Add link to info about "good changeset comments" - #2923 + if (what.key === 'comment') { + _body .append('a') - .attr('class', 'tag-reference-edit') + .attr('class', 'tag-reference-comment-link') .attr('target', '_blank') .attr('tabindex', -1) - .attr('title', t('inspector.edit_reference')) - .attr('href', docs.editURL) - .call(svgIcon('#iD-icon-edit', 'inline')); - - if (docs.wiki) { - _body - .append('a') - .attr('class', 'tag-reference-link') - .attr('target', '_blank') - .attr('tabindex', -1) - .attr('href', docs.wiki.url) - .call(svgIcon('#iD-icon-out-link', 'inline')) - .append('span') - .text(t(docs.wiki.text)); - } - - // Add link to info about "good changeset comments" - #2923 - if (tag.key === 'comment') { - _body - .append('a') - .attr('class', 'tag-reference-comment-link') - .attr('target', '_blank') - .attr('tabindex', -1) - .call(svgIcon('#iD-icon-out-link', 'inline')) - .attr('href', t('commit.about_changeset_comments_link')) - .append('span') - .text(t('commit.about_changeset_comments')); - } - }); + .call(svgIcon('#iD-icon-out-link', 'inline')) + .attr('href', t('commit.about_changeset_comments_link')) + .append('span') + .text(t('commit.about_changeset_comments')); + } } @@ -156,9 +164,9 @@ export function uiTagReference(tag) { tagReference.body = function(selection) { - var tagid = tag.rtype || (tag.key + '-' + tag.value); + var itemID = what.qid || what.rtype || (what.key + '-' + what.value); _body = selection.selectAll('.tag-reference-body') - .data([tagid], function(d) { return d; }); + .data([itemID], function(d) { return d; }); _body.exit() .remove();