Extension:LinkedWiki/WSparql parser

From MediaWiki.org
Jump to navigation Jump to search

This feature will be removed of the LinkedWiki extension in the version 4.


The parameters of the query, endpoint, cache and debug are the same as for #sparql, with a slightly different syntax:

{{#wsparql: WIDGET_NAME |query= SPARQL_QUERY  | endpoint= ENDPOINT  | cache = NO/YES (optional) | debug = NO/YES (optional) | PARAMETERS_OF_WIDGET }}

Example with the Google Static Maps widget:

PREFIX wikibase: <http://wikiba.se/ontology#> 
PREFIX psv: <http://www.wikidata.org/prop/statement/value/> 
PREFIX p: <http://www.wikidata.org/prop/> 
PREFIX wd: <http://www.wikidata.org/entity/> 
PREFIX wdt: <http://www.wikidata.org/prop/direct/> 

select   ?latitude ?longitude
where {
        ?capital wdt:P31/wdt:P279* wd:Q5119 ;
		 wdt:P30 wd:Q46 .
  	?capital p:P625 ?coord .
        ?coord   psv:P625 ?coordValue .
          ?coordValue a wikibase:GlobecoordinateValue ;
                        wikibase:geoLatitude ?latitude ;
                        wikibase:geoLongitude ?longitude .
 |endpoint = https://query.wikidata.org/sparql

Result of wsparql before the parsing of mediawiki:

|endpoint = https://query.wikidata.org/sparql 
| defaultcolor=blue 
| center=Europe 
| zoom=3 
| size=512x512 
| maptype=roadmap

| rows.0.latitude=46.2 
| rows.0.longitude=6.15 

| rows.1.latitude=38 
| rows.1.longitude=23.716666666667 

And after parsing:

Linkedwiki example3.png

As you can see, the wsparql aggregates the sparql results in one list, with this pattern : rows.NUMBER_LINE.VARIABLE_NAME = VARIABLE_VALUE

You have to find a widget compatible with this syntax or you can create/change a widget of mediawikiwidgets.

Build your widget[edit]

  1. Install the extension Widgets
  2. Create the page Widget:NAME_OF_YOUR_WIDGET
  3. Create an API key
  4. Insert your code, example with google static maps and insert your key :
This widget allows you to embed 
'''[http://code.google.com/apis/maps/documentation/staticmaps/ Google Static Maps]''' 
on your wiki page.

=== Sample result ===
|rows.0.latitude= 43.30
|rows.0.longitude= 2.3
|rows.1.latitude= 43.40
|rows.1.longitude= 5.4}}

<!--{if is_array($rows)}-->
         <!--{foreach from=$rows key=myId item=i}-->

And to share your widget in mediawikiwidgets.org.

Now, let's summarize!

  • To read one list like rows.NUMBER_LINE.VARIABLE_NAME = VARIABLE_VALUE, you have to perform a foreach of rows and read keys and items, like that:
<!--{foreach from=$rows key=myId item=i}-->
	<!--{$myId|escape:'html'}--> <!--{$i.point|escape:'html'}-->

It's a bit hard the first time. Use the group google, if you face a problem.