Documentation for this module may be created at ሞድዩል:Lifetime/doc

local p = {}

local function error ( msg )
	return '<strong class="error">ጌጋ፦ ' .. msg .. '</strong>'
		.. '[[መደብ:ዊኪፐድያ:Articles with problem in the Lifetime template]]'
end

local function safereadtable ( ... )
	local arg = {...}
	local key = arg and arg[1]
	for i = 2, #arg do
		local val = arg[i]
		if key == nil or val == nil then
			key = nil
			break
		else
			key = key[val]
		end
	end
	return key
end

--[[
	@name	catSexGenderIdentity
	@global	none
	@param	none
	@return	wikitext (categories)
	@descr	Generates the categories related to the sex or gender of a
			person based on the data obtained from Wikidata
			according to its P21 property.
--]]

function p.catSexGenderIdentity()
	local wd = mw.wikibase.getEntityObject()
	if wd and
		-- wd:formatPropertyValues( 'P31' ).value == 'ወዲ ሰብ'
		-- wd.claims.P31[1].mainsnak.datavalue.value.id == 'Q5'
		safereadtable(wd, 'claims', 'P31', 1, 'mainsnak', 'datavalue', 'value', 'id') == 'Q5'
	then
		--[==[ Main categories are:
				[[መደብ:ደቂ ተባዕትዮ]]
				[[መደብ:ደቂ ኣንስትዮ]]
				[[መደብ:ሰባት መንጎ ጾታ]]
				[[መደብ:ዘይክልተኣውያን ሰባት]]
				[[መደብ:ብጾታ ዘይተመደቡ ሰባት]]
		--]==]
		--[==[ valid sex/genders located in Wikidata: ]==]--
		local validsexgenders = {
		--	-- see https://www.wikidata.org/wiki/Property:P21#P2302
		--	['IDENTIFIER']	--[[ ['TEXT LABEL'] --]]			= 'CATEGORY',
			['Q6581097']	--[[ ['ተባዕታይ'] --]]				= 'ደቂ ተባዕትዮ',
			['Q6581072']	--[[ ['ኣንስተይቲ'] --]]				= 'ደቂ ኣንስትዮ',
			['Q1097630']	--[[ ['መንጎ ጾተኛ'] --]]			= 'ሰባት መንጎ ጾታ',
			['Q48270']		--[[ ['ዘይክልተኣዊ ጾታ'] --]]		= 'ዘይክልተኣውያን ሰባት',
			['Q1399232']	--[[ ['ፋ'ኣፋፋይን'] --]]			= 'ዘይክልተኣውያን ሰባት', -- third gender in Samoan culture
			['Q3277905']	--[[ ['ማሁ'] --]]					= 'ዘይክልተኣውያን ሰባት', -- third gender in traditional Hawaiian, Kanaka and Maohi cultures
			['Q746411']		--[[ ['ካቶይ'] --]]				= 'ዘይክልተኣውያን ሰባት', -- third gender in Thai culture
			['Q350374']		--[[ ['ፋካሌቲ'] --]]				= 'ዘይክልተኣውያን ሰባት', -- third gender in Tongan culture
			['Q660882']		--[[ ['ሂጅራ'] --]]					= 'ዘይክልተኣውያን ሰባት', -- third gender of South Asian cultures
			-- 'unknown value' or 'ዘይፍለጥ ዋጋ'
			-- 'no value' or 'ዋጋ ዘይብሉ'
			['Q301702']		--[[ ['ክልተ መናፍስቲ'] --]]			= 'ዘይክልተኣውያን ሰባት', -- third gender in the indigenous North American cultures
			['Q28873047']	--[[ ['መንጎ ጾታዊ ነብሲ'] --]]	= nil, -- non-human
			['Q1289754']	--[[ ['ገለልተኛ'] --]]				= 'ዘይክልተኣውያን ሰባት',
			['Q179294']		--[[ ['ስሉብ'] --]]				= 'ደቂ ተባዕትዮ',
			['Q7130936']	--[[ ['ኩሉ ጾታ'] --]]				= 'ዘይክልተኣውያን ሰባት',
			['Q52261234']	--[[ ['ጾታ ገለልተኛ'] --]]			= 'ዘይክልተኣውያን ሰባት',
			['Q16674976']	--[[ ['ሄርማፍሮዲትነት'] --]]		= nil, -- non-human
			['Q96000630']   --[[ ['X-ጾታ'] --]]				= 'ዘይክልተኣውያን ሰባት',
			['Q104717073']  --[[ ['ሰብ መንጎ ጾታ'] --]]     = 'ሰባት መንጎ ጾታ',
		}
	--	local sexgenderval = wd.claims.P21[1].mainsnak.datavalue.value.id -- wd:formatPropertyValues( 'P21' ).value
		local sexgenderval = safereadtable( wd, 'claims', 'P21', 1, 'mainsnak', 'datavalue', 'value', 'id')
		local sexgendercat = validsexgenders[ sexgenderval ] or 'ብጾታ ዘይተመደቡ ሰባት'
		return '[[መደብ:' .. sexgendercat .. ']]'
	end
	return ''  -- non-human
end

--[[
	@name	catLivingStatus
	@global	require('Module:Roman')
	@param	birthdate, deathdate, no errors
	@return	wikitext (categories and [optionally] errors)
	@descr	Generates the categories related to the birth and death
			of a person, as well as identifies if they are alive, based
			on the data provided as a parameter or automatically
			obtained from Wikidata based on their P569 and P570 properties.
	@aux	filterBirthDate, filterLiveState, filterDeathDate, getDateStruct
--]]

local function filterBirthDate( value )
	return mw.ustring.match( mw.ustring.upper( value ), "MISSING" )
		or mw.ustring.match( mw.ustring.upper( value ), "UNKNOWN" )
		or mw.ustring.match( value, "%?" )
		or mw.ustring.match( value, "ዘይተፈልጠ ዓመት" )
		or mw.ustring.match( value, "ዘይፍለጥ ዋጋ" )
end

local function filterLiveState( value )
	return mw.ustring.match( mw.ustring.upper( value ), "LIVING" )
		or mw.ustring.match( mw.ustring.upper( value ), "ንበር" )
end

local function filterDeathDate( value )
	return filterLiveState( value )
		or filterBirthDate( value )
end

function p.catLivingStatus( birthdate, deathdate, noerrors )
	local wd = mw.wikibase.getEntityObject()
	local categories, errors = '', ''
	-- Birth date
	local result1, number1, margin1 = getDateStruct(wd, birthdate, 'P569', filterBirthDate)
	-- Death date
	local result2, number2, margin2 = getDateStruct(wd, deathdate, 'P570', filterDeathDate)
	-- Living status
	local living
	if	( result2 == '' and not filterLiveState( margin2 ) )
	or	( result2 ~= '' and result2 )
	then
		living = false -- sure
	elseif result1 and result1 ~= '' then
		if number1 < 0 then
			living = false -- sure
		elseif number1 then
			living = true -- maybe
			local curYear = tonumber(os.date( "%Y" ))
			local maxLive = (number1 + margin1 + 130)
			if curYear > maxLive then
				living = false
			end
		end
	end
	-- Categories
	if result1 == nil then
		errors = error('ዘይብቑዕ ዕለት ልደት')
		categories = categories .. '[[መደብ:ኣብ ዘይተፈልጠ ዓመት ዝተወልዱ]]' -- unknown because not valid
	elseif result1 == false then
		errors = errors .. '[[መደብ:ዊኪፐድያ:Articles with Lifetime template without dates]]'
		categories = categories .. '[[መደብ:ኣብ ዘይተፈልጠ ዓመት ዝተወልዱ]]' -- unknown because not set
	elseif result1 == '' then
		categories = categories .. '[[መደብ:ኣብ ዘይተፈልጠ ዓመት ዝተወልዱ]]' -- known to be unknown
	else -- valid value
		categories = categories .. '[[መደብ:ዝተወልዱ ብ' .. result1 .. ']]'
	end
	if result2 == nil then
		errors = error('ዘይብቑዕ ዕለት ሞት')
		-- categories = categories .. '[[መደብ:ኣብ ዘይተፈልጠ ዓመት ዝሞቱ]]' -- unknown because not valid
	elseif result2 == false then
		if living then
			categories = categories .. '[[መደብ:ንበሩ ሰባት]]'
		elseif living == false then -- can be unknown
			errors = errors .. '[[መደብ:ዊኪፐድያ:Articles with Lifetime template without dates]]'
		end
	elseif result2 == '' then
		if filterLiveState( margin2 ) then
			if living == false then
				categories = categories .. '[[መደብ:ኣብ ዘይተፈልጠ ዓመት ዝሞቱ]]' -- unknown because not set
				errors = errors .. '[[መደብ:ዊኪፐድያ:Articles with Lifetime template without dates]]'
				errors = errors .. '[[መደብ:ዊኪፐድያ:Articles with problem in the Lifetime template]]'
			end
		else
			categories = categories .. '[[መደብ:ኣብ ዘይተፈልጠ ዓመት ዝሞቱ]]' -- known to be unknown
		end
	else -- valid value
		categories = categories .. '[[መደብ:ዝሞቱ ብ' .. result2 .. ']]'
	end
	if noerrors then
		return categories
	end
	return categories .. errors
end

--[[
	@name	sortKey
	@global	none
	@param	value
	@return	string
	@descr	Generates the ordering key of a person's name, through the data
			obtained automatically from Wikidata according to its different
			properties.
--]]

function p.sortKey( value )
	local wd = mw.wikibase.getEntityObject()
	local sortkey = ''
	if value and value ~= '' then
		sortkey = value
	elseif wd and
		-- wd:formatPropertyValues( 'P31' ).value == 'ወዲ ሰብ'
		-- wd.claims.P31[1].mainsnak.datavalue.value.id == 'Q5'
		safereadtable(wd, 'claims', 'P31', 1, 'mainsnak', 'datavalue', 'value', 'id') == 'Q5'
	then
		local givenname = wd:formatPropertyValues( 'P734' ).value
		local familyname = wd:formatPropertyValues( 'P735' ).value
		local secondfamilyname = wd:formatPropertyValues( 'P1950' ).value
		-- local birthname = wd:formatPropertyValues( 'P1477' ).value
		if familyname and familyname~= '' then
			sortkey = familyname
			if secondfamilyname and secondfamilyname ~= '' then
				sortkey = sortkey .. ' ' .. secondfamilyname
			end
			if givenname and givenname ~= '' then
				sortkey = sortkey .. '፣ ' .. givenname
			end
		else
			-- try roman format
			local nomen = wd:formatPropertyValues( 'P2359' ).value
			local cognomen = wd:formatPropertyValues( 'P2365' ).value
			local praenomen = wd:formatPropertyValues( 'P2358' ).value
			local unkval, notval = 'Unknown value', 'No value'
			nomen = nomen:gsub(unkval, ''):gsub(notval, ''):gsub('^, ', ''):gsub(', , ', ', ')
			cognomen = cognomen:gsub(unkval, ''):gsub(notval, ''):gsub('^, ', ''):gsub(', , ', ', ')
			praenomen = praenomen:gsub(unkval, ''):gsub(notval, ''):gsub('^, ', ''):gsub(', , ', ', ')
			if nomen and nomen ~= '' then
				sortkey = nomen
				if cognomen and cognomen ~= '' then
					sortkey = sortkey .. ' ' .. cognomen
				end
				if praenomen and praenomen ~= '' then
					sortkey = sortkey .. '፣ ' .. praenomen
				end
			end
		end
	end
	return sortkey
end

--[[
	@main
	@global	require('Module:ክርክራት')
--]]

function p.main ( frame )
	local getArgs = require('Module:ክርክራት').getArgumentsWithValue
	local args = getArgs( frame )
	
	-- Authority control
	local authorityControl = require('Module:Authority control').authorityControl
	local result = authorityControl(frame) .. '\n' -- frame:preprocess('{{Authority control}}\n')
	local ns = mw.title.getCurrentTitle().namespace
	if ns ~= 0 then
		return result
	end

	-- Sex/Gender
	result = result .. p.catSexGenderIdentity()

	-- Living status
	result = result .. p.catLivingStatus( args and args[1], args and args[2] )

	-- Sort key
	local sortkey = p.sortKey( args and args[3] )
	if sortkey and sortkey ~= '' then
		result = result .. frame:preprocess('{{DEFAULTSORT:' .. sortkey .. '}}\n')
	end

	-- Lifetime
	return result
end

return p