Module:MalformedSPIs

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by imported>MolecularPilot at 06:02, 2 January 2025 (New function "getNumberOfMalformedSPIs"). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
local p = {}

-- Helper function to escape HTML tags
local function escapeHtml(str)
    return str
        :gsub("&", "&")
        :gsub("<", "&lt;")
        :gsub(">", "&gt;")
        :gsub('"', "&quot;")
        :gsub("'", "&#39;")
end

function p.listPages(frame)
    -- Load the raw JSON data from the specified page
    local title = mw.title.new('User:MolecularBot/MalformedSPIs.json')
    local page = title and title:getContent()

    -- Check if the content was fetched correctly
    if not page or page == "" then
        return "Error: Could not load the content from the specified page."
    end

    -- Try to parse the JSON data
    local data, err = mw.text.jsonDecode(page)

    -- Check if the data was parsed correctly
    if not data or type(data) ~= "table" then
        return "Error: Could not parse the JSON data. " .. (err or "Unknown error")
    end

    -- Check if the data is a list of malformed SPI cases
    if type(data) ~= "table" then
        return "Error: The data format is incorrect."
    end

    -- Initialize the output table to hold the wikitext for the wiki table
    local output = {}

    -- Add table headers
    table.insert(output, "{| class='wikitable' style='width:100%;'")
    table.insert(output, "|+ List of Malformed SPI Cases")
    table.insert(output, "|-")
    table.insert(output, "! Page Name")
    table.insert(output, "! Reasons")
    
    -- Iterate through the list of SPI cases
    for _, case in ipairs(data) do
        -- Check if each case has a "page" and "reason"
        if case.page and type(case.reason) == "table" then
        	-- Initialize an empty string for the reasons
        	local reasons = "\n"
        
    		-- Loop through each reason and format it with * and \n
        	for _, reason in ipairs(case.reason) do
            	reasons = reasons .. "* " .. reason .. "\n"
        	end


            reasons = escapeHtml(reasons)  -- Escape HTML tags

            -- Add the row to the table
            table.insert(output, "|-")
            table.insert(output, "| [[" .. case.page .. "]]")
            table.insert(output, "| " .. reasons .. "")
        else
            -- If the case is missing required fields, log an error
            table.insert(output, "|-")
            table.insert(output, "| Error: Invalid case format.")
            table.insert(output, "| Error: Invalid case format.")
        end
    end
    
    -- End the table
    table.insert(output, "|}")
    
    -- Return the table as a string
    return table.concat(output, "\n")
end

-- Function to count the number of malformed SPI cases
function p.getNumberOfMalformedSPIs()
    -- Load the raw JSON data from the specified page
    local title = mw.title.new('User:MolecularBot/MalformedSPIs.json')
    local page = title and title:getContent()

    -- Check if the content was fetched correctly
    if not page or page == "" then
        return "Error: Could not load the content from the specified page."
    end

    -- Try to parse the JSON data
    local data, err = mw.text.jsonDecode(page)

    -- Check if the data was parsed correctly
    if not data or type(data) ~= "table" then
        return "Error: Could not parse the JSON data. " .. (err or "Unknown error")
    end

    -- Check if the data is a list of malformed SPI cases
    if type(data) ~= "table" then
        return "Error: The data format is incorrect."
    end

    -- Initialize a counter for valid cases
    local count = 0

    -- Iterate through the list of SPI cases
    for _, case in ipairs(data) do
        -- Check if each case has a "page" and "reason"
        if case.page and type(case.reason) == "table" then
            count = count + 1
        end
    end

    -- Return the number of valid cases
    return count
end

return p