Lua: Difference between revisions

From Wikisphere
Jump to navigation Jump to search
No edit summary
Tag: 2017 source edit
No edit summary
Tag: 2017 source edit
 
(2 intermediate revisions by the same user not shown)
Line 9: Line 9:
   -- local args = frame:getParent().args
   -- local args = frame:getParent().args


  -- the VisualData query is called from here
   local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} )
   local ret = mw.visualdata.query( 'Book', '[[title::+]]', {'title'}, {} )


Line 27: Line 28:
===2) perform the query on a wiki article and then process the result using 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>
<pre>
local p = {}
local p = {}


Line 50: Line 73:




Template: [[Template:Handle query]]


Module: [[Module:Handle query]]





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":419,"title":"Demo VisualData","data":{"authors":[{"first_name":"Herbert ","last_name":"Wells","name":"Herbert Wells","email":""},{"first_name":"George","last_name":"Wells","name":"George Wells"}],"title":"The first men in the moon","cover":"File:First_Men_in_the_Moon_(1901)_frontispiece.jpg"}},{"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
}}


"module" must be a template


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


support & questions: support at topway.it

Download the latest version!