Group and display instruments feature/wikidata_music_card
authorDan Scott <dscott@laurentian.ca>
Sun, 25 Mar 2018 15:16:26 +0000 (11:16 -0400)
committerDan Scott <dscott@laurentian.ca>
Sun, 25 Mar 2018 15:25:14 +0000 (11:25 -0400)
For multi-instrumentalists like Joni Mitchell, this offers a better
representation of their diverse skills. A side effect of the GROUP_CONCAT()
is that a value is now always returned for the instruments variable, even
if the performer has no associated instruments; avoid displaying it if it is
nothing more than whitespace.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/web/js/ui/default/opac/wikidata_music_card.js

index cf47304..2e4bb13 100644 (file)
@@ -86,7 +86,9 @@
 
   function findPerformer(icon_node, entity_name) {
     var url = 'https://query.wikidata.org/sparql';
-    var query = 'SELECT DISTINCT ?item ?itemLabel ?itemDescription ?image ?instrumentLabel ?birthPlace ?birthPlaceLabel ' +
+    var query = 'SELECT DISTINCT ?item ?itemLabel ?itemDescription ?image ' +
+        '(GROUP_CONCAT(DISTINCT ?instrumentLabel;separator="; ") AS ?instruments) ' +
+        '?birthPlace ?birthPlaceLabel ' +
         '?website ?musicbrainz ?songKick ?twitter ?facebook ?wplink ' +
         'WHERE { ' +
         '?item rdfs:label|skos:altLabel|wdt:P1449 "' + entity_name + '"@en . ' +
           '?wplink schema:inLanguage "en" . ' +
           '?wplink schema:isPartOf <https://en.wikipedia.org/> . ' +
         '} ' +
-        'SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } ' +
+        'SERVICE wikibase:label { ' +
+        '  bd:serviceParam wikibase:language "en". ' +
+        '  ?instrument rdfs:label ?instrumentLabel. ' +
+        '  ?item rdfs:label ?itemLabel. ' +
+        '  ?item schema:description ?itemDescription. ' +
+        '  ?birthPlace rdfs:label ?birthPlaceLabel ' +
+        '} ' +
     '} ' +
+    'GROUP BY ?item ?itemLabel ?itemDescription ?image ?birthPlace ?birthPlaceLabel ?website ?musicbrainz ?songKick ?twitter ?facebook ?wplink'
     'LIMIT 10';
     var q = '?query=' + encodeURIComponent(query);
 
 
     function addWDValue(property, propertyLabel, label, isLink, linkFormatter) {
       var value = '';
-      if (r.hasOwnProperty(property)) {
+      if (r.hasOwnProperty(property) && r[property].value.trim()) {
         value = r[property].value;
         var valueDiv = document.createElement('div');
         var labelText = value;
       }
     }
 
-    addWDValue('instrumentLabel', 'Instrument: ', 'instrumentLabel', false);
+    addWDValue('instruments', 'Instruments: ', 'instruments', false);
     addWDValue('birthPlace', 'Birth place: ', 'birthPlaceLabel', true);
     addWDValue('website', 'Web site: ', null, true);
     addWDValue('musicbrainz', null, 'Discography (Musicbrainz)', true, function(value) { return 'https://musicbrainz.org/artist/' + value });