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 | -- Fallback to raw JSON via frame:preprocess | ||
if not data or type(data) ~= 'table' or not data.countries or next(data.countries) == nil | local jsonText | ||
if (not data or type(data) ~= 'table' or not data.countries or next(data.countries) == nil) | |||
and frame and type(frame) == 'table' and frame.preprocess then | |||
local ok, res = pcall(frame.preprocess, frame, '{{Data:CountryDataset.json}}') | |||
if ok and res then | |||
jsonText = res | |||
end | end | ||
end | end | ||
-- Fallback to DEFAULT_DATA if still | -- 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 | ||
data.countries = data.countries or {} | |||
data.icann_regions = data.icann_regions or {} | |||
dataCache = data | dataCache = data | ||