User:Karima Rafes/DisplayDataOfWikidata.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() {
var idWikidata = null;
var versionLast = null;
var versionInDatabase = null;
var wgWikibaseItemId = mw.config.get("wgWikibaseItemId");
if (!wgWikibaseItemId) {
console.log("no wikidata");
return;
}
//var iriWikidata = null;
//var patternWikibase = /t-wikibase.*href=\"[^\'\"]+(Q[0-9]+)\"/;
//var str = document.documentElement.innerHTML;
//var matchesWikibase = patternWikibase.exec(str);
//if ( matchesWikibase !== null) {
//iriWikidata = "http:"+ matchesWikibase[1];
//idWikidata = (/Q[0-9]+/.exec(iriWikidata))[0];
linkEntity ="https://www.wikidata.org/wiki/Special:EntityData/"+wgWikibaseItemId;
//if(iriWikidata !== null){
$("#firstHeading").append(
"<span style=\"font-size: initial;color: grey;\"> ("+
"<a href=\""+linkEntity+"\" style=\"text-decoration: none;color: inherit;\">"+
wgWikibaseItemId+"</a>"+
" <a href=\""+linkEntity+".ttl?flavor=dump\" style=\"text-decoration: none;color: inherit;\">"+
"ttl</a>"+
" <a href=\""+linkEntity+".rdf\" style=\"text-decoration: none;color: inherit;\">"+
"rdf/xml</a>"+
" <a href=\""+linkEntity+".json\" style=\"text-decoration: none;color: inherit;\">"+
"json</a>"+
" <a id='endpointSPARQL' href=\"https://query.wikidata.org/#select ?p ?v where { <http://www.wikidata.org/entity/"+idWikidata+"> ?p ?v }\" style=\"text-decoration: none;color: inherit;\">"+
"sparql</a>"+
'<script>'+
'function printLabelTTL() {' +
'$(\'#bodyLabelTTL\').is(\':visible\')?$(\'#bodyLabelTTL\').hide():$(\'#bodyLabelTTL\').show();' +
'}'+
'</script>'+
' <small>Export :<a href="#" onclick="printLabelTTL();return false;" style="text-decoration: none;color: inherit;"> labels</small></a>'+
")</span>"
);//&action=purge
//other solution : https://www.wikidata.org/w/api.php?action=wbgetentities&props=info&ids=Qxxxx
/* $.getJSON( linkEntity+".json", function( json ) {
//console.log( json['entities'][idWikidata]['lastrevid']);
versionLast = json.entities[idWikidata].lastrevid;
checkVersion();
});*/
var endpoint = "https://query.wikidata.org/sparql";
var query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\
PREFIX wd: <http://www.wikidata.org/entity/> \
select DISTINCT (xsd:string(?labelFR) as ?strFR) (xsd:string(?labelEN) as ?strEN) \
where { \
wd:"+wgWikibaseItemId+" rdfs:label ?labelFR . \
wd:"+wgWikibaseItemId+" rdfs:label ?labelEN .\
FILTER (langMatches(lang(?labelFR), \"FR\"))\
FILTER (langMatches(lang(?labelEN), \"EN\"))\
} \
LIMIT 10";
$.ajax({
url: endpoint,
dataType: 'json',
data: {
queryLn: 'SPARQL',
query: query ,
limit: 'none',
infer: 'true',
Accept: 'application/sparql-results+json'
},
success: displayWikidataIDWithLabelinTTL,
error: displayError
});
//});
/*
var query = "select ?version where {graph <http://www.wikidata.org/entity/"+idWikidata+"> { ?o <http://schema.org/version> ?version }}";
// $('#bodyContentResearch').append(queryDataset);
$.ajax({
url: endpointWikidata ,
dataType: 'json',
data: {
queryLn: 'SPARQL',
query: query ,
limit: 'none',
infer: 'true',
Accept: 'application/sparql-results+json'
},
success: displayVersion,
error: displayError
});
*/
// }
// }
function displayError(xhr, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
}
function displayVersion(data) {
//console.log( data.results.bindings[0]['version'].value);
if(data.results.bindings.length > 0){
versionInDatabase = data.results.bindings[0].version.value;
}else{
versionInDatabase = 0;
}
checkVersion();
}
function checkVersion() {
console.log( "versionInDatabase="+versionInDatabase+";versionLast="+versionLast);
if(versionInDatabase === null || versionLast === null){
$("#endpointSPARQL").css('color', 'grey');
}else if(versionInDatabase != versionLast){
$("#endpointSPARQL").css('color', 'grey');
$.ajax({
url: cacheWikidata ,
data: {
id: idWikidata
}
}).done(function(data, statusText, xhr){
var status = xhr.status; //200
//var head = xhr.getAllResponseHeaders(); //Detail header info
if(status == 200){
$("#endpointSPARQL").append(" wait 1 min");
}else{
$("#endpointSPARQL").append(" error");
}
});
}else if(versionInDatabase == versionLast ){
$("#endpointSPARQL").css('color', 'green');
}
}
function displayWikidataIDWithLabelinTTL(data) {
var textTTL = 'wd:'+wgWikibaseItemId+'\n';
textTTL += 'rdf:type wikibase:Item ;\n';
if(data.results.bindings.length === 0){
textTTL += 'Sorry, we have a problem...';
}else{
$.each(data.results.bindings, function(index, bs) {
textTTL += 'rdfs:label "' + bs["strEN"].value + '"@en ;\n';
textTTL += 'rdfs:label "' + bs["strFR"].value + '"@fr ;\n';
});
}
textTTL += '.\n';
var content = '<div id="bodyLabelTTL" class="mw-body-content" style="z-index: 5;display:none;background: rgb(222, 245, 220);"><pre>'+textTTL+'</pre></div>';
$(content).insertBefore('#bodyContent');
}
});