Module:WikidataAlder
ښکارېدونکې بڼه
لاسوند لپاره ددې موډيول کېدای سی په Module:WikidataAlder/لاسوند کي وي
local Date = require('Module:Date')._Date
local p = {}
function okProperty(prop)
-- Sjekk at prop finnes
if prop
and prop['mainsnak']
and prop['mainsnak']['datavalue']
and prop['mainsnak']['datavalue']['value']
and prop['mainsnak']['datavalue']['value']['time']
and prop['mainsnak']['datavalue']['value']['precision']
then
return true
end
return false
end
function checkForUnknownValue(prop)
-- Sjekk om vi har "ukjent verdi"
if prop
and prop['mainsnak']
and prop['mainsnak']['snaktype'] == 'somevalue'
then
return true
end
return false
end
function p.formatTime(timestamp)
local year = tonumber(string.sub(timestamp, 2, 5))
local month = tonumber(string.sub(timestamp, 7, 8))
local day = tonumber(string.sub(timestamp, 10, 11))
if string.sub(timestamp, 1, 1) == '-' then
year = 0 - year
end
if year ~= nil and month ~= nil and day ~= nil then
return Date(tonumber(year), tonumber(month), tonumber(day))
else
return nil
end
end
function findBestProperty(qv, pv)
local entity = mw.wikibase.getEntity(qv)
if not entity then
return
end
if not entity['claims'] or not entity['claims'][pv] then
return
end
local prop = entity['claims'][pv]
local i = 0
local oki = 0
while i < #prop do
i = i + 1
local rank = prop[i]['rank']
if rank == 'preferred' then
return prop[i]
end
if rank == 'normal' then
if oki == 0 then
oki = i
end
end
end
if oki > 0 then
return prop[oki]
end
end
function getDate(frame, action)
local propertyID
if action == 'born' then
propertyID = 'P569'
elseif action == 'died' then
propertyID = 'P570'
else
return '', nil
end
local qv = frame.args[1]
local prop = findBestProperty(qv, propertyID)
if not prop then
return '';
end
if checkForUnknownValue(prop) then
return 'unknown value', nil
end
if okProperty(prop) then
local timestamp = prop['mainsnak']['datavalue']['value']['time']
local precision = prop['mainsnak']['datavalue']['value']['precision']
return p.formatTime(timestamp), precision
else
return '', nil
end
end
function p.alderInfoboks(frame)
local timestampBorn, precisionBorn = getDate(frame, 'born')
local timestampDied, precisionDied = getDate(frame, 'died')
if (timestampBorn == 'unknown value' or timestampDied == 'unknown value') then
return ''
end
if (timestampBorn == nil and timestampDied == nil) or precisionBorn == nil then
return ''
end
if timestampDied == '' and precisionBorn >= tonumber(11) then
local diff = Date('currentdate') - timestampBorn
-- capture error
if diff == nil then
return ''
end
return mw.text.pswiki('(' .. diff:age('y') .. ' år)')
end
if timestampDied ~= nil and precisionDied ~= nil and (precisionBorn >= tonumber(11) and precisionDied >= tonumber(11)) then
local diff = timestampBorn - timestampDied
return mw.text.pswiki('(' .. diff:age('y') .. ' år)')
end
return ''
end
return p