local fallback = {'en', 'nb', 'da', 'nn', 'de', 'fr', 'es', 'it', 'pt'}
local formatera = require('Module:Math')
local i18n = {
["errors"] = {
["property-param-not-provided"] = "Property parameter not provided.",
["entity-not-found"] = "Entity not found.",
["unknown-claim-type"] = "Unknown claim type.",
["unknown-snak-type"] = "Unknown snak type.",
["unknown-datatype"] = "Unknown datatype.",
["unknown-entity-type"] = "Unknown entity type.",
["unknown-value-module"] = "You must set both value-module and value-function parameters.",
["value-module-not-found"] = "The module pointed by value-module not found.",
["value-function-not-found"] = "The function pointed by value-function not found."
},
["somevalue"] = "*unknown value*",
["novalue"] = "*no value*"
}
local sortingproperties = {'P585','P571','P580','P569','P582','P570'}  
function versalisering(label, options)
local versalisering = options.versalisering
if options.firstversalisering and options.num == 1 then
versalisering = options.firstversalisering
end
if not versalisering or versalisering == '' then
return label
end
if versalisering == 'lcfirst' then
return mw.getCurrentFrame:preprocess("")
elseif versalisering == 'ucfirst' then
return mw.language.getContentLanguage:ucfirst( label )
elseif versalisering == 'lc' then
return mw.getCurrentFrame:preprocess("")
elseif versalisering == 'uc' then
return mw.getCurrentFrame:preprocess("")
end
return label
end
function getqualifierbysortingproperty(claim, sortingproperty)
for k, v in pairs(sortingproperty) do
if claim.qualifiers and claim.qualifiers[v] and claim.qualifiers[v][1].snaktype == 'value' then
return claim.qualifiers[v][1].datavalue.value.time
end
end
return nil
end  
function getDate(claim, options)
local sortingproperty = sortingproperties
if type(options.sortingproperty) == 'table' then
sortingproperty = options.sortingproperty
elseif type(options.sortingproperty) == 'string' then
sortingproperty = {options.sortingproperty}
end
return getqualifierbysortingproperty(claim, sortingproperty)
end  
function getDateArb(claim, options)
local sortingproperty = options.sortingproperty or 'P569'
if claim.mainsnak.snaktype == 'value' then
local item = claim.mainsnak.datavalue.value['numeric-id']
if claim.mainsnak.datavalue.value['entity-type'] == 'item' then
item = 'Q' .. item
elseif claim.mainsnak.datavalue.value['entity-type'] == 'property' then
item = 'P' .. item
end
return formatStatements({property = sortingproperty, entityId = item, enbarten = 'ja', sortbytime = 'chronological', noref = 'ja'})
end
end  
function comparedates(a, b)
if a and b then
return a > b
elseif a then
return true
end
end  
function sortbyqualifier(claims, options)  
table.sort(claims, function(a,b)
local timeA = getDate(a, options)
local timeB = getDate(b, options)
if options.sortbytime == 'inverted' then
return comparedates(timeB, timeA)
else
return comparedates(timeA, timeB)
end
end
return claims
end  
function sortbyarb(claims, options)  
table.sort(claims, function(a,b)
local timeA = getDateArb(a, options)
local timeB = getDateArb(b, options)
if options.sortbyarbitrary == 'inverted' then
return comparedates(timeB, timeA)
else
return comparedates(timeA, timeB)
end
end
return claims
end  
function getLabelFromFallBack( id )
local entity = getEntityFromId( id )
for k, v in pairs(fallback) do
if entity.labels[v] then
return {value = entity.labels[v].value, language = entity.labels[v].language}
end
end
-- Om inget fallback-språk finns av de i variabeln ovan, så används det först definierade i objektet
if entity.labels then
for v, k in pairs(entity.labels) do
return {value = k.value, language = k.language}
end
end
return {value = '-', language = ''}
end  
function getEntityFromId( id )
if id then
return mw.wikibase.getEntityObject( id )
else
return mw.wikibase.getEntityObject
end
end  
function getEntityIdFromValue( value )
if value['entity-type'] == 'item' then
return 'Q' .. value['numeric-id']
elseif value['entity-type'] == 'property' then
return 'P' .. value['numeric-id']
else
return formatError( 'unknown-entity-type' )
end
end  
function formatError( key )
return '<span>' .. i18n.errors[key] .. '</span>'
end  
function formatStatements( options, ref )
local formattedStatements = {}
local claims = {}
if not options.property then
return formatError( 'property-param-not-provided' )
end  
if type(ref) == 'table' then -- för de fall där funktionen anropas och alla claims redan finns i en tabell
claims = ref[options.property] or {}
else  
--Get entity
local entity = nil
if options.entity and type( options.entity ) == "table" then
entity = options.entity
else
entity = getEntityFromId( options.entityId )
end  
if not entity then
return '' --TODO error?
end  
if not entity.claims or not entity.claims[options.property:upper] then
return '' --TODO error?
end  
--Format statement and concat them cleanly
if options.rank == 'best' or not options.rank then
claims = entity:getBestStatements( options.property:upper )
elseif options.rank == 'valid' then
for i, statement in pairs( entity.claims[options.property:upper] ) do
if statement.rank == 'preferred' or statement.rank == 'normal' then
table.insert( claims, statement )
end
end
elseif options.rank == 'all' then
for i, statement in pairs( entity.claims[options.property:upper] ) do
table.insert( claims, statement )
end
else
for i, statement in pairs( entity.claims[options.property:upper] ) do
if statement.rank == options.rank then
table.insert( claims, statement )
end
end
end
if options.avoidqualifier then
local claims2 = {}
for i, statement in pairs( claims ) do
if not statement.qualifiers or not statement.qualifiers[options.avoidqualifier:upper] then
table.insert( claims2, statement)
end
end
claims = claims2
end
--om det finns vissa statements som har en qualifier som säger "språk = svenska", ta bara med dessa
--alternativt om det finns statements som har en qualifier som säger "skriptsystem == latinska alfabetet"
if not options.langpref or options.langpref == '' then
local claims2 = {}
for i, statement in pairs( claims ) do
if statement.qualifiers and statement.qualifiers.P407 then
for k, v in pairs( statement.qualifiers.P407 ) do
if v.snaktype == 'value' and v.datavalue.value['numeric-id'] == 9027 then -- Q9027 = 'svenska'
table.insert( claims2, statement )
end
end
elseif statement.qualifiers and statement.qualifiers.P282 then
for k, v in pairs( statement.qualifiers.P282 ) do
if v.snaktype == 'value' and v.datavalue.value['numeric-id'] == 8229 then -- Q8229 = 'latinska alfabetet'
table.insert( claims2, statement )
end
end
end
end
if #claims2 > 0 then
claims = claims2
end
end
if options.sortbytime == 'chronological' or options.sortbytime == 'inverted' then
claims = sortbyqualifier(claims, options)
elseif options.sortbyarbitrary == 'chronological' or options.sortbyarbitrary == 'inverted' then
claims = sortbyarb(claims, options)
end
end
if options.enbarten and options.enbarten ~= '' and #claims > 1 then
claims = {claims[1]}
end
local statementsraw = {}
if claims then
for i, statement in pairs( claims ) do
options.num = i
local stat = formatStatement( statement, options )
local s = stat.value
local d = stat.datum
local tf = stat.tifr
local pr = stat.pr
if s == '' then s = nil end
if s then
if d and options.withdate and options.withdate ~= '' then
s = s .. mw.text.tag('small', {}, ' (' .. d .. ')')
end
if tf and options.withintervall and options.withintervall ~= '' then
if options.withintervall == 'gift' then
s = s .. mw.text.tag('br') .. mw.text.tag('small', {}, '(g. ' .. tf .. ')')
else
s = s .. mw.text.tag('small', {}, ' (' .. tf .. ')')
end
end  
if options.getsimpleproperty == 'född' and pr and pr ~= '' then
s = s .. ' ' .. mw.text.tag('small', {}, '(f. ' .. pr .. ')')
end
if type(ref) == 'table' or (options.noref and options.noref ~='') then --Inte leta efter referenser om själva anropet görs från en referens
table.insert( formattedStatements, s )
else
local t = formatReferences( statement, options )
stat.ref = t