|
|
| Line 161: |
Line 161: |
| regionLookupCache = lookup | | regionLookupCache = lookup |
| return lookup | | return lookup |
| end
| |
|
| |
| -- Main data loading function with multiple fallback methods
| |
| local function loadData(frame)
| |
| -- Use the module-level cache if we already loaded data once
| |
| if dataCache then
| |
| return dataCache
| |
| end
| |
|
| |
| 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
| |
|
| |
| -- 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 271: |
Line 172: |
| end | | end |
|
| |
|
| -- Override data loading to use DatasetLoader | | -- Data loading function using DatasetLoader |
| local function loadData(frame) | | local function loadData(frame) |
| if dataCache then | | if dataCache then |