Module:WikidataReferanse

local p = {}

local labeltekst = { ["P50"] = "", -- forfatter ["P123"] = "", -- utgiver ["P248"] = "", -- nevnt i	["P577"] = "utgitt ", -- utgivelsesdato ["P813"] = "besøkt ", -- besøksdato ["P854"] = "" -- referanse-URL } local wait = { ["P577"] = true, -- utgivelsesdato ["P813"] = true, -- besøksdato ["P1683"] = true, -- sitat ["Pxxxx"] = true  -- dummy } local checkLabel = { ["P1810"] = true, -- oppført som ["Pxxxx"] = true  -- dummy } local unknownItem = 0 local done = {} local reftxt = "" local sep = ""

local lang = mw.language.getContentLanguage local maanednavn = {'januar', 'februar', 'mars', 'april', 'mai', 'juni', 'juli', 'august', 'september', 'oktober', 'november', 'desember'}

function splitTimestamp(timestamp) local aar = string.sub(timestamp, 2, 5) or '????' local maaned = maanednavn[tonumber(string.sub(timestamp, 7, 8) or '00')] or '??' local dag = tonumber(string.sub(timestamp, 10, 11) or '??')..'.' local fkr = '' if string.sub(timestamp, 1, 1) == '-' then fkr = ' f.Kr.' end return aar,maaned,dag,fkr end

function kalender(kal) if string.match (kal, 'wikidata.org/entity/Q1985727$' ) then return '' end if string.match (kal, 'wikidata.org/entity/Q1985786$' ) then --Kun når datoen er etter oktober 1582 bør juliansk skrives ut. Alle datoer før dette blir regnet for å være julianske uansett. --		local html = mw.html.create( 'span' ) --			:wikitext( 'juliansk' ) return '' --' ('..tostring( html )..')' end return mw.text.nowiki( kal ) end

local function norskDatoFraClaim(claim) if claim.snaktype ~= "value" then return "ingen verdi" end local value = claim.datavalue.value local presisjon = value.precision local aar,maaned,dag,fkr = splitTimestamp(value.time) local utstring = ''

if presisjon == 11 then utstring = string.format( '%s %s %s%s', dag, maaned, tonumber(aar) or aar, fkr ) elseif presisjon == 10 then utstring = string.format( '%s %s%s', maaned, tonumber(aar) or aar, fkr ) elseif presisjon == 9 then utstring = (tonumber(aar) or aar)..''..fkr utstring = string.format( '%s%s', tonumber(aar) or aar, fkr ) elseif presisjon == 8 then utstring = string.format( '%s-årene%s', math.floor(((tonumber(aar) or 0))/10)*10, fkr ) elseif presisjon == 7 then utstring = string.format( '%s. århundre%s', math.floor(((tonumber(aar) or 0)+99)/100), fkr ) end

return mw.text.nowiki( utstring ).. kalender(value.calendarmodel) end

function addRefTxt(pidLabel,refValue) reftxt = reftxt .. sep .. " " .. pidLabel .. refValue -- .. " ( " .. datatype .. " )" sep = ", " end

local function formatUrl(url) local reftext = url local j1 = string.find(reftext,'//',1,true) if j1 then reftext = string.sub(reftext,j1+2,string.len(reftext)) else reftext = '' end if reftext ~= '' then local i1 = string.find(reftext,'/',1,true) if i1 then reftext = string.sub(reftext,1,i1-1) end else reftext = url end return "[" .. url .. " " .. reftext .. "]" end

local function formatItem(property) local item = nil if property[1].snaktype == "value" then item = property[1].datavalue.value or nil end if item then local value = item.id		local label = mw.wikibase.label( value ) local sitelink = mw.wikibase.sitelink( value ) if sitelink or label then return mw.wikibase.formatValues(property) end unknownItem = unknownItem +1 return  .. value ..  else return "ingen verdi" end end

function fmtTitle(ref) if done["P1476"] then return end done["P1476"] = true title = ref["P1476"] or nil if title then local refValue = "«" .. mw.wikibase.formatValues(title) .. "»"		if ref["P854"] then refValue = "[" .. ref["P854"][1].datavalue.value .. " " .. refValue .. "]"			done["P854"] = true end addRefTxt("",refValue) end return end

function fmtQuote(pid,ref) if done[pid] then return end done[pid] = true local quote = ref[pid] or nil if quote then local refValue = "«" .. mw.wikibase.formatValues(quote) .. "»"		addRefTxt("",refValue) end return end

-- Ignore cases where the value is just the label. function fmtLlabelCheck(pid,ref) if done[pid] then return end done[pid] = true local refItem = ref[pid] or nil if refItem then local refValue = mw.wikibase.formatValues(refItem) local pidLabel = labeltekst[pid] or mw.wikibase.getLabel(pid) .. " "		local label = mw.wikibase.label local refString = ref[pid][1].datavalue.value if not (label == refString ) then addRefTxt(pidLabel,refValue) end end return end

function fmtItem(pid,ref) if done[pid] then return end local property = ref[pid] or nil if property then local pidLabel = labeltekst[pid] or mw.wikibase.getLabel(pid) .. " "		local refValue = formatItem(property) addRefTxt(pidLabel,refValue) end done[pid] = true end

function fmtDato(pid,ref) if done[pid] then return end local property = ref[pid] or nil if property then local pidLabel = labeltekst[pid] or mw.wikibase.getLabel(pid) .. " "		local refValue = norskDatoFraClaim(property[1]) addRefTxt(pidLabel,refValue) end done[pid] = true end

function fmtMisc(ref) for pid, property in pairs(ref) do		if checkLabel[pid] then fmtLlabelCheck(pid,ref) elseif not done[pid] and not wait[pid] then local datatype = property[1].datatype or "xxx" local pidLabel = labeltekst[pid] or mw.wikibase.getLabel(pid) .. " "			local refValue = mw.wikibase.formatValues(property) if datatype == 'url' then refValue = formatUrl(property[1].datavalue.value) elseif datatype == 'time' then refValue = norskDatoFraClaim(property[1]) elseif datatype == 'wikibase-item' then refValue = formatItem(property) end addRefTxt(pidLabel,refValue) done[pid] = true end end end

function fmtSingle(ref) local num = 0 for pid, property in pairs(ref) do		num = num+1 end

if num~=1 then return false end if not ref["P248"] then return false end local P248 = ref["P248"] local item = nil if P248[1].snaktype == "value" then item = P248[1].datavalue.value or nil end if item then local value = item.id		local label = mw.wikibase.label( value ) local description = mw.wikibase.description( value ) local sitelink = mw.wikibase.sitelink( value ) if label and not sitelink then local txt ="«" .. label .. "», " .. description addRefTxt("",txt) return true end end return false end

function p.formatRef(ref) reftxt = "" sep = "" done = {} local args = mw.getCurrentFrame:getParent.args if args and args["wikidatatesting"] == "ref" then if fmtSingle(ref) then return reftxt end end local refValue = nil fmtItem("P123",ref) -- utgiver fmtItem("P248",ref) -- nevnt i	fmtItem("P50",ref) -- fortatter fmtTitle(ref) fmtMisc(ref) fmtDato("P577",ref) -- utgivelsesdato fmtDato("P813",ref) -- besøksdato fmtQuote("P1683",ref) -- sitat return reftxt -- return mw.wikibase.formatValues( ref ) -- return " " .. mw.text.jsonEncode(ref, mw.text.JSON_PRETTY) .. " " end

function findReference(property,args) for k,v in pairs( args ) do		if not property[k] then return false end return true end return true end

local definedArgs = { ["id"] = "id", ["utsagn"] = "utsagn" }

function findArgs(args) local propertyArgs = {} count = 0 for k,v in pairs( args ) do		if not definedArgs[k] then propertyArgs[k] = v			-- table.insert( propertyArgs, v ) count = count + 1 end end return propertyArgs end

function selectRef(frame,refs,args) local txt = "" for k,v in pairs( refs ) do		if findReference(v.snaks,args) then --txt = txt .. findReference(v.snaks,args) local content = p.formatRef(v.snaks) local attrs = { name = string.format( 'hash-%s', v.hash ) } --	txt = txt .. " " .. mw.text.jsonEncode(v.snaks,mw.text.JSON_PRETTY) .. " "			if content and content ~= "" then txt = txt .. -- " Ny:" .. k .. frame:extensionTag( 'ref', content, attrs ) else -- txt = txt .. " " .. mw.text.jsonEncode(v.snaks,mw.text.JSON_PRETTY) .. " "			end --	.. " Ny:" end --			txt = txt .. " " .. mw.text.jsonEncode(v,mw.text.JSON_PRETTY) .. " "	end return txt end

function p.referanse(frame) local args = frame.args local propertyId = args["property"] local entityId = args['id'] or args[1] or nil if entityId == "" then entityId = nil end local entity = mw.wikibase.getEntity(entityId) or nil local entityId = entity.id or nil if not entityId then return "" end -- local statements = mw.wikibase.getBestStatements( entityId, propertyId ) --	local entity = mw.wikibase.getEntity(entityId) local property = entity:getBestStatements(args["utsagn"]) --local txt = " " .. mw.text.jsonEncode(property, 	mw.text.JSON_PRETTY) .. " "	local txt = "" if property and property[1] then local propertyArgs = findArgs(args) local refsnaks = property[1]["references"] or nil txt = selectRef(frame,refsnaks,propertyArgs) end return txt --.. " " .. mw.text.jsonEncode(property[1]["references"], --property[1]["references"][1], 	--	mw.text.JSON_PRETTY) .. " " end

function p.test(frame) return p.referanse(frame) -- return "test test" end

return p