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

Module:GFLCalc/check:修订间差异

来自OGAS数据中枢
imported>GFLCalc
gflcalc data import dv=sha256:e510650bd49bf03c
 
弃权者​(留言 | 贡献)
小 gflcalc: use mw.loadJsonData for JSON pages
第65行: 第65行:
return tostring(tbl) ..
return tostring(tbl) ..
string.format("\n共 %d 项,异常 %d 项。", #rows, bad)
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))),
}
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|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 ok, idx = pcall(mw.loadData, "Module:GFLCalc/index")
if not ok or 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
end


第82行: 第152行:


return p
return p

2026年10月19日 (一) 22:35的版本

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

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

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

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

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

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))),
	}
	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|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 ok, idx = pcall(mw.loadData, "Module:GFLCalc/index")
	if not ok or 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 ok, idx = pcall(mw.loadData, "Module:GFLCalc/index")
	if not ok then
		return '<span class="gfl-error">Module:GFLCalc/index 读取失败:'
			.. esc(idx) .. "</span>"
	end
	if type(idx) ~= "table" then
		return '<span class="gfl-error">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