Module:CountryData: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
Tag: Manual revert
Line 161: Line 161:
end
end


-- Single data loading via DatasetLoader
-- Main data loading function with multiple fallback methods
local function loadData(frame)
local function loadData(frame)
    -- Use the module-level cache if we already loaded data once
     if dataCache then
     if dataCache then
         return dataCache
         return dataCache
     end
     end
     local raw = loader.get('CountryDataset')
 
    dataCache = {
     local success, data = pcall(function()
        countries      = raw.countries      or {},
        -- Get the JSON content using frame:preprocess if available
         icann_regions  = raw.icann_regions  or {},
        local jsonText
        schema_version = raw.schema_version,
        if frame and type(frame) == "table" and frame.preprocess then
         last_updated  = raw.last_updated
            local preprocessSuccess, preprocessResult = pcall(function()
     }
                return frame:preprocess('{{Data:CountryDataset.json}}')
     return dataCache
            end)
           
            if preprocessSuccess and preprocessResult then
                jsonText = preprocessResult
            end
        end
       
        -- If we couldn't get JSON from frame:preprocess, fall back to direct content loading
        if not jsonText then
            -- Try using mw.loadJsonData first (preferred method)
            if mw.loadJsonData then
                local loadJsonSuccess, jsonData = pcall(function()
                    return mw.loadJsonData('Data:CountryDataset.json')
                end)
               
                if loadJsonSuccess and jsonData and type(jsonData) == 'table' then
                    return jsonData
                end
            end
           
            -- Direct content loading approach as fallback
            local pageTitle = mw.title.new('Data:CountryDataset.json')
            if not pageTitle or not pageTitle.exists then
                return DEFAULT_DATA
            end
           
            -- Get raw content from the wiki page
            local contentSuccess, content = pcall(function()
                return pageTitle:getContent()
            end)
           
            if contentSuccess and content and content ~= "" then
                -- Remove any BOM or leading whitespace that might cause issues
                content = content:gsub("^%s+", "")
                if content:byte(1) == 239 and content:byte(2) == 187 and content:byte(3) == 191 then
                    content = content:sub(4)
                end
               
                jsonText = content
            else
                return DEFAULT_DATA
            end
         end
       
        -- Try different JSON decode approaches
        if jsonText and mw.text and mw.text.jsonDecode then
            -- First try WITHOUT PRESERVE_KEYS flag (standard approach)
            local jsonDecodeSuccess, jsonData = pcall(function()
                return mw.text.jsonDecode(jsonText)
            end)
           
            if jsonDecodeSuccess and jsonData then
                return jsonData
            end
           
            -- If that failed, try with JSON_TRY_FIXING flag
            jsonDecodeSuccess, jsonData = pcall(function()
                return mw.text.jsonDecode(jsonText, mw.text.JSON_TRY_FIXING)
            end)
           
            if jsonDecodeSuccess and jsonData then
                return jsonData
            end
        end
       
        -- As absolute last resort, use local default data
         return DEFAULT_DATA
    end)
 
    if not success or not data then
        data = DEFAULT_DATA
    end
 
    -- Ensure minimum data structure
    if not data.countries then
        data.countries = {}
    end
   
     if not data.icann_regions then
        data.icann_regions = {}
    end
 
    dataCache = data
     return data
end
end


Line 183: Line 267:
     propertyCache = {}
     propertyCache = {}
     functionCache = {}
     functionCache = {}
end
-- Override data loading to use DatasetLoader
local function loadData(frame)
    if dataCache then
        return dataCache
    end
    local raw = loader.get('CountryDataset')
    dataCache = {
        countries      = raw.countries      or {},
        icann_regions  = raw.icann_regions  or {},
        schema_version = raw.schema_version,
        last_updated  = raw.last_updated
    }
    return dataCache
end
end