Module:AnotherTool

来自Arcaea中文维基

可在Module:AnotherTool/doc创建此模块的帮助文档

-- 合并了若干个基本没有环境变量的en wikipedia模块
local lang = mw.language.getContentLanguage()
local getArgs = require 'Module:Arguments'.getArgs
local p = {}

-- [*[https://en.wikipedia.org/wiki/Module:Purge, 供养{{Purge}}]*]
local function purge(args)
	-- Make the URL
	local url
	do
		local title
		if args.page then
			title = mw.title.new(args.page)
			if not title then
				error(string.format(
					"'%s'不是有效的页面名称",
					args.page
				), 2)
			end
		else
			title = mw.title.getCurrentTitle()
		end
		if args.jumpto then
			title.fragment = args.jumpto
		end
		url = title:fullUrl {action = 'purge'}
		-- &_={{CURRENTTIMESTAMP}}
	end

	-- Make the display
	local purgeText, purgeTitle = args[1] or '清除缓存', args[2] or '更新服务器所缓存的页面数据'
	local display
	if args.page then
		display = purgeText
	else
		display = tostring(mw.html.create 'span'
			:attr('title', purgeTitle)
			:wikitext(purgeText)
		)
	end

	-- Output the HTML
	local root = mw.html.create 'span'
	root
		:addClass 'noprint'
		-- :addClass 'plainlinks'
		-- :addClass 'purgelink'
		:wikitext(('[%s %s]'):format(url, display))

	return tostring(root)
end

-- [*[https://en.wikipedia.org/wiki/Module:Countdown, 供养{{倒计时}}]*]
local function formatMessage(secondsLeft, event, color)
	local chosenIntervals
	if secondsLeft >= 86400 then
		chosenIntervals = {'days', 'hours'}
	elseif secondsLeft >= 3600 then
		chosenIntervals = {'hours'}
	else
		chosenIntervals = {'minutes', 'seconds'}
	end
	local timeLeft = lang:formatDuration(secondsLeft, chosenIntervals)
	-- Color and bold the numbers, because it makes them look important.
	timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#66F') .. '; font-weight: bold;">%1</span>')
	return string.format('距离%s尚余%s', event, timeLeft)
end

local function countdown(args)
	if not (args.month and args.day) then
		return '<strong class="error">错误:必须指定 month 和 day</strong>'
	end

	local timeArgs = {year = args.year or os.date '*t'.year, month = args.month, day = args.day, hour = args.hour, min = args.minute, sec = args.second}
	for k, v in pairs(timeArgs) do
		if not tonumber(v) then
			error('参数' .. k .. '无法解析为数字:' .. v)
		end
	end
	local eventTime = os.time(timeArgs)
	local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time)

	local text
	if timeToStart > 0 then
		-- Event has not begun yet
		text = formatMessage(timeToStart, args.event or '这次事件', args.color)
	elseif args.duration then
		local timeToEnd
		if args['duration unit'] then
			-- Duration is in unit other than seconds, use formatDate to add
			timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit']))
		else
			timeToEnd = timeToStart + (tonumber(args.duration) or error('args.duration应该是秒数', 0))
		end
		if timeToEnd > 0 then
			-- Event is in progress
			text = args.eventstart or formatMessage(timeToEnd, (args.event or '这次事件') .. '完结', args.color)
		else
			-- Event had a duration and has now ended
			text = args.eventend or ((args.event or '这次事件') .. '已完结')
		end
	else
		-- Event had no duration and has begun
		-- 已经开始?
		text = args.eventend or ((args.event or '这次事件') .. '已完结')
	end

	local refreshLink
	if args.purge then
		refreshLink = '<small>' .. purge '更新' .. '</small>'
	else
		refreshLink = ''
	end
	return text .. refreshLink
end

-- [*[https://en.wikipedia.org/wiki/Module:Separated_entries, 供养{{多行分离}}]*]
local function separatedEntries(args)
	local compressSparseArray = require 'Module:TableTools'.compressSparseArray
	local separator = args.separator
		-- Decode (convert to Unicode) HTML escape sequences, such as "&#32;" for space.
		and mw.text.decode(args.separator) or ''
	-- Discard values before the starting parameter.
	local start = tonumber(args.start)
	if start then
		for i = 1, start - 1 do args[i] = nil end
	end
	-- Discard named parameters.
	local values = compressSparseArray(args)
	return mw.text.listToText(values, separator, separator)
end

local function makeInvokeFunc(func, opt)
	return function(frame)
		local args = getArgs(frame, opt)
		return func(args)
	end
end
p.countdown = makeInvokeFunc(countdown)
p.purge = makeInvokeFunc(purge, {parentOnly = true})
p.separatedEntries = makeInvokeFunc(separatedEntries)

return p