Last active
June 14, 2023 14:02
-
-
Save zuphilip/90acdc3eac4109830db1b3ab855fcb24 to your computer and use it in GitHub Desktop.
Zotero Script for looking up ISSN and other fields in Wikidata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Zotero script look up the ISSN of the selected items in Wikidata. | |
// If results are found then the QIDs are saved in the corresponding | |
// items in the extra field with the label P1433 (published in). | |
// Moreover, it can do the same with ISBN or link to the publisher, place. | |
// Some warnings are given for the other cases; thus watch the | |
// Zotero error console as well during execution of the script. [CC0] | |
// CHOOSE (i.e. uncomment) one property here: | |
var property = "ISSN"; | |
// var property = "publisher"; | |
// var property = "place"; | |
// var property = "ISBN"; | |
mappingProperties = { | |
// for journal article | |
"ISSN": { | |
"query": "P236", | |
"exportProperty": "P1433" | |
}, | |
// for books, chapters, etc. | |
"publisher": { | |
"query": "label", | |
"exportProperty": "P123" | |
}, | |
// for books chapters, etc. | |
"place": { | |
"query": "label", | |
"exportProperty": "P291" | |
}, | |
// for chapters etc. but not for books iteself | |
"ISBN": { | |
"query": "P212|wdt:957", | |
"exportProperty": "P1433", | |
"restrictItemTypes": ["bookSection", "conferencePaper", "dictionaryEntry", "encyclopediaArticle", "journalArticle", "magazineArticle", "newspaperArticle"] | |
} | |
}; | |
var items = Zotero.getActiveZoteroPane().getSelectedItems(); | |
var map = []; | |
for (let item of items) { | |
if ("restrictItemTypes" in mappingProperties[property]) { | |
let itemType = Zotero.ItemTypes.getName(item.itemTypeID); | |
if (!mappingProperties[property]["restrictItemTypes"].includes(itemType)) { | |
continue; | |
} | |
} | |
let valueString = item.getField(property); | |
if (valueString) { | |
if (property == "ISSN" || property == "ISBN") { | |
for (let val of valueString.split(", ")) { | |
if (!(val in map)) { | |
map[val] = []; | |
} | |
map[val].push(item.id); | |
} | |
} | |
else { | |
if (!(valueString in map)) { | |
map[valueString] = []; | |
} | |
map[valueString].push(item.id); | |
} | |
} | |
else { | |
Zotero.log("Skipped id " + item.id + " because no " + property); | |
} | |
} | |
if (Object.keys(map).length == 0) { | |
return "Exiting, because no (restricted) item has field " + property; | |
} | |
var queryUrl = "https://query.wikidata.org/sparql?query="; | |
if (mappingProperties[property]["query"] == "label") { | |
var valuesString = '"' + Object.keys(map).join('"@en "') + '"@en'; | |
var query = encodeURIComponent('SELECT * WHERE { VALUES ?v { ' + valuesString + '} ?item rdfs:label ?v . }'); | |
} else { | |
var valuesString = '"' + Object.keys(map).join('" "') + '"'; | |
var query = encodeURIComponent('SELECT * WHERE { VALUES ?v { ' + valuesString + '} ?item wdt:' + mappingProperties[property]["query"] + ' ?v . }'); | |
} | |
var suffix = "&format=json"; | |
var url = queryUrl + query+ suffix; | |
Zotero.log("URL " + url); | |
await Zotero.HTTP.doGet(url, function (obj) { | |
data = JSON.parse(obj.responseText); | |
Zotero.log(obj.responseText); | |
var results = data.results.bindings; | |
for (let result of results) { | |
let val = result.v.value; | |
// val has to be unique within the results to be added | |
if (results.filter(res => res.v.value == val).length > 1) { | |
Zotero.log("Several entries found for " + val + " and therefore skipping."); | |
continue; | |
} | |
let qid = result.item.value.replace("http://www.wikidata.org/entity/", ""); | |
for (id of map[val]) { | |
let item = Zotero.Items.get(id); | |
let extra = item.getField("extra") || ""; | |
let extraLines = extra.split("\n"); | |
let skip = false; | |
for (line of extraLines) { | |
if (line.startsWith(mappingProperties[property]["exportProperty"] + ":") && line.endsWith(qid)) { | |
skip = true; | |
} | |
} | |
if (!skip) { | |
item.setField("extra", extra + "\n" + mappingProperties[property]["exportProperty"] + ": " + qid); | |
item.saveTx(); | |
} | |
else { | |
Zotero.log("Skipped id " + id + " because " + mappingProperties[property]["exportProperty"] + " " + qid + " already there"); | |
} | |
} | |
} | |
if (results.length == 0) { | |
Zotero.log("Nothing found on Wikidata."); | |
} | |
}); | |
return map; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment