User:Ggrefen/DisplayInriaTeam2.js

From Wikidata
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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$(document).ready(function() {
		//precondition
	if( /^(mediawiki|wikidata|meta|commons)wiki$/.test( mw.config.get( 'wgDBname' ) ))
		return; //end
		
	var wgWikibaseItemId = mw.config.get("wgWikibaseItemId");
	if (!wgWikibaseItemId) {
		console.log("no wikidata");
		return;
	}
	
	var contentTab = 
	'<script>'+
	'function openTabResearch() {' +
    '$(\'#bodyContentResearch\').is(\':visible\')?$(\'#bodyContentResearch\').hide():$(\'#bodyContentResearch\').show();' +
    '}'+
	'</script>'+
	'<li id="ca-research"><span><a href="javascript:openTabResearch()" title="Inria Teams">Inria</a></span></li>';
		
	var tab = $('#p-namespaces ul').append(contentTab);
    var content = '<div id="bodyContentResearch" class="mw-body-content" style="display: none;background: rgb(222, 245, 220);"></div>';
    $(content).insertBefore('#bodyContent');
    
    
var endpointWikidata = "https://query.wikidata.org/bigdata/namespace/wdq/sparql";
var queryFindLabelEnglish = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
PREFIX wd: <http://www.wikidata.org/entity/> \
select  DISTINCT * \
where { \
        wd:"+wgWikibaseItemId+" rdfs:label ?label . \
  FILTER (langMatches( lang(?label), \"EN\" ) ) \
      } \
LIMIT 1";

    $.ajax({
                url: endpointWikidata,
                dataType: 'json', 
                data: { 
                    queryLn: 'SPARQL',
                    query: queryFindLabelEnglish , 
                    limit: 'none',
                    infer: 'true',
                    Accept: 'application/sparql-results+json'
                },
                success: findTeamInria, 
                error: displayError
        });
 });
 
 function capitalize(s)
{
    return s[0].toUpperCase() + s.slice(1);
}


    function displayError(xhr, textStatus, errorThrown) {
        console.log(textStatus);
        console.log(errorThrown);
    }
    
    function findTeamInria(data) {
        var labelEn =     data.results.bindings[0].label.value ;
        var endpointTeamInria = "https://opendata1.opendata.u-psud.fr/sparql/";
var queryFindTeam = "PREFIX inria: <http://inria.fr/> \
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
PREFIX wdt: <http://www.wikidata.org/prop/direct/> \
SELECT DISTINCT ?ACMCode ?ACMCodeLabel ?linkPageTeam ?teamLabel ?teamComment \
WHERE { \
GRAPH <http://pages.saclay.inria.fr/gregory.grefenstette/> \
{ \
   ?article   rdfs:label '''"+capitalize(labelEn)+"''' ; \
	wdt:P2179  ?ACMCode ; \
	rdfs:seeAlso ?linkPageTeam . \
	?linkPageTeam rdfs:label ?teamLabel ; \
	rdfs:comment ?teamComment ;	wdt:P2179  ?ACMCode  . \
	?objectACM a <http://dl.acm.org/ontology#code> ; \
	rdfs:label ?ACMCodeLabel ; \
	wdt:P2179  ?ACMCode . \
} \
} ORDER BY ?ACMCodeLabel ?teamLabel ";
console.log("Debug 6");
console.log(labelEn);
console.log(queryFindTeam);
    $.ajax({
                url: endpointTeamInria,
                dataType: 'json', 
                data: { 
                    queryLn: 'SPARQL',
                    query: queryFindTeam , 
                    limit: 'none',
                    infer: 'true',
                    Accept: 'application/sparql-results+json'
                },
                success: displayTeam, 
                error: displayError
        });
    }
    
   function displayTeam(data) {
        $.each(data.results.bindings, function(index, bs) {
            console.log(bs);
            bs.ACMCode.value
        });
        
var textHtml = '<h2><span class="mw-headline">List of Inria teams in relation with this article</span></h2>';
textHtml += '<p><ul class="reference">';
 $.each(data.results.bindings, function(index, bs) {
        	textHtml += '<li><span class="reference-text">'+
        	'<a href="' + 'http://dl.acm.org/buildccscode.cfm?id='+bs["ACMCode"].value+'&lid=f' + '">' +
        	bs["ACMCodeLabel"].value + '</a> : '+
        	'<a href="http://www.inria.fr/equipes/' + bs["teamLabel"].value + '">' +
        	 bs["teamLabel"].value  + ' <small>('+bs["teamComment"].value +')</small></a>. ' + 
        	'<a href="' + bs["linkPageTeam"].value + '">Source : 2014 Annual report</a>' + 
        	'</span></li>' ;
            });
        textHtml += '</ul></p>';
         $('#bodyContentResearch').append(textHtml);
    }