Module:LearnsetsTestingData: Difference between revisions

From Pokemon Revolution Online Wiki
Jump to navigation Jump to search
Cae (talk | contribs)
Created page with "local p = {} local TARGET_URL = "https://wiki.pokemonrevolution.net/data/Learnsets.json" local function tryExternalDataJson() local ok, data, errors = pcall(function() return mw.ext.externalData.getExternalData{ url = TARGET_URL, format = "json" } end) if not ok then return { method = "ExternalData (format=json)", success = false, message = "Lua error: " .. tostring(data)..."
 
Cae (talk | contribs)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


local TARGET_URL = "https://wiki.pokemonrevolution.net/data/Learnsets.json"
local function testFileAccess()
 
local function tryExternalDataJson()
     local ok, data, errors = pcall(function()
     local ok, data, errors = pcall(function()
         return mw.ext.externalData.getExternalData{
         return mw.ext.externalData.getFileData{
             url = TARGET_URL,
             source = "data",
             format = "json"
            ["file name"] = "Learnsets.json",
             format = "json",
            data = {all = "__json"}
         }
         }
     end)
     end)
Line 13: Line 13:
     if not ok then
     if not ok then
         return {
         return {
            method = "ExternalData (format=json)",
             success = false,
             success = false,
             message = "Lua error: " .. tostring(data)
             message = "Lua error: " .. tostring(data)
Line 19: Line 18:
     end
     end


     if errors then
     if errors and #errors > 0 then
         return {
         return {
            method = "ExternalData (format=json)",
             success = false,
             success = false,
             message = "ExternalData errors: " .. table.concat(errors, " | ")
             message = "ExternalData error: " .. table.concat(errors, " | ")
         }
         }
     end
     end
Line 29: Line 27:
     if data and data.__json then
     if data and data.__json then
         return {
         return {
            method = "ExternalData (format=json)",
             success = true,
             success = true,
             message = "__json received"
             message = "JSON successfully loaded via filesystem (source=data)"
         }
         }
     end
     end


     return {
     return {
        method = "ExternalData (format=json)",
         success = false,
         success = false,
         message = "No __json returned (likely HTML instead of JSON)"
         message = "No JSON returned. File likely not present in the configured ExternalData source."
     }
     }
end
end




local function tryExternalDataRaw()
function p.run()
     local ok, data, errors = pcall(function()
     local result = testFileAccess()
        return mw.ext.externalData.getExternalData{
            url = TARGET_URL
        }
    end)


    if not ok then
        return {
            method = "ExternalData (auto-detect)",
            success = false,
            message = "Lua error: " .. tostring(data)
        }
    end
    if errors then
        return {
            method = "ExternalData (auto-detect)",
            success = false,
            message = "ExternalData errors: " .. table.concat(errors, " | ")
        }
    end
    if data then
        return {
            method = "ExternalData (auto-detect)",
            success = true,
            message = "Some data returned"
        }
    end
    return {
        method = "ExternalData (auto-detect)",
        success = false,
        message = "No data returned"
    }
end
local function tryApiFetch()
    local apiUrl =
        "https://wiki.pokemonrevolution.net/api.php?action=query&meta=siteinfo&format=json"
    local ok, data, errors = pcall(function()
        return mw.ext.externalData.getExternalData{
            url = apiUrl,
            format = "json"
        }
    end)
    if not ok then
        return {
            method = "API test (control test)",
            success = false,
            message = "Lua error: " .. tostring(data)
        }
    end
    if errors then
        return {
            method = "API test (control test)",
            success = false,
            message = "ExternalData errors: " .. table.concat(errors, " | ")
        }
    end
    if data and data.__json then
        return {
            method = "API test (control test)",
            success = true,
            message = "API works (server allows HTTP requests)"
        }
    end
    return {
        method = "API test (control test)",
        success = false,
        message = "API returned no JSON"
    }
end
local function render(results)
     local out = {}
     local out = {}
     table.insert(out, "== Learnsets.json Access Test ==")
     table.insert(out, "== ExternalData Filesystem Test ==")
     table.insert(out, "")
     table.insert(out, "")
     table.insert(out, "Target URL: " .. TARGET_URL)
     table.insert(out, "* Source: data")
    table.insert(out, "* File: Learnsets.json")
     table.insert(out, "")
     table.insert(out, "")
 
     table.insert(out, "* Success: " .. tostring(result.success))
     for _, r in ipairs(results) do
    table.insert(out, "* Message: " .. result.message)
        table.insert(out, "=== " .. r.method .. " ===")
        table.insert(out, "* Success: " .. tostring(r.success))
        table.insert(out, "* Message: " .. r.message)
        table.insert(out, "")
    end


     return table.concat(out, "\n")
     return table.concat(out, "\n")
end
function p.run()
    local results = {
        tryExternalDataJson(),
        tryExternalDataRaw(),
        tryApiFetch()
    }
    return render(results)
end
end


return p
return p

Latest revision as of 14:00, 6 March 2026

Documentation for this module may be created at Module:LearnsetsTestingData/doc

local p = {}

local function testFileAccess()
    local ok, data, errors = pcall(function()
        return mw.ext.externalData.getFileData{
            source = "data",
            ["file name"] = "Learnsets.json",
            format = "json",
            data = {all = "__json"}
        }
    end)

    if not ok then
        return {
            success = false,
            message = "Lua error: " .. tostring(data)
        }
    end

    if errors and #errors > 0 then
        return {
            success = false,
            message = "ExternalData error: " .. table.concat(errors, " | ")
        }
    end

    if data and data.__json then
        return {
            success = true,
            message = "JSON successfully loaded via filesystem (source=data)"
        }
    end

    return {
        success = false,
        message = "No JSON returned. File likely not present in the configured ExternalData source."
    }
end


function p.run()
    local result = testFileAccess()

    local out = {}
    table.insert(out, "== ExternalData Filesystem Test ==")
    table.insert(out, "")
    table.insert(out, "* Source: data")
    table.insert(out, "* File: Learnsets.json")
    table.insert(out, "")
    table.insert(out, "* Success: " .. tostring(result.success))
    table.insert(out, "* Message: " .. result.message)

    return table.concat(out, "\n")
end

return p