SparqlResultFormat

From BITPlan Wiki
Jump to navigation Jump to search

Extension

Extension
edit
name  SparqlResultFormat
url  https://www.mediawiki.org/wiki/Extension:SparqlResultFormat
purpose  Allows to render SPARQL query results
since  2019/08/07
wiki  Crwiki, Rqwiki

Installation

https://www.mediawiki.org/wiki/Extension:SparqlResultFormat

sudo git clone https://github.com/imolainformatica/SparqlResultFormat
Cloning into 'SparqlResultFormat'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 1050 (delta 2), reused 5 (delta 2), pack-reused 1035
Receiving objects: 100% (1050/1050), 2.14 MiB | 3.74 MiB/s, done.
Resolving deltas: 100% (712/712), done.

sudo chown -R  www-data.www-data SparqlResultFormat

LocalSettings.php

wfLoadExtension( 'SparqlResultFormat' );

Examples

barchart Example

{{#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
}}

example HTML table

{{#sparql2table:
|divId=table1 
|sparqlEndpoint=wikidata
|tableClass=wikitable sortable
|sparqlEscapedQuery=
SELECT ?item ?itemLabel WHERE {  ?item wdt:P31 wd:Q146.  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
}}

Examples from Extension page

Formats & Examples

These examples works 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 graph

Members of European Union with their capitals

Sparql-result-format-graph.png

{{#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 Chart

Top 20 countries by population size

Sparql-result-format-barchart.png

{{#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 Chart

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 Chart

Sparql-result-format-donut.png

{{#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 Chart

File:Sparql-result-format-bubble.png


Treemap

File:Sparql-result-format-treemap.png


HTML table

{{#sparql2table:
|divId=table1 
|sparqlEndpoint=wikidata
|tableClass=wikitable sortable
|sparqlEscapedQuery=
SELECT ?item ?itemLabel WHERE {  ?item wdt:P31 wd:Q146.  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
}}

CSV

{{#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". } }
}}