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.

Usage[edit]

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:

{{#wsparql:Google_Static_Maps
 |query=
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
 |defaultcolor=blue
 |center=Europe
 |zoom=3
 |size=512x512
 |maptype=roadmap
}}

Result of wsparql before the parsing of mediawiki:

{{#widget:Google_Static_Maps
|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 :
<nowiki>
<noinclude>__NOTOC__
This widget allows you to embed 
'''[http://code.google.com/apis/maps/documentation/staticmaps/ Google Static Maps]''' 
on your wiki page.

=== Sample result ===
{{#widget:{{PAGENAME}}
|defaultcolor=blue
|center=France
|zoom=5
|size=512x512
|maptype=roadmap
|sensor=false
|rows.0.color=red
|rows.0.latitude= 43.30
|rows.0.longitude= 2.3
|rows.1.color=blue
|rows.1.latitude= 43.40
|rows.1.longitude= 5.4}}

</noinclude>
<includeonly>
<img 
src="http://maps.google.com/maps/api/staticmap?key=YOUR_KEY
&center=<!--{$center|default:'France'|escape:'html'}-->
&zoom=<!--{$zoom|default:'5'|escape:'html'}-->
&size=<!--{$size|default:'512x512'|escape:'html'}-->
&maptype=<!--{$maptype|default:'roadmap'|escape:'html'}-->
&sensor=<!--{$sensor|default:'false'|escape:'html'}-->
<!--{if is_array($rows)}-->
         <!--{foreach from=$rows key=myId item=i}-->
              &markers=color:<!--{$i.color|default:$defaultcolor|escape:'html'}-->|label:<!--{$myId|escape:'html'}-->|<!--{$i.latitude|escape:'html'}-->,<!--{$i.longitude|escape:'html'}-->
         <!--{/foreach}-->
<!--{/if}-->"/>
</includeonly>

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'}-->
<!--{/foreach}-->

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