Semantic Scribunto example

De Semantic MediaWiki - Sandbox

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.