Jump to content

Module:DatasetLoader: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Tag: Reverted
Line 19: Line 19:


     local data
     local data
    local loadMethod = "none"


     -- Try JsonConfig extension (mw.ext.data.get)
     -- Try JsonConfig extension (mw.ext.data.get)
Line 26: Line 27:
     if success and type(result) == 'table' then
     if success and type(result) == 'table' then
         data = result
         data = result
        loadMethod = "JsonConfig"
     else
     else
         -- Fallback to mw.loadData from Data: namespace
         -- Fallback to mw.loadData from Data: namespace
Line 33: Line 35:
         if success and type(result) == 'table' then
         if success and type(result) == 'table' then
             data = result
             data = result
            loadMethod = "mw.loadData"
         end
         end
     end
     end
Line 39: Line 42:
     if not data or type(data) ~= 'table' then
     if not data or type(data) ~= 'table' then
         data = {}
         data = {}
        loadMethod = "failed"
    end
    -- DEBUG: Log what we loaded for ASP2025
    if name == "Campaigns/ASP2025" and data.defaults then
        local ErrorHandling = require('Module:ErrorHandling')
        local context = ErrorHandling.createContext('DatasetLoader')
        ErrorHandling.addStatus(context, 'datasetLoader', 'ASP2025 loaded via ' .. loadMethod, 'Title field: "' .. tostring(data.defaults.title) .. '"')
        -- Force output the debug info immediately
        mw.log(ErrorHandling.formatCombinedOutput(context))
     end
     end



Revision as of 02:10, 2 August 2025

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

-- Module:DatasetLoader
-- Central loader for Data: JSON assets via JsonConfig or mw.loadData, cached per render

local DatasetLoader = {}
local cache = {}

-- Retrieve a dataset by name (without extension). Returns a Lua table or {}.
function DatasetLoader.get(name)
    -- Return cached if available
    if cache[name] then
        return cache[name]
    end

    -- Ensure filename has .json extension
    local fullName = name
    if not fullName:match('%.%w+$') then
        fullName = fullName .. '.json'
    end

    local data
    local loadMethod = "none"

    -- Try JsonConfig extension (mw.ext.data.get)
    local success, result = pcall(function()
        return mw.ext.data.get(fullName)
    end)
    if success and type(result) == 'table' then
        data = result
        loadMethod = "JsonConfig"
    else
        -- Fallback to mw.loadData from Data: namespace
        success, result = pcall(function()
            return mw.loadData('Data:' .. fullName)
        end)
        if success and type(result) == 'table' then
            data = result
            loadMethod = "mw.loadData"
        end
    end

    -- Default to empty table on any failure
    if not data or type(data) ~= 'table' then
        data = {}
        loadMethod = "failed"
    end

    -- DEBUG: Log what we loaded for ASP2025
    if name == "Campaigns/ASP2025" and data.defaults then
        local ErrorHandling = require('Module:ErrorHandling')
        local context = ErrorHandling.createContext('DatasetLoader')
        ErrorHandling.addStatus(context, 'datasetLoader', 'ASP2025 loaded via ' .. loadMethod, 'Title field: "' .. tostring(data.defaults.title) .. '"')
        -- Force output the debug info immediately
        mw.log(ErrorHandling.formatCombinedOutput(context))
    end

    -- Cache and return
    cache[name] = data
    return data
end

return DatasetLoader