跳转到内容

模組:CCHE/2025/stats

维基百科,自由的百科全书
(重定向自Module:CCHPE/2025/stats
local p = {}

-- 内部辅助函数,用于获取并解析所有数据
function p.getDataInternal(title)
    local pageTitle = mw.title.new(title, 'Talk')
    --[[测试发现.exists有缓存,反复查同一个标题也只计一次。但能用页面内容来替代,故注释
    --其实相比之下.categories每次都会计expensive function
    if not pageTitle or not pageTitle.exists then
        return nil -- 如果讨论页不存在,返回nil
    end
    ]]

    local templateName = "CCHE/2025/talk"
    local redirectTemplateName = "CP25T"
    --本年受模板限制只能使用“CP25T”,后续请改用“CExxT”作为简称,相关模板页亦有注释

    local score = nil
    local incentive_low = false
    local incentive_medium = false
    local incentive_high = false
    local improve = false

    local content = pageTitle:getContent() -- 获取讨论页内容
    if content then
        local templateContent = nil
        local function findTemplateContent(tmplName)
            local pattern = "{{%s*" .. tmplName .. "%s*%|([^}]*)}}"
            return string.match(content, pattern)
        end

        templateContent = findTemplateContent(templateName)
        if not templateContent then
            templateContent = findTemplateContent(redirectTemplateName)
        end

        if templateContent then
			-- 读取分数。这个正则要求分数是第一个出现的参数
            local scoreMatch = string.match(templateContent, "^%s*(%d+%.?%d*)%s*")
            if scoreMatch then
                score = tonumber(scoreMatch)
            end

            -- 检查激励参数
            if string.find(templateContent, "%|%s*长%s*=%s*[^|}%s]+") then
                incentive_low = true
            end
            if string.find(templateContent, "%|%s*国%s*=%s*[^|}%s]+") then
                incentive_medium = true
            end
            if string.find(templateContent, "%|%s*待%s*=%s*[^|}%s]+") then
                incentive_high = true
            end
            if string.find(templateContent, "%|%s*改%s*=%s*[^|}%s]+") then
                improve = true
            end
        end
    end

    return {
        score = score,
        incentive_low = incentive_low,
        incentive_medium = incentive_medium,
        incentive_high = incentive_high,
        improve = improve
    }
end

-- p.get 函数 (模板实际调用的入口点)
function p.get(frame)
    local args = frame.args
    local title = args[1]
    local key = args[2] -- 期待第二个参数是 'score', 'incentive_low' 等

    local data = p.getDataInternal(title) -- 调用内部函数获取所有数据
    if data and data[key] ~= nil then -- 检查 data 是否存在,并且 data[key] 不是 nil
        return data[key]
    else
        return nil -- 如果数据不存在,或者特定键的值为 nil,则返回 nil
    end
end

-- p.getData 函数 (为了兼容您原始代码结构而保留,但其逻辑将与 p.getDataInternal 保持一致)
function p.getData(frame)
    local args = frame:getParent().args
    local title = args[1] -- 获取条目名称

    return p.getDataInternal(title) -- 直接调用核心逻辑
end

-- 替代原Template:CCHE/2025/total的格式化功能,避免在模板里反复调本模块的get方法
function p.formatRow(frame)
    local args = frame:getParent().args
    local title = args[1] -- 条目名称

    local data = p.getDataInternal(title)

    local cols = {
	    ' style="text-align: left;" | [[' .. title .. ']]',
		data.score or '<span class="error">错误:无法读取分数值,请检查是否为条目讨论页挂上编辑松模板并正确填写分数值,或条目是否已被评审</span>',
		args['多媒体'] or '',
		data.incentive_low and '✓' or '',
		data.incentive_medium and '✓' or '',
		data.incentive_high and '✓' or '',
		data.improve and '✓' or ''
	}

    -- 生成表格行的wikitable代码
    local parts = {}
	for i, cell in ipairs(cols) do
	    parts[#parts+1] = '|' .. tostring(cell) .. '\n'
	end
	parts[#parts+1] = "|-\n"
	return table.concat(parts)
end

return p