Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the URL of an item via wikidata P953 value #237

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/itemWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,14 @@ export default class ItemWrapper{
}

setPID(type, value, save=true) {
type = type.toUpperCase();

// the url field type as to be stay in lower case
if (type != 'url') {
type = type.toUpperCase();
}
switch (type) {
case 'DOI':
case 'ISBN':
case 'ISBN', 'url':
if (this.isValidField(type)) {
this.item.setField(type, value);
} else {
Expand Down
27 changes: 27 additions & 0 deletions src/zoteroOverlay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,32 @@ const zoteroOverlay = {
}
},

/**
* Set the URL value of a Zotero item with the value of it's P953 wikidata element statement
*/
fetchOpenAccessUrls: async function(menuName) {
const items = await this.getSelectedItems(menuName);

for (const item of items) {
// get the QID of the item
const qid = item.getPID('QID');

// fetch the value of the P953 property of the element via the Wikidata API
const url = `https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=${qid}&props=claims&languages=en`;
const response = await Zotero.HTTP.request('GET', url);
const data = JSON.parse(response.responseText);
const claims = data.entities[qid].claims;
const propertyId = 'P953';

if (claims[propertyId]) {
const value = claims[propertyId][0].mainsnak.datavalue.value;

// set the URL field with the new Open Access URL
item.setPID('url', value);
}
}
},

syncWithWikidata: async function(menuName) {
const items = await this.getSelectedItems(menuName);
if (items.length) {
Expand Down Expand Up @@ -1057,6 +1083,7 @@ const zoteroOverlay = {
createMenuItems: function(menuName, menuPopup, IDPrefix, elementsAreRoot, doc) {
const menuFunctions = [
'fetchQIDs',
'fetchOpenAccessUrls',
'syncWithWikidata',
'getFromCrossref',
'getFromOCC',
Expand Down
3 changes: 2 additions & 1 deletion static/chrome/locale/en-US/wikicite.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ wikicite.editor.title = Citation Editor
wikicite.extract.unsupported = Extracting citations from file attachments not yet supported.
wikicite.global.name = Cita
wikicite.global.unsupported = Unsupported
wikicite.item-menu.autolink-citations = Auto link citations with Zotero items
wikicite.item-menu.autolink-citations = Auto link citations with Zotero items
wikicite.item-menu.export-file = Export citations to file
wikicite.item-menu.export-croci = Export citations to CROCI
wikicite.item-menu.fetch-citation-qids = Fetch QIDs for cited items
Expand Down Expand Up @@ -146,6 +146,7 @@ wikicite.source-item.import.progress.error = Importing citations failed
wikicite.source-item.import.progress.loading = Importing citations...
wikicite.source-item.import.progress.none = No citations detected
wikicite.source-item.sync-single-citation.unsupported = Syncing individual citations with Wikidata not yet supported
wikicite.submenu.fetch-open-access-urls = Fetch open access url(s)
wikicite.submenu.add-as-citations = Add as citation(s) to...
wikicite.submenu.fetch-qids = Fetch QID(s)
wikicite.submenu.get-from-attachments = Extract citations from attachments
Expand Down
2 changes: 2 additions & 0 deletions static/chrome/locale/fr/wikicite.properties
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ wikicite.source-item.import.progress.error=L’importation de citations a échou
wikicite.source-item.import.progress.loading=Importation de citations...
wikicite.source-item.import.progress.none=Aucune citation détectée
wikicite.source-item.sync-single-citation.unsupported=Synchronisation de citations individuelles avec Wikidata non encore prise en charge
# Fuzzy
wikicite.submenu.fetch-open-access-urls=Obtenir les urls en libre accès
wikicite.submenu.add-as-citations=Ajouter comme citations vers...
wikicite.submenu.fetch-qids=Obtenir les QID
wikicite.submenu.get-from-attachments=Extraire les citations des pièces jointes
Expand Down