Jump to content

Module:User Wikisource For

From Wikisource

-- Implements [[Template:User Wikisource For]]

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local userbox = require('Module:Userbox').userbox

function p._user_wikisource_for(args)
	local given_date = {
		year = args.year or 0,
		month = args.month or 0,
		day = args.day or 0
	}
	mw.logObject(os.date("*t", os.time(given_date)))
	local current_date = {
		year = os.date('%Y'),
		month = os.date('%m'),
		day = os.date('%d')
	}
	mw.logObject(os.date("*t", os.time(current_date)))
	
	local age_string = ""
	if os.time(current_date) == os.time(given_date) then
		age_string = "0 days"
	else
		local minus_time = ""
		if os.time(current_date) < os.time(given_date) then
			minus_time = "-"
			local temp = current_date
			current_date = given_date
			given_date = temp
		end
		
		local age = {
			years = current_date.year - given_date.year,
			months = current_date.month - given_date.month,
			days = current_date.day - given_date.day
		}
		if age.years > 0 and (age.months < 0 or age.days < 0) then
			age.years = age.years - 1
			age.months = age.months + 12
		end
		if age.months > 0 and age.days < 0 then
			age.months = age.months - 1
			local current_month_end = {year = current_date.year, month = current_date.month, day = 31}
			local previous_month_end = {year = current_date.year, month = current_date.month - 1, day = 31}
			if current_date.month == 1 then
				previous_month_end = {year = current_date.year - 1, month = 12, day = 31}
			end
			age.days = age.days + os.date("*t", os.time(current_month_end)).yday - os.date("*t", os.time(previous_month_end)).yday
		end
		
		local age_string_table = {}
		for k, v in pairs({"year", "month", "day"}) do
			if age[v .. "s"] == 1 then
				table.insert(age_string_table, "1 " .. v)
			elseif age[v .. "s"] > 0 then
				table.insert(age_string_table, age[v .. "s"] .. " " .. v .. "s")
			end
		end
		age_string_table[#age_string_table] = "and " .. age_string_table[#age_string_table]
		age_string = minus_time .. table.concat(age_string_table, ", ")
	end
	mw.logObject(age_string)
	
	local info = ''
	if yesno(args.wikibirthday or true) then
		if os.time(current_date) == os.time(given_date) then
			info = "Today is this user's '''first day''' on [[Wikisource]]. "
		elseif given_date.month == current_date.month and given_date.day == current_date.day then
			info = "Today is this user's '''[[Wikisource]] Birthday'''. "
		end
	end
	info = info .. "This user has been on [[Wikisource]] for '''" .. age_string .. "'''."
	
	local box_args = {
		['border-c'] = args.border or '#55A0AA',
		['id'] = '[[File:Noia 64 apps karm.svg|44px]]',
		['id-c'] = args.imgbg or '#DDEDEE',
		['info'] = info,
		['info-c'] = args.bgcolor or '#AAD0DD'
	}
	return userbox(box_args)
end

function p.user_wikisource_for(frame)
	return p._user_wikisource_for(getArgs(frame))
end

return p