From: Dan Scott Date: Tue, 19 Sep 2017 02:14:04 +0000 (-0400) Subject: Realtime Wikipedia description lookups X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=281e7517243edfd5eb32cd400b8f359373c10ff4;p=contrib%2FConifer.git Realtime Wikipedia description lookups These are English-only but provide the first sentence of the Wikipedia description of the performer, as well as a link to the related Wikipedia page via the WP icon. We also remove the non-functioning toggle code and generate unique DOM IDs for each of the musicians. Signed-off-by: Dan Scott --- diff --git a/Open-ILS/web/js/ui/default/opac/wikidata_music_card.js b/Open-ILS/web/js/ui/default/opac/wikidata_music_card.js index 35b06ad7d7..a71a334765 100644 --- a/Open-ILS/web/js/ui/default/opac/wikidata_music_card.js +++ b/Open-ILS/web/js/ui/default/opac/wikidata_music_card.js @@ -23,6 +23,7 @@ */ var icon_node; var note; + var img; var wd; /* Ensure this is a musical recording */ @@ -33,11 +34,15 @@ /* Insert clickable icon here */ icon_node = node.querySelector('span[property="description"]'); if (icon_node) { - note = document.createElement('span'); - note.class = 'wikidata'; - note.innerText = ' ♪'; - note.addEventListener('click', perform, { once: true }); - icon_node.insertBefore(note, null); + img = document.createElement('img'); + img.src = 'https://www.wikidata.org/static/favicon/wikidata.ico'; + img.alt = 'Show data from Wikidata'; + img.class = 'wikidata'; + img.style.width = '1em'; + img.style.paddingLeft = '0.5em'; + img.style.verticalAlign = 'middle'; + img.addEventListener('click', perform, { once: true }); + node.insertBefore(img, icon_node.nextSibling); } } } @@ -56,37 +61,19 @@ } function perform(e) { - var entity_name = getContribName(this.parentNode.parentNode); + var entity_name = getContribName(this.parentNode); findPerformer(icon_node, entity_name); e.preventDefault(); e.stopPropagation(); } - function toggleMode(el) { - wd = document.getElementById('magic_musician'); - wd.style.display = 'inherit'; - el.addEventListener('click', toggle); - } - - function toggle(e) { - wd = document.getElementById('magic_musician'); - - if (wd.style.display === 'inherit') { - wd.style.display = 'none'; - } else { - wd.style.display = 'inherit'; - } - e.preventDefault(); - e.stopPropagation(); - } - function findPerformer(icon_node, entity_name) { var url = 'https://query.wikidata.org/sparql'; var query = 'SELECT DISTINCT ?item ?itemLabel ?itemDescription ?image ?instrumentLabel ?birthPlace ?birthPlaceLabel ' + - '?website ?musicbrainz ?songKick ?twitter ?facebook ' + + '?website ?musicbrainz ?songKick ?twitter ?facebook ?wplink ' + 'WHERE { ' + '?item rdfs:label|skos:altLabel|wdt:P1449 "' + entity_name + '"@en . ' + - '{ ?item wdt:P31 wd:Q215380 . } ' + // band + '{ ?item wdt:P31/wdt:P279* wd:Q215380 . } ' + // band 'UNION ' + '{ ?item wdt:P31 wd:Q5741069 . } ' + // rock band 'UNION ' + @@ -101,6 +88,11 @@ 'OPTIONAL { ?item wdt:P2002 ?twitter } . ' + 'OPTIONAL { ?item wdt:P2013 ?facebook } . ' + 'OPTIONAL { ?item wdt:P18 ?image } . ' + + 'OPTIONAL { ' + + '?wplink schema:about ?item . ' + + '?wplink schema:inLanguage "en" . ' + + '?wplink schema:isPartOf . ' + + '} ' + 'SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } ' + '} ' + 'LIMIT 10'; @@ -114,7 +106,6 @@ var r = req.response.results.bindings[0]; if (r !== undefined) { generateCard(icon_node, r); - toggleMode(note); // console.log(r); } } @@ -124,7 +115,6 @@ var r = JSON.parse(req.responseText).results.bindings[0]; if (r !== undefined) { generateCard(icon_node, r); - toggleMode(note); // console.log(r); } } @@ -132,10 +122,54 @@ req.send(); } + function addWikipedia(musician, r) { + var wpapi = 'https://en.wikipedia.org/w/api.php?origin=*&format=json&action=query&prop=extracts&explaintext=true&exintro=true&titles='; + var wplink = r.wplink.value; + // Strip the WP title from the link + wptitle = wplink.substring(wplink.lastIndexOf('/') + 1); + + var req = new window.XMLHttpRequest(); + req.open('GET', wpapi + wptitle); + req.onload = function (evt) { + var r = JSON.parse(req.response); + var k = Object.getOwnPropertyNames(r.query.pages)[0]; + var description = r.query.pages[k].extract; + + // Just the first line will do + var linefeed = description.indexOf('\n'); + var period = description.indexOf('. '); + if (linefeed > 0) { + description = description.substring(0, linefeed); + } else if (period > 0) { + description = description.substring(0, period + 1); + } + var wdd = document.createElement('div'); + wdd.style['margin-top'] = '1em'; + var wpa = document.createElement('a'); + wpa.href = wplink; + + // Link to Wikipedia via their icon + var img = document.createElement('img'); + img.src = 'https://en.wikipedia.org/static/favicon/wikipedia.ico'; + img.alt = 'View on Wikipedia'; + img.class = 'wikidata'; + img.style.width = '1em'; + img.style.paddingLeft = '0.5em'; + + wpa.appendChild(img); + wdd.innerText = description; + wdd.appendChild(wpa); + musician.appendChild(wdd); + } + req.send(); + } + function generateCard(icon_node, r) { var auth_div = document.querySelector('div[class="rdetail_authors_div"]'); var musician = document.createElement('div'); - musician.id = 'magic_musician'; + var uri = r.item.value; + var wdid = uri.substr(uri.lastIndexOf('/') + 1); + musician.id = 'musician_' + wdid; musician.style.padding = '0.5em 1em 1em 1em'; musician.style.border = 'thin blue solid'; musician.style.overflow = 'hidden'; @@ -160,6 +194,11 @@ musician.appendChild(wdd); } + // Get the description from Wikipedia + if (r.hasOwnProperty('wplink')) { + addWikipedia(musician, r); + } + function addWDValue(property, propertyLabel, label, isLink, linkFormatter) { var value = ''; if (r.hasOwnProperty(property)) { @@ -209,13 +248,12 @@ addWDValue('twitter', 'Twitter: ', null, true, function(value) { return 'https://twitter.com/' + value }); addWDValue('facebook', 'Facebook: ', null, true, function(value) { return 'https://www.facebook.com/' + value }); - var uri = r.item.value; var wd = document.createElement('div'); var wdl = document.createElement('label'); wdl.innerText = 'Edit on Wikidata: '; var wdv = document.createElement('a'); wdv.href = uri; - wdv.innerText = uri.substr(uri.lastIndexOf('/') + 1); + wdv.innerText = wdid; wd.appendChild(wdl); wd.appendChild(wdv); musician.appendChild(wd); diff --git a/test_wikidata.html b/test_wikidata.html new file mode 100644 index 0000000000..3383a22d58 --- /dev/null +++ b/test_wikidata.html @@ -0,0 +1,331 @@ + + + + + + + + + The 1985 Juno Awards collection - Laurentian University + + + + + + + + + + + +

Catalog

+ + +
+ +
+ + + +

Record Details

+

Catalog Search

+ +
+ +
+ +
+ + + + + +
+ +
+
+

The 1985 Juno Awards collection

+ +
Adams, Bryan (Singer). Hart, Corey (Singer). Silver, Liberty (Singer). McLauchlan, Murray (Singer). Thompson, Don (Instrumentalist). Luba (Singer). Janz, Paul (Singer). lang, k.d. (Kathryn Dawn), 1961- (Singer). Idle Eyes (Performer). The Family Brown (Performer). Parachute Club (Performer). Oxford String Quartet (Performer). +
+ + + +
+
+ Image of item + +
+
+
+ + + + Phonograph music recording + Phonograph music recording + + +
+
+ +
+
+ Print / Email Actions Image + Print / + Email +
+ + + +
+
+
+ +
+

Available copies

+
    +
  • + 1 of 1 copy available at Conifer. + (Show) +
  • +
  • + 1 of 1 copy available at Laurentian University. +
  • +
+
+ + +

Current holds

+

+ 0 current holds with 1 total copy. +

+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LocationCall Number / Copy NotesBarcodeShelving LocationStatusDue Date
J.N. Desmarais Library + + + LP 3689 + 30007006013980 Long-play records (3rd floor)Available-
+
+
+ + +
+ +

Record details

+
    +
  • + Physical Description: + 1 sound disc : 33 1/3 rpm, stereo +
  • +
  • + Publisher: + + Montréal, Qué. : + Radio Canada International, + + 1986.
  • +
+ + +

Content descriptions

+ + + + + + +
Formatted Contents Note: Heaven / Adams -- Never surrender / Hart -- Lost somewhere inside your love / Silver -- Railroad man / McLauchlan -- Tokyo Rose / Idle Eyes -- I've never been in love before / Thompson -- Let it go / Luba -- Go to pieces / Janz -- Wouldn't you love us together again / The Family Brown -- At the feet of the moon / Parachute Club -- Pine and stew / lang -- Minuetto from Quartet in B flat major / Mozart.
+ + + + + + + + + + + + +
Subject: Popular music > Canada.
String quartets.
Country music > Canada.
+ + + +
+
+ +
+
+ +
+
+ + + + +
+ +
+ +
+
+ +
+
+ +
+ +
+
+ +
+
+ +
+ +
+
+ + + +
+ +
+
+
+
+

Additional Resources

+ + + + + + +