打开/关闭菜单
28
6680
50
1.4万
OGAS数据中枢
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:GFLCalc/check

来自OGAS数据中枢
弃权者​(留言 | 贡献)2026年10月19日 (一) 22:42的版本 (gflcalc: make check/index use mw.loadJsonData)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在Module:GFLCalc/check/doc创建

--[[
Module:GFLCalc/check —— 部署自检

用法:
    {{#invoke:GFLCalc/check|report}}                 -- 只查核心页面
    {{#invoke:GFLCalc/check|report|M4A1|TT33}}       -- 顺便查这几个单位的数据页
    {{#invoke:GFLCalc/check|index}}                  -- 只看索引页能否被读取

输出一张表:页面 / 是否存在 / 实际内容模型 / 期望模型 / 状态。
]]

local p = {}

local CORE = {
	{ "Module:GFLCalc", "Scribunto" },
	{ "Module:GFLCalc/index", "json" },
	{ "Module:GFLCalc/panel", "Scribunto" },
	{ "Template:GFLCalc", "wikitext" },
	{ "Template:GFLCalc/styles.css", "sanitized-css" },
	{ "Widget:GFLCalcPanel", "wikitext" },
}

local function probe(title)
	local ok, t = pcall(mw.title.new, title)
	if not ok or not t then
		return { exists = false, model = "?" }
	end
	return { exists = t.exists, model = t.contentModel }
end

local function esc(s)
	return (tostring(s):gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;"))
end

local function load_json(title)
	if type(mw.loadJsonData) == "function" then
		local ok, data = pcall(mw.loadJsonData, title)
		if ok and type(data) == "table" then return data end
	end
	local ok, data = pcall(mw.loadData, title)
	if ok and type(data) == "table" then return data end
	return nil
end

function p.report(frame)
	local rows, bad = {}, 0

	local function add(title, want)
		local i = probe(title)
		local okk = i.exists and i.model == want
		if not okk then bad = bad + 1 end
		rows[#rows + 1] = { title = title, want = want, got = i.model, exists = i.exists, ok = okk }
	end

	for _, c in ipairs(CORE) do add(c[1], c[2]) end
	for _, code in ipairs(frame.args) do
		add("Module:GFLCalc/data/" .. code, "json")
		add("Module:GFLCalc/brief/" .. code, "json")
	end

	local tbl = mw.html.create("table"):addClass("wikitable"):addClass("gfl-check")
	local hr = tbl:tag("tr")
	for _, h in ipairs({ "页面", "存在", "实际模型", "期望模型", "状态" }) do
		hr:tag("th"):wikitext(h)
	end
	for _, r in ipairs(rows) do
		local tr = tbl:tag("tr")
		tr:tag("td"):wikitext(esc(r.title))
		tr:tag("td"):wikitext(r.exists and "是" or "否")
		tr:tag("td"):wikitext(esc(r.got or "-"))
		tr:tag("td"):wikitext(esc(r.want))
		tr:tag("td"):wikitext(r.ok and "OK" or '<span class="gfl-error">✗</span>')
	end

	return tostring(tbl) ..
		string.format("\n共 %d 项,异常 %d 项。", #rows, bad)
end

--- 单个数据页诊断:{{#invoke:GFLCalc/check|probe|M4A1}}
function p.probe(frame)
	local code = frame.args[1] or "M4A1"
	local title = "Module:GFLCalc/data/" .. code
	local t = mw.title.new(title)
	local ok, res = pcall(mw.loadData, title)
	local parts = {
		"标题 = " .. title,
		"存在 = " .. tostring(t and t.exists),
		"模型 = " .. tostring(t and t.contentModel or "?"),
		"loadData = " .. (ok and (res ~= nil and "成功" or "返回 nil") or ("报错:" .. tostring(res))),
	}
	if type(mw.loadJsonData) == "function" then
		local ok2, data = pcall(mw.loadJsonData, title)
		parts[#parts + 1] = "loadJsonData = " ..
			(ok2 and (type(data) == "table" and "成功" or "返回 " .. type(data)) or ("报错:" .. tostring(data)))
	end
	return table.concat(parts, "<br>")
end

--- 对比三条读取路径:{{#invoke:GFLCalc/check|load|Module:GFLCalc/data/M4A1}}
function p.load(frame)
	local title = mw.text.trim(frame.args[1] or "")
	if title == "" then title = "Module:GFLCalc/data/M4A1" end
	local t = mw.title.new(title)
	local out = { "标题 = " .. title }

	if type(mw.loadJsonData) == "function" then
		local ok, data = pcall(mw.loadJsonData, title)
		out[#out + 1] = "loadJsonData = " .. (ok and (type(data) == "table" and "成功" or "返回 " .. type(data)) or ("报错:" .. tostring(data)))
	else
		out[#out + 1] = "loadJsonData = 不存在"
	end

	local ok, data = pcall(mw.loadData, title)
	out[#out + 1] = "loadData = " .. (ok and (type(data) == "table" and "成功" or "返回 " .. type(data)) or ("报错:" .. tostring(data)))

	local text = t and t:getContent()
	out[#out + 1] = "getContent = " .. (text and ("成功,长度 " .. #text) or "返回 nil")
	if text then
		local ok2, parsed = pcall(mw.text.jsonDecode, text)
		out[#out + 1] = "jsonDecode = " .. (ok2 and (type(parsed) == "table" and "成功" or "返回 " .. type(parsed)) or ("报错:" .. tostring(parsed)))
	end

	return table.concat(out, "<br>")
end

--- 数据页顶层/改造表的键类型:{{#invoke:GFLCalc/check|mods|M4A1}}
function p.mods(frame)
	local code = frame.args[1] or "M4A1"
	local title = "Module:GFLCalc/data/" .. code
	local ok, d = pcall(mw.loadJsonData, title)
	if not ok or type(d) ~= "table" then
		return "loadJsonData 失败:" .. tostring(d)
	end
	local keys = {}
	for k in pairs(d) do keys[#keys + 1] = type(k) .. ":" .. tostring(k) end
	table.sort(keys)
	local out = { "顶层键 = " .. table.concat(keys, ", ") }
	if type(d.mods) == "table" then
		local mk = {}
		for k in pairs(d.mods) do mk[#mk + 1] = type(k) .. ":" .. tostring(k) end
		table.sort(mk)
		out[#out + 1] = "mods 键 = " .. table.concat(mk, ", ")
		for _, k in ipairs({ "0", "3", 0, 3, 1, 2 }) do
			local v = d.mods[k]
			out[#out + 1] = string.format("mods[%s(%s)] = %s", tostring(k), type(k), type(v))
		end
	else
		out[#out + 1] = "mods 类型 = " .. type(d.mods)
	end
	return table.concat(out, "<br>")
end

--- 打印数据页开头:{{#invoke:GFLCalc/check|raw|M4A1}}
function p.raw(frame)
	local code = frame.args[1] or "M4A1"
	local t = mw.title.new("Module:GFLCalc/data/" .. code)
	if not t or not t.exists then
		return '<span class="gfl-error">页面不存在</span>'
	end
	local text = t:getContent() or ""
	local head = text:sub(1, 160)
	local ok, err = pcall(mw.text.jsonDecode, text)
	return string.format("长度 %d<br>JSON 解析:%s<br><br><code>%s</code>",
		#text, ok and "成功" or ("失败:" .. tostring(err)), mw.text.nowiki(head))
end

--- 列出索引里的键(诊断用):{{#invoke:GFLCalc/check|keys}}
function p.keys(frame)
	local idx = load_json("Module:GFLCalc/index")
	if type(idx) ~= "table" then
		return '<span class="gfl-error">index 读取失败</span>'
	end
	local ks = {}
	for k in pairs(idx) do ks[#ks + 1] = tostring(k) end
	table.sort(ks)
	local show = {}
	for i = 1, math.min(#ks, 40) do show[#show + 1] = ks[i] end
	return string.format("索引共 %d 键;前 %d 个:\n%s", #ks, #show, table.concat(show, ", "))
end

function p.index()
	local idx = load_json("Module:GFLCalc/index")
	if type(idx) ~= "table" then
		return '<span class="gfl-error">Module:GFLCalc/index 读取失败(页面不存在、模型不对,或两种加载方式都失败)</span>'
	end
	local n = 0
	for _ in pairs(idx) do n = n + 1 end
	return string.format("index 正常:%d 条,数据版本 %s", n, tostring(idx.__dv__ or "?"))
end

return p