模組: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: 调用轮展模块
效果:
在要显示轮展内容的位置,按下述方式调用模块:
{{#invoke:Carousel|main|candidateList=User:PhiLiP/carousel-example.json}}
可选参数
效果:
(当前时间戳为20250526041628,显示第1条)
除了candidateList
外,有两个可选参数timeStart
和timeInterval
。timeStart
定义轮展的起始时间(以MediaWiki时间戳规定的UTC时间,默认值19700101000000,即UTC时间1970年1月1日0时0分0秒);timeInterval
定义每轮展示的秒数(默认值86400秒,即1天)。下为示例:
(从UTC时间2025年5月26日0时0分0秒起,每小时更换一次。)
{{#invoke:Carousel|main|candidateList=User:PhiLiP/carousel-example.json|timeStart=20250526000000|timeInterval=3600}}
local p = {}
local lang = mw.language.new('zh')
function tostringOrNil(value)
if value ~= nil then
value = tostring(value)
end
end
function getCandidateList(args)
local page = mw.title.new(args.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])
)
)
end
end
return candidates
end
function pickCandidate(candidateList, currentTime, timeStart, timeInterval)
local processedTime = timeStart
local minRangeStart = 0xffffffffffffffff
local currentDisplayStart =
math.floor(currentTime / timeInterval) * timeInterval
local currentDisplayEnd = currentDisplayStart + timeInterval
while true do
for _, item in pairs(candidateList) do
for _, range in pairs(item.displayTimeRanges) do
local rangeStart = range[1]
local rangeEnd =
math.ceil(range[2] / timeInterval) * timeInterval
minRangeStart = math.min(minRangeStart, rangeStart)
if processedTime < range[1] or
processedTime > rangeEnd then
-- continue
else
processedTime = processedTime + timeInterval
break
end
end
if processedTime >= currentDisplayStart and
processedTime < currentDisplayEnd then
return item.title
end
end
processedTime = math.max(processedTime, minRangeStart)
end
end
function p.main(frame)
local candidateList = getCandidateList{pageName = frame.args.candidateList}
local currentTime = tonumber(
lang:formatDate('U', tostringOrNil(frame.args.currentTime))
)
local timeStart = tonumber(
lang:formatDate(
"U", tostringOrNil(frame.args.timeStart) or '19700101000000'
)
)
local timeInterval = tonumber(frame.args.timeInterval) or 86400
mw.log(pickCandidate(candidateList, currentTime, timeStart, timeInterval))
--local index0 = math.floor((os.time() - timeStart) / 86400) % #candidateList
--local currentPage = candidateList[index0 + 1]
--return mw.getCurrentFrame():expandTemplate { title = currentPage }
end
return p