Module:CountryData: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
Tag: Reverted
Line 169: Line 169:
     local data = loader.get('CountryDataset')
     local data = loader.get('CountryDataset')


     -- Fallback to server-side JSON loader if loader fails
     -- Fallback to raw JSON via frame:preprocess
     if not data or type(data) ~= 'table' or not data.countries or next(data.countries) == nil then
    local jsonText
        if mw.loadJsonData then
     if (not data or type(data) ~= 'table' or not data.countries or next(data.countries) == nil)
            local ok, jsonData = pcall(mw.loadJsonData, 'Data:CountryDataset.json')
      and frame and type(frame) == 'table' and frame.preprocess then
            if ok and type(jsonData) == 'table' then
        local ok, res = pcall(frame.preprocess, frame, '{{Data:CountryDataset.json}}')
                data = jsonData
        if ok and res then
            end
            jsonText = res
         end
         end
     end
     end


     -- Fallback to DEFAULT_DATA if still empty
    -- Fallback to mw.loadJsonData if available and no jsonText
    if not jsonText and mw.loadJsonData then
        local ok, tbl = pcall(mw.loadJsonData, 'Data:CountryDataset.json')
        if ok and type(tbl) == 'table' then
            data = tbl
        end
    end
 
    -- Decode jsonText if present
    if jsonText and mw.text and mw.text.jsonDecode then
        local ok, tbl = pcall(mw.text.jsonDecode, jsonText)
        if ok and type(tbl) == 'table' then
            data = tbl
        end
    end
 
     -- Fallback to DEFAULT_DATA if still no countries
     if not data or type(data) ~= 'table' or not data.countries then
     if not data or type(data) ~= 'table' or not data.countries then
         data = DEFAULT_DATA
         data = DEFAULT_DATA
Line 185: Line 201:


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


     dataCache = data
     dataCache = data