Semantic Scribunto example
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
Ask
mw.smw.ask | |
---|---|
Data retrieval function | |
Links | |
Documentation | external |
Example module | Module:Smw |
Function signature | |
Input | (string|table) query |
Return | (table) simple query 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 simple table containing your query result data in a propertyname/headline => semantic data
format. See example below for more information. Please also see Get query result for a more detailed equivalent.
{{#invoke:smw|ask|[[Category:Comic books]][[Has comic book writer::Neil Gaiman]]|?#-=page|?Has main character=protagonist|?Has comic book artist|?Creation date|limit=2|mainlabel=-}}
mw.smw.ask example result |
---|
(table)[2]: 1: (table)[0]: Date de création: (string) '14 décembre 2016 08:39:18' protagonist: (table)[2]: 1: (string) 'Dream (comics)' 2: (string) 'Death (DC Comics)' page: (string) 'Sandman - Dream Country' 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)[0]: Date de création: (string) '14 décembre 2016 08:30:19' protagonist: (table)[4]: 1: (string) 'Dream (comics)' 2: (string) 'Cain and Abel (comics)' 3: (string) 'John Constantine' 4: (string) 'Lucifer (DC Comics)' page: (string) 'Sandman - Preludes and Nocturnes' Has comic book artist: (table)[3]: 1: (string) 'Sam Kieth' 2: (string) 'Mike Dringenberg' 3: (string) 'Malcolm Jones III' |
Get query result
mw.smw.getQueryResult | |
---|---|
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]. Please see ask for a simpler equivalent.
{{#invoke:smw|getQueryResult|[[Category:Comic books]][[Has comic book writer::Neil Gaiman]]|?Has main character=protagonist|?Has comic book artist|?Creation date|limit=2}}
mw.smw.getPropertyType example result |
---|
(table)[0]: serializer: (string) 'SMW\Serializers\QueryResultSerializer' meta: (table)[0]: offset: (number) 0 count: (number) 2 time: (string) '0.002273' hash: (string) '13d7ebfaba2a4bdbc85523aa5c51afac' source: (string) printrequests: (table)[4]: 1: (table)[0]: key: (string) label: (string) format: (boolean) false mode: (number) 2 redi: (string) typeid: (string) '_wpg' 2: (table)[0]: key: (string) 'Has_comic_book_artist' label: (string) 'Has comic book artist' format: (string) mode: (number) 1 redi: (string) typeid: (string) '_wpg' 3: (table)[0]: key: (string) '_CDAT' label: (string) 'Date de création' format: (string) mode: (number) 1 redi: (string) typeid: (string) '_dat' 4: (table)[0]: key: (string) 'Has_main_character' label: (string) 'protagonist' format: (string) mode: (number) 1 redi: (string) typeid: (string) '_wpg' version: (number) 2 results: (table)[2]: 1: (table)[0]: exists: (string) '1' displaytitle: (string) printouts: (table)[0]: protagonist: (table)[2]: 1: (table)[0]: exists: (string) '1' displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Dream (comics)' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Dream_(comics)' 2: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Death (DC Comics)' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Death_(DC_Comics)' Date de création: (table)[1]: 1: (table)[0]: raw: (string) '1/2016/12/14/8/39/18/0' timestamp: (string) '1481704758' Has comic book artist: (table)[5]: 1: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Malcolm Jones III' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Malcolm_Jones_III' 2: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Kelley Jones' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Kelley_Jones' 3: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Steve Oliff' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Steve_Oliff' 4: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Charles Vess' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Charles_Vess' 5: (table)[0]: exists: (string) '1' displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Colleen Doran' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Colleen_Doran' namespace: (number) 0 fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Sandman_-_Dream_Country' fulltext: (string) 'Sandman - Dream Country' 2: (table)[0]: exists: (string) '1' displaytitle: (string) printouts: (table)[0]: protagonist: (table)[4]: 1: (table)[0]: exists: (string) '1' displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Dream (comics)' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Dream_(comics)' 2: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Cain and Abel (comics)' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Cain_and_Abel_(comics)' 3: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'John Constantine' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/John_Constantine' 4: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Lucifer (DC Comics)' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Lucifer_(DC_Comics)' Date de création: (table)[1]: 1: (table)[0]: raw: (string) '1/2016/12/14/8/30/19/0' timestamp: (string) '1481704219' Has comic book artist: (table)[3]: 1: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Sam Kieth' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Sam_Kieth' 2: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Mike Dringenberg' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Mike_Dringenberg' 3: (table)[0]: exists: (string) displaytitle: (string) namespace: (number) 0 fulltext: (string) 'Malcolm Jones III' fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Malcolm_Jones_III' namespace: (number) 0 fullurl: (string) 'https://sandbox.semantic-mediawiki.net/wiki/Sandman_-_Preludes_and_Nocturnes' fulltext: (string) 'Sandman - Preludes and Nocturnes' |
Get property type
mw.smw.getPropertyType | |
---|---|
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|getPropertyType|Modification date}}
Produces:
- Modification date is of type _dat.
Set
mw.smw.set | |
---|---|
Data storage function | |
Links | |
Documentation | external |
Example module | Module:Smw |
Function signature | |
Input | (string|table) Property=>Value pair(s) |
Return | (bool) true or (table) in case of error |
#1: {{#invoke:smw|set|has author=User:Oetterer|has keyword=documentation|has keyword=example|has keyword=ssc|has keyword=SemanticScribunto}} #2: {{#invoke:smw|set|has number=foobar}} #3: {{#invoke:smw|set|has text=draft;live;work in progress|+sep=;}}
Produces:
- 1: Your data was stored successfully
- 2: An error occurred during the storage process. Message reads « foobar » n’est pas un nombre.
- 3: An error occurred during the storage process. Message reads « +sep » contient un caractère « + » répertorié comme faisant partie du libellé de la propriété et a conséquemment été classé comme non valide.
Note that query #1 places the page in the gardening category Category:Pages using duplicate arguments in template calls ( functionality live since MW 1.25). This call also ignores all but one assigment. This occurs due to the fact, that Scribunto receives the arguments in a key=>value pair, leading to latter value assignments overwrite former. Query #3 also does not yield the desired result. Again Scribunto is to blame, since it scrambles the order of tables entries having non-numbers as keys. There the +sep
argument is read before the has text=
assignment. However, using mw.smw.set
inside a module (and not filled by frame.args) gives you more control over the argument order and allows both the desired effects of #1 and #3.
Subobject
mw.smw.subobject | |
---|---|
Data storage function | |
Links | |
Documentation | external |
Example module | Module:Smw |
Function signature | |
Input | (string) Property=>Value pair(s), [(string) subobjectId] |
Return | (bool) true or (table) in case of error |
#1: {{#invoke:smw|subobject|has firstname=Donald|has lastname=Duck}} #2: {{#invoke:smw|subobject|has firstname=Daisy|has lastname=Duck|subobjectId=LadyDuck}}
- 1: Your data was stored successfully in a subobject
table#1 { metatable = table#2 ["has firstname"] = "Donald", ["has lastname"] = "Duck", }
- 2: Your data was stored successfully in a subobject
table#1 { metatable = table#2 ["has firstname"] = "Daisy", ["has lastname"] = "Duck", }
See also
- ↑ Programming in Lua (1st edition): https://www.lua.org/pil/contents.html
- ↑ Scribunto's lua reference manual: https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual
- ↑ SSC lua library functions: https://github.com/SemanticMediaWiki/SemanticScribunto/blob/master/docs/README.md
- ↑ SMW API Json format: https://www.semantic-mediawiki.org/wiki/Serialization_(JSON)