Jump to content

Module:Arbcom election banner

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 13:37, 2 November 2013 (pluralise the time left properly). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local messageBox = require('Module:Message box')
local lang = mw.language.getContentLanguage()

local p = {}

local function makeOmbox(omboxArgs)
	return messageBox.main('ombox', omboxArgs)
end

local function randomizeArray(t)
	-- Iterate through the array backwards, each time swapping the entry "i" with a random entry.
	-- Courtesy of Xinhuan at http://forums.wowace.com/showthread.php?p=279756
	for i = #t, 2, -1 do
		local r = math.random(i)
		t[i], t[r] = t[r], t[i]
	end
	return t
end

local function getArgNums(args, prefix)
	-- Returns a table containing the numbers of the arguments that exist for the specified prefix. For example, if the prefix
	-- was 'data', and 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
	local nums = {}
	for k, v in pairs(args) do
		k = tostring(k)
		local num = mw.ustring.match(k, '^' .. prefix .. '([1-9]%d*)$')
		if num then
			table.insert(nums, tonumber(num))
		end
	end
	table.sort(nums)
	return nums
end

local function countdown(date, event)
	if type(event) ~= 'string' then return end
	-- Get the current Unix timestamp and the date Unix timestamp.
	local success, dateUnix = pcall(lang.formatDate, lang, 'U', date)
	if not success then return end
	dateUnix = tonumber(dateUnix)
	local currentDateUnix = lang:formatDate('U')
	currentDateUnix = tonumber(currentDateUnix)
	-- Subtract the timestamps to find the time left, and output that in a readable way.
	local secondsLeft = dateUnix - currentDateUnix
	if secondsLeft <= 0 then return end
	local timeLeft = lang:formatDuration(secondsLeft, {'weeks', 'days', 'hours'})
	-- Find whether we are plural or not.
	local isOrAre
	if mw.ustring.match(timeLeft, '^%d+') == '1' then
		isOrAre = 'is'
	else
		isOrAre = 'are'
	end
	-- Make the numbers red and bold, because that's what {{countdown}} does and it makes them look important.
	local timeLeft = mw.ustring.gsub(timeLeft, '(%d+)', '<span style="color: #F00; font-weight: bold;">%1</span>')
	-- Make the refresh link, and join it all together.
	local refreshLink = mw.title.getCurrentTitle():fullUrl{action = 'purge'}
	refreshLink = mw.ustring.format('<small><span class="plainlinks">([%s refresh])</span></small>', refreshLink)
	return mw.ustring.format('There %s %s until %s. %s', isOrAre, timeLeft, event, refreshLink)
end

function p._main(args)
	return countdown(args[1], 'foo')
end
 
function p.main(frame)
        -- If called via #invoke, use the args passed into the invoking template, or the args passed to #invoke if any exist.
        -- Otherwise assume args are being passed directly in from the debug console or from another Lua module.
        local origArgs
        if frame == mw.getCurrentFrame() then
                origArgs = frame:getParent().args
                for k, v in pairs(frame.args) do
                        origArgs = frame.args
                        break
                end
        else
                origArgs = frame
        end
        -- Trim whitespace and remove blank arguments.
        local args = {}
        for k, v in pairs(origArgs) do
                if type(v) == 'string' then
                        v = mw.text.trim(v)
                end
                if v ~= '' then
                        args[k] = v
                end
        end
        return p._main(args)
end
 
return p