Jump to content

Module:DatasetLoader: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:DatasetLoader
--[[
-- Central loader for Data: JSON assets via JsonConfig or mw.loadData, cached per render
* Name: DatasetLoader
* Author: Mark W. Datysgeld
* Description: Central loader for Data: JSON assets via JsonConfig or mw.loadData, cached per render
* Notes: Tries JsonConfig extension first, then falls back to mw.loadData from Data: namespace; caches results to improve performance
]]


local DatasetLoader = {}
local DatasetLoader = {}
Line 19: Line 23:


     local data
     local data
    local loadMethod = "none"


     -- Try JsonConfig extension (mw.ext.data.get)
     -- Try JsonConfig extension (mw.ext.data.get)
Line 27: Line 30:
     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 35: Line 37:
         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 42: Line 43:
     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 any Campaign request
    if name:match("Campaigns/") then
        local ErrorHandling = require('Module:ErrorHandling')
        local context = ErrorHandling.createContext('DatasetLoader')
        ErrorHandling.addStatus(context, 'datasetLoader', 'Campaign request: "' .. name .. '" via ' .. loadMethod, 'FullName: "' .. fullName .. '"')
       
        if data and type(data) == "table" then
            if data.defaults then
                local titleValue = data.defaults.title or "MISSING"
                ErrorHandling.addStatus(context, 'datasetLoader', 'Campaign data structure found', 'Title field: "' .. tostring(titleValue) .. '"')
            else
                ErrorHandling.addStatus(context, 'datasetLoader', 'Campaign data loaded but no defaults', 'Keys: ' .. table.concat(vim.tbl_keys(data or {}), ", "))
            end
        else
            ErrorHandling.addStatus(context, 'datasetLoader', 'Campaign data load failed', 'Data type: ' .. type(data))
        end
       
        -- Force output the debug info immediately
        mw.log(ErrorHandling.formatCombinedOutput(context))
     end
     end



Latest revision as of 02:58, 25 August 2025

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

--[[
* Name: DatasetLoader
* Author: Mark W. Datysgeld
* Description: Central loader for Data: JSON assets via JsonConfig or mw.loadData, cached per render
* Notes: Tries JsonConfig extension first, then falls back to mw.loadData from Data: namespace; caches results to improve performance
]]

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

    -- 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
    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
        end
    end

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

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

return DatasetLoader