模組:Loop/archive
外观
local p = {}
local mYesno
local mTemplateParameters
local function yesno(value)
if not mYesno then
mYesno = require('Module:Yesno')
end
return mYesno(value)
end
local function buildLink(prefix, year, template, checkExists, checkExistsPF)
local titleText
if template then
titleText = string.format(template, year)
else
titleText = string.format('%s/%04d年', prefix, year)
end
local success, title = pcall(mw.title.new, titleText)
if not success or not title then
return ''
end
if checkExists and not title.exists then
return ''
end
if checkExistsPF then
return string.format('{{#ifexists:%s|[[%s|%s]]}}', title.prefixedText, title.prefixedText, year)
end
return string.format('[[%s|%s]]', title.prefixedText, year)
end
function p.byYear(frame)
local args = frame.args
local prefix = args.prefix -- 存檔母頁面
local template = args.template -- 存檔頁面名模板 預設是套用 prefix/%04d年
if not prefix and not template then
error('One of prefix or template must be provided.')
end
local startYear = tonumber(args.start) -- 循環開始年分
local endYear = tonumber(args['end']) or tonumber(os.date('%Y')) -- 循環終止年分
local range = tonumber(args.range) or 1 -- 循環分組
local offset = tonumber(args.offset) or 0 -- 正常會用 2001~2005 2006~2010 此方法分組 此參數指定後對範圍進行偏移偏移
local checkExists = args.checkExists and yesno(args.checkExists) or false -- 檢查存檔目標是否存在
local checkExistsPF = not checkExists and (args.checkExistsPF and yesno(args.checkExistsPF) or false) -- 檢查存檔目標是否存在(解析器函式版)
local outputTableBox = args.outputTableBox and yesno(args.outputTableBox) or false -- 是否輸出表格的框 {| |}
local tableProps = outputTableBox and (args.tableProps ~= nil and args.tableProps or 'style="width:100%;"') -- 表格屬性組
local output = {}
if outputTableBox then
local tableBox = '{|'
if tableProps then
tableBox = tableBox .. ' ' .. mw.text.trim(tableProps)
end
output[#output + 1] = tableBox
end
local index = math.floor((startYear - offset) / range)
while index * range + offset <= endYear do
local i = 1
while i <= range do
local curYear = index * range + offset + i
local link = (curYear >= startYear and curYear <= endYear)
and buildLink(prefix, curYear, template, checkExists, checkExistsPF)
or ''
if i == 1 then
output[#output + 1] = '|-'
end
output[#output + 1] = '|' .. link
if i == range then
output[#output + 1] = '|' .. '年'
end
i = i + 1
end
index = index + 1
end
if outputTableBox then
output[#output + 1] = '|}'
end
if checkExistsPF then
return mw.getCurrentFrame():preprocess(table.concat(output, '\n'))
else
return table.concat(output, '\n')
end
end
return p