Module:Learn Lua: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| 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 = { | |||
"[[Source:+]]", | |||
"[[Mentions::Person:Nellie I. Boston]]", | |||
"?Item Number", | |||
"?Newspaper Title", | |||
"?Newspaper Page", | |||
"?Newspaper Column", | |||
"?Publication Date", | |||
"?Full Text", | |||
"?Excerpt", | |||
"?Summary", | |||
"?Online Source", | |||
"format=array" | |||
} | |||
return p | -- 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 | |||
Latest revision as of 23:53, 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 = {
"[[Source:+]]",
"[[Mentions::Person:Nellie I. Boston]]",
"?Item Number",
"?Newspaper Title",
"?Newspaper Page",
"?Newspaper Column",
"?Publication Date",
"?Full Text",
"?Excerpt",
"?Summary",
"?Online Source",
"format=array"
}
-- 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