Différences entre les versions de « Semantic Scribunto example »

De Semantic MediaWiki - Sandbox

Ligne 1 : Ligne 1 :
[[Module:Smw]]
+
On this page you can find a short introduction and some examplary use cases for the [https://www.semantic-mediawiki.org/wiki/Extension:Semantic_Scribunto SMW Extension SemanticScribunto]. This documentation aims to get you aquainted on how to utilize lua in conjunction with SMW and shows its advantages on more complex operations. It does not, however give you an introduction to lua. For this, please visit <ref name=luadoc>Programming in Lua (1st edition): https://www.lua.org/pil/contents.html</ref> or <ref name=scriblua>Scribunto's lua reference manual: https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual</ref>. It is furthermore assumed that you have installed SMW, [https://www.mediawiki.org/wiki/Extension:Scribunto Scribunto] and [https://github.com/SemanticMediaWiki/SemanticScribunto/blob/master/README.md this extension].
  
<pre>
+
== Introduction ==
 +
Scribunto allows the usage of the scripting language Lua inside of Mediawiki. This allows for more complex and better maintainable code in comparison to just using the normal template engine. Scribunto has a build-in method of integrating all available parser functions inside your lua modules. However, in case of a data orientated extension like SMW this falls short, because all you get as return values are parsed strings. Wanting to access semantic data via #ask would make it necessary to parse the string produced by the parser function. Enter Extension:Semantic Scribunto ('''SSC'''), which provides a new lua library<ref name=ssc_functions>SSC lua library functions: https://github.com/SemanticMediaWiki/SemanticScribunto/blob/master/docs/README.md</ref> for retrieving and storing semantic data inside lua modules. This relieves users of the necessity to use <code>frame:callParserFunction</code> and a cumbersome manual result parsing, accelerating all SMW operations in the process. In conjunction with lua's intrically capabilites of manipulating data tables, this allows for some advanced operations in a very elegant way.
 +
 
 +
== Simple examples ==
 +
=== Get property type ===
 +
{{infobox
 +
| title          = SSC library function
 +
| above          = mw.smw.getQueryResult
 +
| subheader      = Data retrieval function
 +
| subheaderstyle = background:#ccf
 +
| header1        = Links
 +
| label4        = Documentation
 +
| data4          = [https://github.com/SemanticMediaWiki/SemanticScribunto/blob/master/docs/mw.smw.getQueryResult.md external]
 +
| label6        = Example module
 +
| data6          = [[Module:Smw]]
 +
| header9        = Function signature
 +
| label11        = Input
 +
| data11        = (string) Property name
 +
| label13        = Return
 +
| data13        = (string) Type identifier
 +
}}
 +
 
 +
<pre style="overflow:auto; width:auto">
 
[[Property:Modification date|Modification date]] is of type {{#invoke:smw|type|Modification date}}
 
[[Property:Modification date|Modification date]] is of type {{#invoke:smw|type|Modification date}}
 
</pre>
 
</pre>
 +
Produces:
 +
:[[Property:Modification date|Modification date]] is of type {{#invoke:smw|type|Modification date}}.
 +
<div style="clear:both;"></div>
 +
 +
=== Get query result ===
 +
{{infobox
 +
| title          = SSC library function
 +
| above          = mw.smw.getPropertyType
 +
| subheader      = Data retrieval function
 +
| subheaderstyle = background:#ccf
 +
| header1        = Links
 +
| label4        = Documentation
 +
| data4          = [https://github.com/SemanticMediaWiki/SemanticScribunto/blob/master/docs/mw.smw.getPropertyType.md external]
 +
| label6        = Example module
 +
| data6          = [[Module:Smw]]
 +
| header9        = Function signature
 +
| label11        = Input
 +
| data11        = (string{{!}}table) query
 +
| label13        = Return
 +
| data13        = (table) query json data
 +
}}
 +
This is one of two functions that allows for access to semantic data. It takes as input query parameters akin to the #ask parser function. As return you get a table containing your query result data. The table format is like the json format you get when using the api<ref name=smwapi>SMW API Json format: https://www.semantic-mediawiki.org/wiki/Serialization_(JSON)</ref>.
 +
 +
 +
<pre style="overflow:auto; width:auto">
 +
{{#invoke:smw|ask|[[Category:Comic books]][[Has comic book writer::Neil Gaiman]]|?Has main character=protagonist|?Has comic book artist|?Creation date|limit=2}}
 +
</pre>
 +
Produces:
 +
{{#invoke:smw|ask|[[Category:Comic books]][[Has comic book writer::Neil Gaiman]]|?Has main character=protagonist|?Has comic book artist|?Creation date|limit=2}}
 +
<div style="clear:both;"></div>
 +
  
[[Property:Modification date|Modification date]] is of type {{#invoke:smw|type|Modification date}}.
+
==See also==
 +
<references />
  
 
[[Category:Semantic Scribunto example]]
 
[[Category:Semantic Scribunto example]]

Version du 18 janvier 2017 à 15:05

On this page you can find a short introduction and some examplary use cases for the SMW Extension SemanticScribunto. This documentation aims to get you aquainted on how to utilize lua in conjunction with SMW and shows its advantages on more complex operations. It does not, however give you an introduction to lua. For this, please visit [1] or [2]. It is furthermore assumed that you have installed SMW, Scribunto and this extension.

Introduction

Scribunto allows the usage of the scripting language Lua inside of Mediawiki. This allows for more complex and better maintainable code in comparison to just using the normal template engine. Scribunto has a build-in method of integrating all available parser functions inside your lua modules. However, in case of a data orientated extension like SMW this falls short, because all you get as return values are parsed strings. Wanting to access semantic data via #ask would make it necessary to parse the string produced by the parser function. Enter Extension:Semantic Scribunto (SSC), which provides a new lua library[3] for retrieving and storing semantic data inside lua modules. This relieves users of the necessity to use frame:callParserFunction and a cumbersome manual result parsing, accelerating all SMW operations in the process. In conjunction with lua's intrically capabilites of manipulating data tables, this allows for some advanced operations in a very elegant way.

Simple examples

Get property type

SSC library function
mw.smw.getQueryResult
Data retrieval function
Links
Documentation external
Example module Module:Smw
Function signature
Input (string) Property name
Return (string) Type identifier
[[Property:Modification date|Modification date]] is of type {{#invoke:smw|type|Modification date}}

Produces:

Modification date is of type Erreur de script : la fonction « type » n’existe pas..

Get query result

SSC library function
mw.smw.getPropertyType
Data retrieval function
Links
Documentation external
Example module Module:Smw
Function signature
Input (string|table) query
Return (table) query json data

This is one of two functions that allows for access to semantic data. It takes as input query parameters akin to the #ask parser function. As return you get a table containing your query result data. The table format is like the json format you get when using the api[4].


{{#invoke:smw|ask|[[Category:Comic books]][[Has comic book writer::Neil Gaiman]]|?Has main character=protagonist|?Has comic book artist|?Creation date|limit=2}}

Produces:

(table)[2]:
	1: (table)[1]:
		1: (string) 'Sandman - Dream Country'
		protagonist: (table)[2]:
			1: (string) 'Dream (comics)'
			2: (string) 'Death (DC Comics)'
		Date de création: (string) '14 décembre 2016 08:39:18'
		Has comic book artist: (table)[5]:
			1: (string) 'Malcolm Jones III'
			2: (string) 'Kelley Jones'
			3: (string) 'Steve Oliff'
			4: (string) 'Charles Vess'
			5: (string) 'Colleen Doran'
	2: (table)[1]:
		1: (string) 'Sandman - Preludes and Nocturnes'
		protagonist: (table)[4]:
			1: (string) 'Dream (comics)'
			2: (string) 'Cain and Abel (comics)'
			3: (string) 'John Constantine'
			4: (string) 'Lucifer (DC Comics)'
		Date de création: (string) '14 décembre 2016 08:30:19'
		Has comic book artist: (table)[3]:
			1: (string) 'Sam Kieth'
			2: (string) 'Mike Dringenberg'
			3: (string) 'Malcolm Jones III'


See also

Les cookies nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de cookies.