模組:Selected recent additions
外观
![]() | 此模块已评为alpha版,可接受第三方输入,并可用于少量页面以检查是否存在问题,但需要受到检查。欢迎提供新功能或修改其输入输出机制的建议。 |
![]() | 此模块使用Lua语言: |
用法
{{#invoke:Selected recent additions|main}}
参数将从父模板{{Transclude selected recent additions}}处传递;请见该模板的文档。
参见
local randomModule = require('Module:Random')
function cleanupArgs(argsTable)
local cleanArgs = {}
for key, val in pairs(argsTable) do
if type(val) == 'string' then
val = val:match('^%s*(.-)%s*$')
if val ~= '' then
cleanArgs[key] = val
end
else
cleanArgs[key] = val
end
end
return cleanArgs
end
function isAffirmed(val)
if not(val) then return false end
local affirmedWords = ' add added affirm affirmed include included on true yes y '
return string.find(affirmedWords, ' '..string.lower(val)..' ', 1, true ) and true or false
end
function makeOutput(allItems, maxItems, more, notRandom)
local output
if notRandom then
output = ''
local itemIndex = 1
local maxCount = math.min(#allItems, maxItems)
while itemIndex <= maxCount do
output = output .. allItems[itemIndex] .. '\n'
itemIndex = itemIndex + 1
end
else
local randomiseArgs = {
['t'] = allItems,
['limit'] = maxItems
}
local randomisedItems = randomModule.main('array', randomiseArgs )
output = table.concat(randomisedItems, '\n')
end
if more then
output = output .. more
end
return mw.text.trim(output)
end
-- 清理维基文本以便进行模式匹配
function cleanForPatternMatching(wikitext)
-- 首先处理字词转换:根据当前页面语言环境转换文本
-- 例如,-{zh-cn:索尔; zh-tw:薩爾;}- 在简体环境下会变成 "索尔"
local cleaned = mw.language.convert(wikitext)
-- 移除维基链接的方括号,保留链接文本
cleaned = mw.ustring.gsub(cleaned, "%[%[(.-)%]%]","%1")
-- 移除管道符(通常在管道链接中使用),替换为空格
cleaned = mw.ustring.gsub(cleaned, "%|"," ")
-- 移除外部链接
cleaned = mw.ustring.gsub(cleaned, "%[.-%]"," ")
return cleaned
end
function makeCollapsed(outerText, innerText)
return "{{Hidden begin | titlestyle = font-weight:normal | title = " .. outerText .. "}}" .. innerText .. "{{Hidden end}}"
end
-- 获取指定日期子页面的新条目推荐项目。返回一个列表项的表。
function getRecentAdditions(subpage, keepPatterns, skipPatterns, showWikitext)
-- 中文维基百科的新条目推荐存档路径
local title = mw.title.new('Wikipedia:新条目推荐/存档' .. subpage)
if not title or not title.exists then -- 检查页面是否存在
return {}
end
local raw = title:getContent()
if not raw then return {} end -- 如果页面内容为空则返回
local items = {}
-- 中文维基百科的新条目推荐通常是每行一个,以 '*' 开头
-- 因此我们按行分割处理
local lines = mw.text.split(raw, '\n')
for _, line in ipairs(lines) do
-- 检查是否为列表项 (以 '*' 开头,后面可以有空格)
if mw.ustring.match(line, '^%*%s*') then
local item = line -- 原始的列表项文本,例如 "* [[示例条目]] ...?"
local keep = false
local skip = false
-- 使用 cleanForPatternMatching 清理后的文本进行匹配
local textToMatch = cleanForPatternMatching(item)
for _, keepPatt in pairs(keepPatterns) do
if not keep and mw.ustring.find(textToMatch, keepPatt, 1, false) then -- 使用 false 进行普通查找
keep = true
end
end
if #skipPatterns > 0 then
for _, skipPatt in pairs(skipPatterns) do
if not skip and mw.ustring.find(textToMatch, skipPatt, 1, false) then -- 使用 false 进行普通查找
skip = true
end
end
end
if keep and not skip then
local cleanItem = item -- 默认使用原始item
-- 中文维基的新条目推荐一般不含 (pictured) 或 (illustrated)
-- 但为防万一,保留注释掉的清理逻辑,若有需要可恢复
-- cleanItem = mw.ustring.gsub(item, "%s*''%(.-pictured.-%)''", "")
-- cleanItem = mw.ustring.gsub(cleanItem, "%s*''%(.-illustrated.-%)''", "")
if showWikitext then
-- 移除html注释
local tempCleanItem = mw.ustring.gsub(cleanItem, "%<%!%-%-(.-)%-%-%>", "")
local itemWikitext = "<pre>" .. mw.text.nowiki( tempCleanItem ) .. "</pre>"
cleanItem = makeCollapsed(tempCleanItem, itemWikitext)
end
table.insert(items, cleanItem)
end
end
end
return items
end
-- 获取过去数个月的新条目推荐项目
function getItems(maxMonths, patterns, skipPatterns, showWikitext)
local allItems = {}
local lang = mw.getContentLanguage()
local currentYear = tonumber(lang:formatDate('Y', 'now'))
local currentMonth = tonumber(lang:formatDate('n', 'now'))
local monthsAgo = 0
while monthsAgo < maxMonths do
local yearToFetch = currentYear
local monthToFetch = currentMonth - monthsAgo
-- 处理月份跨年的情况
while monthToFetch <= 0 do
monthToFetch = monthToFetch + 12
yearToFetch = yearToFetch - 1
end
-- 构建中文维基百科存档子页面名称,例如 /2023年5月
local subpage = '/' .. yearToFetch .. '年' .. monthToFetch .. '月'
local monthlyItems = getRecentAdditions(subpage, patterns, skipPatterns, showWikitext)
for i, item in ipairs(monthlyItems) do
table.insert(allItems, item)
end
monthsAgo = monthsAgo + 1
end
return allItems
end
function getPatterns(args, prefix)
local patterns = {}
local ii = 1
while args[prefix and prefix..ii or ii] do
patterns[ii] = args[prefix and prefix..ii or ii]
ii = ii + 1
end
return patterns
end
local p = {}
p.main = function(frame)
local parent = frame:getParent()
local parentArgs = parent.args
local args = cleanupArgs(parentArgs)
if args['not'] and not args['not1'] then
args['not1'] = args['not']
end
local patterns = getPatterns(args)
if #patterns < 1 then
-- 错误信息中文化
return error("未设定搜索模式(缺少编号参数如 `|1=模式`)")
end
local skipPatterns = getPatterns(args, 'not')
local months = tonumber(args.months) or 30
local showWikitext = isAffirmed(args.wikitext)
local allItems = getItems(months, patterns, skipPatterns, showWikitext)
if #allItems < 1 then
-- 默认提示信息中文化
return args.header and '' or args.none or '未找到符合条件的新条目推荐'
end
local maxItems = tonumber(args.max) or 6
local more = args.more
if isAffirmed(args.more) then
-- "更多"链接中文化,指向新条目推荐存档主页
more = "\n'''[[Wikipedia:新条目推荐/存档|更多以往推荐...]]'''"
end
local nonRandom = isAffirmed(args.latest)
local output = makeOutput(allItems, maxItems, more, nonRandom)
if args.header then
output = args.header .. '\n' .. output .. '\n' .. (args.footer or '{{Box-footer}}')
end
local needsExpansion = mw.ustring.find(output, '{{', 1, true)
if needsExpansion then
return frame:preprocess(output)
else
return output
end
end
return p