Module:Learn Lua: Difference between revisions

From Livermore History Collaborative
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
p = {}
local p = {}


p.loop = function(frame)
    function p.processSMWData(frame)
local out = {}
        if not mw.smw then
            return "mw.smw module not found"
        end


for idx = 1, 5 do
        -- Define your SMW query parameters as a table
table.insert(out, idx)
        local queryParams = {
end
            "[[Category:Persons]]",
            "?Has surname",
            "format=array", -- Important for structured output
            "limit=5"
        }


return table.concat(out, "<br>")
        -- Execute the query
end
        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