模組:沙盒/PexEric/1
外观
local p = {}
-- WikiProject banner shell的所有重定向列表(小写)
local banner_shell_patterns = {
"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 function getCandidates(title, pattern, blackregex, black, linkprefix)
-- 调用PatternedCandidateUtils模块获取候选项目
local frame = {
args = {
title = title,
pattern = pattern,
blackregex = blackregex,
black = black,
linkprefix = linkprefix
}
}
local candidateUtils = require('Module:PatternedCandidateUtils')
return candidateUtils.list(frame)
end
-- 从讨论页获取专题信息
local function getProjects(pageName)
-- 构建讨论页名称
local talkPage = "Talk:" .. pageName
-- 使用Template parameter value模块获取WikiProject banner shell模板的参数1
local templateParamValue = require('Module:Template parameter value')
local projects = {}
-- 尝试所有可能的banner shell模板名称
for _, pattern in ipairs(banner_shell_patterns) do
local success, result = templateParamValue.getParameter(talkPage, pattern, "1", {treat_as_regex = true})
if success and result ~= "" then
-- 从结果中提取所有模板名称
for template in string.gmatch(result, "{{([^|{}]+)") do
-- 清理模板名称(去除空白)
template = mw.text.trim(template)
table.insert(projects, template)
end
break -- 找到一个匹配的banner shell模板就退出
end
end
return projects
end
-- 检查项目是否匹配指定的专题模式
local function matchesProject(projects, projectPatterns)
if not projects or #projects == 0 or not projectPatterns then
return false
end
for _, project in ipairs(projects) do
for _, pattern in pairs(projectPatterns) do
if pattern and pattern ~= "" and mw.ustring.match(project, pattern) then
return true
end
end
end
return false
end
-- 包裹文本(添加前缀和后缀)
local function wrapText(text, prefix, suffix)
prefix = prefix or ""
suffix = suffix or ""
return prefix .. text .. suffix
end
-- 包裹模板名(默认使用{{tl|模板名}}格式)
local function wrapTemplate(template, prefix, suffix)
if not prefix and not suffix then
return "{{tl|" .. template .. "}}"
else
return wrapText(template, prefix, suffix)
end
end
-- 主函数1:列出所有候选项目及其专题
function p.listAllProjects(frame)
local args = require('Module:Arguments').getArgs(frame)
-- 获取参数
local title = args.title -- 评选页面标题
local pattern = args.pattern -- 匹配候选项的模式
local blackregex = args.blackregex -- 排除项的正则表达式
local black = args.black -- 排除项列表
local linkprefix = args.linkprefix -- 链接前缀
-- 包裹参数
local itemPrefix = args.itemPrefix or "" -- 条目名前缀
local itemSuffix = args.itemSuffix or "" -- 条目名后缀
local templatePrefix = args.templatePrefix -- 模板名前缀
local templateSuffix = args.templateSuffix -- 模板名后缀
-- 获取候选项目
local candidatesText = getCandidates(title, pattern, blackregex, black, linkprefix)
if candidatesText == "暂无" then
return "暂无候选项目"
end
-- 解析候选项目文本,提取条目名
local candidates = {}
for link in mw.ustring.gmatch(candidatesText, "%[%[:[^|]+|([^%]]+)%]%]") do
table.insert(candidates, link)
end
-- 构建结果表格
local result = '{| class="wikitable sortable"\n!条目!!专题\n'
for _, candidate in ipairs(candidates) do
-- 获取该条目的专题
local projects = getProjects(candidate)
-- 添加到结果表格
result = result .. "|-\n|"
result = result .. wrapText(candidate, itemPrefix, itemSuffix)
result = result .. "||"
if #projects > 0 then
local wrappedProjects = {}
for i, project in ipairs(projects) do
wrappedProjects[i] = wrapTemplate(project, templatePrefix, templateSuffix)
end
result = result .. table.concat(wrappedProjects, "、")
else
result = result .. "无专题信息"
end
result = result .. "\n"
end
result = result .. "|}\n"
return result
end
-- 主函数2:获取属于特定专题的候选项目
function p.getProjectCandidates(frame)
local args = require('Module:Arguments').getArgs(frame)
-- 获取参数
local title = args.title -- 评选页面标题
local pattern = args.pattern -- 匹配候选项的模式
local blackregex = args.blackregex -- 排除项的正则表达式
local black = args.black -- 排除项列表
local linkprefix = args.linkprefix -- 链接前缀
-- 包裹参数
local itemPrefix = args.itemPrefix or "" -- 条目名前缀
local itemSuffix = args.itemSuffix or "" -- 条目名后缀
-- 收集所有project参数
local projectPatterns = {}
local i = 1
while true do
local projectKey = "project" .. i
local projectPattern = args[projectKey]
if not projectPattern then
break
end
projectPatterns[projectKey] = projectPattern
i = i + 1
end
-- 如果没有提供project参数,返回错误
if i == 1 then
return "<span class=\"error\">错误:未提供project参数</span>"
end
-- 获取候选项目
local candidatesText = getCandidates(title, pattern, blackregex, black, linkprefix)
if candidatesText == "暂无" then
return "暂无候选项目"
end
-- 解析候选项目文本,提取条目名
local candidates = {}
for link in mw.ustring.gmatch(candidatesText, "%[%[:[^|]+|([^%]]+)%]%]") do
table.insert(candidates, link)
end
-- 筛选符合专题条件的候选项
local matchedCandidates = {}
for _, candidate in ipairs(candidates) do
-- 获取该条目的专题
local projects = getProjects(candidate)
-- 检查是否匹配任一专题模式
if matchesProject(projects, projectPatterns) then
table.insert(matchedCandidates, candidate)
end
end
-- 如果没有匹配的候选项,返回提示
if #matchedCandidates == 0 then
return "没有找到符合条件的候选项目"
end
-- 构建结果列表
local result = "<ol>\n"
for _, candidate in ipairs(matchedCandidates) do
result = result .. "<li>" .. wrapText(candidate, itemPrefix, itemSuffix) .. "</li>\n"
end
result = result .. "</ol>"
return result
end
return p