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



local wikidata = require('Modul:Wikidata2').formatStatementsFromLua
local wikidata2 = require('Modul:Wikidata2').formatEntityId
local Ortsfaktarat = require('Modul:Ortsfakta').rat
local HjalpSverige = require('Modul:Ortsfakta Sverige WD Grupp2').hjalp
local aux = require('Modul:Ortsfakta/Aux').aux
local ComCat = require('Modul:Comcat').com
local namnversalisering = require('Modul:Textformatering').namnversalisering

p = {}

function p.ort( entity, args )
	local a = {}
	table.insert(a, "{{Geobox\n| Settlement")
	local ortnamn = args.ortnamn 
	if not ortnamn or ortnamn == '' then
		if entity.labels and entity.labels.sv then
			ortnamn = entity.labels.sv.value 
		else
			ortnamn = mw.title.getCurrentTitle().text
		end
	end
	table.insert(a, "\n| name = " .. ortnamn)
	if args.alternativnamn then
		table.insert(a, "\n| other_name = " .. (args.alternativnamn or ''))
	end
	local form = args.form
	if form then
		if mw.ustring.lower(form) == 'tätorter' then
			form = '[[Tätort]]er'
		elseif mw.ustring.lower(form) == 'småort' then
			form = '[[Småort]]er'
		end
	end
	if not form or form == '' then
		form = "Ort"
	end
	table.insert(a, "\n| category = " .. form)
	local entitytable = {}
	local v = wikidata({entity = entity, property = 'P527', raw = 'true'})
	if v and #v > 0 then
		for i, j in pairs(v) do
			table.insert(entitytable, mw.wikibase.getEntityObject( j.item ))
		end
	end
	
	if args.smeknamn then
		table.insert(a, "\n| nickname      = " .. args.smeknamn)
		table.insert(a, "\n| nickname_type = Smeknamn")
	else
		a = nickname(a, entity, entitytable)
	end	
	if args.slogan then
		table.insert(a, "\n| motto      = " .. args.slogan)
		table.insert(a, "\n| motto_type = Slogan")
	end

	if args.bild then
		table.insert(a, "\n| image         = " .. args.bild)
		if args.bildtext then
			table.insert(a, "\n| image_caption = " .. args.bildtext)
		end
		if args.bildstorlek then
			table.insert(a, "\n| image_size    = " .. args.bildstorlek)
		end
	else
		a = image(a, entity, entitytable)
	end
	
	table.insert(a, "\n| country       = Sverige")
	table.insert(a, "\n| country_flag  = Flag of Sweden.svg")

	local landskap = args.landskap or args.landskap1
	local landskap2 = args.landskap2 or ''
	local landskap3 = args.landskap3 or ''
	if not landskap then
		local v = harva(entity, entitytable, 'Q127448', 'Q193556', 99)
		landskap =  indexera(v, 1)
		landskap2 = indexera(v, 2)
		landskap3 = indexera(v, 3)
	end
	if landskap and landskap ~= '' then
		table.insert(a, "\n| state  = " .. landskap)
		if landskap2 and landskap2 ~= '' then
			table.insert(a, "\n| state1 = " .. landskap2)
			if landskap3 and landskap3 ~= '' then
				table.insert(a, "\n| state2 = " .. landskap3)
			end
		end
		table.insert(a, "\n| state_type            = [[Landskap i Sverige|Landskap]]")
		table.insert(a, "\n| state_label           = Landskap orten ligger i")
	end
	local lan = args['län'] or args['län1'] 
	local lan2 = args['län2'] 
	local lan3 = args['län3'] 
	if not lan then
		local v = harva(entity, entitytable, 'Q127448', 'Q200547', 99)
		lan = indexera(v, 1) 
		lan2 = indexera(v, 2)
		lan3 = indexera(v, 3)
	end
	if lan and lan ~= ''  then
		table.insert(a, "\n| region  = " .. lan)
		if lan2 and lan2 ~= ''  then
			table.insert(a, "\n| region1 = " .. lan2)
			if lan3 and lan3 ~= ''  then
				table.insert(a, "\n| region2 = " .. lan3)
			end
		end
		table.insert(a, "\n| region_type            = [[Sveriges län|Län]]")
		table.insert(a, "\n| region_label           = Län orten ligger i")
	end

	local comiqs = {}
	local antalkommuner = 0
	local kommun = args['kommun'] or args['kommun1']
	local kommun2 = args['kommun2']
	local kommun3 = args['kommun3']
	local kommun4 = args['kommun4']
	local kommun5 = args['kommun5']
	local kommun6 = args['kommun6']
	local kommun7 = args['kommun7']
	local kommun8 = args['kommun8']
	local kommun9 = args['kommun9']
	local kommun10 = args['kommun10']
	local kommun11 = args['kommun11']
	local kommun12 = args['kommun12']
	local kommun13 = args['kommun13']
	local kommun14 = args['kommun14']
	local kommun15 = args['kommun15']
	if not kommun then
		local v = harva(entity, entitytable, 'Q127448', 'Q127448', 99)
		kommun   = indexera(v, 1)
		kommun2  = indexera(v, 2)
		kommun3  = indexera(v, 3)
		kommun4  = indexera(v, 4)
		kommun5  = indexera(v, 5)
		kommun6  = indexera(v, 6)
		kommun7  = indexera(v, 7)
		kommun8  = indexera(v, 8)
		kommun9  = indexera(v, 9)
		kommun10 = indexera(v, 10)
		kommun11 = indexera(v, 11)
		kommun12 = indexera(v, 12)
		kommun13 = indexera(v, 13)
		kommun14 = indexera(v, 14)
		kommun15 = indexera(v, 15)
	end
	
	if kommun and kommun ~= '' then
		table.insert(a, "\n| municipality = " .. kommun)
		antalkommuner = 1
		if kommun2 and kommun2 ~= '' then
			table.insert(a, "\n| municipality1 = " .. kommun2)
			antalkommuner = 2
			if kommun3 and kommun3 ~= '' then
				table.insert(a, "\n| municipality2 = " .. kommun3)
				antalkommuner = 3
				if kommun4 and kommun4 ~= '' then
					table.insert(a, "\n| municipality3 = " .. kommun4)
					antalkommuner = 4
					if kommun5 and kommun5 ~= '' then
						table.insert(a, "\n| municipality4 = " .. kommun5)
						antalkommuner = 5
						if kommun6 and kommun6 ~= '' then
							table.insert(a, "\n| municipality5 = " .. kommun6)
							antalkommuner = 6
							if kommun7 and kommun7 ~= '' then
								table.insert(a, "\n| municipality6 = " .. kommun7)
								antalkommuner = 7
								if kommun8 and kommun8 ~= '' then
									table.insert(a, "\n| municipality7 = " .. kommun8)
									antalkommuner = 8
									if kommun9 and kommun9 ~= '' then
										table.insert(a, "\n| municipality8 = " .. kommun9)
										antalkommuner = 9
										if kommun10 and kommun10 ~= '' then
											table.insert(a, "\n| municipality9 = " .. kommun10)
											antalkommuner = 10
											if kommun11 and kommun11 ~= '' then
												table.insert(a, "\n| municipality10 = " .. kommun11)
												antalkommuner = 11
												if kommun12 and kommun12 ~= '' then
													table.insert(a, "\n| municipality11 = " .. kommun12)
													antalkommuner = 12
													if kommun13 and kommun13 ~= '' then
														table.insert(a, "\n| municipality12 = " .. kommun13)
														antalkommuner = 13
														if kommun14 and kommun14 ~= '' then
															table.insert(a, "\n| municipality13 = " .. kommun14)
															antalkommuner = 14
															if kommun15 and kommun15 ~= '' then
																table.insert(a, "\n| municipality14 = " .. kommun15)
																antalkommuner = 15
															end
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end							
			end
		end
		if kommun2 and kommun2 ~= '' then
			table.insert(a, "\n| municipality_type = [[Sveriges kommuner|Kommuner]]")
			table.insert(a, "\n| municipality_label    = Kommuner orten ligger i")
		else
			table.insert(a, "\n| municipality_type = [[Sveriges kommuner|Kommun]]")
			table.insert(a, "\n| municipality_label    = Kommun orten ligger i")
		end
	end	

		
	
	local distrikt = args.distrikt or args.distrikt1
	local distrikt2 = args.distrikt2
	local distrikt3 = args.distrikt3
	if not distrikt then
		local v = harva(entity, entitytable, 'Q18333556', 'Q18333556', 3)
		distrikt = indexera(v, 1)
		distrikt2 = indexera(v, 2)
		distrikt3 = indexera(v, 3)
	end
	if distrikt and distrikt ~= '' then
		table.insert(a, "\n| parish = " .. distrikt)
		if distrikt2 and distrikt2 ~= '' then
			table.insert(a, "\n| parish1 = " .. distrikt2)
			table.insert(a, "\n| parish2 = " .. distrikt3)
		end
		table.insert(a, "\n| parish_type           = [[Sveriges distrikt|Distrikt]]")
		table.insert(a, "\n| parish_label          = Distrikt orten ligger i")
	end

	if args['höjdläge'] then
		table.insert(a, "\n| elevation = " .. args['höjdläge'])
	else
		table.insert(a, hojdlage(entity, entitytable))
	end
	
	if args.koordinat then
		table.insert(a, "\n| location = " .. args.koordinat)
	end
	

	if args['högsta punkt namn'] then
		table.insert(a, "\n| highest = " .. args['högsta punkt namn'] )
		if args['högsta punkt meter']  then
			table.insert(a, "\n| highest_elevation = " .. args['högsta punkt meter']  )
		end
	else
		table.insert(a, hogstapunkt(entity, entitytable, '+'))
	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
		table.insert(a, hogstapunkt(entity, entitytable, '-'))
	end

	if args.lat_dec or args.lat_g then
		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 = koordfix(entity, entitytable)
		koordentity = v.entity
		if v then
			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_s   = " .. (v.lat_s 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_s  = " .. (v.long_s or ''))
			table.insert(a, "\n| long_EW = " .. (v.long_EW or ''))
		end
	end


	table.insert(a, HjalpSverige(entity, entitytable, args, ortnamn))
	
	if args.grundades then
		table.insert(a, "\n| established = " .. args.grundades)
	else
		table.insert(a, grund(entity, entitytable))
	end
	
	table.insert(a, '\n| timezone     = [[Centraleuropeisk tid|CET]] | utc_offset = +1 | timezone_type = [[Tidszon]]')
	table.insert(a, '\n| timezone_DST = [[Centraleuropeisk tid|CEST]]| utc_offset_DST = +2')
	
	if args.postnummer then
		table.insert(a, '\n| postal_code = ' .. args.postnummer)
		table.insert(a, '\n| postal_code_type = [[Postnummer i Sverige|Postnummer]]')
	else
		table.insert(a, postnummer(entity, entitytable))
	end
		
	if args.postort then
		table.insert(a, '\n| postal_town = ' .. namnversalisering(args.postort))
		table.insert(a, '\n| postal_town_type = [[Postort]]')
		if args.postort_not then
			table.insert(a, '\n| postal_town_note = ' .. args.postort_not)
		end
	end
	
	if args.riktnummer then
		table.insert(a, '\n| area_code = ' .. args.riktnummer)
		table.insert(a, '\n| area_code_type = [[Riktnummer]]')
	else
		table.insert(a, riktnummer(entity, entitytable))
	end

	if args['tätortskod'] or args['tätortskod1'] then
		table.insert(a, '\n| code_type = [[Tätortskod]]')
		if args['tätortskod'] then
			table.insert(a, '\n| code      = ' .. args['tätortskod'])
		end
		if args['tätortskod1'] then
			table.insert(a, '\n| codea      = ' .. args['tätortskod1'])
			table.insert(a, '\n| codea_type = ' .. args['tätortskod1_text'])
			if args['tätortskod2'] then
				table.insert(a, '\n| codeb      = ' .. args['tätortskod2'])
				table.insert(a, '\n| codeb_type = ' .. args['tätortskod2_text'])
				if args['tätortskod3'] then
					table.insert(a, '\n| codec      = ' .. args['tätortskod3'])
					table.insert(a, '\n| codec_type = ' .. args['tätortskod3_text'])
				end
			end
		end
	else
		table.insert(a, tatort(entity, entitytable))
	end
	
	if args['småortskod'] or args['småortskod1'] then
		table.insert(a, '\n| code1_type = [[Småortskod]]')
		if args['småortskod'] then
			table.insert(a, '\n| code1      = ' .. args['småortskod'])
		end
		if args['småortskod1'] then
			table.insert(a, '\n| code1a      = ' .. args['småortskod1'])
			table.insert(a, '\n| code1a_type = ' .. args['småortskod1_text'])
			if args['småortskod2'] then
				table.insert(a, '\n| code1b      = ' .. args['småortskod2'])
				table.insert(a, '\n| code1b_type = ' .. args['småortskod2_text'])
				if args['småortskod3'] then
					table.insert(a, '\n| code1c      = ' .. args['småortskod3'])
					table.insert(a, '\n| code1c_type = ' .. args['småortskod3_text'])
				end
			end
		end
	else
		table.insert(a, smaort(entity, entitytable))
	end
	if args['fritidshusområdeskod'] or args['fritidshusområdeskod1'] then
		table.insert(a, '\n| code2_type = [[Fritidshusområde|Fritidshusomr.kod]]')
		if args['fritidshusområdeskod'] then
			table.insert(a, '\n| code2      = ' .. args['fritidshusområdeskod'])
		end
		if args['fritidshusområdeskod1'] then
			table.insert(a, '\n| code2a      = ' .. args['fritidshusområdeskod1'])
			table.insert(a, '\n| code2a_type = ' .. args['fritidshusområdeskod1_text'])
			if args['fritidshusområdeskod2'] then
				table.insert(a, '\n| code2b      = ' .. args['fritidshusområdeskod2'])
				table.insert(a, '\n| code2b_type = ' .. args['fritidshusområdeskod2_text'])
				if args['fritidshusområdeskod3'] then
					table.insert(a, '\n| code2c      = ' .. args['fritidshusområdeskod3'])
					table.insert(a, '\n| code2c_type = ' .. args['fritidshusområdeskod3_text'])
				end
			end
		end
	else
		table.insert(a, fritid(entity, entitytable))
	end

	if args.statistikkod then
		table.insert(a, '\n| code3      = ' .. args.statistikkod)
		table.insert(a, '\n| code3_type = Statistikkod')
	end
	
	if args.geonames then
		table.insert(a, '\n| geonames = ' .. args.geonames)
	else
		table.insert(a, geonames(entity, entitytable))
	end
			

	if args.positionskarta then
		table.insert(a, '\n| map           = ' .. (args.positionskartnamn or ''))
		table.insert(a, '\n| map_caption   = ' .. (args.karttext or ''))
		table.insert(a, '\n| map_size      = ' .. (args.kartstorlek or ''))
		if args['prickpos x'] then
			table.insert(a, '\n| map_locator_x = ' .. (args['prickpos x'] or ''))
			table.insert(a, '\n| map_locator_y = ' .. (args['prickpos y'] or ''))
		elseif args.positionskarta then
			table.insert(a, '\n| map_locator   = ' .. args.positionskarta)
		end
	elseif args['prickpos x'] then
		table.insert(a, '\n| map           = Sverigekarta infobox.svg')
		table.insert(a, '\n| map_caption   = ' .. (args.karttext or ''))
		table.insert(a, '\n| map_size      = ' .. (args.kartstorlek or ''))
		table.insert(a, '\n| map_locator_x = ' .. (args['prickpos x'] or ''))
		table.insert(a, '\n| map_locator_y = ' .. (args['prickpos y'] or ''))
	else
		local s1 = positionskartnamn(entity, entitytable)
		local s2 = positionskarta(entity, entitytable)
		table.insert(a, '\n| map           = ' .. s1)
		local c = {['Uppsala'] = true,	['Kalmar'] = true, ['Blekinge'] = true, ['Skåne'] = true, ['Örebro'] = true}
		
		if s2 == '' then
			table.insert(a, '\n| map_caption   = Ortens läge i Sverige')
		elseif c[s2] then
			table.insert(a, '\n| map_caption   = Ortens läge i ' .. s2 .. ' län')
		else
			table.insert(a, '\n| map_caption   = Ortens läge i ' .. s2 .. 's län')
		end
		if not s2 or s2 == '' then
			s2 = 'Sverige'
		end
		table.insert(a, '\n| map_locator   = ' .. s2)
	end

	if args.karta then
		table.insert(a, '\n| | map2            = ' .. args.karta)
		if args.karttext2 then
			table.insert(a, '\n| map2_caption  = ' .. args.karttext2)
		end
		if args.kartstorlek2 then
			table.insert(a, '\n| map2_size     = ' .. args.kartstorlek2)
		end
	end
	table.insert(a, '\n| KML                   = ' .. mw.getCurrentFrame():preprocess('{{KML faktaruta}}'))
	if args.fotnot then
		table.insert(a, '\n| footnotes         = ' .. args.fotnot .. '<br/>Redigera [[d:'.. entity.id .. '|Wikidata]]')
	else
		table.insert(a, '\n| footnotes         = Redigera [[d:'.. entity.id .. '|Wikidata]]')
	end

	if args.commons then
		table.insert(a, '\n| commons               = ' .. args.commons)
	else
		local v = commonsense(entity, entitytable)
		if v and v ~= '' then
			table.insert(a, '\n| commons               = ' .. v)
		end
	end

	table.insert(a, "\n}}")
	
	return mw.getCurrentFrame():preprocess(table.concat(a))
end		

function commonsense(entity, entitytable)
	local a = Comcat(entity)
	if a and a ~= '' then
		return a
	else
		for i, j in pairs(entitytable) do
			local b = Comcat(j)
			if b and b ~= '' then
				return b
			end
		end
	end
	return nil
end

function postnummer(entity, entitytable)
	local a = {}
	local s = wikidata({entity = entity, property = "P281"})
	if s and s ~= '' then
		table.insert(a, '\n| postal_code = ' .. s)
		table.insert(a, '\n| postal_code_type = [[Postnummer i Sverige|Postnummer]]')
		return table.concat(a)
	else
		local b = {}
		for i, j in pairs(entitytable) do
			local s2 = wikidata({entity = j, property = "P281"})
			if s2 and s2 ~= '' then
				table.insert(a, '\n| postal_code = ' .. s2)
				table.insert(a, '\n| postal_code_type = [[Postnummer i Sverige|Postnummer]]')
				return table.concat(a)
			end
		end
	end
	return ''
end
	
function riktnummer(entity, entitytable)
	local a = {}
	local s = wikidata({entity = entity, property = "P473"})
	if s and s ~= '' then
		table.insert(a, '\n| area_code = ' .. s)
		table.insert(a, '\n| area_code_type = [[Riktnummer]]')
		return table.concat(a)
	else
		local b = {}
		for i, j in pairs(entitytable) do
			local s2 = wikidata({entity = j, property = "P473"})
			if s2 and s2 ~= '' then
				table.insert(a, '\n| area_code = ' .. s2)
				table.insert(a, '\n| area_code_type = [[Riktnummer]]')
				return table.concat(a)
			end
		end
	end
	return ''
end

function indexera(v, i)
	if v and #v >= i then
		if v[i].pr and v[i].pr ~= '' then
			return '[[File:' .. v[i].pr .. '|15px]]&nbsp;' .. v[i].value
		end
		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 lankod2lan(lankod)
local lan = ''
if lankod == '01' then lan = 'Stockholm'
	elseif lankod == '03' then lan = 'Uppsala'
	elseif lankod == '04' then lan = 'Södermanland'
	elseif lankod == '05' then lan = 'Östergötland'
	elseif lankod == '06' then lan = 'Jönköping'
	elseif lankod == '07' then lan = 'Kronoberg'
	elseif lankod == '08' then lan = 'Kalmar'
	elseif lankod == '09' then lan = 'Gotland'
	elseif lankod == '10' then lan = 'Blekinge'	
    elseif lankod == '12' then lan = 'Skåne'
    elseif lankod == '13' then lan = 'Halland'
	elseif lankod == '14' then lan = 'Västra Götaland'
	elseif lankod == '17' then lan = 'Värmland'
	elseif lankod == '18' then lan = 'Örebro'
	elseif lankod == '19' then lan = 'Västmanland'
	elseif lankod == '20' then lan = 'Dalarna'
	elseif lankod == '21' then lan = 'Gävleborg'
	elseif lankod == '22' then lan = 'Västernorrland'
	elseif lankod == '23' then lan = 'Jämtland'
	elseif lankod == '24' then lan = 'Västerbotten'
	elseif lankod == '25' then lan = 'Norrbotten'
	else lan = ''
end
return lan
end

function lankod(entity) -- hämtar länskod från i första hand det län och i andra hand den kommun som objektet ingår i
	local INOM_ADMINISTRATIVT_OMRADE = 'P131'
	local               SVERIGES_LAN = 'Q200547'
	local          SVERIGES_KOMMUNER = 'Q127448'
	local                    LANSKOD = 'P507'
	local        KOMMUNKOD_I_SVERIGE = 'P525'
	local t = {}
	t = wikidata({entity = entity, property=INOM_ADMINISTRATIVT_OMRADE,noref='ja',nolink='ja',getonly=SVERIGES_LAN,raw='ja'}) -- hämta län som objektet ingår i 
	if t and t[1] then 
    	return wikidata({property=LANSKOD,noref='ja',nolink='ja',entityId=t[1].item}) -- returnera länskoden 
	end
	t = wikidata({entity = entity, property=INOM_ADMINISTRATIVT_OMRADE,noref='ja',nolink='ja',getonly=SVERIGES_KOMMUNER,raw='ja'}) -- hämta kommun om län ej var angivet
	if t and t[1] then
		return mw.ustring.sub(wikidata({property=KOMMUNKOD_I_SVERIGE,noref='ja',nolink='ja',entityId=t[1].item}),1,2) -- returnera de två första siffrorna i kommunkoden	
	end
	return ''
end

function positionskartnamn(frame, frame2) -- kartbild för länet som objektet tillhör. används av till exempel mallen "Ortsfakta Sverige WD"
	local a = lankod(frame)
	if a and a ~= '' then
		return mw.getCurrentFrame():preprocess('{{Kartposition Sverige '..lankod2lan(a)..'|bild}}')
	else
		for i, j in pairs(frame2) do
			local b = lankod(j)
			if b and b ~= '' then
				return mw.getCurrentFrame():preprocess('{{Kartposition Sverige '..lankod2lan(b)..'|bild}}')
			end
		end
	end
	return ''
end 

function positionskarta(frame, frame2) -- kartlokalisering (Geolocator) för länet som objektet tillhör. används av till exempel mallen "Ortsfakta Sverige WD"
	local a = lankod2lan(lankod(frame))
	if a and a ~= '' then
		return a
	else
		for i, j in pairs(frame2) do
			local b = lankod2lan(lankod(j))
			if b and b ~= '' then
				return b
			end
		end
	end
	return ''
end

function harva(entity, entitytable, first, second, limit)
	local l = wikidata({entity = entity, property = "P131", noref='true', avoidqualifier='P582', getonly=second, raw = 'true', getsimpleproperty = '15px', getproperty = 'P94', getenbarten = 'true'})
	if l and #l > limit then 
		return {}
	end
	if l and #l > 0 then
		return l
	else 
		l = wikidata({entity = entity, property = "P131", noref='true', avoidqualifier='P582', getonly=first, raw = 'true', getsimpleproperty = '15px', getproperty = 'P94', getenbarten = 'true'})
		if l and #l > limit then 
			return {}
		end
		local b = {}
		if l and #l > 0 then
			for i3, j3 in pairs(l) do
				l2 = wikidata({entityId = j3.item, property = "P131", noref='true', avoidqualifier='P582', getonly=second, raw = 'true', getsimpleproperty = '15px', getproperty = 'P94', getenbarten = 'true'})
				for i, j in pairs(l2) do
					local f = false
					for i2, j2 in pairs(b) do
						if j.item == j2.item then
							f = true
						end
					end
					if not f then
						table.insert(b, j)
					end
				end
			end
		end
		if #b > limit then 
			return {}
		elseif #b > 0 then
			return b
		else
			for i, j in pairs(entitytable) do
				local m = harva(j, {}, first, second, limit)
				if m and #m > 0 then
					for i2, j2 in pairs(m) do
						local f = false
						for i3, j3 in pairs(b) do
							if j3.item == j2.item then
								f = true
							end
						end
						if not f then
							table.insert(b, j2)
						end
					end
				end
			end
			if #b > limit then
				return {}
			else
				return b
			end
		end
	end
	return {}
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 nickname(a, entity, entitytable)
	local s = {}
	local v = wikidata({entity = entity, property = 'P1449', raw = 'true', noref='noimport'})
	if v and #v > 0 then
		for i, j in pairs(v) do
			table.insert(s, j.value .. j.ref)
		end
	end
	if #s > 0 then
		table.insert(a, "\n| nickname      = " .. table.concat(s, ", "))
		table.insert(a, "\n| nickname_type = Smeknamn")
		return a
	else
		for i, j in pairs(entitytable) do
			v = wikidata({entity = j, property = 'P1449', raw = 'true', noref='noimport'})
			if v and #v > 0 then
				for i2, j2 in pairs(v) do
					table.insert(s, j2.value .. j2.ref)
				end
			end
		end
		if #s > 0 then
			table.insert(a, "\n| nickname      = " .. table.concat(s, ", "))
			table.insert(a, "\n| nickname_type = Smeknamn")
			return a
		end
	end
	return a
end

function image(a, entity, entitytable)
	local v = wikidata({entity = entity, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
	if v and #v > 0 then
		table.insert(a, '\n| image             = ' .. v[1].value)
		if v[1].bildtext then
			table.insert(a, '\n| image_caption = ' .. v[1].bildtext)
		elseif v[1].motiv then
			table.insert(a, '\n| image_caption = ' .. v[1].motiv)
		end
		return a
	end
	for i, j in pairs(entitytable) do
		v =  wikidata({entity = j, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if v and #v > 0 then
			table.insert(a, '\n| image             = ' .. v[1].value)
			if v[1].bildtext then
				table.insert(a, '\n| image_caption = ' .. v[1].bildtext)
			end
			return a
		end
	end
	return a
end

function hojdlage(entity, entitytable)
	local a = {}
	local v = wikidata({entity=entity, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
	if v and #v > 0 then
		table.insert(a, "\n| elevation = " .. math.abs( tonumber(v[1].amount)) )
		if tonumber(v[1].amount) < 0 then
			table.insert(a, "\n| elevation_unit = muh" )
		else
			table.insert(a, "\n| elevation_unit = möh" )
		end
		if v[1].ref and v[1].ref ~= '' then
			table.insert(a, "\n| elevation_note = " .. v[1].ref)
		end
		return table.concat(a)
	end
	local ent1 = nil
	local lowest = 100000000000000000000
	for i, j in pairs(entitytable) do
		local v2 = wikidata({entity = j, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref = 'true'})
		if v2 and #v2 > 0 and tonumber(v2[1].amount) < lowest then
			lowest = tonumber(v2[1].amount)
			ent1 = j
		end
	end
	if ent1 then
		local v2 = wikidata({entity = ent1, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
		table.insert(a, "\n| elevation = " .. math.abs( tonumber(v2[1].amount)) )
		if tonumber(v2[1].amount) < 0 then
			table.insert(a, "\n| elevation_unit = muh" )
		else
			table.insert(a, "\n| elevation_unit = möh" )
		end
		if v2[1].ref and v2[1].ref ~= '' then
			table.insert(a, "\n| elevation_note = " .. v2[1].ref)
		end
		return table.concat(a)
	end
	return table.concat(a)
end

function koordfix(entity, entitytable)
	local v = wikidata({entity = entity, property = 'P625', raw = 'true', noref='true', enbarten = 'true', nogeodataparser = 'true'})
	if v and #v > 0 then
		return Ortsfaktarat(v[1], 'sekund')
	else
		for i, j in pairs(entitytable) do
			v = wikidata({entity = j, property = 'P625', raw = 'true', noref='true', enbarten = 'true', nogeodataparser = 'true'})
			if v and #v > 0 then
				return Ortsfaktarat(v[1], 'sekund')
			end
		end
	end	
	return nil
end

function hogstapunkt(entity, entitytable, typ)
	local a = {}
	local prop = nil
	local quack = nil
	if typ == '-' then -- Lowest point
		quack = 1000000000000000 
		prop = 'P1589'
	else -- Highest point
		quack = -1000000000000000
		prop = 'P610'
	end
	local v = wikidata({entity=entity, property='P610', enbarten = 'true', raw = 'true', noref='noimport'})
	if v and #v > 0 then
		if typ == '-' then
			table.insert(a, "\n| lowest = " .. v[1].value .. v[1].ref)
		else
			table.insert(a, "\n| highest = " .. v[1].value .. v[1].ref)
		end
		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
				if typ == '-' then
					table.insert(a, "\n| lowest_elevation = " .. math.abs( tonumber(v2[1].amount)) )
				else
					table.insert(a, "\n| highest_elevation = " .. math.abs( tonumber(v2[1].amount)) )
				end
				if tonumber(v2[1].amount) < 0 then
					if typ == '-' then
						table.insert(a, "\n| lowest_elevation_unit = muh" )
					else
						table.insert(a, "\n| highest_elevation_unit = muh" )
					end
				else
					if typ == '-' then
						table.insert(a, "\n| lowest_elevation_unit = möh" )
					else
						table.insert(a, "\n| highest_elevation_unit = möh" )
					end
				end
				if typ == '-' then
					table.insert(a, "\n| lowest_elevation_note = " .. v2[1].ref)
				else
					table.insert(a, "\n| highest_elevation_note = " .. v2[1].ref)
				end
			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')
			if typ == '-' then
				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 ''))
			else
				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
		
		return table.concat(a)
	else
		local ent1 = nil
		local ent2 = nil
		for i, j in pairs(entitytable) do
			local v = wikidata({entity = j, property = prop, enbarten = 'true', raw = 'true', noref = 'true'})
			if v and #v > 0 then
				if not ent2 then -- backup om det inte finns någon höjduppgift i något objekt
					ent2 = j
				end
				local v2 = wikidata({entityId=v[1].item, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref = 'true'})
				if v2 and #v2 > 0 and tonumber(v2[1].amount) then
					if typ == '-' then
						if tonumber(v2[1].amount) < quack then
							quack = tonumber(v2[1].amount)
							ent1 = j
						end
					else
						if tonumber(v2[1].amount) > quack then
							quack = tonumber(v2[1].amount)
							ent1 = j
						end
					end
				end		
			end
		end
		if ent1 or ent2 then
			local v = wikidata({entity = (ent1 or ent2), property = prop, enbarten = 'true', raw = 'true', noref='noimport'})
			if v and #v > 0 then
				if typ == '-' then
					table.insert(a, "\n| lowest = " .. v[1].value .. v[1].ref)
				else
					table.insert(a, "\n| highest = " .. v[1].value .. v[1].ref)
				end
				local v2 = wikidata({entityId=v[1].item, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
				if v2 and #v2 > 0 and tonumber(v2[1].amount) then
					if typ == '-' then
						table.insert(a, "\n| lowest_elevation = " .. math.abs( tonumber(v2[1].amount)) )
					else
						table.insert(a, "\n| highest_elevation = " .. math.abs( tonumber(v2[1].amount)) )
					end
					if tonumber(v2[1].amount) < 0 then
						if typ == '-' then
							table.insert(a, "\n| lowest_elevation_unit = muh" )
						else
							table.insert(a, "\n| highest_elevation_unit = muh" )
						end
					else
						if typ == '-' then
							table.insert(a, "\n| lowest_elevation_unit = möh" )
						else
							table.insert(a, "\n| highest_elevation_unit = möh" )
						end
					end
					if typ == '-' then
						table.insert(a, "\n| lowest_elevation_note = " .. v2[1].ref)
					else
						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')
					if typ == '-' then
						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 ''))
					else
						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
				return table.concat(a)
			end
		end
	end
	return nil
end

function grund(entity, entitytable)
	local a = {}
	local v = wikidata({entity = entity, property = 'P571', raw = 'true', som = 'true'})
	if v and #v > 0 then
		table.insert(a, '\n| established  = ' .. v[1].value)
		return table.concat(a)
	end
	local b = {}
	for i, j in pairs(entitytable) do
		v =  wikidata({entity = j, property = 'P571', raw = 'true', raw='true', som = 'true', noref='noimport'})
		if v and #v > 0 then
			for i2, j2 in pairs(v) do
				table.insert(b, j2.value .. j2.ref)
			end
		end
	end
	if #b > 0 then
		table.insert(a, '\n| established = ' .. mw.text.listToText(b, '<br/>', '<br/>'))
	end
	return table.concat(a)
end

function tatort(entity, entitytable)
	local a = {}
	local v = wikidata({entity = entity, property = 'P775', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>', rank = 'valid' })
	if v and v ~= '' then
		table.insert(a, '\n| code_type = [[Tätortskod]]')
		table.insert(a, '\n| code      = ' .. v)
		return table.concat(a)
	end
	local b = {}
	local b2 = {}
	local extra = 1
	for i, j in pairs(entitytable) do
		v =  wikidata({entity = j, property = 'P775', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>', rank = 'valid' })
		if v and v ~= '' then
			table.insert(b, '\n| code_' .. extra .. ' = ' .. v)
			table.insert(b, '\n| code_' .. extra .. '_type = ' .. aux(j.id, labb(j)))
			table.insert(b2, v)
			extra = extra + 1 
		end
	end
	if extra == 2 then
		table.insert(a, '\n| code_type = [[Tätortskod]]')
		table.insert(a, '\n| code      = ' .. table.concat(b2))
	elseif extra > 2 then
		table.insert(a, '\n| code_type = [[Tätortskod]]')
		table.insert(a, table.concat(b))
	end
	return table.concat(a)
end

function smaort(entity, entitytable)
	local a = {}
	local v = wikidata({entity = entity, property = 'P776', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>', rank = 'valid' })
	if v and v ~= '' then
		table.insert(a, '\n| code1_type = [[Småortskod]]')
		table.insert(a, '\n| code1      = ' .. v)
		return table.concat(a)
	end
	local b = {}
	local b2 = {}
	local extra = 1
	for i, j in pairs(entitytable) do
		v =  wikidata({entity = j, property = 'P776', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>', rank = 'valid' })
		if v and v ~= '' then
			table.insert(b, '\n| code1_' .. extra .. ' = ' .. v)
			table.insert(b, '\n| code1_' .. extra .. '_type = ' .. aux(j.id, labb(j)))
			table.insert(b2, v)
			extra = extra + 1 
		end
	end
	if extra == 2 then
		table.insert(a, '\n| code1_type = [[Småortskod]]')
		table.insert(a, '\n| code1      = ' .. table.concat(b2))
	elseif extra > 2 then
		table.insert(a, '\n| code1_type = [[Småortskod]]')
		table.insert(a, table.concat(b))
	end
	return table.concat(a)
end

function fritid(entity, entitytable)
	local a = {}
	local v = wikidata({entity = entity, property = 'P980', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>', rank = 'valid' })
	if v and v ~= '' then
		table.insert(a, '\n| code2_type = [[Fritidshusområde|Fritidshusomr.kod]]')
		table.insert(a, '\n| code2      = ' .. v)
		return table.concat(a)
	end
	local b = {}
	local b2 = {}
	local extra = 1
	for i, j in pairs(entitytable) do
		v =  wikidata({entity = j, property = 'P980', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>', rank = 'valid' })
		if v and v ~= '' then
			table.insert(b, '\n| code2_' .. extra .. ' = ' .. v)
			table.insert(b, '\n| code2_' .. extra .. '_type = ' .. aux(j.id, labb(j)))
			table.insert(b2, v)
			extra = extra + 1 
		end
	end
	if extra == 2 then
		table.insert(a, '\n| code2_type = [[Fritidshusområde|Fritidshusomr.kod]]')
		table.insert(a, '\n| code2      = ' .. table.concat(b2))
	elseif extra > 2 then
		table.insert(a, '\n| code2_type = [[Fritidshusområde|Fritidshusomr.kod]]')
		table.insert(a, table.concat(b))
	end
	return table.concat(a)
end

function geonames(entity, entitytable)
	if entity.claims and entity.claims.P1566 then
		return nil
	end
	local a = {}
	for i, j in pairs(entitytable) do
		if j.claims and j.claims.P1566 then
			for i2, j2 in pairs(j.claims.P1566) do
				if j2.mainsnak.snaktype == 'value' and j.rank ~= 'deprecated' then
					table.insert(a, j2.mainsnak.datavalue.value)
				end
			end
		end
	end
	if #a > 0 then
		return '\n| geonames = ' .. table.concat(a, ',')
	end
	return nil
end

function labb(entity)
	if entity.labels then
		if entity.labels.sv then
			return entity.labels.sv.value
		end
	else
		if entity.labels.en then
			return entity.labels.en.value
		end
	end
	return nil
end

return p