模組:沙盒/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 options = {treat_as_regex = true, only_subtemplates = true}
local success, result = templateParamValue.getParameter(talkPage, pattern, "1", options)
if success and result ~= "" then
-- 直接从结果中提取所有模板名称
for template in mw.text.gsplit(result, "\n") do
-- 提取模板名称
local templateName = mw.ustring.match(template, "^{{([^|{}]+)")
if templateName then
-- 清理模板名称(去除空白)
templateName = mw.text.trim(templateName)
if templateName ~= "" then
table.insert(projects, templateName)
end
end
end
-- 如果找到了专题,就不再继续查找
if #projects > 0 then
break
end
end
end
-- 如果上面的方法没有找到专题,尝试直接解析讨论页内容
if #projects == 0 then
local title = mw.title.new(talkPage)
if title and title.exists then
local content = title:getContent() or ""
-- 查找所有WikiProject banner shell模板
for _, pattern in ipairs(banner_shell_patterns) do
local bannerPattern = "{{%s*" .. pattern .. "[^}]*}}(.-)}}"
local bannerContent = mw.ustring.match(content, bannerPattern)
if bannerContent then
-- 从banner内容中提取所有模板
for templateName in mw.ustring.gmatch(bannerContent, "{{%s*([^|{}]+)") do
templateName = mw.text.trim(templateName)
if templateName ~= "" and not mw.ustring.match(templateName:lower(), table.concat(banner_shell_patterns, "|")) then
table.insert(projects, templateName)
end
end
-- 如果找到了专题,就不再继续查找
if #projects > 0 then
break
end
end
end
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
-- 从维基链接中提取条目名
local function extractTitleFromLink(link)
-- 尝试匹配带有管道符的链接 [[:prefix:title|display]]
local display = mw.ustring.match(link, "%[%[:.-|(.-)%]%]")
if display then
return display
end
-- 尝试匹配不带管道符的链接 [[:title]]
local title = mw.ustring.match(link, "%[%[:(.-)%]%]")
if title then
return title
end
-- 如果都不匹配,返回原始链接
return link
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
local title = extractTitleFromLink(link)
table.insert(candidates, title)
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
local title = extractTitleFromLink(link)
table.insert(candidates, title)
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