跳转到内容

模組:Carousel

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

这是本页的一个历史版本,由PhiLiP留言 | 贡献2017年3月5日 (日) 01:20编辑。这可能和当前版本存在着巨大的差异。

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 iii = 100
	while iii > 0 do
		local cycleItems = 0
		local nextStatusChanged = 0xffffffffffffffff
		for _, item in pairs(candidateList) do
			if processedTime >= currentDisplayStart and
				processedTime < currentDisplayEnd then
				return item.title
			end
			for _, range in pairs(item.displayTimeRanges) do
				local rangeStart = range[1]
				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('---------')
				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
			-- TODO: nextStatusChanged - processedTime < timeInterval?
			processedTime = processedTime + math.floor(
				math.ceil((nextStatusChanged - processedTime) / timeInterval)
				/ cycleItems
			)
			* cycleItems * timeInterval;
		else
			processedTime = math.max(processedTime, nextStatusChanged)
		end
		mw.log("ProcessedTime: ", processedTime)
		iii = iii - 1
	end
	return 'NO'
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