Extension:SparqlResultFormat

MediaWiki extensions manual
OOjs UI icon advanced-invert.svg
SparqlResultFormat
Release status: beta
Implementation Parser function
Author(s) Gabriele Cornacchia, Matteo Busanelli
Latest version 1.0.16 (2019-10-17)
MediaWiki 1.25+
PHP 5.3.3+
Database changes No
License MIT License
Download

Check usage and version matrix.


DescriptionEdit

SparqlResultFormat is a free, open-source extension to MediaWiki that lets you query different Sparql endpoints and represent output data in various formats.

InstallationEdit

  1. Unzip the downloaded zip file to the extensions folder of your mediawiki and rename it to 'SparqlResultFormat'.
  2. Include the following line at the end of your LocalSettings.php:
    wfLoadExtension( 'SparqlResultFormat' );
    
  3. Check documentation at the Special:SparqlResultFormat page on your mediawiki


Formats & ExamplesEdit

These examples work with Wikidata sparql query endpoint.

Define it in your LocalSettings.php

$wgSparqlEndpointDefinition['wikidata'] = array(
		'url' => 'https://query.wikidata.org/sparql',
		'connectionTimeout' => 0,
		'requestTimeout' => 30);

Node graphEdit

Members of European Union with their capitals

 

{{#sparql2graph:
|divId=mynetwork
|divStyle=width:100%;height:530px;
|linkBasePath={{SERVER}}{{SCRIPTPATH}}/index.php/
|nodeConfiguration=[
   { category:"country",nodeColor:"#00FF00", image:"https://cdn1.vectorstock.com/i/1000x1000/59/35/visited-country-icon-symbol-premium-quality-vector-16435935.jpg"},
   { category:"city",nodeColor:"#00FFFF", image:"https://cdn0.iconfinder.com/data/icons/good-view/500/View-08-512.png"}
]
|edgeConfiguration={}
|defaultNodeColor=#CCC   
|defaultEdgeColor=#CCC
|maxLabelLength=200
|showLegend=true
|rootElement=http://www.wikidata.org/entity/Q458
|rootElementImage=https://cdn3.iconfinder.com/data/icons/50-flags-of-the-world-circular-shape-2/60/Circular_world_Flag_134-512.png
|rootElementColor=
|layout=dagre
|nodeStyle={}
|edgeStyle={}
|layoutOptions={}
|splitQueryByUnion=false
|minZoom=0.2
|maxZoom=1.2
|sparqlEndpoint=wikidata
|sparqlEscapedQuery=
select distinct ?parent_uri ?parent_name ?child_uri ?child_name ?parent_type ?parent_type_uri ?child_type ?child_type_uri ?relation_uri ?relation_name
where { 
  {
?parent_uri  ?relation_uri ?child_uri.


  SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
      ?parent_uri rdfs:label ?parent_name.
      ?child_uri rdfs:label ?child_name.
      ?parent_type_uri rdfs:label ?parent_type.  
      ?child_type_uri rdfs:label ?child_type.                     
     }
  
  BIND (wdt:P150 AS ?relation_uri)
  BIND (wd:Q458 AS ?parent_uri)
  BIND (wd:Q3623811 AS ?parent_type_uri)
  BIND (wd:Q6256 AS ?child_type_uri)
  BIND ("Has members" AS ?relation_name)
} UNION {
  
    ?root wdt:P150 ?parent_uri.
    ?parent_uri  ?relation_uri ?child_uri.
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
      ?parent_uri rdfs:label ?parent_name.
      ?child_uri rdfs:label ?child_name.
      ?parent_type_uri rdfs:label ?parent_type.  
      ?child_type_uri rdfs:label ?child_type.     
      #?relation_uri rdfs:label ?relation_name
     }
    
    BIND (wd:Q458 AS ?root)
    BIND (wdt:P36 AS ?relation_uri)
  BIND (wd:Q6256 AS ?parent_type_uri)
  BIND (wd:Q515 AS ?child_type_uri)
  BIND ("Has capital" AS ?relation_name)
    
  
  }
}

Bar ChartEdit

Top 20 countries by population size

 

{{#sparql2barchart:
|divId=chart2
|sparqlEndpoint=wikidata
|seriesConfiguration=[{label:'Population', color:'blue',showLink:'false'} ]
|sparqlEscapedQuery=
SELECT DISTINCT ?countryLabel ?population
{
  ?country wdt:P31 wd:Q6256 ;
           wdt:P1082 ?population .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?population ?countryLabel
ORDER BY DESC(?population)
limit 20
|extraOption=chart.bar.width:15
|extraOption=chart.bar.padding:2
|extraOption=chart.bar.margin:5
|extraOption=chart.axis.x.label:Country
|extraOption=chart.axis.y.label:Population
|extraOption=chart.axis.x.angle:-45
|extraOption=chart.axis.x.font.size:9pt
}}

Pie ChartEdit

File:Sparql-result-format-pie.png

{{#sparql2piechart:
|divId=donut1
|divStyle=width:100%; height:100%;
|sparqlEndpoint=wikidata
|sparqlEscapedQuery=
SELECT DISTINCT ?countryLabel ?population
{
  ?country wdt:P31 wd:Q6256 ;
           wdt:P1082 ?population .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?population ?countryLabel
ORDER BY DESC(?population)
limit 10
|extraOption=chart.title:Top 10 countries by population size
}}

Donut ChartEdit

 

{{#sparql2donutchart:
|divId=donut1
|divStyle=width:100%; height:100%;
|sparqlEndpoint=wikidata
|sparqlEscapedQuery=
SELECT DISTINCT ?countryLabel ?population
{
  ?country wdt:P31 wd:Q6256 ;
           wdt:P1082 ?population .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?population ?countryLabel
ORDER BY DESC(?population)
limit 10
|extraOption=chart.title:Top 10 countries by population size
}}

Bubble ChartEdit

File:Sparql-result-format-bubble.png


TreemapEdit

File:Sparql-result-format-treemap.png


HTML tableEdit

{{#sparql2table:
|divId=chart2
|sparqlEndpoint=wikidata
|tableClass=wikitable dashboard-table sortable
|columnConfiguration=[{queryField:'country',visible:false},{queryField:'countryLabel', label:'Country', showLink:'true', cellLinkPattern:'{%s[country]}'}, {queryField:'population', label:'Population', showLink:'false',cellValuePattern:'{%n[0,00.00]@en}'}]
|sparqlEscapedQuery=
SELECT DISTINCT ?country ?countryLabel ?population
{
  ?country wdt:P31 wd:Q6256 ;
           wdt:P1082 ?population .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?population ?countryLabel ?country
ORDER BY DESC(?population)
limit 20

}}

CSVEdit

{{#sparql2csv:
|divId=csvButton1 
|sparqlEndpoint=wikidata
|linkButtonLabel=Download CSV
|filename=wikidata_house_cats.csv
|separator=;
|sparqlEscapedQuery=
SELECT ?item ?itemLabel WHERE {  ?item wdt:P31 wd:Q146.  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
}}