table.insert( formattedStatements, s )
else
local t = formatReferences( statement, options )
stat.ref = t
table.insert( formattedStatements, s .. t )
end
end
table.insert(statementsraw, stat)
end
end
local tot = mw.text.listToText( formattedStatements, options.separator, options.conjunction )
if tot == '' then tot = nil end
if options.raw and options.raw ~= '' then
return statementsraw
end
return tot
end
function formatReferences( statement, options )
local reference = {}
if statement.references then
local cite = require('Modul:Cite')
for i, ref in pairs(statement.references) do
local items, s = {}, nil
if ref.snaks then
if ref.snaks.P248 then
for j, prop in pairs(ref.snaks.P248) do
table.insert(items, 'Q' .. prop.datavalue.value['numeric-id'])
end
end
s = cite.citeitem( items, ref.snaks, options )
if s == '' or not s then
s = 'Källangivelsen på Wikidata använder propertys som inte känns igen av Modul:Cite'
end
end
table.insert(reference, mw.getCurrentFrame:extensionTag( 'ref', s, {name = ref.hash} ) )
end
end
return table.concat(reference)
end
function formatStatement( statement, options )
if statement.type == 'statement' then
local s = formatSnak( statement.mainsnak, options )
if statement.qualifiers then
if statement.qualifiers.P585 then
s.datum = formatStatements({property = "P585", enbarten = 'true', noref = 'true', modifytime = options.modifyqualifiertime}, statement.qualifiers)
end
if statement.qualifiers.P574 then
s.dateoftaxpub = formatStatements({property = "P574", enbarten = 'true', noref = 'true', modifytime = options.modifyqualifiertime}, statement.qualifiers)
end
if statement.qualifiers.P405 then
local auktorer = {}
for i, j in pairs(statement.qualifiers.P405) do
if j.snaktype == 'value' then
local item = 'Q' .. j.datavalue.value['numeric-id']
local auktorsnamn = formatStatements({property = "P428", entityId = item, noref = 'ja', enbarten = 'ja'})
if auktorsnamn and auktorsnamn ~= '' then
table.insert(auktorer, formatEntityId(item, {label = auktorsnamn}).value)
else
table.insert(auktorer, formatEntityId(item, {}).value)
end
end
end
s.auktor = mw.text.listToText(auktorer, ', ', ' & ')
end
if statement.qualifiers.P580 or statement.qualifiers.P582 then
local f = formatStatements({property = "P580", enbarten = 'true', noref = 'true', modifytime = options.modifyqualifiertime}, statement.qualifiers) or ''
local t = formatStatements({property = "P582", enbarten = 'true', noref = 'true', modifytime = options.modifyqualifiertime}, statement.qualifiers) or ''
s.tifr = f .. '–' .. t
end
end
return s
elseif not statement.type then
return formatSnak( statement, options )
end
return {value = formatError( 'unknown-claim-type' )}
end  
function formatSnak( snak, options )
if snak.snaktype == 'somevalue' then
if options.somevalue then
if options.somevalue == '' then
return nil
else
return {value = options.somevalue}
end
end
return {value = i18n['somevalue']}
elseif snak.snaktype == 'novalue' then
if options.novalue then
if options.novalue == '' then
return nil
else
return {value = options.novalue}
end
end
return {value = i18n['novalue']}
elseif snak.snaktype == 'value' then
local s = formatDatavalue( snak.datavalue, options, snak.datatype )
if s.item and options.getsimpleproperty and options.getsimpleproperty ~= * and options.getproperty and options.getproperty ~= * then
local pr = formatStatements({property = options.getproperty, entityId = s.item, enbarten = options.getenbarten, noref = 'ja', modifytime = options.getmodifytime, raw = options.getraw})
if pr then s.pr = pr end
end
return s
else
return {value = formatError( 'unknown-snak-type' )}
end
end  
function formatDatavalue( datavalue, options, datatype )
--Use the customize handler if provided
if options['value-module'] or options['value-function'] then
if not options['value-module'] or not options['value-function'] then
return {value = formatError( 'unknown-value-module' )}
end
local formatter = require ('Module:' .. options['value-module'])
if not formatter then
return {value = formatError( 'value-module-not-found' )}
end
local fun = formatter[options['value-function']]
if not fun then
return {value = formatError( 'value-function-not-found' )}
end
return {value = fun( datavalue.value, options )}
end  
--Default formatters
if datatype == 'wikibase-item' then
local s = formatEntityId( getEntityIdFromValue( datavalue.value ), options )
s.item = getEntityIdFromValue( datavalue.value )
return s  
elseif datatype == 'string' or datatype == 'commonsMedia' then
if options.pattern and options.pattern ~= '' then
if options.pattern == "auktoritetsdata" then
local patter = formatStatements( {property = "P1630", entityId = options.property, enbarten = 'true', noref = 'true' })
return {value = formatFromPattern( datavalue.value, {pattern = patter} )}
elseif options.pattern == "auktoritetsdata2" then
local patter = formatStatements( {property = "P1630", entityId = options.property, enbarten = 'true', noref = 'true' })
return {value = '[' .. formatFromPattern( datavalue.value, {pattern = patter} ) .. ' ' .. datavalue.value .. ']' }
else
return {value = formatFromPattern( versalisering(datavalue.value, options), options )}
end
else
return {value = versalisering(datavalue.value, options), label = datavalue.value}
end  
elseif datatype == 'time' then
local Time = require 'Module:Time'
local tid = Time.newFromWikidataValue( datavalue.value ):toHtml
if options.modifytime == 'longdate' then
local mall = ''
if datavalue.value.precision >= 11 then
mall = ''
tid = mw.getCurrentFrame:preprocess(mall)
elseif datavalue.value.precision == 10 then
mall = ''
tid = mw.getCurrentFrame:preprocess(mall)
elseif datavalue.value.precision == 9 then
mall = ''
tid = mw.getCurrentFrame:preprocess(mall)
end
elseif options.modifytime == 'Y' then
local mall = ''
tid = mw.getCurrentFrame:preprocess(mall)
end
return {value = tid}  
elseif datatype == 'globecoordinate' then
local GlobeCoordinate = require 'Module:GlobeCoordinate'
return {value = GlobeCoordinate.newFromWikidataValue( datavalue.value ):toHtml}  
elseif datatype == 'quantity' then
local amount, unit, cat = datavalue.value.amount, datavalue.value.unit, nil
if unit then
unit = unit:match('Q%d+')
end  
local number = formatera.newFromWikidataValue(datavalue.value)  
local unitraw = unit
if unit then
-- Kontrollerar om det finns någon förkortning för denna 'unit'
local lab = options.label or formatStatements({property = 'P558', entityId = unit, enbarten = 'true', langpref = options.langpref, noref = 'true'})
if lab and ( not options.nounitshort or options.nounitshort == '' ) then
local s = formatEntityId( unit, {label = lab, nolink = (options.nounitlink or options.nolink) })
unit = s.value
cat = s.cat
else -- om det inte finns en förkortning
local s = formatEntityId( unit, {nolink = options.nounitlink})
unit = s.value
cat = s.cat
end
end
return {value = number .. ' ' .. (unit or ''), amount = amount, unit = unit, unitraw = unitraw, cat = cat}  
elseif datatype == 'url' then
if options.label and options.label ~= '' then
return {value = '[' .. datavalue.value .. ' ' .. options.label .. ']'}
else
return {value = datavalue.value}
end  
elseif datavalue.type == 'monolingualtext' then
return {value = mw.text.tag('span', {title = mw.language.fetchLanguageName(datavalue.value.language, 'sv')}, datavalue.value.text)}  
else
return {value = formatError( 'unknown-datatype' )}
end
end  
function formatEntityId( entityId, options )
local label = options.label or mw.wikibase.label( entityId )
if label == '' then
label = mw.wikibase.label( entityId ) or nil
end  
local link = mw.wikibase.sitelink( entityId )  
if link and (not options.nolink or options.nolink == '') then
if label and label ~= '' then
return {value = * .. versalisering(label, options) .. *, label = label }
else
return {value = ':' .. versalisering(link, options) .. '', label = link }
end
else
if label then
return {value = versalisering(label, options), label = label}
else
local s = getLabelFromFallBack( entityId )