Wikidata music cards: use RWO URIs from $1 if available
authorDan Scott <dan@coffeecode.net>
Mon, 13 Jan 2020 01:47:52 +0000 (20:47 -0500)
committerDan Scott <dan@coffeecode.net>
Mon, 13 Jan 2020 02:24:19 +0000 (21:24 -0500)
If the bib record contains a $1 subfield that points to a Wikidata Real
World URI, such as http://www.wikidata.org/entity/Q709024, surface that
in the author's RDfa as a schema.org/sameAs relation.

For music cards, the presence of a Wikidata RWO URI enables us to skip
searching by name and serve up the data directly based on the RWO URI.

The explicit RWO URI avoids disambiguation problems as arises with names
such as "Alexander Young" who is both a Scottish musician and an English
tenor, and sets the stage for expanding the Wikidata entity card
functionality outside of the domain of music.

Signed-off-by: Dan Scott <dan@coffeecode.net>
Open-ILS/src/templates/opac/parts/record/authors.tt2
Open-ILS/web/js/ui/default/opac/wikidata_music_card.js
test_wikidata.html

index 25dac9a..0b0e978 100644 (file)
@@ -61,6 +61,7 @@ BLOCK build_author_links;
         birthdate = '';
         deathdate = '';
         graphics = [];
+        rwo_uris = [];
         tag = node.getAttribute('tag');
         FOR subfield IN node.childNodes;
             indexed_term = '';
@@ -79,6 +80,10 @@ BLOCK build_author_links;
                linked_fields = [subfield.textContent()];
                get_linked_880s;
             END;
+            # Gather $1 Real World Object (RWO) URIs - per http://www.loc.gov/marc/bibliographic/ecbdcntf.html
+            IF code == '1';
+               rwo_uris.push( subfield.textContent() );
+            END;
             NEXT UNLESS code.match('[a-z]');
             sf = subfield.textContent | html;
 
@@ -172,6 +177,12 @@ BLOCK build_author_links;
             authtml = authtml _ '</span>';
         END;
         authtml = authtml _ ' (<span property="description">' _ author_type _ '</span>). ';
+
+        # Embed RWO URIs
+        FOREACH rwo_uri IN rwo_uris;
+            authtml = authtml _ '<link property="sameAs" href="' _ rwo_uri _ '">';
+        END;
+
         authtml = authtml _ '</span>'; # End author span
         authlist.push(authtml);
     END;
index 2e4bb13..6596818 100644 (file)
     }
   }
 
+  function getWikidataId(node) {
+    var wikidata_id = node.querySelector("link[property='sameAs'][href^='http://www.wikidata.org/entity']");
+    if (wikidata_id) {
+      wikidata_id = wikidata_id.href.substring(wikidata_id.href.lastIndexOf('/') + 1);
+    }
+    return wikidata_id;
+  }
+
   function getContribName(node) {
     var raw_name = node.querySelector(contributor_name).textContent.trim();
     var lastchar = raw_name[raw_name.length - 1];
   }
 
   function perform(e) {
+    var wikidata_id = getWikidataId(this.parentNode);
     var entity_name = getContribName(this.parentNode);
-    findPerformer(icon_node, entity_name);
+    findPerformer(icon_node, entity_name, wikidata_id);
     e.preventDefault();
     e.stopPropagation();
   }
 
-  function findPerformer(icon_node, entity_name) {
+  function findPerformer(icon_node, entity_name, wikidata_id) {
     var url = 'https://query.wikidata.org/sparql';
-    var query = 'SELECT DISTINCT ?item ?itemLabel ?itemDescription ?image ' +
+    var query = Object;
+    query.select = 'SELECT DISTINCT ?item ?itemLabel ?itemDescription ?image ' +
         '(GROUP_CONCAT(DISTINCT ?instrumentLabel;separator="; ") AS ?instruments) ' +
         '?birthPlace ?birthPlaceLabel ' +
         '?website ?musicbrainz ?songKick ?twitter ?facebook ?wplink ' +
-        'WHERE { ' +
+        'WHERE { ';
+
+    // Default: search by entity name
+    query.where =
         '?item rdfs:label|skos:altLabel|wdt:P1449 "' + entity_name + '"@en . ' +
         '{ ?item wdt:P31/wdt:P279* wd:Q215380 . } ' + // instance of = any subclass of band
         'UNION ' +
         '{ ?item wdt:P106/wdt:P279* wd:Q639669 . } ' + // occupation = any subclass of musician
         'UNION ' +
-        '{ ?item wdt:P31/wdt:P279* wd:Q2088357 . } ' + // instance of = any subclass of musical ensemble
+        '{ ?item wdt:P31/wdt:P279* wd:Q2088357 . } ';  // instance of = any subclass of musical ensemble
+
+    // We have a Wikidata QID? Wonderful!
+    if (wikidata_id) {
+        query.where = 'VALUES ?item { wd:' + wikidata_id + ' }. ';
+    }
+
+    query.rest =
         'OPTIONAL { ?item wdt:P3478 ?songKick } . ' +
         'OPTIONAL { ?item wdt:P19 ?birthPlace } . ' +
         'OPTIONAL { ?item wdt:P1303 ?instrument } . ' +
     '} ' +
     'GROUP BY ?item ?itemLabel ?itemDescription ?image ?birthPlace ?birthPlaceLabel ?website ?musicbrainz ?songKick ?twitter ?facebook ?wplink'
     'LIMIT 10';
-    var q = '?query=' + encodeURIComponent(query);
+    var q = '?query=' + encodeURIComponent(query.select + query.where + query.rest);
 
     var req = new window.XMLHttpRequest();
     req.open('GET', url + q);
index 3383a22..ca9fe42 100644 (file)
@@ -82,7 +82,7 @@
     <div id='rdetail_title_div'>
         <h1 id='rdetail_title' property="name">The 1985 Juno Awards collection</h1>
 
-<div class='rdetail_authors_div'> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib1"><a href="/eg/opac/results?query=Adams%20%20Bryan;qtype=author" rel="nofollow"><span resource="#schemacontrib1"><span property="name">Adams, Bryan </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib2"><a href="/eg/opac/results?qtype=author;query=Hart%20%20Corey" rel="nofollow"><span resource="#schemacontrib2"><span property="name">Hart, Corey </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib3"><a href="/eg/opac/results?query=Silver%20%20Liberty;qtype=author" rel="nofollow"><span resource="#schemacontrib3"><span property="name">Silver, Liberty </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib4"><a href="/eg/opac/results?qtype=author;query=McLauchlan%20%20Murray" rel="nofollow"><span resource="#schemacontrib4"><span property="name">McLauchlan, Murray </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/itr" resource="#schemacontrib5"><a href="/eg/opac/results?qtype=author;query=Thompson%20%20Don" rel="nofollow"><span resource="#schemacontrib5"><span property="name">Thompson, Don </span></span></a> (<span property="description">Instrumentalist</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib6"><a href="/eg/opac/results?query=Luba;qtype=author" rel="nofollow"><span resource="#schemacontrib6"><span property="name">Luba </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib7"><a href="/eg/opac/results?query=Janz%20%20Paul;qtype=author" rel="nofollow"><span resource="#schemacontrib7"><span property="name">Janz, Paul </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib8"><a href="/eg/opac/results?qtype=author;query=lang%20%20k%20d%20%20%20Kathryn%20Dawn%20%201961" rel="nofollow"><span resource="#schemacontrib8"><span property="name">lang, k.d. (Kathryn Dawn),</span> <span property="birthDate">1961</span>-</span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Organization" property="contributor http://id.loc.gov/vocabulary/relators/prf" resource="#schemacontrib9"><a href="/eg/opac/results?qtype=author;query=Idle%20Eyes" rel="nofollow"><span resource="#schemacontrib9"><span property="name">Idle Eyes </span></span></a> (<span property="description">Performer</span>). </span> <span class="rdetail-author-div" typeof="Organization" property="contributor http://id.loc.gov/vocabulary/relators/prf" resource="#schemacontrib10"><a href="/eg/opac/results?qtype=author;query=The%20Family%20Brown" rel="nofollow"><span resource="#schemacontrib10"><span property="name">The Family Brown </span></span></a> (<span property="description">Performer</span>). </span> <span class="rdetail-author-div" typeof="Organization" property="contributor http://id.loc.gov/vocabulary/relators/prf" resource="#schemacontrib11"><a href="/eg/opac/results?qtype=author;query=Parachute%20Club" rel="nofollow"><span resource="#schemacontrib11"><span property="name">Parachute Club </span></span></a> (<span property="description">Performer</span>). </span> <span class="rdetail-author-div" typeof="Organization" property="contributor http://id.loc.gov/vocabulary/relators/prf" resource="#schemacontrib12"><a href="/eg/opac/results?qtype=author;query=Oxford%20String%20Quartet" rel="nofollow"><span resource="#schemacontrib12"><span property="name">Oxford String Quartet </span></span></a> (<span property="description">Performer</span>). </span>
+        <div class='rdetail_authors_div'> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib1"><a href="/eg/opac/results?query=Adams%20%20Bryan;qtype=author" rel="nofollow"><span resource="#schemacontrib1"><span property="name">Adams, Bryan </span></span></a> (<span property="description">Singer</span><link property="sameAs" href="http://www.wikidata.org/entity/Q482907">). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib2"><a href="/eg/opac/results?qtype=author;query=Hart%20%20Corey" rel="nofollow"><span resource="#schemacontrib2"><span property="name">Hart, Corey </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib3"><a href="/eg/opac/results?query=Silver%20%20Liberty;qtype=author" rel="nofollow"><span resource="#schemacontrib3"><span property="name">Silver, Liberty </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib4"><a href="/eg/opac/results?qtype=author;query=McLauchlan%20%20Murray" rel="nofollow"><span resource="#schemacontrib4"><span property="name">McLauchlan, Murray </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/itr" resource="#schemacontrib5"><a href="/eg/opac/results?qtype=author;query=Thompson%20%20Don" rel="nofollow"><span resource="#schemacontrib5"><span property="name">Thompson, Don </span></span></a> (<span property="description">Instrumentalist</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib6"><a href="/eg/opac/results?query=Luba;qtype=author" rel="nofollow"><span resource="#schemacontrib6"><span property="name">Luba </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib7"><a href="/eg/opac/results?query=Janz%20%20Paul;qtype=author" rel="nofollow"><span resource="#schemacontrib7"><span property="name">Janz, Paul </span></span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Person" property="contributor http://id.loc.gov/vocabulary/relators/sng" resource="#schemacontrib8"><a href="/eg/opac/results?qtype=author;query=lang%20%20k%20d%20%20%20Kathryn%20Dawn%20%201961" rel="nofollow"><span resource="#schemacontrib8"><span property="name">lang, k.d. (Kathryn Dawn),</span> <span property="birthDate">1961</span>-</span></a> (<span property="description">Singer</span>). </span> <span class="rdetail-author-div" typeof="Organization" property="contributor http://id.loc.gov/vocabulary/relators/prf" resource="#schemacontrib9"><a href="/eg/opac/results?qtype=author;query=Idle%20Eyes" rel="nofollow"><span resource="#schemacontrib9"><span property="name">Idle Eyes </span></span></a> (<span property="description">Performer</span>). </span> <span class="rdetail-author-div" typeof="Organization" property="contributor http://id.loc.gov/vocabulary/relators/prf" resource="#schemacontrib10"><a href="/eg/opac/results?qtype=author;query=The%20Family%20Brown" rel="nofollow"><span resource="#schemacontrib10"><span property="name">The Family Brown </span></span></a> (<span property="description">Performer</span>). </span> <span class="rdetail-author-div" typeof="Organization" property="contributor http://id.loc.gov/vocabulary/relators/prf" resource="#schemacontrib11"><a href="/eg/opac/results?qtype=author;query=Parachute%20Club" rel="nofollow"><span resource="#schemacontrib11"><span property="name">Parachute Club </span></span></a> (<span property="description">Performer</span>). </span> <span class="rdetail-author-div" typeof="Organization" property="contributor http://id.loc.gov/vocabulary/relators/prf" resource="#schemacontrib12"><a href="/eg/opac/results?qtype=author;query=Oxford%20String%20Quartet" rel="nofollow"><span resource="#schemacontrib12"><span property="name">Oxford String Quartet </span></span></a> (<span property="description">Performer</span>). </span>
 </div>