User:So9q/wikidata-orcid-scraper-link.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Function to fetch data using SPARQL query
function fetchDataUsingSPARQL(entityId, propertyId) {
// SPARQL query to get the best rank value of a property
const sparqlQuery = `
SELECT ?value WHERE {
wd:${entityId} p:${propertyId} ?statement.
?statement a wikibase:BestRank.
?statement ps:${propertyId} ?value.
}
`;
console.log(sparqlQuery);
// SPARQL endpoint URL
const sparqlEndpointUrl = 'https://query.wikidata.org/sparql';
// Constructing the query URL
const queryUrl = sparqlEndpointUrl + '?query=' + encodeURIComponent(sparqlQuery) + '&format=json';
// Fetching data from the SPARQL endpoint and returning a Promise
return fetch(queryUrl)
.then(response => response.json())
.then(data => {
// Check if results exist
if (data.results.bindings.length > 0) {
// Extracting the value from the response
const value = data.results.bindings[0].value.value;
// Log the retrieved value
//console.log("Best Rank Value:", value);
return value;
} else {
//console.log("No best rank value found for property:", propertyId);
return null; // Return null if no value found
}
})
.catch(error => {
console.error('Error fetching data:', error);
return null; // Return null in case of error
});
}
(function (mw, $) {
var entityId = mw.config.get('wbEntityId');
if (!entityId) {
return;
}
var orcid_length = $('#P496').length;
//console.log('orcid_length: ' + orcid_length);
if (orcid_length >= 1) {
// Fetch best ranked orcid
fetchDataUsingSPARQL(entityId, "P496")
.then(orcid => {
console.log(orcid);
if (orcid && orcid !== "") {
//console.log("got non-empty orcid");
// Add portlet link for item
mw.util.addPortletLink(
'p-tb',
'https://orcid-scraper.toolforge.org/results?qid=' + entityId + '&orcid=' + orcid,
'Wikidata Orcid Scraper',
't-os',
'This Wikidata item in Wikidata Orcid Scraper'
);
}
});
}
}(mediaWiki, jQuery));