Modul:WikidataAlder

Fra wikisida.no
Sideversjon per 7. jun. 2025 kl. 19:32 av nb>Haros (Tar ibruk alder på angjeldende tid (P3629) når den finnes, og alternativet er ingen alder. Spores i kategorien Sider som bruker P3629)
Hopp til navigering Hopp til søk

Formål

Å presentere datoer fra Wikidata.

Bruk

Modulen brukes ved å legge inn en av:

{{#invoke:WikidataAlder|alderInfoboks|qv=Qxxx}}

i en mal.

Feilmelding

Om du ser en feilmelding i en infoboks angående fødsels- eller dødsdato, dobbeltsjekk at fødsels- eller dødsdato er riktig på Wikidata. Dette kan forekomme bl.a. av ikke-eksisterende datoer som ved skuddår.

Eksempler

Artikkel Manuell bruk av maler
{{Fødselsdato og alder}} / {{Dødsdato og alder}}
Modul:WikidataAlder
({{ #invoke:WikidataAlder | alderInfoboks }}
Kommentar
Sverige (Q34) (har ikke, siden dette ikke er en person) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value). Har ikke fødsels- eller dødsdatoer, så da prøver vi ikke å regne ut alder
Bjørn Ousland (Q4919913) 14. mai 1959 (66 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Zdeněk Grygera (Q168997) 14. mai 1980 (45 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Synnøve Solemdal (Q257682) 15. mai 1989 (36 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Veronica Campbell-Brown (Q5796) 15. mai 1982 (43 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Jens Stoltenberg (Q57665) 16. mars 1959 (67 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Susann Goksør Bjerkrheim (Q2153171) 7. juli 1970 (55 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Lady Gaga (Q19848) 28. mars 1986 (40 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Arve Opsahl (Q716993) 29. april 2007 (85 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
B.B. King (Q188969) 14. mai 2015 (89 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Einar Gerhardsen (Q164346) 19. september 1987 (90 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Marie Curie (Q7186) 4. juli 1934 (66 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Isaac Newton (Q935) 31. mars 1727 (84 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Karl III Johan (Q52927) 8. mars 1844 (81 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Karl XII av Sverige (Q52934) (ikke brukt i artikkel) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Elisabeth I (Q7207) 24. mars 1603 (69 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Vladimir Lenin (Q1394) 21. januar 1924 (53 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Arthur Townsend (Q4800483) (ikke brukt) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value). Wikidata har ukjent verdi for dødsdato, så da skal vi ikke regne ut alder.
Julius Cæsar (Q1048) (ikke brukt, men manuelt oppgitt til 55 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value). Wikidata har ikke nøyaktige dager, så da prøver vi ikke å regne ut alder
Aleksander den store (Q8409) (ikke brukt, men manuelt oppgitt til 32 år) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value).
Pythagoras (Q10261) (ikke brukt i artikkel) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value). Wikidata har ikke nøyaktige dager, så da prøver vi ikke å regne ut alder
Ramses II (Q1523) (ikke brukt i artikkel) Lua-feil i linje 121: attempt to concatenate local 'ageDeath' (a nil value). Wikidata har ikke nøyaktige dager, så da prøver vi ikke å regne ut alder

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 qid = frame.args[1]
	if qid and qid == "" then
		qid = nil
	end

		
	local prop = findBestProperty(qid, 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']
		local qualifiers = prop.qualifiers
		local ageDeath = ""
		if qualifiers and qualifiers.P3629 then
			ageDeath = mw.wikibase.formatValues(qualifiers.P3629)
		end
		return p.formatTime(timestamp), precision,ageDeath
	else
		return '', nil,""
	end

end

function p.alderInfoboks(frame)
	
	local timestampBorn, precisionBorn,dummy = getDate(frame, 'born')
	local timestampDied, precisionDied,ageDeath = getDate(frame, 'died')
	if ageDeath ~= "" then
		ageDeath = "[[Kategori:sider som bruker P3629]]"..'(' .. ageDeath .. ')'
	end
	if (timestampBorn == 'unknown value' or timestampDied == 'unknown value') then
		return ageDeath
	end

	if (timestampBorn == nil and timestampDied == nil) or precisionBorn == nil then
		return ageDeath	
	end

	if timestampDied == '' and precisionBorn >= tonumber(11) then
		local localtime = mw.language.getContentLanguage():formatDate( 'Y-m-d', nil, true )
		local diff = Date(localtime) - timestampBorn
		-- capture error
		if diff == nil then
			return ageDeath
		end
		return mw.text.nowiki('(' .. 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.nowiki('(' .. diff:age('y') .. ' år)')
	end

	return ageDeath
	
end


return p