Module:Learn Lua

From Livermore History Collaborative
Jump to navigation Jump to search

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