模組:Carousel
頁面輪展模塊,可用於首頁特色條目、優良條目展示。
用法
[編輯]Step 1: 創建JSON頁面
[編輯]首先,需要創建一個JSON頁面,其中記錄了要輪展嵌入的頁面,以及輪展的順序。以下是一個例子(User:PhiLiP/carousel-example.json):
[
{
"title": "1689年波士顿起义",
"displayTimeRanges": [
[
20150304123013,
null
]
]
},
{
"title": "孔子鸟属",
"displayTimeRanges": [
[20060313005324, 20090614110222]
]
},
{
"title": "1850年大西洋飓风季",
"displayTimeRanges": [
[
20141030151730,
null
]
]
},
{
"title": "1873年铸币法案",
"displayTimeRanges": [
[
20160805143015,
null
]
]
},
{
"title": "1880年民主党全国大会",
"displayTimeRanges": [
[
20141213150114,
null
]
]
}
]
Step 2: 調用輪展模塊
[編輯]效果:
Lua錯誤:expandTemplate: template "Wikipedia:特色條目/1873年鑄幣法案" does not exist。
在要顯示輪展內容的位置,按下述方式調用模塊:
{{#invoke:Carousel|main|candidateList=User:PhiLiP/carousel-example.json}}
可選參數
[編輯]效果:
(當前時間戳為20250529014146,顯示第2條)

1850年大西洋颶風季是美國國家颶風中心建立的大西洋颶風資料庫中未予收錄的最近一個大西洋颶風季。雖然這年的氣象有欠完整,但仍然有三場熱帶氣旋對陸地構成顯著影響,每場都造成一定程度的破壞。其中第一個系統於7月18日吹襲北卡羅萊納州,造成嚴重破壞後又給大西洋地區帶去高漲的海潮和狂風暴雨。8月22日,古巴哈瓦那受到一場強烈颶風的衝擊,摧毀果林的同時還擾亂了航運,接下來又在佛羅里達州西北狹長地帶登陸並帶去了高漲的風暴潮。海上有一艘引水船因海況惡劣導致與大船相撞並沉沒。9月7到8日,紐約州到鱈魚角受到一場颶風的衝擊,產生陣風和可觀降水,造成許多船隻遇險。
除了candidateList
外,有兩個可選參數timeStart
和timeInterval
。timeStart
定義輪展的起始時間(以MediaWiki時間戳規定的UTC時間,默認值19700101000000,即UTC時間1970年1月1日0時0分0秒);timeInterval
定義每輪展示的秒數(默認值86400秒,即1天)。下為示例:
(從UTC時間2025年5月29日0時0分0秒起,每小時更換一次。)
{{#invoke:Carousel|main|candidateList=User:PhiLiP/carousel-example.json|timeStart=20250529000000|timeInterval=3600}}
local p = {}
local lang = mw.language.new('zh')
function tostringOrNil(value)
if value ~= nil then
value = tostring(value)
end
return value
end
function getCandidateList(pageName, currentTime)
local page = mw.title.new(pageName)
local candidates =
mw.text.jsonDecode(page:getContent(), mw.text.JSON_TRY_FIXING)
-- change mw timestamp to unix timestamp
for _, item in pairs(candidates) do
for i in pairs(item.displayTimeRanges) do
item.displayTimeRanges[i][1] = tonumber(
lang:formatDate('U', tostring(item.displayTimeRanges[i][1]))
)
-- use current time when the "end time" is null
item.displayTimeRanges[i][2] = tonumber(
lang:formatDate(
'U', tostringOrNil(
item.displayTimeRanges[i][2] or currentTime
)
)
)
end
end
return candidates
end
function pickCandidate(candidateList, currentTime, timeStart, timeInterval)
local processedTime = timeStart
local currentDisplayStart =
math.floor(currentTime / timeInterval) * timeInterval
local currentDisplayEnd = currentDisplayStart + timeInterval
local maxCycles = 100
mw.log("currentDisplayStart: ", currentDisplayStart)
while maxCycles > 0 do
local cycleItems = 0
local nextStatusChanged = 0xffffffffffffffff
for _, item in pairs(candidateList) do
for _, range in pairs(item.displayTimeRanges) do
local rangeStart =
math.ceil(range[1] / timeInterval) * timeInterval
local rangeEnd =
math.ceil(range[2] / timeInterval) * timeInterval
if rangeStart > processedTime then
nextStatusChanged = math.min(nextStatusChanged, rangeStart)
end
if rangeEnd > processedTime then
nextStatusChanged = math.min(nextStatusChanged, rangeEnd)
end
if processedTime < rangeStart or
processedTime >= rangeEnd then
-- continue
-- mw.log(rangeStart)
-- mw.log(rangeEnd)
-- mw.log('---------')
elseif processedTime >= currentDisplayStart and
processedTime < currentDisplayEnd then
return item.title
else
-- processedTime = processedTime + timeInterval
cycleItems = cycleItems + 1
break
end
end
-- mw.log(processedTime)
end
mw.log("nextStatusChanged: ", nextStatusChanged)
mw.log("CycleItems: ", cycleItems)
mw.log("ProcessedTime (before): ", processedTime)
if cycleItems > 0 then
local times =
math.ceil((nextStatusChanged - processedTime) / timeInterval)
processedTime =
processedTime + math.floor(times / cycleItems) *
cycleItems * timeInterval;
local remaining = times % cycleItems
mw.log("remaining: ", remaining)
-- process remaining items in the "partial" cycle
if remaining > 0 then
for _, item in pairs(candidateList) do
-- mw.log(item.title)
for _, range in pairs(item.displayTimeRanges) do
-- mw.log(remaining, range[1], range[2])
local rangeStart =
math.ceil(range[1] / timeInterval) * timeInterval
local rangeEnd =
math.ceil(range[2] / timeInterval) * timeInterval
if processedTime < rangeStart or
processedTime > rangeEnd then
-- continue
--elseif remaining > 0 then
-- remaining = remaining - 1
-- break
elseif processedTime >= currentDisplayStart and
processedTime < currentDisplayEnd then
return item.title
else
processedTime = processedTime + timeInterval
break
end
end
end
end
else
processedTime = math.max(processedTime, nextStatusChanged)
end
mw.log("ProcessedTime: ", processedTime)
maxCycles = maxCycles - 1
end
-- TODO: raise an error
return 'EXCEED LIMITATION'
end
function p.getCandidate(frame)
local args = frame.args
local currentTime = tonumber(
lang:formatDate('U', tostringOrNil(args.currentTime))
)
local candidateList = getCandidateList(
args.candidateList,
args.currentTime
)
local timeStart = tonumber(
lang:formatDate(
"U", tostringOrNil(args.timeStart) or '19700101000000'
)
)
local timeInterval = tonumber(args.timeInterval) or 86400
local title = args.titlePrefix or 'Wikipedia:特色条目/'
title = title .. pickCandidate(
candidateList, currentTime, timeStart, timeInterval)
return title
end
function p.main(frame)
return mw.getCurrentFrame():expandTemplate({
title = p.getCandidate(frame)
})
end
return p