模組:PatternedCandidateUtils/sandbox
外观
![]() | 这是Module:PatternedCandidateUtils(差异)的沙盒。 |
![]() | 此模块sandbox被引用於系統介面中。 任何對此模块sandbox的修改會立即反映於維基百科的用户介面。 為了避免造成大規模的影響,编者可以在本模块的沙盒 和测试样例 页面进行实验。2 測試後無誤的版本可以一次性地加入此模块sandbox中,但是修改前請務必於討論頁發起討論。 |
本模块用于生成页面的标题列表。
用法
[编辑]基本用法:{{#invoke:PatternedCandidateUtils|list|title=Wikipedia:特色圖片評選|pattern={{((}}Wikipedia:特色圖片評選/(.-){{))}}|black=header|blackregex=除名/|linkprefix=Wikipedia:特色圖片評選/}}
具体用法可参见Wikipedia:特色圖片評選/列表。
参见
[编辑]local z = {}
-- 将 getCandidates 定义为 z 的一个函数/方法
function z.getCandidates( frame )
-- 检查 frame 和 frame.args 是否存在
if not frame or not frame.args or not frame.args.title or not frame.args.pattern then
mw.log("PatternedCandidateUtils:getCandidates - Error: frame, frame.args, title, or pattern is nil.")
if frame and frame.args and not frame.args.title then
mw.log("PatternedCandidateUtils: Missing title argument.")
end
if frame and frame.args and not frame.args.pattern then
mw.log("PatternedCandidateUtils: Missing pattern argument.")
end
return {} -- 返回空表以防止进一步错误
end
local page_content
-- 添加对 title 对象的检查
local title_obj = mw.title.new( frame.args.title )
if not title_obj then
-- 可以选择记录错误或返回空,这里返回空以避免下游错误
mw.log("PatternedCandidateUtils: Invalid title provided: " .. tostring(frame.args.title))
return {}
end
page_content = title_obj:getContent()
if not page_content then
-- 标题有效但页面可能不存在或无内容
-- 某些情况下,如新条目推荐列表页本身,这可能是正常的(例如,如果列表页为空)
-- 但如果期望有内容而没有,这也是一个问题。
-- mw.log("PatternedCandidateUtils: No content for title: " .. tostring(frame.args.title))
return {} -- 如果页面无内容,则没有候选者
end
local matches = {}
local black = {}
if frame.args.black then
for b in mw.text.gsplit( frame.args.black, '|', true ) do
if type(b) == "string" and b ~= "" then -- 确保 b 是非空字符串
black[b] = true
end
end
end
-- 确保 pattern 是字符串
if type(frame.args.pattern) ~= "string" then
mw.log("PatternedCandidateUtils: Pattern is not a string: " .. tostring(frame.args.pattern))
return {}
end
for m in mw.ustring.gmatch( page_content, frame.args.pattern ) do
if not black[m] and not ( frame.args.blackregex and type(frame.args.blackregex) == "string" and mw.ustring.match( m, frame.args.blackregex ) ) then
table.insert( matches, m )
end
end
return matches
end
function z.count( frame )
-- 现在调用 z.getCandidates
return #z.getCandidates( frame )
end
function z.list( frame )
-- 现在调用 z.getCandidates
local list = z.getCandidates( frame )
local linkprefix = frame.args.linkprefix
for i = 1, #list do
local display_text = string.gsub(list[i], "_", " ")
if linkprefix then
list[i] = '[[:' .. linkprefix .. list[i] .. '|' .. display_text .. ']]'
else
list[i] = '[[:' .. display_text .. ']]' -- 如果没有 linkprefix,条目名自身作为链接文本
end
end
if #list > 0 then
return table.concat( list, '-' )
else
return '暂无'
end
end
return z