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 | local success, data = pcall(function() | ||
-- Get the JSON content using frame:preprocess if available | |||
local jsonText | |||
if frame and type(frame) == "table" and frame.preprocess then | |||
local preprocessSuccess, preprocessResult = pcall(function() | |||
return frame:preprocess('{{Data:CountryDataset.json}}') | |||
end) | |||
if preprocessSuccess and preprocessResult then | |||
jsonText = preprocessResult | |||
end | |||
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 | 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 | end | ||
end | |||
-- As absolute last resort, use local default data | |||
return DEFAULT_DATA | |||
end) | |||
if not success or not data then | |||
if not | |||
data = DEFAULT_DATA | data = DEFAULT_DATA | ||
end | end | ||
-- Ensure minimum data structure | -- Ensure minimum data structure | ||
data.countries | if not data.countries then | ||
data.icann_regions | data.countries = {} | ||
end | |||
if not data.icann_regions then | |||
data.icann_regions = {} | |||
end | |||
dataCache = data | dataCache = data | ||
return | return data | ||
end | end | ||