Home
Random
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Sandbox/Premeditated/Nobel
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
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
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Sandbox/Premeditated/Nobel/doc
(
edit
)