Module:Italian municipality
Appearance
| This module is rated as alpha. It is ready for limited use and third-party feedback. It may be used on a small number of pages, but should be monitored closely. Suggestions for new features or adjustments to input and output are welcome. |
| This module's documentation is missing, inadequate, or does not accurately describe its functionality or the parameters in its code. Please help add, expand, or improve it. |
Usage
[edit]{{#invoke:Italian municipality|function_name}}
| This module has not been added to any categories. Please help out by adding categories to it so that it can be listed with similar modules. |
local e = {}
e.publisher="[[Italian National Institute of Statistics|ISTAT]]"
e.countryofbirth_countries_index=2
e.countryofbirth_text_head="As of %s, of the known countries of birth of %s residents, the most numerous are: "
e.countryofbirth_and=" and "
e.countryofbirth_procent_yes_up="0.5%"
local function lround(number, decimals)
local full=string.format("%.1f", number)
local length=#full
local i_start
local i_end
local full_l
local tail
i_start, _=string.find(full, "%.")
if i_start then
while 1 do
full_l=string.len(full)
tail=string.sub(full, full_l-1)
if tail == ".0" then
full=string.sub(full, 1, full_l-2)
break
end
tail=string.sub(full, full_l)
if tail == "0" then
full=string.sub(full, 1, full_l-1)
else
break
end
end
end
return full
end
function e.get_place_codes()
local code=mw.wikibase.getEntityIdForCurrentPage()
local code2
local place_info
if code == nil then
return nil
end
codes={}
place_info=mw.wikibase.getAllStatements(code,"P635")
if place_info then
for i,v in ipairs(place_info) do
code=v["mainsnak"]['datavalue']['value']
table.insert(codes, code)
end
end
if #codes == 0 then
return nil
end
return codes
end
local function percent(now, full, lang)
if now == 0 or full == 0 then
return "0%"
end
local rate=now/(full/100)
return lang:formatNum(tonumber(lround(rate, 1))).."%"
end
function e.get_line_data(data, id)
index=1
while 1 do
item=data.data[index]
if item == nil then
break
end
if item[1] == id then
break
end
index=index+1
end
return item
end
function e.percent(now, full, lang)
if now == 0 or full == 0 then
return "0%"
end
local rate=now/(full/100)
return lang:formatNum(tonumber(lround(rate))).."%"
end
function e.popstructure()
local data=mw.ext.data.get("Italian municipality populations.tab")
local record
local code=nil
if code == nil or code == "" then
code=e.get_place_codes()
if code == nil then
return ""
end
else
code={code}
end
record = e.get_line_data(data, code[1])
if record == nil then
record = e.get_line_data(data, code[2])
end
local year=e.year(data)
local ref=e.get_ref(data)
local all_count=record[2]
local male_count=record[3]
local female_count=record[4]
local lang=mw.language.getContentLanguage()
local percent_male=e.percent(male_count, all_count, lang)
local percent_female=e.percent(female_count, all_count, lang)
local percent_immature=e.percent(record[5], all_count, lang)
local percent_old_age=e.percent(record[6], all_count, lang)
return "As of "..year..", the population is "..lang:formatNum(all_count)..", of which "
..percent_male.." are male, and "
..percent_female.." are female. "..
"Minors make up "..percent_immature.." of the population, and seniors make up "..percent_old_age.."."
end
function e.population_ref()
local data=mw.ext.data.get("Italian municipality populations.tab")
return e.get_ref(data)
end
function e.population_dref()
local data=mw.ext.data.get("Italian municipality populations.tab")
return e.get_ref_dataset(data)
end
function e.population(data, code)
local record
if code == nil or code == "" then
code=e.get_place_codes()
if code == nil then
return nil
end
else
if type(code) == "string" then
code={code}
end
end
for i,c in ipairs(code) do
record = e.get_line_data(data, c)
if record then
return record[2]
end
end
return ""
end
function e.year(data)
local i_start
local i_end
local start
local description=data.description.en
if description == nil then
description=data.description
end
i_start, i_end=string.find(description, "%(")
start=i_start+1
return string.sub(description, start, start+3)
end
function e.public_date(data)
local i_start
local i_end
local start
local description=data.description.en
if description == nil then
description=data.description
end
i_start, i_end=string.find(description, "%(")
start=i_start+1
return string.sub(description, start, start+9)
end
function e.year_h()
local data=mw.ext.data.get("Italian municipality populations.tab")
return e.year(data)
end
function e.density_old(args)
local area=args.args[1]
local code=args.args[2]
local data=mw.ext.data.get("Italian municipality populations.tab")
local population = e.population(data, code)
if area == nil or population == nil then return "" end
if area == "" then return "" end
if area == 0 then return 0 end
if population == 0 then return 0 end
-- hr to "C" format
local i_start
i_start, _=string.find(area, ",")
if i_start then
area=string.gsub(area, "%.", "")
area=string.gsub(area, "%,", ".")
end
local area_pop=tonumber(area)
if area_pop == nil then return "" end
local result = population/area_pop
local lang=mw.language.getContentLanguage()
return lang:formatNum(tonumber(lround(result, 0)))
end
function e.density(args)
local code=args.args[1]
local data=mw.ext.data.get("Italian municipality populations.tab")
local area_pop
if code == nil or code == "" then
code=e.get_place_codes()
if code == nil then
return ""
end
else
code={code}
end
local population = e.population(data, code)
area_pop=e.area_value(code)
if area_pop == nil or population == nil then return "" end
if population == 0 then return "0" end
local result = population/area_pop
local lang=mw.language.getContentLanguage()
return lang:formatNum(tonumber(lround(result, 0)))
end
function e.population_h(args)
local code=args.args[1]
local original=args.args[2]
local data=mw.ext.data.get("Italian municipality populations.tab")
local population = e.population(data, code)
if original == nil then original = "" end
if population == nil then return original end
if population == "" then return original end
if population == 0 then return "0" end
local lang=mw.language.getContentLanguage()
return lang:formatNum(population)
end
function e.get_ref(data)
local source=data.sources
local i_start
local i_end
local start
local title
local url
-- title
i_start, i_end=string.find(source, "''")
start=i_start+2
i_start, i_end=string.find(source, "''", start)
title=string.sub(data.sources, start, i_start-1)
title=string.gsub(title, "''", "")
-- url
i_start, i_end=string.find(source, "%(")
start=i_start+1
i_start, i_end=string.find(source, ",", start)
url=string.sub(data.sources, start, i_start-1)
-- website
i_start, i_end=string.find(source, "//")
start=i_start+2
i_start, i_end=string.find(source, "/", start)
local website=string.sub(data.sources, start, i_start-1)
-- access date
i_start, i_end=string.find(source, "downloaded: ")
start=i_start+12
local acces_date=string.sub(data.sources, start, start+9)
local year=e.year(data)
local pdate=e.public_date(data)
return "{{Cite web |date="..pdate.." |title= "..title.." |url="..url.." |website="..website.." |publisher="..e.publisher.." |access-date="..acces_date.."}}"
end
function e.get_ref_dataset(data)
local source=data.sources
local i_start
local i_end
local start
local title
local url
-- title
i_start, i_end=string.find(source, "''")
start=i_start+2
i_start, i_end=string.find(source, "''", start)
title=string.sub(data.sources, start, i_start-1)
title=string.gsub(title, "''", "")
-- url
i_start, i_end=string.find(source, ": ")
start=i_start+1
i_start, i_end=string.find(source, ": ", start)
start=i_start+1
i_start, i_end=string.find(source, ",", start)
url=string.sub(data.sources, start, i_start-1)
-- website
i_start, i_end=string.find(source, "//")
start=i_start+2
i_start, i_end=string.find(source, "/", start)
local website=string.sub(data.sources, start, i_start-1)
-- access date
i_start, i_end=string.find(source, "downloaded: ")
start=i_start+12
local acces_date=string.sub(data.sources, start, start+9)
local year=e.year(data)
local pdate=e.public_date(data)
return "{{Cite web |date="..pdate.." |title= "..title.." |url="..url.." |website="..website.." |publisher="..e.publisher.." |access-date="..acces_date.."}}"
end
function e.countryofbirth_ref()
local data=mw.ext.data.get("Italian municipality birthday countries – supmunicipalies.tab") -- identic for municipalies/supmunicipalies
local ref=e.get_ref(data)
return ref
end
function e.countryofbirth_dref()
local data=mw.ext.data.get("Italian municipality birthday countries – supmunicipalies.tab") -- identic for municipalies/supmunicipalies
local ref=e.get_ref_dataset(data)
return ref
end
function e.str_implode(middle, array) --middle is unused
local count=#array
local stop=count-1
local ret=""
if count == 1 then
return array[1]
end
for i=1,stop do
ret=ret..array[i].." "
end
ret=ret..array[count]
return ret
end
function e.d0cmf_to_table(text)
local items=mw.text.split(text, "%s")
local values={}
local values_index=1
local zeros
local count=#items
for i=1,count do
if text.sub(items[i], 1,1) == "^" then
zeros=tonumber(string.sub(items[i], 2))
for j=1,zeros do
values[values_index]="0"
values_index=values_index+1
end
else
values[values_index]=items[i]
values_index=values_index+1
end
end
return values
end
function e.array_without_first(array)
local ret={}
local count=#array
for i=2,count do
table.insert(ret, array[i])
end
return ret
end
function e.countryofbirth()
local data_countries=mw.ext.data.get("Italian municipality – country list.tab")
local code
local data
local values
local ethnicity={}
local ethnicity_name
local ethnicity_tail_minus
local place_population
local procent
local ret
local i_start
local start
local year
local record
if code == nil or code == "" then
code=e.get_place_codes()
if code == nil then
return ""
end
else
do return "" end
end
if #code[1] == 6 then
data=mw.ext.data.get("Italian municipality birthday countries – municipalies.tab")
else
data=mw.ext.data.get("Italian municipality birthday countries – supmunicipalies.tab")
end
record = e.get_line_data(data, code[1])
if record == nil then
record = e.get_line_data(data, code[2])
end
if record == nil then return "" end
values=e.d0cmf_to_table(e.str_implode(" ", e.array_without_first(record)))
place_population=0;
for index, population in ipairs(values) do
if population ~= "0" then
population=tonumber(population)
ethnicity_name=data_countries.data[index][e.countryofbirth_countries_index]
table.insert(ethnicity, {ethnicity_name,population})
place_population=place_population+population
end
end
table.sort(ethnicity, function (v, w) if v[2] == w[2] then return v[1]>w[1] end return v[2]>w[2] end)
local lang=mw.language.getContentLanguage()
-- The year of date
i_start, _=string.find(data.sources, ", year ")
start=i_start+7
year=string.sub(data.sources, start, start+3)
-- Render output
ret=string.format(e.countryofbirth_text_head, year, lang:formatNum(place_population))
ethnicity_tail_minus=#ethnicity-1
local index_now=1
for index, cols in ipairs(ethnicity) do
procent=percent(cols[2], place_population, lang)
if procent > e.countryofbirth_procent_yes_up then
ret=ret..cols[1].." ".."("..lang:formatNum(tonumber(cols[2])).." – "..procent.."), "
else
do break end
-- ret=ret..cols[1].." ".."("..lang:formatNum(tonumber(cols[2])).."), "
end
index_now=index_now+1
if index == ethnicity_tail_minus then
ret=string.sub(ret, 1, #ret-2)
ret=ret..e.countryofbirth_and
end
end
ret=string.sub(ret, 1, #ret-2)
ret=ret.."."
return ret
end
function e.geo(data, code)
local record
if code == nil then
code=e.get_place_codes()
if code == nil then
return nil
end
else
if type(code) == "string" then
code={code}
end
end
for i,c in ipairs(code) do
record = e.get_line_data(data, c)
if record then
return record
end
end
return ""
end
function e.area_value(code)
local area
local record
local data=mw.ext.data.get("Italian municipalities – geological data.tab")
if not code then
return ""
end
record = e.get_line_data(data, code[1])
if record == nil then
record = e.get_line_data(data, code[2])
end
if record ~= nil then
area=record[2]
else
return nil
end
-- if original == nil then original = "" end -- if none dataset value then it return the value from original_template_call.
if area == nil then return original end
if area == "" then return original end
if area == 0 then return "0" end
return area
end
function e.area(args)
local code=args.args[1]
local original=args.args[2]
local area
if code == nil or code == "" then
code=e.get_place_codes()
else
code={code}
end
area=e.area_value(code)
if original == nil then original = "" end
if area == nil then return original end
if area == "" then return original end
if area == 0 then return "0" end
local lang=mw.language.getContentLanguage()
return lang:formatNum(tonumber(lround(area, 1)))
end
function e.area_ref()
local data=mw.ext.data.get("Italian municipalities – geological data.tab")
return e.get_ref(data)
end
function e.area_dref()
local data=mw.ext.data.get("Italian municipalities – geological data.tab")
return e.get_ref_dataset(data)
end
function e.alti(args)
local code=args.args[1]
local original=args.args[2]
local area
local record
local data=mw.ext.data.get("Italian municipalities – geological data.tab")
if code == nil or code == "" then
code=e.get_place_codes()
if code == nil then
return ""
end
else
if type(code) == "string" then
code={code}
end
end
for i,c in ipairs(code) do
record = e.get_line_data(data, c)
if record then
break
end
end
if record then
do return record[3] end
else
do return "" end
end
end
function e.table_list(args)
local encoded=args.args[1]
local ret=""
local data_popul=mw.ext.data.get("Italian municipality populations.tab")
local data_geo=mw.ext.data.get("Italian municipalities – geological data.tab")
local lang=mw.language.getContentLanguage()
local popul
local area
local area_record
local records=mw.text.split(encoded, ";", true)
for _,cols in ipairs(records) do
ret=ret.."<tr>"
local columns=mw.text.split(cols, "::", true)
if columns[2] then ret=ret.."<td>"..columns[2].."</td>" else ret=ret.."<td></td>" end
if columns[3] then ret=ret.."<td>"..columns[3].."</td>" else ret=ret.."<td></td>" end
local codes=mw.text.split(columns[1], ",", true)
area=nil
popul=nil
for _,code in ipairs(codes) do
popul=e.population(data_popul, code)
if popul then
break
end
end
for _,code in ipairs(codes) do
area_record=e.geo(data_geo, code)
if area_record then
area=area_record[2]
end
end
if area then
ret=ret.."<td>"..lang:formatNum(tonumber(lround(area))).."</td>"
else
ret=ret.."<td></td>"
end
if popul then
ret=ret.."<td>"..lang:formatNum(popul).."</td>"
--ret=ret.."<td>"..lang:formatNum(tonumber(lround(popul))).."</td>"
else
ret=ret.."<td></td>"
end
ret=ret.."</tr>"
end
return ret
end
return e