Module:User/JsonProbe: Difference between revisions

From Foxopedia
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 16: Line 16:


function p.fetch()
function p.fetch()
if not hasLuaJson() then
local function hasLuaJson()
return type(mw.ext) == 'table'
return 'JsonConfig Lua: MISSING (no mw.ext.data.get)'
and type(mw.ext.data) == 'table'
end
and type(mw.ext.data.get) == 'function'
end
if not hasLuaJson() then
return 'JsonConfig Lua: MISSING (no mw.ext.data.get)'
end


-- Try both title forms (spaces vs underscores)
-- Try both space and underscore versions, WITH the Data: namespace
local name1 = 'CS1/Identifier limits.tab'
local names = {
local name2 = 'CS1/Identifier_limits.tab'
'Data:CS1/Identifier limits.tab',
'Data:CS1/Identifier_limits.tab',
}


local function tryGet(name)
local ok, res
for _, name in ipairs(names) do
return pcall(function()
return mw.ext.data.get(name)
ok, res = pcall(function() return mw.ext.data.get(name) end)
if ok and type(res) == 'table' and type(res.data) == 'table' then
end)
return 'Commons Data reachable: rows=' .. tostring(#res.data) .. ' (' .. name .. ')'
end
end
end


if not ok then
-- First attempt
return 'pcall error: ' .. tostring(res)
local ok, res = tryGet(name1)
end
if not ok or type(res) ~= 'table' or type(res.data) ~= 'table' then
return 'mw.ext.data.get returned non-table; likely no access to remote Data:'
-- Second attempt
ok, res = tryGet(name2)
end

if not ok then
return 'pcall error: ' .. tostring(res)
end

if type(res) == 'table' and type(res.data) == 'table' then
return 'Commons Data reachable: rows=' .. tostring(#res.data)
else
return 'mw.ext.data.get returned type: ' .. tostring(type(res))
end
end
end



Latest revision as of 03:20, 12 August 2025

Documentation for this module may be created at Module:User/JsonProbe/doc

local p = {}

local function hasLuaJson()
	return type(mw.ext) == 'table'
		and type(mw.ext.data) == 'table'
		and type(mw.ext.data.get) == 'function'
end

function p.run()
	if hasLuaJson() then
		return 'JsonConfig Lua: OK'
	else
		return 'JsonConfig Lua: MISSING (no mw.ext.data.get)'
	end
end

function p.fetch()
  local function hasLuaJson()
    return type(mw.ext) == 'table'
      and type(mw.ext.data) == 'table'
      and type(mw.ext.data.get) == 'function'
  end
  if not hasLuaJson() then
    return 'JsonConfig Lua: MISSING (no mw.ext.data.get)'
  end

  -- Try both space and underscore versions, WITH the Data: namespace
  local names = {
    'Data:CS1/Identifier limits.tab',
    'Data:CS1/Identifier_limits.tab',
  }

  local ok, res
  for _, name in ipairs(names) do
    ok, res = pcall(function() return mw.ext.data.get(name) end)
    if ok and type(res) == 'table' and type(res.data) == 'table' then
      return 'Commons Data reachable: rows=' .. tostring(#res.data) .. ' (' .. name .. ')'
    end
  end

  if not ok then
    return 'pcall error: ' .. tostring(res)
  end
  return 'mw.ext.data.get returned non-table; likely no access to remote Data:'
end

return p