Module:CountryData: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
Tag: Manual revert
Line 162: Line 162:
-- Main data loading function with multiple fallback methods
-- 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 loader = require('Module:DatasetLoader')
     local success, data = pcall(function()
    local data = loader.get('CountryDataset')
        -- Get the JSON content using frame:preprocess if available
 
        local jsonText
    -- Fallback to raw JSON via frame:preprocess
        if frame and type(frame) == "table" and frame.preprocess then
    local jsonText
            local preprocessSuccess, preprocessResult = pcall(function()
    if (not data or type(data) ~= 'table' or not data.countries or next(data.countries) == nil)
                return frame:preprocess('{{Data:CountryDataset.json}}')
      and frame and type(frame) == 'table' and frame.preprocess then
            end)
        local ok, res = pcall(frame.preprocess, frame, '{{Data:CountryDataset.json}}')
           
        if ok and res then
            if preprocessSuccess and preprocessResult then
            jsonText = res
                jsonText = preprocessResult
            end
         end
         end
    end
       
 
        -- If we couldn't get JSON from frame:preprocess, fall back to direct content loading
    -- Fallback to mw.loadJsonData if available and no jsonText
        if not jsonText then
    if not jsonText and mw.loadJsonData then
            -- Try using mw.loadJsonData first (preferred method)
        local ok, tbl = pcall(mw.loadJsonData, 'Data:CountryDataset.json')
            if mw.loadJsonData then
        if ok and type(tbl) == 'table' then
                local loadJsonSuccess, jsonData = pcall(function()
             data = tbl
                    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
         end
    end
       
 
        -- Try different JSON decode approaches
    -- Decode jsonText if present
        if jsonText and mw.text and mw.text.jsonDecode then
    if jsonText and mw.text and mw.text.jsonDecode then
            -- First try WITHOUT PRESERVE_KEYS flag (standard approach)
        local ok, tbl = pcall(mw.text.jsonDecode, jsonText)
            local jsonDecodeSuccess, jsonData = pcall(function()
        if ok and type(tbl) == 'table' then
                return mw.text.jsonDecode(jsonText)
             data = tbl
            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
         end
     end
       
        -- As absolute last resort, use local default data
        return DEFAULT_DATA
     end)


    -- Fallback to DEFAULT_DATA if still no countries
     if not success or not data then
     if not data or type(data) ~= 'table' or not data.countries then
         data = DEFAULT_DATA
         data = DEFAULT_DATA
     end
     end


     -- Ensure minimum data structure
     -- Ensure minimum data structure
     data.countries = data.countries or {}
     if not data.countries then
     data.icann_regions = data.icann_regions or {}
        data.countries = {}
     end
   
    if not data.icann_regions then
        data.icann_regions = {}
    end


     dataCache = data
     dataCache = data
     return dataCache
     return data
end
end