Modul:Liste over eldste personer/sandkasse
Utseende
Dette er modulsandkassa for Modul:Liste over eldste personer (forskjell). |
Denne modulen brukes for å vise en formatert nummerert liste over personer sortert etter alder og fødselsdato. Se malen {{Liste over eldste personer}} for informasjon om argumenter.
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 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 style="display:none">' .. 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 result = '{|class="wikitable sortable"\n!Nr!!class="unsortable"|Navn!!Alder!!Født!!Død'
local lastAge = nil
local rank = 0
for key, person in ipairs(persons) do
if lastAge == nil or not equalAges(lastAge, person.age) then -- New age: increment rank
rank = key
lastAge = person.age
end
result = result .. '\n|-'
if not person.dateDeath then -- Not dead
result = result .. 'bgcolor="99FF99"'
end
result = result .. '\n||' .. rank .. '||' .. person.name
if person.ref ~= nil and person.ref ~= '' then -- Add reference?
result = result .. person.ref
end
result = result .. '||' .. ageInYearsAndDaysFormat(person.age) .. '||' .. formatDateSortable(person.dateBirth) .. '||'
if person.dateDeath ~= nil then -- Dead
result = result .. formatDateSortable(person.dateDeath)
else
result = result .. '<center>—</center>'
end
if key == 100 then break end -- Display max 100 entries
end
result = result .. '\n|}'
return result
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
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) .. '–' .. 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
if key == 100 then break end -- Display max 100 entries
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 ''
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