跳转到内容

模組:Selected recent additions

维基百科,自由的百科全书

这是本页的一个历史版本,由PexEric留言 | 贡献2025年5月31日 (六) 08:16编辑。这可能和当前版本存在着巨大的差异。

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)
	-- 移除维基链接括号,保留链接文本
	local cleaned = mw.ustring.gsub(wikitext, "%[%[(.-)%]%]","%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(subpagePath, keepPatterns, skipPatterns, showWikitext)
	-- MODIFIED: 修改基础页面路径为中文维基百科的路径
	local title = mw.title.new('Wikipedia:新条目推荐/存档' .. subpagePath)
	if not title or not title.exists then
		return {} -- 如果页面不存在,返回空表
	end
	local raw = title:getContent()
	if not raw then
		return {} -- 如果无法获取内容,返回空表
	end

	-- MODIFIED: 修改项目匹配模式以适应中文维基的格式
	-- 匹配以 '*' 开头,后面跟一个空格,然后是任意字符直到行尾的行
	local itemPattern = '%* [^\n]+'
	local items = {}

	for itemLine in mw.ustring.gmatch(raw, itemPattern) do
		local keep = false
		local skip = false
		-- 确保我们处理的是列表项(虽然itemPattern已经保证了这点)
		local isListItem = ( string.sub(itemLine, 1, 1) == '*' )

		if isListItem then
			local textToSearch = cleanForPatternMatching(itemLine) -- 在清理后的文本中搜索
			if #keepPatterns == 0 then -- 如果没有指定keepPatterns,则默认保留所有条目
				keep = true
			else
				for _, keepPatt in pairs(keepPatterns) do
					if not keep and mw.ustring.find(textToSearch, keepPatt, 1, false) then -- plain search
						keep = true
					end
				end
			end

			if #skipPatterns > 0 then
				for _, skipPatt in pairs(skipPatterns) do
					if not skip and mw.ustring.find(textToSearch, skipPatt, 1, false) then -- plain search
						skip = true			
					end
				end
			end
		end

		if keep and not skip then
			local cleanItem = itemLine -- 默认使用原始行
			-- REMOVED: 移除了 (pictured) 和 (illustrated) 的清理,中文维基DYK似乎没有这些
			-- cleanItem = mw.ustring.gsub(item, "%s*''%(.-pictured.-%)''", "")
			-- cleanItem = mw.ustring.gsub(cleanItem, "%s*''%(.-illustrated.-%)''", "")

			if showWikitext then
				-- 移除HTML注释
				local itemWikitextStripped = mw.ustring.gsub(cleanItem, "%<%!%-%-(.-)%-%-%>", "")
				local itemWikitext = "<pre>" .. mw.text.nowiki(itemWikitextStripped) .. "</pre>"
				-- 对于中文,可能原始行就足够作为标题,避免太长
				local displayTitle = mw.text.truncate(mw.ustring.gsub(itemWikitextStripped, "^%*%s*", ""), 100, "...")
				cleanItem = makeCollapsed(displayTitle, itemWikitext)
			end
			table.insert(items, cleanItem)
		end
	end
	return items
end

-- 获取过去数月的新条目推荐项目
function getItems(maxMonths, patterns, skipPatterns, showWikitext)
	local allItems = {}
	-- MODIFIED: 使用 mw.language.getContentLanguage() 获取当前维基的内容语言对象
	local lang = mw.language.getContentLanguage() -- CORRECTED LINE
	
	local currentYear = tonumber(lang:formatDate('Y', 'now'))
	local currentMonth = tonumber(lang:formatDate('n', 'now'))
	
	local monthsSearched = 0 
	local monthsToTry = 0 
	local maxArchiveAge = math.max(maxMonths, 60)

	while monthsSearched < maxMonths and monthsToTry < maxArchiveAge do
		local yearToFetch = currentYear
		local monthToFetch = currentMonth - monthsToTry

		while monthToFetch <= 0 do
			monthToFetch = monthToFetch + 12
			yearToFetch = yearToFetch - 1
		end
		
		local subpagePath = '/' .. yearToFetch .. '年' .. monthToFetch .. '月'
		
		local monthlyItems = getRecentAdditions(subpagePath, patterns, skipPatterns, showWikitext)
		if #monthlyItems > 0 then
			for _, item in ipairs(monthlyItems) do
				table.insert(allItems, item)
			end
			monthsSearched = monthsSearched + 1
		end
		monthsToTry = monthsToTry + 1
	end
	return allItems
end

-- 从参数中获取模式列表
function getPatterns(args, prefix)
	local patterns = {}
	local i = 1
	local key
	while true do
		key = prefix and (prefix .. i) or tostring(i)
		if args[key] then
			-- 直接使用原始字符串作为模式,不进行 mw.text.trim,因为用户可能需要匹配带空格的模式
			patterns[i] = args[key]
		else
			-- 如果 prefix..i 不存在,对于主pattern (prefix=nil), 尝试不带数字的 prefix (即参数本身)
			if not prefix and args[prefix] then 
				patterns[i] = args[prefix]
				i = i + 1 -- 确保下一个迭代的key不同
			end
			break -- 退出循环
		end
		i = i + 1
	end
	return patterns
end

local p = {}

p.main = function(frame)
	local parent = frame:getParent()
	local parentArgs = parent.args
	local args = cleanupArgs(parentArgs) -- 清理父框架的参数

	-- 向后兼容:如果设置了 args['not'] 但没有 args['not1'],则将 args['not'] 赋给 args['not1']
	if args['not'] and not args['not1'] then
		args['not1'] = args['not']
		args['not'] = nil -- 清除旧的 'not',避免混淆 getPatterns
	end
	
	local patterns = getPatterns(args) -- 获取主要的搜索模式
	-- MODIFIED: 允许没有搜索模式,此时会列出所有条目
	-- if #patterns < 1 then
	--	 return error("错误:未设置搜索模式 (参数 1, 2, ...)。")
	-- end

	local skipPatterns = getPatterns(args, 'not') -- 获取要排除的模式

	-- MODIFIED: 默认搜索月份从30个月改为6个月,因为DYK存档通常按月,30页可能过多
	local months = tonumber(args.months) or 6 
	
	local showWikitext = isAffirmed(args.wikitext)

	local allItems = getItems(months, patterns, skipPatterns, showWikitext)
	if #allItems < 1 then
		-- MODIFIED: 本地化提示信息
		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 参数为 'yes' 或类似肯定词
		-- MODIFIED: 本地化 "More recent additions..." 链接
		more = "\n'''[[Wikipedia:新条目推荐|更多新条目推荐……]]'''"
	elseif type(more) == 'string' and more ~= '' then
		-- 如果 more 是非空字符串,则直接使用它
		more = "\n" .. more
	else
		more = nil -- 否则不显示 "more" 链接
	end


	local notRandom = isAffirmed(args.latest) -- 'latest' 参数表示按时间顺序(非随机)

	local output = makeOutput(allItems, maxItems, more, notRandom)
	if args.header then
		output = args.header .. '\n' .. output .. '\n' .. (args.footer or '{{Box-footer}}')
	end
	
	-- 如果输出中包含需要展开的模板(例如 {{Hidden begin}}),则预处理
	if mw.ustring.find(output, '{{', 1, true) then
		return frame:preprocess(output)
	else
		return output
	end
end

return p