Module:Sandbox/Premeditated/Nobel

.mw-parser-output .documentation,.mw-parser-output .documentation-metadata{border:1px solid var(--border-color-base,#a2a9b1);background-color:#ecfcf4;color:inhe

Module:Sandbox/Premeditated/Nobel
local wd = require('Module:Wd')

local p = {}

local function comma_to_dots(x)
	if x then
		res, _ = string.gsub(x, "%,", ".")
		return tonumber(res) or x
	end
	return ''
end

local function find(f, l)
  for _, v in pairs(l) do
    if v == f then
      return true
    end
  end
  return false
end

local function get_unit(result)
	local allowed_lang = {"nb", "nn", "en"}
	local lang = {}

	for index, type_unit in pairs(mw.wikibase.getBestStatements( result, 'P5061' )) do
    	if find(type_unit["mainsnak"]["datavalue"]["value"]["language"], allowed_lang) then
    		lang[type_unit["mainsnak"]["datavalue"]["value"]["language"]] = type_unit["mainsnak"]["datavalue"]["value"]["text"]
		end
	end

	if lang["nb"] then
    	return lang["nb"]
	elseif lang["nn"] then
    	return lang["nn"]
	elseif lang["en"] then
    	return lang["en"]
	else
    	return mw.wikibase.getLabel( result )
	end
end

local function round(num, numDecimalPlaces)
  return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

local function find_population_density(popu, area, item_p)
	local population_density = round(popu/area, 2)
	local type_SI
	if item_p then
		claims = mw.wikibase.getBestStatements( item_p, 'P2046' )
		result = claims[1].mainsnak.datavalue.value.unit
		result = mw.ustring.sub(result, mw.ustring.find(result, "Q"), -1)
		type_SI = '[[' .. mw.wikibase.getSitelink( result ) .. '|' .. get_unit(result) .. ']]'
	else
		type_SI = "[[kvadratkilometer|km²]]"
	end
		
	return mw.language.getContentLanguage():formatNum(population_density) .. " [[Innbygger|innb.]]/" .. type_SI
end

local function wd_match(item_p, pid, last_val)
	
	if wd._property{item_p, pid} ~= "" then
		if pid == "P2046" then
			return find_population_density(last_val, tonumber(wd._property{'raw','normal+', item_p, 'P2046'}), item_p)
		elseif pid == "P1082" then
			return find_population_density(tonumber(wd._property{'raw','normal+', item_p, 'P1082'}), last_val)
		end
	end
	
	if wd._property{item_p,'P1082'} ~= "" and wd._property{item_p,'P2046'} ~= "" then
		return find_population_density(tonumber(wd._property{'raw','normal+', item_p, 'P1082'}), tonumber(wd._property{'raw','normal+', item_p, 'P2046'}), item_p)
	end
	
	return ''
end

function p.main(frame)
	local area = comma_to_dots(frame.args[1]) --P2046
	local population = comma_to_dots(frame.args[2]) --P1082
	local population_density = comma_to_dots(frame.args[3])
	local item_p = mw.wikibase.getEntityIdForCurrentPage()
	local item_p = "Q55"
	
	if population_density == "uten" then
		return ''
	end
	
	if area == '' and population == '' and population_density == '' then
		return wd_match(item_p)
	end
	
	if population_density ~= '' then
		if type(population_density) == "number" then
			return mw.language.getContentLanguage():formatNum(population_density) .. " [[Innbygger|innb.]]/[[kvadratkilometer|km²]]"
		else
			return population_density .. " [[Innbygger|innb.]]/[[kvadratkilometer|km²]]"
		end
	end
	
	if type(area) == "number" and type(population) == "number" then
		return find_population_density(population, area)
	end
	
	if type(area) == "number" and population == '' then
		return wd_match(item_p, 'P1082', area)
	end
	
	if type(population) == "number" and area == '' then
		return wd_match(item_p, 'P2046', population)
	end
	
	return "[[Kategori:Sider med feil for befolkningstetthet]]"
	
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.