Module:Learn Lua: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
p = {} | local p = {} | ||
p. | function p.processSMWData(frame) | ||
if not mw.smw then | |||
return "mw.smw module not found" | |||
end | |||
-- Define your SMW query parameters as a table | |||
local queryParams = { | |||
"[[Category:Persons]]", | |||
"?Has surname", | |||
"format=array", -- Important for structured output | |||
"limit=5" | |||
} | |||
-- Execute the query | |||
local queryResult = mw.smw.ask(queryParams) | |||
return p | if queryResult == nil then | ||
return "(no values)" | |||
end | |||
-- Process the queryResult table | |||
local output = "" | |||
if type(queryResult) == "table" then | |||
for num, row in pairs(queryResult) do | |||
output = output .. " * Result #" .. num .. "\\n" | |||
for property, data in pairs(row) do | |||
local dataOutput = data | |||
if type(data) == 'table' then | |||
dataOutput = mw.text.listToText(data, ', ', ' and ') | |||
end | |||
output = output .. " ** " .. property .. ": " .. dataOutput .. "\\n" | |||
end | |||
end | |||
else | |||
output = tostring(queryResult) -- Handle cases where it's not a table | |||
end | |||
return output | |||
end | |||
return p | |||
Revision as of 23:47, 25 November 2025
Documentation for this module may be created at Module:Learn Lua/doc
local p = {}
function p.processSMWData(frame)
if not mw.smw then
return "mw.smw module not found"
end
-- Define your SMW query parameters as a table
local queryParams = {
"[[Category:Persons]]",
"?Has surname",
"format=array", -- Important for structured output
"limit=5"
}
-- Execute the query
local queryResult = mw.smw.ask(queryParams)
if queryResult == nil then
return "(no values)"
end
-- Process the queryResult table
local output = ""
if type(queryResult) == "table" then
for num, row in pairs(queryResult) do
output = output .. " * Result #" .. num .. "\\n"
for property, data in pairs(row) do
local dataOutput = data
if type(data) == 'table' then
dataOutput = mw.text.listToText(data, ', ', ' and ')
end
output = output .. " ** " .. property .. ": " .. dataOutput .. "\\n"
end
end
else
output = tostring(queryResult) -- Handle cases where it's not a table
end
return output
end
return p