Modul:WikidataAlder: Forskjell mellom sideversjoner

Fra wikisida.no
Hopp til navigering Hopp til søk
always escape on output unless you use mw.html
tar inn omskrevet modul
Linje 1: Linje 1:
local Date = require('Module:Date')._Date
local p = {}
local p = {}


local function frameargs(frame)
function okProperty(prop)
    local args = frame.args
-- Sjekk at prop finnes
    if args[1] == nil then
if prop
        local pFrame = frame:getParent();
and prop['mainsnak']
        args = pFrame.args;
and prop['mainsnak']['datavalue']
        for k,v in pairs( frame.args ) do
and prop['mainsnak']['datavalue']['value']
            args[k] = v;
and prop['mainsnak']['datavalue']['value']['time']
        end
and prop['mainsnak']['datavalue']['value']['precision']
    end
then
    return args['qv'],args['pv']
return true
end
return false
end
end


function splitTimestamp(timestamp)
function p.formatTime(timestamp)
-- tonumber can return nil, what happens then?
     local year = tonumber(string.sub(timestamp, 2, 5))
     local aar = tonumber(string.sub(timestamp, 2, 5))
     local month = tonumber(string.sub(timestamp, 7, 8))
-- tonumber can return nil, what happens then?
     local day = tonumber(string.sub(timestamp, 10, 11))
     local maaned = tonumber(string.sub(timestamp, 7, 8))
-- tonumber can return nil, what happens then?
     local dag = tonumber(string.sub(timestamp, 10, 11))
         if string.sub(timestamp, 1, 1) == '-' then
         if string.sub(timestamp, 1, 1) == '-' then
         aar = 0 - aar
         year = 0 - year
    end
    return dag,maaned,aar
end
 
function ageFromBirthAndDeathDates(birthtimestamp, birthprecision, deathtimestamp, deathprecision)
    local birthday, birthmonth, birthyear = splitTimestamp(birthtimestamp)
    local deathday, deathmonth, deathyear = splitTimestamp(deathtimestamp)
    if birthprecision == 11 and deathprecision == 11 then
        local age = deathyear - birthyear
        if deathmonth < birthmonth then
            age = age - 1
        end
        if deathmonth == birthmonth and deathday < birthday then
            age = age - 1
        end
        return '('..age..' år)'
     end
     end
     if birthprecision >= 10 and deathprecision >= 10 then
   
        local age = deathyear - birthyear
     if year ~= nil and month ~= nil and day ~= nil then
        if deathmonth < birthmonth then
    return Date(tonumber(year), tonumber(month), tonumber(day))
            age = age - 1
    else
        end
    return nil
        return '('..age..' år)'
     end
     end
     if birthprecision >= 9 and deathprecision >= 9 then
      
        local age = deathyear - birthyear
        local age1 = age - 1
        return '('..age1..'–'..age..' år)'
    end
    return ''
end
end


function p.finnBesteProperty(qv,pv)
function findBestProperty(qv, pv)
     local entity = mw.wikibase.getEntity(qv)
     local entity = mw.wikibase.getEntity(qv)
     if not entity then
     if not entity then
Linje 65: Linje 43:
     local i = 0
     local i = 0
     local oki = 0
     local oki = 0
    -- use for i,prop in iprop(entity['claims'][pv])
     while i < #prop do
     while i < #prop do
         i = i + 1
         i = i + 1
        -- her mangler det en test på snaktype som isteden blir gjort i p.frawikidata,
        -- dermed kan et utsagn bli satt til normal og novalue, og blokkere et ennet som er normal og somevalue
         local rank = prop[i]['rank']
         local rank = prop[i]['rank']
         if rank == 'preferred' then
         if rank == 'preferred' then
             return prop[i]
             return prop[i]
         end
         end
        -- dette gir siste med normal rank, det er ikke gitt at hverken første eller siste er riktig
        -- alle oppføringer er riktige i dette tilfellet
         if rank == 'normal' then
         if rank == 'normal' then
             if oki == 0 then
             if oki == 0 then
Linje 82: Linje 55:
         end
         end
     end
     end
    -- merk tidligere kommentar
     if oki > 0 then
     if oki>0 then
         return prop[oki]
         return prop[oki]
     end
     end
end
end


function p.frawikidata(frame)
function getDate(frame, action)
    local qv,pv = frameargs(frame)
 
    local birthClaim = p.finnBesteProperty(qv, 'P569')
local propertyID
    local deathClaim = p.finnBesteProperty(qv, 'P570')
 
    -- @todo gi en forklarende tekst når utsagn finnes, men prosessering av dem feiler
if action == 'born' then
    if not deathClaim or not birthClaim then
propertyID = 'P569'
        return ''
elseif action == 'died' then
    end
propertyID = 'P570'
    -- @todo gi en forklarende tekst når snaktype er noe annet
else
    if birthClaim['mainsnak']['snaktype'] ~= 'value' or deathClaim['mainsnak']['snaktype'] ~= 'value' then
return '', nil
        return ''
end
    end
 
local qv = frame.args[1]
local prop = findBestProperty(qv, propertyID)
if not prop then
return '';
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 == nil and timestampDied == nil) or precisionBorn == nil then
return ''
end
 
if timestampDied == '' and precisionBorn >= tonumber(11) then
local diff = Date('currentdate') - timestampBorn
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


    local birthValue = birthClaim['mainsnak']['datavalue']['value']
return ''
    local deathValue = deathClaim['mainsnak']['datavalue']['value']
    -- @todo please run the result through mw.text.nowiki( s )
    return ageFromBirthAndDeathDates(
        birthValue['time'], birthValue['precision'],
        deathValue['time'], deathValue['precision']
    )
end
end


return p
return p

Sideversjonen fra 17. mai 2017 kl. 11:43

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) 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)
Zdeněk Grygera (Q168997) 14. mai 1980 (45 år)
Synnøve Solemdal (Q257682) 15. mai 1989 (36 år)
Veronica Campbell-Brown (Q5796) 15. mai 1982 (43 år)
Jens Stoltenberg (Q57665) 16. mars 1959 (67 år)
Susann Goksør Bjerkrheim (Q2153171) 7. juli 1970 (55 år)
Lady Gaga (Q19848) 28. mars 1986 (40 år)
Arve Opsahl (Q716993) 29. april 2007 (85 år)
B.B. King (Q188969) 14. mai 2015 (89 år)
Einar Gerhardsen (Q164346) 19. september 1987 (90 år)
Marie Curie (Q7186) 4. juli 1934 (66 år)
Isaac Newton (Q935) 31. mars 1727 (84 år)
Karl III Johan (Q52927) 8. mars 1844 (81 år)
Karl XII av Sverige (Q52934) (ikke brukt i artikkel)
Elisabeth I (Q7207) 24. mars 1603 (69 år)
Vladimir Lenin (Q1394) 21. januar 1924 (53 år)
Arthur Townsend (Q4800483) (ikke brukt) 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) 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)
Pythagoras (Q10261) (ikke brukt i artikkel) Wikidata har ikke nøyaktige dager, så da prøver vi ikke å regne ut alder
Ramses II (Q1523) (ikke brukt i artikkel) 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 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 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 == nil and timestampDied == nil) or precisionBorn == nil then
		return ''	
	end

	if timestampDied == '' and precisionBorn >= tonumber(11) then
		local diff = Date('currentdate') - timestampBorn
		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 ''
	
end


return p