模块:GFLCalc/check
来自OGAS数据中枢
更多操作
此模块的文档可以在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("&", "&"):gsub("<", "<"):gsub(">", ">"))
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
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