Extension:LinkedWiki/Sparql parser

From mediawiki.org

The SPARQL parser is able to build a visualization in your Wiki with results of a SPARQL query.

You can select two types of visualization: with Sgvizler2 (a JavaScript lib) with a lot of charts/tables or via a HTML table where you can customize each columns with templates.

If your data is only accessible with credentials, you must use the HTML table generated by PHP to preserve your credentials. Moreover, by default, the HTML table of SPARQL results is saved in the wiki cache. In this manner, your SPARQL service is also less solicited (Details).

If your data is public, you can use the two methods.

Usage[edit]

With the default endpoint[edit]

  • Querying without the parameter "endpoint" is possible but the variable $wgLinkedWikiConfigDefaultEndpoint has to be in the localsettings (see installation). By default the extension uses the endpoint of Wikidata.
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5}}
  • Querying the properties of the current page :
{{#sparql:select * where { <PAGEIRI> ?y ?z . } }}

With an endpoint defined in the configuration[edit]

{{#sparql:select * where { ?x ?y ?z . } LIMIT 5|config=https://query.wikidata.org}}

With a specific endpoint[edit]

Print a simple Array with the following results:

  • Querying of the default endpoint and all the graphs of this endpoint.
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5  |endpoint=https://query.wikidata.org/sparql}}

Parameters of Sgvizler2 charts and tables[edit]

Sgvizler2 is a javascript wrapper for easy visualisation of SPARQL result sets (and a jQuery plugin).

You can find the list of available visualizations in the "special:SPARQL editor" page and in the doc of Sgvizler.

Parameter: chart[edit]

  • Select the visualization :
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5  | chart=bordercloud.visualization.DataTable}}

Parameter: options[edit]

  • Each visualization has its own options, the options parameter contains the visualization's options and ! is their separator:
{{#sparql:
select * where { ?x ?y ?z . } LIMIT 5
| chart=bordercloud.visualization.DataTable
| options=width=100%!height=500px
}}

Parameters to customize the HTML table[edit]

Parameter: preview[edit]

  • The "preview" option allows you to limit the number of lines displayed in the wiki page without limiting the number of lines in the CSV export.
{{#sparql:select * where { ?x ?y ?z . } LIMIT 15  | preview = 5 }}

Parameter: class[edit]

  • "class" option allows to specify the css class to be used on the generated tables (by default is 'wikitable sortable').
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5  | endpoint = https://query.wikidata.org/sparql | class = table }}

Parameter: headers[edit]

  • Change the headers.
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5  | endpoint = https://query.wikidata.org/sparql |headers=name1,name2,name3}}

Parameter: classHeaders[edit]

  • You can hide or change the style of your headers. Here, the first column has no header and is unsortable.
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5  | endpoint = https://query.wikidata.org/sparql |headers= &nbsp;,name2,name3 |classHeaders= class="unsortable",, }}

Example:

Parameter: templates[edit]

With an example of template like Template:Test1

<includeonly>{{{x}}}, {{{y}}} and {{{z}}} </includeonly>
  • You can use this template in the following way:
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5  | endpoint = https://query.wikidata.org/sparql |headers=Name1 |templates= Test1 }}
  • And you can use a different template for each column:
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5 | endpoint = https://query.wikidata.org/sparql  |headers=Name1,Name2 |templates= Test1,Test1 }}

Parameter: templatesInSingleCell (3.7.0)[edit]

You can use several templates in one single column.

{{#sparql:select * where { ?x ?y ?z . } LIMIT 5 | endpoint = https://query.wikidata.org/sparql  |headers=Name1 |templatesInSingleCell= Test1,Test1 
}}

Parameter: userparam (3.7.0)[edit]

You can add your parameter "userparam" with your data at all your templates.

{{#sparql:select * where { ?x ?y ?z . } LIMIT 5 | endpoint = https://query.wikidata.org/sparql  |headers=Name1 |templates= Test1,Test1 
| userparam = mydata
}}

Parameter: footer[edit]

  • you can remove the footer of array:
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5 | endpoint = https://query.wikidata.org/sparql  |footer=NO }}

Parameter: cache[edit]

  • Your data may be very volatile and you need accordingly to refresh the result of this query each time. To do so, you can disable the cache with the cache parameter, as follows:
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5  | endpoint = https://query.wikidata.org/sparql |cache=no }}

Parameter: templateBare[edit]

  • You can remove the footer and the header. With the value "tableCell", you can catch only the cells.
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5  | endpoint = https://query.wikidata.org/sparql |templateBare=tableCell }}

Parameter: default[edit]

  • The "default" option is displayed instead of an empty table.
{{#sparql:select * where { ?x ?y <http://example.com/IMPOSSIBLE> . } LIMIT 5  | endpoint = https://query.wikidata.org/sparql | default= Sorry, no results. }}

Parameter: debug[edit]

  • Well, you can add several parameters at the same time but doing so, the wiki text can be very hard to debug. This parameter gives you the result of this parser before the last parsing of mediawiki:
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5 | endpoint = https://query.wikidata.org/sparql  |debug=YES }}

Parameter: log[edit]

You can control the log levels about possible SPARQL errors. If there is an SPARQL error :

  • level 0 : any messages
  • level 1 : a short message
  • level 2 : a short message and details, if available.
{{#sparql:select * where { ?x ?y ?z . } LIMIT 5 | endpoint = https://query.wikidata.org/sparql  | log=2 }}

Parameter: table (3.7.0)[edit]

The value of parameter table is 'html' by default (in extension.json) but you can replace it by 'wiki'.

This parameter controls the method of building a table by the parser in HTML or in WikiText.

BETA feature: grid[edit]

You can replace the table in HTML by your code.

Parameter: gridIntro and gridOutro (3.7.0)[edit]

Add your code via the parameters: intro and outro.

Parameter: gridIntroTemplate and gridOutroTemplate (3.7.0)[edit]

Add your code via templates via the parameters: gridIntroTemplate and gridOutroTemplate.

Parameter: gridRowTemplate (3.7.0)[edit]

Add your code via one template for all rows via the parameter gridRowTemplate.