跳转到内容

模組:沙盒/PexEric/1

维基百科,自由的百科全书

这是本页的一个历史版本,由PexEric留言 | 贡献2025年5月31日 (六) 19:12编辑。这可能和当前版本存在着巨大的差异。

local p = {}

-- 辅助函数:获取候选项目列表
-- 从指定页面使用正则表达式获取候选项目列表。
local function get_candidates(title, pattern, black, blackregex)
    local page = mw.title.new(title):getContent()
    local matches = {}
    local black_set = {}
    if black then
        for b in mw.text.gsplit(black, '|', true) do
            black_set[b] = true
        end
    end
    for m in mw.ustring.gmatch(page, pattern) do
        if not black_set[m] and not (blackregex and mw.ustring.match(m, blackregex)) then
            table.insert(matches, m)
        end
    end
    return matches
end

-- 辅助函数:获取讨论页的专题列表
-- 从条目讨论页获取所属专题列表。
local function get_projects(talkpage)
    local TemplateParameterValue = require("Module:Template parameter value")
    local match_list = {
        "wiki%s*project%s*banner%s*shell", 
        "w?pj?%s*banner%s*shell", 
        "wiki%s*project%s*banners", 
        "multiple%s*wikiprojects?", 
        "wiki%s*project%s*shell",
        "pjbs",
        "[維维]基[专專][题題][橫横]幅", 
        "多?[個个]?[維维]?基?[专專][题題][橫横]幅",
        "[維维]基[专專][题題]", 
        "多?[個个]?[維维]?基?[专專][题題]",
        "[专專][题題][橫横]幅", 
        "通用[評评][級级]",
    }
    local banner_shell_regex = table.concat(match_list, "|")
    local success, value = TemplateParameterValue.getParameter(talkpage, banner_shell_regex, "1", {treat_as_regex=true})
    if not success then
        return {}
    end
    local projects = {}
    for template in mw.ustring.gmatch(value, "{%b{}}") do
        local name = mw.ustring.match(template, "^{{%s*(.-)%s*[|}]")
        if name then
            table.insert(projects, name)
        end
    end
    return projects
end

-- 评选配置
-- 每个评选的页面标题、正则表达式等配置。
local selections = {
    ["DYKC"] = {
        title = "Wikipedia:新条目推荐/候选",
        pattern = "%{{!}}%s*article%s*=%s*(.-)%s*[%}{{!}}]",
        black = "",
        blackregex = nil
    },
    ["PR"] = {
        title = "Wikipedia:同行评审/提案区",
        pattern = "====%s*(.-)%s*===",
        black = "",
        blackregex = nil
    },
    ["GAC"] = {
        title = "Wikipedia:優良條目評選/提名區",
        pattern = "====%s*(.-)%s*===%f[^=]",
        black = "",
        blackregex = "==%s*(.-)%s*="
    },
    ["FAC"] = {
        title = "Wikipedia:典范条目评选/提名区",
        pattern = "====%s*(.-)%s*===%f[^=]",
        black = "",
        blackregex = "==%s*(.-)%s*="
    },
    ["FLC"] = {
        title = "Wikipedia:特色列表评选/提名区",
        pattern = "====%s*(.-)%s*===%f[^=]",
        black = "",
        blackregex = "==%s*(.-)%s*="
    },
    ["ITNC"] = {
        title = "Wikipedia:新闻动态候选",
        pattern = "%{{!}}%s*article%s*=%s*(.-)%s%{{!}}",
        black = "",
        blackregex = nil
    }
}

-- 主函数:list_projects
-- 以表格形式列出所有候选项目的所属专题。
function p.list_projects(frame)
    local args = require("Module:Arguments").getArgs(frame)
    local selection = args.selection
    if not selection or not selections[selection] then
        return "错误:未知的评选 '" .. (selection or "") .. "'"
    end
    local config = selections[selection]
    local candidates = get_candidates(config.title, config.pattern, config.black, config.blackregex)
    local prefix = args.prefix or ""
    local suffix = args.suffix or ""
    local tl_prefix = args.tl_prefix or "{{tl|"
    local tl_suffix = args.tl_suffix or "}}"
    local rows = {}
    for _, title in ipairs(candidates) do
        local talkpage = "Talk:" .. title
        local projects = get_projects(talkpage)
        local project_str = ""
        if #projects > 0 then
            local tl_projects = {}
            for _, proj in ipairs(projects) do
                table.insert(tl_projects, tl_prefix .. proj .. tl_suffix)
            end
            project_str = table.concat(tl_projects, "、")
        end
        local row = "|-\n| " .. prefix .. "[[" .. title .. "]]" .. suffix .. "\n| " .. project_str
        table.insert(rows, row)
    end
    local table_str = "{| class=\"wikitable\"\n! 条目 !! 专题\n" .. table.concat(rows, "\n") .. "\n|}"
    return table_str
end

-- 辅助函数:获取projects_patterns
-- 从args中获取所有projectN参数。
local function get_projects_patterns(args)
    local patterns = {}
    local i = 1
    while args["project" .. i] do
        table.insert(patterns, args["project" .. i])
        i = i + 1
    end
    return patterns
end

-- 主函数:filter_projects
-- 获取属于给定专题的候选项目,以列表形式输出。
function p.filter_projects(frame)
    local args = require("Module:Arguments").getArgs(frame)
    local selection = args.selection
    if not selection or not selections[selection] then
        return "错误:未知的评选 '" .. (selection or "") .. "'"
    end
    local config = selections[selection]
    local candidates = get_candidates(config.title, config.pattern, config.black, config.blackregex)
    local prefix = args.prefix or ""
    local suffix = args.suffix or ""
    local projects_patterns = get_projects_patterns(args)
    if #projects_patterns == 0 then
        return "错误:未提供project参数"
    end
    local results = {}
    for _, title in ipairs(candidates) do
        local talkpage = "Talk:" .. title
        local projects = get_projects(talkpage)
        local matched = false
        for _, proj in ipairs(projects) do
            for _, pattern in ipairs(projects_patterns) do
                if mw.ustring.match(proj, pattern) then
                    matched = true
                    break
                end
            end
            if matched then break end
        end
        if matched then
            table.insert(results, title)
        end
    end
    local output = ""
    for i, title in ipairs(results) do
        output = output .. "# " .. prefix .. "[[" .. title .. "]]" .. suffix .. "\n"
    end
    return output
end

return p