Mine sisu juurde

Moodul:Mall:Persoon

Allikas: Vikipeedia

Selle mooduli dokumentatsiooni saab kirjutada asukohta Moodul:Mall:Persoon/doc.

local wdLoadSuccess, mdlWikidata = pcall(require, "Moodul:Wikidata")
assert (wdLoadSuccess, "Wikidata mooduli laadimine ebaõnnestus.")

local p = {}
local gregorianCalendar = "Q1985727"
local propertyId = {
	["birthdate"] = "P569",
	["deathdate"] = "P570"
}
local trackingCatLink = "[[Kategooria:Wikidata kuupäevi kasutavad isikuartiklid]]"

local function getDateParts(strDate)
    _,_,y,m,d=string.find(strDate, "(%d+)-(%d+)-(%d+)")
    return tonumber(y),tonumber(m),tonumber(d)
end

local function isAlive()
	local wdParameters = {}
	local boolIsAlive = true

	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = propertyId["deathdate"]
    deathdate = mdlWikidata.claim(wdParameters) or nil
    if (deathdate) then boolIsAlive = false end
  
    return boolIsAlive
end

local function calcAge(birthDate, deathDate)
	-- UTC +3
	local TIMEZONE_OFFSET = 3 * 60 * 60
	local MAX_AGE = 150
	local age = nil
	local ageErrorMsg = nil
	
	fYear, fMonth, fDay = getDateParts(birthDate)
	fMonth = fMonth or 0
	fDay = fDay or 0
	if (deathDate) then
		sYear, sMonth, sDay = getDateParts(deathDate)
	else
		nowTable = os.date('*t', os.time() + TIMEZONE_OFFSET)
        sYear = nowTable.year
        sMonth = nowTable.month
        sDay = nowTable.day
	end
	sMonth = sMonth or 0
	sDay = sDay or 0
	
	if (fYear and sYear) then
		age = sYear - fYear
		if (sMonth<fMonth or (sMonth==fMonth and sDay<fDay)) then
			age = age - 1
		end
		if (age > MAX_AGE) then
			ageErrorMsg = "[[Kategooria:Wikidata järgi liiga vanad inimesed]]"
		end		
	end

	return age, ageErrorMsg
end

function p.birthdateWithAge()
	local outputStr = ""
	local wdParameters = {}

	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = propertyId["birthdate"]
    wdParameters["args"]["parameter"] = "calendarmodel"
    calendarType = mdlWikidata.claim(wdParameters) or nil
    if (calendarType and calendarType == gregorianCalendar) then 
	    wdParameters["args"]["parameter"] = "time:j. F Y"
	    birtdate = mdlWikidata.claim(wdParameters) or nil
	    
	    if (birtdate) then
		    if (isAlive()) then
		    	wdParameters["args"]["parameter"] = "time:Y-m-d"
		    	birtdateISO = mdlWikidata.claim(wdParameters) or nil
		    	age, ageErrorMsg = calcAge(birtdateISO)
		    end
		    
		    if age then
		    	outputStr = birtdate .. ' <span class="noprint" style="white-space:nowrap">(' .. age .. "-aastane)</span>"
		    	if ageErrorMsg then outputStr = outputStr .. ageErrorMsg end
		    else 
		    	outputStr = birtdate
		    end
		    local showIcon = require("Moodul:EditAtWikidata").showMessage
		    local iconParameters = {}
		    iconParameters["args"] = nil
			iconParameters["args"] = {}
			iconParameters["args"]["pid"] = propertyId["birthdate"]
		    outputStr = outputStr .. showIcon(iconParameters) .. trackingCatLink
	    end
    end
    
    return outputStr
end

function p.deathdateWithAge()
	local outputStr = ""
	local wdParameters = {}

	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = propertyId["deathdate"]
    wdParameters["args"]["parameter"] = "calendarmodel"
    calendarType = mdlWikidata.claim(wdParameters) or nil
    if (calendarType and calendarType == gregorianCalendar) then
    
	    wdParameters["args"]["parameter"] = "time:j. F Y"
	    deathdate = mdlWikidata.claim(wdParameters) or nil
	
	    if (deathdate) then
	    	wdParameters["args"]["parameter"] = "time:Y-m-d"
	    	deathdateISO = mdlWikidata.claim(wdParameters) or nil
	    	
			wdParameters["args"] = {}
			wdParameters["args"][1] = propertyId["birthdate"]
		    wdParameters["args"]["parameter"] = "time:Y-m-d"
		    birtdateISO = mdlWikidata.claim(wdParameters) or nil
		    wdParameters["args"]["parameter"] = "calendarmodel"
		    calendarType = mdlWikidata.claim(wdParameters) or nil
		    if (birtdateISO and calendarType == gregorianCalendar and deathdateISO) then
		    	age, ageErrorMsg = calcAge(birtdateISO, deathdateISO)
		    end
		    
		    if age then
		    	outputStr = deathdate .. ' <span class="noprint" style="white-space:nowrap">(' .. age .. "-aastaselt)</span>"
		    	if ageErrorMsg then outputStr = outputStr .. ageErrorMsg end
		    else 
		    	outputStr = deathdate
		    end
		    local showIcon = require("Moodul:EditAtWikidata").showMessage
		    local iconParameters = {}
		    iconParameters["args"] = nil
			iconParameters["args"] = {}
			iconParameters["args"]["pid"] = propertyId["deathdate"]
		    outputStr = outputStr .. showIcon(iconParameters) .. trackingCatLink 
	    end
    end
    
    return outputStr
end 

return p