Lua: Difference between revisions
		
		
		
		Jump to navigation
		Jump to search
		
 Created page with "{{Template:Test_Lua}}"  | 
				No edit summary  | 
				||
| (9 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
There are 2 ways to use Lua.  | |||
===1) perform the query directly from Lua, as follows:===  | |||
<pre>  | |||
local p = {}  | |||
function p.main(frame)  | |||
  -- local args = frame:getParent().args  | |||
  -- the VisualData query is called from here  | |||
  local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} )  | |||
  return mw.text.jsonEncode(ret)  | |||
end  | |||
return p  | |||
</pre>  | |||
Template: [[Template:Test Lua]]   | |||
Module: [[Module:TestLua]]   | |||
{{Template:Test_Lua}}  | {{Template:Test_Lua}}  | ||
===2) perform the query on a wiki article and then process the result using Lua===  | |||
<pre>  | |||
{{#visualdataquery: [[title::+]]  | |||
|schema=Book  | |||
|?cover  | |||
|?title  | |||
|?authors/name  | |||
|?authors/email  | |||
|format=lua  | |||
|module=Handle query  | |||
}}  | |||
</pre>  | |||
{{#visualdataquery: [[title::+]]  | |||
|schema=Book  | |||
|?cover  | |||
|?title  | |||
|?authors/name  | |||
|?authors/email  | |||
|format=lua  | |||
|module=Handle query  | |||
}}  | |||
<pre>  | |||
local p = {}  | |||
function p.main(frame)  | |||
  -- local args = frame:getParent().args  | |||
  local origArgs = (type(frame.getParent) == "function") and frame:getParent().args or frame  | |||
  local success, json = pcall(mw.text.jsonDecode, origArgs[0])  | |||
  -- do more processing here  | |||
  local ret = json  | |||
  return mw.text.jsonEncode(ret)  | |||
end  | |||
return p  | |||
</pre>   | |||
Template: [[Template:Handle query]]   | |||
Module: [[Module:Handle query]]   | |||
== See also ==  | |||
{{Demo VisualData footer}}  | |||
Latest revision as of 14:33, 31 August 2025
There are 2 ways to use Lua.
1) perform the query directly from Lua, as follows:
local p = {}
function p.main(frame)
  -- local args = frame:getParent().args
  -- the VisualData query is called from here
  local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} )
  return mw.text.jsonEncode(ret)
end
return p
Template: Template:Test Lua 
Module: Module:TestLua
[{"pageid":1806,"title":"Test Book","data":{"authors":[{"first_name":"Test","last_name":"Author","name":"Test Author","email":""}],"title":"Test Book","cover":"File:"}}]
2) perform the query on a wiki article and then process the result using Lua
{{#visualdataquery: [[title::+]]
|schema=Book
|?cover
|?title
|?authors/name
|?authors/email
|format=lua
|module=Handle query
}}
[{"categories":[],"pageid":1806,"title":"Test Book","data":{"authors":[{"name":"Test Author","email":""}],"title":"Test Book","cover":"File:"}}]
local p = {}
function p.main(frame)
  -- local args = frame:getParent().args
  local origArgs = (type(frame.getParent) == "function") and frame:getParent().args or frame
  local success, json = pcall(mw.text.jsonDecode, origArgs[0])
  -- do more processing here
  local ret = json
  return mw.text.jsonEncode(ret)
end
return p
 
Template: Template:Handle query 
Module: Module:Handle query
See also
- Demo VisualData 1.0
 - New task
 - Page upload
 - CRUD
 - Result formats
 - Dependent values
 - Geolocation
 - Queries
 - Query cities (join queries)
 - Dynamic catalogue
 - Datatable cards
 - Calendar
 - Carousel
 - Nested schemas
 - Table mode
 - Lua
 - Official docs
 
support & questions: support at topway.it