Hopp til innhold

Modul:Liste over eldste personer

Fra Wikipedia, den frie encyklopedi

require('strict')
local getArgs = require('Module:Arguments').getArgs

local ageUtil = require('Module:Age utilities')
local ageInYearsAndDays = ageUtil.ageInYearsAndDays
local compareAges = ageUtil.compareAges
local equalAges = ageUtil.equalAges
local ageInYearsAndDaysFormat = ageUtil.ageInYearsAndDaysFormat
local displayMax = 100 -- Display max 100 entries

--local language = mw.language
--local language = mw.getLanguage('no')
local language = mw.getContentLanguage()

local p = {}

-- Returns true if personA is older than personB
local function comparePersons(personA, personB)
	local diffAge = compareAges(personA.age, personB.age)
	if diffAge == 0 then -- Same age, let the person who is born first "win"
		if personA.dateBirth ~= personB.dateBirth then return personA.dateBirth < personB.dateBirth end
	end
	return diffAge > 0
end

local function formatDateIso8061(year, month, day)
	return tostring(year) .. '-' .. string.format('%02d', month) .. '-' .. string.format('%02d', day)
end

local function formatDateSortable(dateN)
	local date = os.date('!*t', dateN)
	local dateIso = formatDateIso8061(date.year, date.month, date.day)
	local result = '<span class="hide">' .. dateIso .. '</span>'
	result = result .. tostring(date.day) .. '. ' .. language:formatDate('F', dateIso) .. ' ' .. tostring(date.year)
	return result
end

-- Expects a table of persons as input.
-- Returns a string with sorted and formatted output (as a sortable table)
function p.displaySortedTable(persons)
	if #persons == 0 then return '' end
	
	table.sort(persons, comparePersons) -- Sort according to age and birth date
	
	local dateNow = os.date('!*t')
	local dateIsoNow = formatDateIso8061(dateNow.year, dateNow.month, dateNow.day)
	local result = '{|class="wikitable sortable"\n!Nr!!class="unsortable"|Navn!!Alder<br /><small class="ikkefet">pr. ' .. language:formatDate('j. F Y', dateIsoNow) .. '</small>!!Født!!Død!!class="unsortable"|Nasjonalitet'
	local lastAge = nil
	local rank, keyLast = 0, 0
	local numberOfLiving = 0
	local row, rankCell = nil, nil
	local root = mw.html.create()
	for key, person in ipairs(persons) do
		if lastAge == nil or not equalAges(lastAge, person.age) then -- New age: increment rank
			if rankCell ~= nil and key - rank > 1 then
				rankCell:attr('rowspan', tostring(key - rank))
			end
			rankCell = nil
			if key > displayMax then break end -- Display max 100 entries
			rank = key
			lastAge = person.age
			row = root:tag('tr')
			rankCell = row:tag('td'):wikitext(tostring(rank))
		else
			row = root:tag('tr')
		end
		keyLast = key
		row:attr('valign', 'top')
		
		local color, cell = nil, nil
		if not person.dateDeath then -- Not dead
			color = 'CCFFCC' -- Alive color
			cell = row:tag('td'):wikitext('<b>' .. person.name .. '</b>')
			cell:attr('bgcolor', color)
			numberOfLiving = numberOfLiving + 1
		else
			row:tag('td'):wikitext(person.name)
		end
		cell = row:tag('td'):wikitext(ageInYearsAndDaysFormat(person.age))
		if color ~= nil then cell:attr('bgcolor', color) end
		cell = row:tag('td'):wikitext(formatDateSortable(person.dateBirth))
		if color ~= nil then cell:attr('bgcolor', color) end
		if person.dateDeath ~= nil then -- Dead
			row:tag('td'):wikitext(formatDateSortable(person.dateDeath))
		else
			cell = row:tag('td'):wikitext('—')
			cell:cssText('text-align: center')
			cell:attr('bgcolor', color)
		end
		cell = row:tag('td'):wikitext(person.ref or '')
		if color ~= nil then cell:attr('bgcolor', color) end
	end
	if rankCell ~= nil and keyLast - rank > 0 then
		rankCell:attr('rowspan', tostring(keyLast - rank + 1))
	end
	root:allDone()
	local living = 'Tabellen inneholder '
	if numberOfLiving == 0 then
		living = living .. 'ingen levende personer.'
	elseif numberOfLiving == 1 then
		living = living .. 'en levende person (markert med grønt).'
	else
		living = living .. numberOfLiving .. ' levende personer (markert med grønt).'
	end
	living = living .. '\n'
	return living .. result .. tostring(root) .. '\n|}'
end

-- Expects a table of persons as input.
-- Returns a string with sorted and formatted output
function p.displaySorted2(persons)
	if #persons == 0 then return '' end
	
	table.sort(persons, comparePersons) -- Sort according to age and birth date
	
	local result = '<ol>'
	local lastAge = nil
	for key, person in ipairs(persons) do
		if lastAge ~= nil and equalAges(lastAge, person.age) then -- Same age as previous: Use <br> to append to same <li>
			result = result .. '<br />'
		else -- New age: need a new <li>
			if lastAge ~= nil then result = result .. '</li>' end -- Not first entry: End the previous entry
			if key > displayMax then break end -- Display max 100 entries
			lastAge = person.age
			result = result .. '<li value="' .. tostring(key) .. '">'
		end
		local dateBirth = os.date('!*t', person.dateBirth)
		if person.dateDeath ~= nil then -- Dead
			local dateDeath = os.date('!*t', person.dateDeath)
			result = result .. person.name .. ' ' .. tostring(dateBirth.year) .. '&#150;' .. tostring(dateDeath.year) .. ': ' .. ageInYearsAndDaysFormat(person.age)
		else -- Still alive
			result = result .. '<b>' .. person.name .. ' født i ' .. tostring(dateBirth.year) .. ' er nå ' .. ageInYearsAndDaysFormat(person.age) .. '</b>'
		end
		if person.ref ~= nil and person.ref ~= '' then -- Add reference?
			result = result .. person.ref
		end
	end
	
	result = result .. '</li></ol>'
	return result
end

-- Input string dateStr is expected on the format "YYYY-MM-DD"
-- Returns a numeric representation of the date or nil
local function decodeDate(dateStr)
	if dateStr == nil or #dateStr < 8 then return nil end
	
	local strings = mw.text.split(dateStr, '-', true)
	if strings == nil or #strings ~= 3 then return nil end
	
	local date = {}
	date.year = tonumber(strings[1]) or 0
	date.month = tonumber(strings[2]) or 1
	date.day = tonumber(strings[3]) or 1
	return os.time(date);
end

--[[ Input is expected as a table of arguments.
	Expects a semi colon seperated entry for each person on the format:
		"Name etc ; Date of birth ; Date of death ; References etc"
			Name and DoB are mandatory, DoD and reference are optional.
			Dates should be in the format "YYYY-MM-DD". ]]
-- Returns a table with the persons
local function decodeArgs(args)
	local dateNow = os.time()
	local persons = {}
	for name, value in pairs(args) do
		local strings = mw.text.split(value, ';', true)
		if strings ~= nil and #strings >= 2 then -- Need at least name and birth date
			local person = {}
			person.name = strings[1] or ''
			person.dateBirth = decodeDate(strings[2]) -- Date of birth
			if person.dateBirth ~= nil then
				person.dateDeath = decodeDate(strings[3]) -- Date of death
				if person.dateDeath ~= nil then -- Dead
					person.age = ageInYearsAndDays(person.dateDeath, person.dateBirth)
				else -- Still alive
					person.age = ageInYearsAndDays(dateNow, person.dateBirth)
				end
				person.ref = strings[4] or ''
				-- For USA-like templates and/or html codes with semicolon
				for k = 5, #strings do person.ref = person.ref .. ';' .. strings[k] or '' end
				table.insert(persons, person)
			end
		end
	end
	return persons
end

-- Decodes args from a frame and displas list
local function displaySorted_(frameArgs, display)
	local args = getArgs(frameArgs)
	local persons = decodeArgs(args)
	if display and display == 'tabell' then
		return p.displaySortedTable(persons) -- Display as a sortable table
	end
	return p.displaySorted2(persons) -- Display as a numbered list
end

-- To be used from a template. Uses args from parent to template
function p.displaySorted(frame)
	return displaySorted_(frame:getParent().args, frame.args.vis)
end

-- To be used directly with #invoke
function p.displaySorted0(frame)
	return displaySorted_(frame.args, frame.args.vis)
end

return p