模組:沙盒/PexEric/1
外观
-- Module:CandidateProjectUtils
local p = {}
-- #############################################################################
-- # 依赖模块加载
-- #############################################################################
local PatternedCandidateUtils -- Lazily loaded
local TplParamValue -- Lazily loaded for matchAllTemplates
-- #############################################################################
-- # 内部辅助函数:从PatternedCandidateUtils获取候选条目
-- #############################################################################
local function get_raw_candidates_from_pcu(pcu_args_table)
if not PatternedCandidateUtils then
PatternedCandidateUtils = require('Module:PatternedCandidateUtils')
end
-- PatternedCandidateUtils.getCandidates expects a frame-like object with an 'args' field
return PatternedCandidateUtils.getCandidates({ args = pcu_args_table })
end
-- #############################################################################
-- # 内部辅助函数:提取模板名 (改编自 Module:Template parameter value)
-- #############################################################################
-- string.gmatch will check the largest block it can without re-scanning whats inside, but we need whats inside
local function matchAllTemplates(str)
if not TplParamValue then
-- We only need a small part, so copy it here to reduce full module dependency if not strictly needed elsewhere
-- or, if Module:Template parameter value is guaranteed available, use:
-- TplParamValue = require("Module:Template parameter value")
-- return TplParamValue.matchAllTemplates(str)
end
-- Copied implementation from Module:Template parameter value
local matches = {}
if type(str) ~= "string" then return matches end
for template in string.gmatch(str, "{%b{}}") do
table.insert(matches, template)
local innerContent = string.sub(template, 3, -3)
-- Recursive call for nested templates
local sub_matches = matchAllTemplates(innerContent)
for _,subtemplate in ipairs(sub_matches) do -- Use ipairs for ordered array
table.insert(matches, subtemplate)
end
end
return matches
end
-- #############################################################################
-- # 内部辅助函数:获取条目讨论页的专题列表
-- #############################################################################
-- 辅助函数借鉴自 Module:PJBSClass/main
local function pjbs_getTemplateSection(input_text)
local text = (input_text or '')..'\n'
local i,j = mw.ustring.find(text, "\n%=+[^\n]*%=+%s*\n")
if i == nil then
i,j = mw.ustring.find(text, "%{%{%s*[Dd][Yy][Kk][Ee]ntry/archive")
end
text = mw.ustring.sub(text, 1, (i or 0)-1)
return text
end
local function pjbs_processWPBSRedirect(input_text)
local text = input_text
if not text then return "" end
local norm_name = "WPBS"
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 lotext = mw.ustring.gsub(mw.ustring.lower(text), "_", " ")
local normtext = text
for i = 1,#match_list do
local j, k = mw.ustring.find(lotext, match_list[i])
while j ~= nil do
normtext = mw.ustring.sub(normtext, 1, j-1)..norm_name..mw.ustring.sub(normtext, k+1, -1)
lotext = mw.ustring.gsub(mw.ustring.lower(normtext), "_", " ")
j, k = mw.ustring.find(lotext, match_list[i])
end
end
normtext = mw.ustring.gsub(normtext, "[Ww][Pp][Bb][Ss]", "WikiProject banner shell")
text = normtext
return text
end
local function pjbs_getWPBSTemplateContent(input_text)
if not input_text then return '' end
local text = input_text
local re_WPBS_header = "%{%{[^%{%}%|]*[Ww]iki[Pp]roject[%s_]*[Bb]anner[%s_]*[Ss]hell"
local it = text
local old_it = text.."-"
while it ~= old_it do
if not mw.ustring.match(it, re_WPBS_header) then break end
old_it = it
it = mw.ustring.gsub(it, "%{[^%{%}]*%}", "")
end
local WPBS = mw.ustring.match(old_it, re_WPBS_header.."[^%{%}]*%}%}") or ''
return WPBS
end
local function get_article_wikiprojects(article_title_str)
local projects = {}
local unique_project_names = {} -- 用于确保专题名称的唯一性 (小写)
if not article_title_str or article_title_str == '' then
return {}
end
local article_title_obj = mw.title.new(article_title_str)
if not article_title_obj then
return {}
end
local talk_page_title = article_title_obj.talkPageTitle
if not talk_page_title or not talk_page_title.exists or talk_page_title.isRedirect then
return {}
end
local talk_content = talk_page_title:getContent()
if not talk_content or talk_content == '' then
return {}
end
-- 1. 获取可能包含专题横幅的区域
local banner_section_text = pjbs_getTemplateSection(talk_content)
-- 2. 处理 WikiProject banner shell (WPBS) 的重定向
local processed_banner_section_text = pjbs_processWPBSRedirect(banner_section_text)
-- 3. 尝试提取 WPBS 模板本身的内容
local wpbs_template_wikitext = pjbs_getWPBSTemplateContent(processed_banner_section_text)
local text_to_scan_for_templates
if wpbs_template_wikitext and wpbs_template_wikitext ~= "" then
-- 如果找到WPBS,则扫描其内部参数部分
text_to_scan_for_templates = mw.ustring.match(wpbs_template_wikitext, "{{[^|]+|(.*)}}") or ""
else
-- 否则,扫描整个处理过的横幅区域
text_to_scan_for_templates = processed_banner_section_text
end
-- 4. 使用 matchAllTemplates 提取所有模板调用
local all_found_templates_code = matchAllTemplates(text_to_scan_for_templates)
for _, tpl_code in ipairs(all_found_templates_code) do
-- 从 {{模板名|参数}} 或 {{模板名}} 中提取 "模板名"
local core_part = mw.ustring.sub(tpl_code, 3, -3) -- 移除首尾的 {{ 和 }}
local tpl_name = mw.text.trim( (mw.ustring.match(core_part, "^([^|]+)") or core_part) ) -- 取第一个 | 前的部分,或整个(如果没有 |)
if tpl_name and #tpl_name > 0 then
-- 规范化模板名:首字母大写,其余不变
local normalized_tpl_name = mw.ustring.upper(mw.ustring.sub(tpl_name, 1, 1)) .. mw.ustring.sub(tpl_name, 2)
local lower_tpl_name = mw.ustring.lower(normalized_tpl_name)
-- 检查是否为专题模板,并排除WPBS自身及其别名,以及沙盒和文档页
if (mw.ustring.find(lower_tpl_name, "^wikiproject") or
mw.ustring.find(lower_tpl_name, "^wp[%s_]") or -- e.g. "WP Foo" but not "WPFoo"
mw.ustring.find(tpl_name, "专题") or -- 检查原始大小写以匹配 "专题"
mw.ustring.find(tpl_name, "專題")) and -- 繁体
lower_tpl_name ~= "wikiproject banner shell" and
lower_tpl_name ~= "wpbs" and
lower_tpl_name ~= "pjbs" and
not mw.ustring.find(lower_tpl_name, "/sandbox$") and
not mw.ustring.find(lower_tpl_name, "/doc$")
then
if not unique_project_names[lower_tpl_name] then
table.insert(projects, normalized_tpl_name)
unique_project_names[lower_tpl_name] = true
end
end
end
end
if #projects > 0 then
table.sort(projects) -- 确保输出顺序一致
end
return projects
end
-- #############################################################################
-- # 主要函数 1: 列出特定内容评选中所有候选项目对应的所属专题
-- #############################################################################
function p.list_candidates_with_projects(frame)
local args = frame.args
local pcu_args_table = {
title = args.pcu_title,
pattern = args.pcu_pattern,
black = args.pcu_black,
blackregex = args.pcu_blackregex
}
local item_prefix = args.item_prefix or ''
local item_suffix = args.item_suffix or ''
local candidates = get_raw_candidates_from_pcu(pcu_args_table)
if #candidates == 0 then
return args.no_candidates_msg or "暂无候选项目。"
end
local output_rows = {}
table.insert(output_rows, '{| class="wikitable sortable"')
table.insert(output_rows, '! 条目名 !! 所属专题')
for i, article_name_raw in ipairs(candidates) do
-- PatternedCandidateUtils might return names with underscores
local article_name_display = string.gsub(article_name_raw, "_", " ")
local projects = get_article_wikiprojects(article_name_raw)
local formatted_projects_list = {}
if #projects > 0 then
for _, proj_name in ipairs(projects) do
table.insert(formatted_projects_list, "{{tl|" .. proj_name .. "}}")
end
else
-- no projects found for this article
end
local projects_str = table.concat(formatted_projects_list, "、")
if projects_str == "" then
projects_str = args.no_projects_found_msg or "(未找到专题)"
end
local formatted_article_link = item_prefix .. article_name_display .. item_suffix
table.insert(output_rows, '|-\n| ' .. formatted_article_link .. ' \n| ' .. projects_str)
end
table.insert(output_rows, '|}')
return table.concat(output_rows, '\n')
end
-- #############################################################################
-- # 主要函数 2: 获取特定内容评选栏目中指定的属于一个或多个给定专题的候选项目
-- #############################################################################
function p.filter_candidates_by_projects(frame)
local args = frame.args
local pcu_args_table = {
title = args.pcu_title,
pattern = args.pcu_pattern,
black = args.pcu_black,
blackregex = args.pcu_blackregex
}
local item_prefix = args.item_prefix or ''
local item_suffix = args.item_suffix or ''
local target_projects_str = args.target_projects or ""
if target_projects_str == "" then
return "错误:未指定目标专题 (target_projects 参数为空)。"
end
local target_projects_map = {}
for proj in mw.text.gsplit(target_projects_str, ',', true) do -- true to omit empty
local trimmed_proj = mw.text.trim(proj)
if trimmed_proj ~= "" then
target_projects_map[mw.ustring.lower(trimmed_proj)] = true
end
end
if next(target_projects_map) == nil then -- Check if map is empty after processing
return "错误:目标专题列表处理后为空 (target_projects 参数可能只包含逗号或空格)。"
end
local candidates = get_raw_candidates_from_pcu(pcu_args_table)
if #candidates == 0 then
return args.no_candidates_msg or "暂无候选项目。"
end
local output_items = {}
for i, article_name_raw in ipairs(candidates) do
local article_name_display = string.gsub(article_name_raw, "_", " ")
local article_projects = get_article_wikiprojects(article_name_raw)
local match_found = false
for _, found_proj_name in ipairs(article_projects) do
if target_projects_map[mw.ustring.lower(found_proj_name)] then
match_found = true
break
end
end
if match_found then
local formatted_article_link = item_prefix .. article_name_display .. item_suffix
table.insert(output_items, formatted_article_link)
end
end
if #output_items == 0 then
return args.no_match_msg or "暂无符合指定专题的候选项目。"
end
-- 返回 # 号列表
local result_list = {}
for i, item in ipairs(output_items) do
table.insert(result_list, "# " .. item)
end
return table.concat(result_list, "\n")
end
return p