Hoppa till innehållet

Modul:Wikidata2/Aux

Från Wikipedia

Dokumentationen för denna modul kan skapas på Modul:Wikidata2/Aux/dok

p = {}

local wikidata = require('Modul:Wikidata2').formatStatementsFromLua


function p.data(frame)
	local args = frame.args
	if args[1] == 'ref' then
		local data = wikidata({entityId = args.item, property = args.property, sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518'})
		return data[1].ref
	end
	if args[1] == 'year' then
		local data = wikidata({entityId = args.item, property = args.property, sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', noref='true', modifyqualifiertime = 'Y'})
		return data[1].datum
	end
	if args[1] == 'number' then
		local data = wikidata({entityId = args.item, property = args.property, sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', noref='true'})
		if not data then 
			return nil
		end
		local data2 = tostring(data[1].amount)
		if string.sub(data2, 1, 1) == '+' then
			return string.sub(data2, 2)
		else
			return data2
		end
	end
	if args[1] == 'unit' then
		local data = wikidata({entityId = args.item, property = args.property, sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', noref='true'})
		if data then
			return data[1].unit
		else
			return nil
		end
	end
	if args[1] == 'kommun' then
		local s = {}
		local data = wikidata({entityId = args.item, property = args.property, noref='true', avoidqualifier='P582', getonly=args.typ, claimindex=args.index})
		return data
	end
	if args[1] == 'bild' then
		local data = wikidata({entityId = args.item, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'en'})
		if data and #data > 0 then 
			return data[1].value
		end

	end
	if args[1] == 'bild2' then
		local data = wikidata({entityId = args.item, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'en'})
		if data and #data > 1 then 
			return data[2].value
		end

	end
	
	if args[1] == 'bildtext_med_motivlänk' then
		local data = wikidata({entityId = args.item, property = 'P18', qual1 = 'P180', qualifierseparator = '§§§', qualifierconjunction = '§§§', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data > 0 then
			data = data[1]
			if data and data.bildtext then
				local bildtext = data.bildtext
				if data.motiv then
					motivtable = mw.text.split(data.qp1,'§§§')
					for k,v in pairs(motivtable) do
						linktext = v:match("%|(.*)%]%]") or v
						bildtext, n = bildtext:gsub(linktext,v,1)
					end
				end
				return bildtext
			elseif data and data.motiv then
				return data.motiv
			end
		end
	end
	
	if args[1] == 'bildtext' then
		local data = wikidata({entityId = args.item, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data > 0 then
			data = data[1]
			if data and data.bildtext then
				return '<span class="Wikidata_P18_P2096">' .. data.bildtext .. '</span>'
			end
		end
	end
	
	if args[1] == 'bildtext2' then
		local data = wikidata({entityId = args.item, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data > 1 then
			data = data[2]
			if data then
				return data.bildtext
			end
		end
	end
	
	if args[1] == 'motiv' then
		local data = wikidata({entityId = args.item, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data > 0 then
			data = data[1]
			if data then
				return data.motiv
			end
		end
	end
	
	if args[1] == 'motiv2' then
		local data = wikidata({entityId = args.item, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data > 1 then
			data = data[2]
			if data then
				return data.motiv
			end
		end
	end
	
	if args[1] == 'utbredningskarta' then
		local data = wikidata({entityId = args.item, property = 'P181', raw = 'true', noref='true', enbarten = 'true', langpref = 'en'})
		if data and #data > 0 then 
			return data[1].value
		end

	end
	
	if args[1] == 'utbredningskartabeskrivning' then
		local data = wikidata({entityId = args.item, property = 'P181', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data > 0 then
			data = data[1]
			if data then
				return data.bildtext
			end
		end
	end
	
	if args[1] == 'vapenbild' then
		local data = wikidata({entityId = args.item, property = 'P94', raw = 'true', noref='true', enbarten = 'true', langpref = 'en'})
		if data and #data > 0 then 
			return data[1].value
		end
	end
	
	if args[1] == 'vapenbildbeskrivning' then
		local data = wikidata({entityId = args.item, property = 'P94', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data > 0 then
			data = data[1]
			if data then
				return data.bildtext
			end
		end
	end

	return nil
end

p.datediff = function(frame)
	local wikidata = require('Modul:Wikidata2').formatStatementsFromLua
	local entityId = frame.args['entityId']
	local entity = mw.wikibase.getEntityObject()
	if not entityId and entity then entityId = entity.id end
	if not entityId then return '' end
	local date1 = frame.args['date1'] 
	local date2 = frame.args['date2'] 
	local prefix = frame.args['prefix'] or ''
	local suffix = frame.args['suffix'] or ''
	t1 = wikidata({entityId=entityId, property=date1, noref='ja', nolink='ja', rank='best'})
	if date2=='nu' then
		t2=os.date(">%Y-%m-%d")
	else
		t2 = wikidata({entityId=entityId, property=date2, noref='ja', nolink='ja', rank='best'})
	end
    if string.match(t1,'och') or string.match(t2, 'och') then  
    	return ''  --skip if more than one date with best rank	
    end
	y1,m1,d1=string.match(t1,'>(%d+)-(%d+)-(%d+)')
	y2,m2,d2=string.match(t2,'>(%d+)-(%d+)-(%d+)')
	if not d1 or not d2 then
		return '' -- skip if not both dates are full dates
	end
	diff = y2-y1
	if m2 < m1 then diff = diff-1 end
	if m2 == m1 and d2 < d1 then diff = diff - 1 end
	if diff == 0 then 
		diff = frame.args['visa0som'] or 0
    end
	return prefix .. diff .. suffix
end

function tablesize(T)
   local n = 0
   for _ in pairs(T) do n = n + 1 end
   return n
end 

p.property_med_land = function(frame)
	local res = ''
	local args = frame.args
	local item = args.item or mw.wikibase.getEntityIdForCurrentPage()
	if item == nil or item == '' then return nil end
	local entity = mw.wikibase.getEntityObject(item)
	if entity == nil then return nil end
	local property = args['property'] or 'P19'
	local noref = args['noref'] or 'noimport'
	local labelfromnameproperty = args['labelfromnameproperty'] or 'P569'
	local somevalue = args['somevalue'] or ''
	if entity['claims'] == nil then return nil end
	if entity['claims'][property] == nil then return nil end
	local claims = entity['claims'][property]
	res = res .. (wikidata({entityId=item, property=property, noref=noref, labelfromnameproperty=labelfromnameproperty, somevalue='', novalue='', redlink='yes', conjunction=args.conjunction}) or '')
    local numberofclaims = tablesize(entity['claims'][property]) -- TODO: ignore possible claims with deprecated rank
	if numberofclaims > 1 then return res end
	if entity['claims'][property][1].mainsnak.datatype ~= 'wikibase-item' then return res end
	if entity['claims'][property][1].mainsnak.snaktype == 'somevalue' then return res end
    if entity['claims'][property][1].mainsnak.snaktype == 'novalue' then return res end
    local claim = entity['claims'][property][1].mainsnak.datavalue.value.id
	local claimentity = mw.wikibase.getEntityObject(claim)
	if claimentity['claims'] == nil then return res end
	if claimentity['claims']['P17'] == nil then return res end
	-- TODO: select P17 that don't give an anachronism, for now just skip if there are more than one P17
    if tablesize(claimentity['claims']['P17']) > 1 then return res end
    if claimentity['claims']['P17'][1].mainsnak.datatype ~= 'wikibase-item' then return res end
    if claimentity['claims']['P17'][1].mainsnak.snaktype == 'value' and claimentity['claims']['P17'][1].mainsnak.datavalue.value.id == claim then return res end -- don't repeat if P17 is self-referencing
	local result = wikidata({property='p17', noref='ja', entityId=claim, novalue=''})
	if result then
		res = res .. ', ' .. result
	end
	return res
end

p.socken2distrikt = function(frame)
-- traverse a number of intermediate object via property "property" (default is P1366) until the target object is reached
-- the intermediate objects must have intermediateproperty (default is P31) in the set "intemediatepropertyvalues" (default is Q615980)  
-- the target objekt must have a property of targetproperty (default is P31) in the set of "targepropertyvalues" (default is 'Q18333556')
	item = frame.args['item']
	traverseproperty = frame.args['traverseproperty'] or 'P1366'
	intermediateproperty = frame.args['intermediateproperty'] or 'P31'
	intermediatepropertyvalues = frame.args['intermediatepropertyvalues'] or 'Q615980'
	targetproperty = frame.args['targetproperty'] or 'P31'
	targetpropertyvalues = frame.args['targetpropertyvalues'] or 'Q18333556'

	while (item~=nil) do
	  ready = wikidata({entityId = item, property = traverseproperty, getonlyproperty = targetproperty, getonly = targetpropertyvalues, raw='ja', noref='ja'})
	  if (ready == nil or ready[1] == nil) then
		nextitem = wikidata({entityId = item, property = traverseproperty, getonlyproperty = intermediateproperty, getonly = intermediatepropertyvalues, enbarten='ja', raw='ja', noref='ja'})
		if (nextitem ~= nil and nextitem[1]) then 
			item = nextitem[1]['item']
		else
			item = nil 
		end
	  else
  			item = nil
  	  end	
	end
	if ready and ready[1] and ready[1]['item'] then
		res = ready[1]['item']
	else
		res = ''
    end
	return res
end

p.wikisourcelink = function(frame)
	res = nil
	local args = frame.args
	if args.item ~= '' then
		entity = mw.wikibase.getEntityObject(args.item)
		if entity['sitelinks']['svwikisource'] then
			res = '[[:src:' .. entity['sitelinks']['svwikisource']['title'] ..'|' .. entity['sitelinks']['svwikisource']['title'] .. ']]'
		end
	end
	return res
end

return p