Module:User:Erutuon/Wonderfool

This is a private module sandbox of Erutuon, for his own experimentation. Items in this module may be added and removed at Erutuon's discretion; do not rely on this module's stability.


local export = {}

local function is_empty(str)
	return str == nil or str:find "^%s*$" ~= nil
end

-- From [[Module:number list]].
local function add_separator(number, separator, group, start)
	number = tostring(number)
	start = start or group
	if start >= #number then
		return number
	end
	
	local parts = { number:sub(-start) }
	for i = start + 1, #number, group do
		table.insert(parts, 1, number:sub(-(i + group - 1), -i))
	end
	
	return table.concat(parts, separator)
end

local function add_thousands_separator(number)
	return add_separator(tostring(number), ",", 3)
end

function export.show(frame)
	local output = require "Module:array"()
	
	output:insert [[
{| class="wikitable sortable"
! Name
! First edit
! Blocked
! style="text-align:right" | # of edits
]]

	local args = require "Module:table".shallowcopy(frame.args)
	
	if not require "Module:table".isArray(args) then
		error("Sequential numbered parameters required.")
	elseif #args % 6 ~= 0 then
		error("Total number of arguments must be a multiple of 6.")
	end
	
	local total_edits = 0
	for i = 1, #args, 6 do
		local start_date_arg, block_date_arg, edits_arg = i + 2, i + 3, i + 5
		local username, username_comment, start_date, block_date, date_comment, edits = unpack(args, i)
		
		local user_links = require "Module:linkbar".make_user_linkbar({ args = {
			user = username,
			"talk", "contribs"
		}})
	
		local start_date_sortkey = ""
		local start_date_spelled_out = "—"
		local block_date_sortkey = "9999-12-31"
		local block_date_spelled_out = "no"

		if not is_empty(start_date) then
			local year, month, day = start_date:match("(%d%d%d%d)[/-](%d%d)[/-](%d%d)")
			local time = os.time {year = year, month = month, day = day}
			
			start_date_sortkey = os.date("%F", time)
			start_date_spelled_out = os.date("%B %e, %Y", time)
		end

		if not is_empty(block_date) then
			local year, month, day = block_date:match("(%d%d%d%d)[/-](%d%d)[/-](%d%d)")
			local time = os.time {year = year, month = month, day = day}
			
			block_date_sortkey = os.date("%F", time)
			block_date_spelled_out = os.date("%B %e, %Y", time)
		end
		
		if not is_empty(edits) then
			edits = tonumber(edits)
			if not (edits and (edits == 0 or require "Module:table".isPositiveInteger(edits))) then
				error("Argument " .. edits_arg .. " (" .. edits .. ") should be a positive integer")
			end
			total_edits = total_edits + edits
			edits = tostring(edits)
		end
		
		output:insert(([[
|-
| %s%s
| data-sort-value="%s" | %s
| data-sort-value="%s" | %s%s
| %s
]]):format(
		user_links,
		not is_empty(username_comment) and " (" .. mw.text.trim(username_comment) .. ")" or "",
		start_date_sortkey,
		start_date_spelled_out,
		block_date_sortkey,
		block_date_spelled_out,
		not is_empty(date_comment) and " (" .. mw.text.trim(date_comment) .. ")" or "",
		add_thousands_separator(edits)))
	end

		output:insert(([[
|- class="sortbottom"
| Total
| —
| —
| %s
]]):format(add_thousands_separator(total_edits)))
	
	output:insert "|}"
	
	return output:concat()
end

return export