Modul:Distriktfakta Afghanistan WD

Från Wikipedia

Dokumentation [visa] [redigera] [historik] [rensa sidcachen]


local wikidata = require('Modul:Wikidata2').formatStatementsFromLua
local Ortsfaktarat = require('Modul:Ortsfakta').rat
local namnversalisering = require('Modul:Textformatering').namnversalisering
local established = require('Modul:Ortsfakta/Aux').established

p = {}

function p.ort( entity, args )
	local divkat = ''
	local a = {}
	table.insert(a, "{{Geobox\n| Region")
	
	local namn = args['namn']
	if not namn or namn == '' then
		if entity.labels and entity.labels.sv then
			namn = entity.labels.sv.value
		else
			namn = mw.title.getCurrentTitle().text
		end
	end
	table.insert(a, "\n| name = " .. namn)
	
	
	if args['orginalnamn'] then
		table.insert(a, "\n| other_name = " .. (args['orginalnamn'] ))
	else
		local v = wikidata({entity = entity, property='P37', noref = 'true', raw = 'true'})
		local s1 = {}
		if v and #v > 0 then
			local fa = false
			local faaf = false
			local ps = false
			for i, j in pairs(v) do
				local v1 = wikidata({entityId = j.item, noref = 'true', raw = 'true', property = 'P424'})
				local wow = false
				if v1 and #v1 > 0 then
					for i1, j1 in pairs(v1) do
						if entity.labels[j1.value] then
							if not wow then
								table.insert(s1, mw.getCurrentFrame():preprocess('{{Lang|' .. j1.value .. '|' .. entity.labels[j1.value].value .. '}}' ))
								if j1.value == 'fa' then
									fa = true
								elseif j1.value == 'fa-af' then
									faaf = true
								elseif j1.value == 'ps' then
									ps = true
								end
							end
						end
					end
				end
			end
			if not ps then -- skriver alltid ut Pashto, som officiellt språk i hela Afghanistan
				if entity.labels.ps then
					table.insert(s1, mw.getCurrentFrame():preprocess('{{Lang|ps|' .. entity.labels.ps.value .. '}}' ))
				end
			end
			if not faaf then -- skriver alltid ut Dari, som officiellt språk i hela Afghanistan, har Persiska som backup
				if entity.labels['fa-af'] then
					table.insert(s1, mw.getCurrentFrame():preprocess('{{Lang|fa-af|' .. entity.labels['af-af'].value .. '}}' ))
				elseif entity.labels['fa'] and not fa then -- Persiska som backup, om det inte redan skrivits ut
					table.insert(s1, mw.getCurrentFrame():preprocess('{{Lang|fa|' .. entity.labels.fa.value .. '}}' ))
				end
			end
		else
			local ps = ''
			local faaf = ''
			local fa = ''
			if entity.labels.ps then
				ps = entity.labels.ps.value
			end
			if entity.labels['fa-af'] then
				faaf = entity.labels['fa-af'].value
			elseif entity.labels['fa'] then
				fa = entity.labels.fa.value
			end
			if ps ~= '' then
				table.insert(s1, mw.getCurrentFrame():preprocess('{{Lang|ps|' .. ps .. '}}' ))
			end
			if faaf ~= '' then
				table.insert(s1, mw.getCurrentFrame():preprocess('{{Lang|fa-af|' .. faaf .. '}}' ))
			end
			if fa ~= '' then
				table.insert(s1, mw.getCurrentFrame():preprocess('{{Lang|fa|' .. fa .. '}}' ))
			end
		end
		if #s1 > 0 then
			table.insert(a, "\n| other_name = " .. mw.text.listToText( s1, '<br/>', '<br/>' ))
		else
			divkat = divkat .. '[[Kategori:Underhållskategori för Ortsfakta WD|Σ]]'
		end
	end	
	local fd = false
	local v = wikidata({entity = entity, property = 'P31', avoidqualifier='P582', raw = 'true', noref = 'true'})
	if v[1].item == 'Q158683' then
		table.insert(a, "\n| category = [[Afghanistans provinser|Provins]]")
	elseif v[1].item == 'Q496825' then
		table.insert(a, "\n| category = [[Afghanistans distrikt|Distrikt]]")
	end
	
			
	local bild = args.bild
	local bildtext = args.bildtext
	if not bild then
		local data = wikidata({entity = entity, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data > 0 then
			bild = data[1].value
			bildtext = data[1].bildtext or data[1].motiv
		end
	end
	if bild and bild ~= '' then
		table.insert(a, "\n| image         = " .. (bild or ''))
		if bildtext then
			table.insert(a, "\n| image_caption = " .. (bildtext or ''))
		end
		if bildstorlek then
			table.insert(a, "\n| image_size    = " .. bildstorlek)
		end
	end

	local flagga = args.flagga
	if flagga and flagga ~= '' then
		table.insert(a, "\n| flag = " .. flagga)
		table.insert(a, "\n| flag_type = flagga" )
	elseif not flagga then
		local data = wikidata({entity = entity, property = 'P41', raw = 'true', noref='true', enbarten = 'true'})
		if data and #data > 0 then
			table.insert(a, "\n| flag = " .. data[1].value)
			local data2 = wikidata({entity = entity, property = 'P163', raw = 'true', noref='true', enbarten = 'true'})
			if data2 and #data2 > 0 then
				table.insert(a, "\n| flag_type = " .. data2[1].value)
			else
				table.insert(a, "\n| flag_type = flagga" )
			end
		end
	end
	
	local vapen = args.vapen or args.vapenbild
	if vapen and vapen ~= '' then
		table.insert(a, "\n| symbol      = " .. args.vapen or args.vapenbild)
		table.insert(a, "\n| symbol_type = [[Heraldiskt vapen|vapen]]")
	elseif not vapen then
		local data = wikidata({entity = entity, property = 'P94', raw = 'true', noref='true', enbarten = 'true'})
		local data2 = wikidata({entity = entity, property = 'P237', raw = 'true', noref='true', enbarten = 'true'})
		if data and #data > 0 then
			table.insert(a, "\n| symbol = " .. data[1].value)
			if data2 and #data2 > 0 then
				table.insert(a, "\n| symbol_type = " .. data2[1].value)
			else
				table.insert(a, "\n| symbol_type = [[Heraldiskt vapen|vapen]]")
			end
		end
	end

	table.insert(a, "\n| country       = Afghanistan")
	table.insert(a, "\n| country_flag  = 1")

	if args.provins and args.provins ~= '' then
		table.insert(a, "\n| state       = " .. args.provins)
		table.insert(a, "\n| state_type  = [[Afghanistans provinser|Provins]]")
		if args.distrikt and args.distrikt ~= '' then
			table.insert(a, "\n| region       = " .. args.distrikt)
			table.insert(a, "\n| region_type  = [[Afghanistans distrikt|Distrikt]]")
		end
	else
		local v = USAharva(entity, {})
		if v and #v > 0 then
			table.insert(a, "\n| state       = " .. v[#v].value )
			table.insert(a, "\n| state_type  = " .. v[#v].typ   )
			if #v > 1 then
				table.insert(a, "\n| region       = " .. v[#v-1].value)
				table.insert(a, "\n| region_type  = " .. v[#v-1].typ  )
			end
		end
	end

	if args.slogan then
		table.insert(a, "\n| motto      = " .. args.slogan )
		table.insert(a, "\n| motto_type = [[Slogan]]" )
	else
		local data = wikidata({entity = entity, property = 'P6251', noref = 'noimport'})
		if data and data ~= '' then
			table.insert(a, "\n| motto      = " .. data )
			table.insert(a, "\n| motto_type = [[Slogan]]" )
		end
	end
	
	if args.koordinat then
		table.insert(a, "\n| location = " .. args.koordinat)
	end
	
	local elevation = args['höjdläge'] 
	if elevation then
		table.insert(a, "\n| elevation = " .. elevation)
	else
		local v = wikidata({entity=entity, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
		if v and #v > 0 then
			v = v[1]
			if tonumber(v.amount) then
				table.insert(a, "\n| elevation = " .. math.abs( tonumber(v.amount)) )
				if tonumber(v.amount) < 0 then
					table.insert(a, "\n| elevation_unit = muh" )
				else
					table.insert(a, "\n| elevation_unit = möh" )
				end
				if v.ref and v.ref ~= '' then
					table.insert(a, "\n| elevation_note = " .. v.ref)
				end
			end
		end
	end

	local highest = args['högsta punkt namn'] 
	if highest then
		table.insert(a, "\n| highest = " .. highest )
		local highest_elevation = args['högsta punkt meter'] 
		if highest_elevation then
			table.insert(a, "\n| highest_elevation = " .. highest_elevation )
		end
	else
		local v = wikidata({entity=entity, property='P610', enbarten = 'true', raw = 'true'})
		if v and #v > 0 then
			table.insert(a, "\n| highest = " .. v[1].value .. v[1].ref)
			local v2 = wikidata({entityId=v[1].item, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
			if v2 and #v2 > 0 then
				if tonumber(v2[1].amount) then
					table.insert(a, "\n| highest_elevation = " .. math.abs( tonumber(v2[1].amount)) )
					if tonumber(v2[1].amount) < 0 then
						table.insert(a, "\n| highest_elevation_unit = muh" )
					else
						table.insert(a, "\n| highest_elevation_unit = möh" )
					end
					table.insert(a, "\n| highest_elevation_note = " .. v2[1].ref)
				end
			end
			v2 = wikidata({entityId=v[1].item, property='P625', enbarten = 'true', raw = 'true', noref = 'true', nogeodataparser = 'true'})
			if v2 and #v2 > 0 then
				v2 = Ortsfaktarat(v2[1], 'sekund')
				table.insert(a, "\n| highest_lat_d   = " .. (v2.lat_g or ''))
				table.insert(a, "\n| highest_lat_m   = " .. (v2.lat_m or ''))
				table.insert(a, "\n| highest_lat_s   = " .. (v2.lat_s or ''))
				table.insert(a, "\n| highest_lat_NS  = " .. (v2.lat_NS or ''))
				table.insert(a, "\n| highest_long_d  = " .. (v2.long_g or ''))
				table.insert(a, "\n| highest_long_m  = " .. (v2.long_m or ''))
				table.insert(a, "\n| highest_long_s  = " .. (v2.long_s or ''))
				table.insert(a, "\n| highest_long_EW = " .. (v2.long_EW or ''))
			end
		end
	end

	local lowest = args['lägsta punkt namn'] 
	if lowest then
		table.insert(a, "\n| lowest = " .. lowest )
		local lowest_elevation = args['lägsta punkt meter'] 
		if lowest_elevation then
			table.insert(a, "\n| lowest_elevation = " .. lowest_elevation )
		end
	else
		local v = wikidata({entity=entity, property='P1589', enbarten = 'true', raw = 'true'})
		if v and #v > 0 then
			table.insert(a, "\n| lowest = " .. v[1].value .. v[1].ref)
			local v2 = wikidata({entityId=v[1].item, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
			if v2 and #v2 > 0 then
				if tonumber(v2[1].amount) then
					table.insert(a, "\n| lowest_elevation = " .. math.abs( tonumber(v2[1].amount)) )
					if tonumber(v2[1].amount) < 0 then
						table.insert(a, "\n| lowest_elevation_unit = muh" )
					else
						table.insert(a, "\n| lowest_elevation_unit = möh" )
					end
					table.insert(a, "\n| lowest_elevation_note = " .. v2[1].ref)
				end
			end
			v2 = wikidata({entityId=v[1].item, property='P625', enbarten = 'true', raw = 'true', noref = 'true', nogeodataparser = 'true'})
			if v2 and #v2 > 0 then
				v2 = Ortsfaktarat(v2[1], 'sekund')
				table.insert(a, "\n| lowest_lat_d   = " .. (v2.lat_g or ''))
				table.insert(a, "\n| lowest_lat_m   = " .. (v2.lat_m or ''))
				table.insert(a, "\n| lowest_lat_s   = " .. (v2.lat_s or ''))
				table.insert(a, "\n| lowest_lat_NS  = " .. (v2.lat_NS or ''))
				table.insert(a, "\n| lowest_long_d  = " .. (v2.long_g or ''))
				table.insert(a, "\n| lowest_long_m  = " .. (v2.long_m or ''))
				table.insert(a, "\n| lowest_long_s  = " .. (v2.long_s or ''))
				table.insert(a, "\n| lowest_long_EW = " .. (v2.long_EW or ''))
			end
		end
	
	end

	local nordligt = nil
	if args.lat_dec or args.lat_g then
		nordligt = tonumber(args.lat_dec or args.lat_g)
		table.insert(a, "\n| lat_d   = " .. (args.lat_dec or args.lat_g))
		table.insert(a, "\n| lat_m   = " .. (args.lat_m or ''))
		table.insert(a, "\n| lat_s   = " .. (args.lat_s or ''))
		table.insert(a, "\n| lat_NS  = " .. (args.lat_NS or ''))
		table.insert(a, "\n| long_d  = " .. (args.long_dec or args.long_g or ''))
		table.insert(a, "\n| long_m  = " .. (args.long_m or ''))
		table.insert(a, "\n| long_s  = " .. (args.long_s or ''))
		table.insert(a, "\n| long_EW = " .. (args.long_EW or ''))
	elseif not args.koordinat then
		local v = wikidata({entity = entity, property = 'P625', raw = 'true', noref='true', enbarten = 'true', nogeodataparser = 'true'})
		if v and #v > 0 then
			v = Ortsfaktarat(v[1], 'minut')
			if v then
				nordligt = tonumber(v.lat_g)
				table.insert(a, "\n| lat_d   = " .. (v.lat_g or ''))
				table.insert(a, "\n| lat_m   = " .. (v.lat_m or ''))
				table.insert(a, "\n| lat_NS  = " .. (v.lat_NS or ''))
				table.insert(a, "\n| long_d  = " .. (v.long_g or ''))
				table.insert(a, "\n| long_m  = " .. (v.long_m or ''))
				table.insert(a, "\n| long_EW = " .. (v.long_EW or ''))
			end
		end
	end
	
	a = established(entity, a, args) -- hämtar uppgifter om bildande, gränsjusteringar och upplösning mm
	
	table.insert(a, '\n| timezone     = AFT | utc_offset = +4:30 | timezone_type = [[Tidszon]]')

	if args.iso3166 and args.iso3166 ~= '' then
		table.insert(a, '\n| code = ' .. args.iso3166)
		table.insert(a, '\n| code_type = [[ISO 3166-2]]')
	elseif not args.nummer then
		local data = wikidata({entity = entity, property = 'P300', noref='noref'})
		if data and #data > 0 then
			table.insert(a, '\n| code = ' .. data)
			table.insert(a, '\n| code_type = [[ISO 3166-2]]')
		end
	end
	if args['guvernör'] and args['guvernör'] ~= '' then
		ledare = true
		table.insert(a, '\n| leader2 = ' .. args['guvernör'])
		table.insert(a, '\n| leader2_type = [[Guvernör]]')
	elseif not args['guvernör'] then
		local data = wikidata({entity = entity, property = 'P6', withintervall = 'yes', modifyqualifiertime = 'Y', noref = 'noimport', raw = 'true', cat = 'true'})
		if data and #data > 0 then
			ledare = true
			if data[1].tifr then
				table.insert(a, '\n| leader2 = ' .. data[1].value .. '<small> (' .. data[1].tifr .. ')</small>')
			else
				table.insert(a, '\n| leader2 = ' .. data[1].value )
			end
			table.insert(a, '\n| leader2_type = [[Guvernör]]')
			if data[1].ref then
				table.insert(a, '\n| leader2_note = ' .. data[1].ref)
			end
		end
	end
	if ledare then
		table.insert(a, '\n| leader = &nbsp;')
		table.insert(a, '\n| leader_type = Politik')
	end

	local y1 = false
	local y2 = false

	local area = args.yta
	if area and area ~= '' then
		table.insert(a, "\n| area = " .. area)
		if args['yta_datum'] then
			table.insert(a, "\n| area_date = " .. args['yta_datum'])
		end
		if args['yta_ref'] then
			table.insert(a, "\n| area_note = " .. args['yta_ref'])
		end
		if args['yta_land'] then
			table.insert(a, "\n| area_land = " .. args['yta_land'])
			y1 = true
			if args['yta_land_datum'] then
				table.insert(a, "\n| area_land_date = " .. args['yta_land_datum'])
			end
			if args['yta_land_ref'] then
				table.insert(a, "\n| area_land_note = " .. args['yta_land_ref'])
			end
		end		
		if args['yta_vatten'] then
			table.insert(a, "\n| area_water = " .. args['yta_vatten'])
			if args['yta_vatten_datum'] then
				table.insert(a, "\n| area_water_date = " .. args['yta_vatten_datum'])
			end
			if args['yta_land_ref'] then
				table.insert(a, "\n| area_water_note = " .. args['yta_vatten_ref'])
			end
		end		
	elseif not area then
		-- area total
		local v = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q16868672', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='noimport'})		
		if not v or #v == 0 then 
			-- area total, utan qualifier om ovan inte funkade
			v = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', rank = 'valid', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='noimport'})
		end
		if v and #v > 0 then
			y1 = true
			table.insert(a, "\n| area = " .. tonumber(plus(v[1].amount)))
			table.insert(a, "\n| area_decimals = 2")
			if v[1].datum and v[1].datum ~= '' then
				table.insert(a, "\n| area_date = " .. v[1].datum)
			end
			if v[1].ref then
				table.insert(a, "\n| area_note = " .. v[1].ref)
			end
		end
		local v2 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q205895', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='true'})
		local v4 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q23397', convertunit = 'Q712226', noref='true', modifyqualifiertime = 'Y'}) -- insjövatten
		local v5 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q4022', convertunit = 'Q712226', noref='true', modifyqualifiertime = 'Y'}) -- floder
		if v2 and #v2 > 0 then
			local land = tonumber(plus(v2[1].amount))
			if v4 and #v4 > 0 then
				land = land - tonumber(plus(v4[1].amount))
			end
			if v5 and #v5 > 0 then
				land = land - tonumber(plus(v5[1].amount))
			end
			table.insert(a, "\n| area_land = " .. land )
			y1 = true
			table.insert(a, "\n| area_land_decimals = 2")
			if v2[1].datum and v2[1].datum ~= '' then
				table.insert(a, "\n| area_land_date = " .. v2[1].datum)
			end
		end
		local vatten = 0
		local v3 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q213204', convertunit = 'Q712226', noref='true', modifyqualifiertime = 'Y'})
		if v3 and #v3 > 0 then
			vatten = vatten + tonumber(plus(v3[1].amount))
		end
		if v4 and #v4 > 0 then
			vatten = vatten + tonumber(plus(v4[1].amount))
		end
		if v5 and #v5 > 0 then
			vatten = vatten + tonumber(plus(v5[1].amount))
		end
		if vatten > 0 then
			table.insert(a, "\n| area_water = " .. vatten)
			table.insert(a, "\n| area_water_decimals = 2")
			if v3[1] and v3[1].datum and v4[1] and v4[1].datum and v5[1] and v5[1].datum and v3[1].datum == v4[1].datum and v3[1].datum == v5[1].datum then
				table.insert(a, "\n| area_water_date = " .. v3[1].datum)
			end
		end
	end
		
	local inv = args['folkmängd']
	if inv and inv ~= '' then
		table.insert(a, "\n| population = " .. inv)
		y2 = true
		if args.invdatum then
			table.insert(a, "\n| population_date = " .. args['folkmängd_datum'] )
		end
		if args.invref then
			table.insert(a, "\n| population_note = " .. args['folkmängd_ref'] )
		end
	elseif not inv then
		local v = wikidata({entity = entity, property='P1082', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		if v and v[1] and v[1].amount then
			table.insert(a, "\n| population = " .. tonumber(plus(v[1].amount)))
			y2 = true
			if v[1].datum then
				table.insert(a, "\n| population_date = " .. v[1].datum)
			end
			if v[1].ref then
				table.insert(a, "\n| population_note = " .. v[1].ref)
			end
		end
		v = wikidata({entity = entity, property='P1540', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		if v and v[1] and v[1].amount then
			table.insert(a, "\n| population1 = " .. tonumber(plus(v[1].amount)))
			if v[1].datum then
				table.insert(a, "\n| population1_date = " .. v[1].datum)
			end
			if v[1].ref then
				table.insert(a, "\n| population1_note = " .. v[1].ref)
			end
			table.insert(a, "\n| population1_type = män ")
		end
		v = wikidata({entity = entity, property='P1539', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		if v and v[1] and v[1].amount then
			table.insert(a, "\n| population2 = " .. tonumber(plus(v[1].amount)))
			if v[1].datum then
				table.insert(a, "\n| population2_date = " .. v[1].datum)
			end
			if v[1].ref then
				table.insert(a, "\n| population2_note = " .. v[1].ref)
			end
			table.insert(a, "\n| population2_type = kvinnor ")
		end
	end
	
	
	if y1 and y2 then
		table.insert(a, "\n| population_density = auto")
	end

	local residensstad = args.residensstad or args.centralort
	if residensstad and residensstad ~= '' then
		table.insert(a, "\n| capital = " .. residensstad)
		table.insert(a, "\n| capital_type = [[Centralort]]")
	elseif not residensstad then
		local v = wikidata({entity = entity, property='P36', noref='true'})
		if v and v ~= '' then
			table.insert(a, "\n| capital = " .. v)
			table.insert(a, "\n| capital_type = [[Centralort]]")
		end
	end

	if args.geonames then
		table.insert(a, '\n| geonames = ' .. args.geonames)
	end

	if args.karta and args.karta ~= '' then
		table.insert(a, '\n| map           = ' .. (args.karta or ''))
		table.insert(a, '\n| map_caption   = ' .. (args.karttext or 'Översiktskarta'))
	elseif not args.karta then
		data = wikidata({entity = entity, property = 'P1943', raw = 'true', noref='true', enbarten = 'true'})
		if not data or #data == 0 then
			data = wikidata({entity = entity, property = 'P242', raw = 'true', noref='true', enbarten = 'true'})
		end
		if data and #data > 0 then 
			table.insert(a, '\n| map   = ' .. data[1].value)
			local mapcaption = 'Översiktskarta'
			table.insert(a, '\n| map_caption   = ' .. mapcaption)
		else
			table.insert(a, '\n| map   = Afghanistan location map.svg')
			table.insert(a, '\n| map_locator          = Afghanistan')
		end
	end	

	local kmlfaktaruta = require('Modul:KML faktaruta').main
	local k = kmlfaktaruta(entity)
	if k then
		table.insert(a, '\n| KML                   = ' .. k)
	end
	
	if args.fotnot and args.fotnot ~= '' then
		table.insert(a, '\n| footnotes         = ' .. args.fotnot ..  '<br />[[d:'.. entity.id .. '|Redigera Wikidata]]')
	else
		table.insert(a, '\n| footnotes         = [[d:'.. entity.id .. '|Redigera Wikidata]]')
	end

	if args.commons then
		table.insert(a, '\n| commons               = ' .. args.commons)
	else
		table.insert(a, '\n| commons               = ' .. Comcat(entity))
	end
	
	if args.hemsida and args.hemsida ~= '' then
		table.insert(a, '\n| website = ' .. args.hemsida)
	elseif not args.hemsida then
		local v = wikidata({entity = entity, property = "P856", enbarten = 'yes', noref='true'})
		if v and v ~= '' then
			table.insert(a, '\n| website = ' .. v)
		end
	end
	table.insert(a, '\n}}')
	return mw.getCurrentFrame():preprocess(table.concat(a)) .. divkat
end		


function indexera(v, i)
	if v and #v >= i then
		return v[i].value
	end
	return ''
end

function kommunqids(v)
	local s = {}
	for i, j in pairs(v) do
		table.insert(s, v.item)
	end
	return s
end



function positionskartnamn(frame) -- kartbild för länet som objektet tillhör. används av till exempel mallen "Ortsfakta Sverige WD"
   return mw.getCurrentFrame():preprocess('{{Kartposition Sverige '..lankod2lan(lankod(frame))..'|bild}}')
end 

function positionskarta(frame) -- kartlokalisering (Geolocator) för länet som objektet tillhör. används av till exempel mallen "Ortsfakta Sverige WD"
	return lankod2lan(lankod(frame))
end

function USAharva(entity, s)
	local v = wikidata({entity = entity, property = 'P131', noref = 'true', raw = 'true', avoidqualifier='P582'})
	if not v or #v == 0 or v[1].item == 'Q889' then 
		return s
	elseif #v > 1 then -- bypass en nivå om den innhåller mer än en p131
		local entity2 = mw.wikibase.getEntityObject( v[1].item )
		return USAharva( entity2, s)
	else
		local entity2 = mw.wikibase.getEntityObject( v[1].item )
		local flag = wikidata({entity = entity2, property = 'P41', noref = 'true', enbarten = 'true'})
		local p31 = wikidata({entity = entity2, property = 'P31', noref = 'true'})
		local p32 = wikidata({entity = entity2, property = 'P31', noref = 'true', raw = true})
		if test("Q158683", p32) then
			p31 = '[[Afghanistans provinser|Provins]]'
		elseif test("Q496825", p32) then
			p31 = '[[Afghanistans distrikt|Distrikt]]'
		else
			p31 = p31 .. '[[Kategori:Underhållskategori för Ortsfakta WD|B]]'
		end
		table.insert(s, {item = v[1].item, flag = flag, typ = p31, value = v[1].value}) -- 
		return USAharva( entity2, s)
	end
end


function Comcat(entity)
	if not entity or not entity.claims or not entity.claims.P373 then
		return ''
	else
		if entity.claims.P373[1].mainsnak.snaktype ~= 'value' then
			return ''
		else
			return entity.claims.P373[1].mainsnak.datavalue.value
		end
	end
end

function plus(a)
	if string.sub(a, 1, 1) == '+' then
		return string.sub(a, 2)
	end
	return a
end

return p