User:Molarus/SPARQL.js

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(){
	console.log ( 'Hello, World!' ) ;

	var d = "<div style='position:absolute;right:10px;top:0px;z-index:1'>" ;
	d += "<div id='target' style='cursor:pointer;width:15em;border:1px solid black;padding:0.2em'>SPARQL Query";
	d += "<span style='float:right; padding-right:1em'><input type='text' id='nrRows' size='2' title='Input limits the SPARQL result list. \nStandard for this tool is 10 rows.' class='numbersOnly' value=''/> rows</span></div>";
	d += "<div id='div1' style='cursor:pointer;display:none;background-color:white;'>";
	d += "<table id='tableID'><tbody class='container'><tr><td style='border-bottom:solid 1px black;padding:2px;'>List all cycling teams</td></tr></tbody></table>";
	d +="</div></div>";
	$('#mw-content-text').append ( d ) ;

	var boolA = false;
	var numA = 10;

	$('.numbersOnly').keyup(function () { 
		this.value = this.value.replace(/[^0-9\.]/g,'');
	});

	$( "#target" ).click(function() {
		if (boolA === true) { 
			$( "#div1" ).hide();
			boolA = false;
		} else {
			$( "#div1" ).fadeIn(); 
			boolA = true;
		} 
	});	

	$( "#div1" ).click(function() {
//		getLabel ('Q721234', 'de') ; // entity, language
		SPARQL() ;
	});

	function SPARQL() {
		console.log ( "SPARQL" ) ;
		var api = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql';
		var SPARQL = 'select * WHERE { wd:Q19286461 schema:dateModified ?a }';
		var SPARQL2 = 'SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q146 . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } } LIMIT 10';
		var SPARQL3 = 'SELECT DISTINCT ?x ?xLabel WHERE {';
		SPARQL3 += 'VALUES ?team {wd:Q1785271 wd:Q20639847 wd:Q20653563 wd:Q20638319 wd:Q6154783 wd:Q382927 wd:Q20653564 wd:Q1756006 wd:Q20653566 wd:Q20639848 wd:Q20652655}';
		SPARQL3 += '?x wdt:P31 ?team.';
		SPARQL3 += 'FILTER NOT EXISTS {?x wdt:P361 ?g}';
		SPARQL3 += 'SERVICE wikibase:label {bd:serviceParam wikibase:language "de,en,fr,es,nl,ca"}';
		SPARQL3 += '} ORDER BY DESC(?x) Limit 10';

		var numB = $( "#nrRows" ).val(); // Man könnte auch "Limit x" and die SPARQL-query anhängen und nicht x ersetzen
		if ( numB !== '') {
			SPARQL3 = SPARQL3.replace(SPARQL3.match(/Limit (\d+)/)[1], numB);
			numA = Number(numB);
			//console.log(numB, SPARQL3);
		} 	
		
		$.getJSON ( api , {
			query : SPARQL3 
		} , function ( data ) {
			//console.log('num is: ', numA);
			console.log(data);
			//console.log('Label1=' + data.results.bindings[0].xLabel.value+' '+ ' Label2=' + data.results.bindings[0].xLabel.value);
			var i = 0;
			while (i < numA) {
				var str = "<tr><td>"+data.results.bindings[i].xLabel.value;
				str += " (<a href="+data.results.bindings[i].x.value+">item</a>)";
				str += "</td></tr>";
				$(".container").append($(str));
				i++;
			}
		} ) ;
		}


	function getLabel (entity, language) {	
		console.log ( "getLabel" ) ;
		//console.log ( entity, language) ;
		//entity = 'Q721234';
		//language = 'de';

		$.getJSON ( '/w/api.php' , {
			action : 'wbgetentities',
			format : 'json',
			ids : entity,
			props : 'labels',
			languages : language
		} , function ( data ) {
			console.log(data.entities[entity].labels[language].value); 
			var str = data.entities[entity].labels[language].value;
			$( ".container" ).append( $( "<tr><td>"+ str +"</td></tr>" ) );
		} ) ;
	} 


});