Mooduul:Population table
Olgoldâshäämi
Taan mooduul ravvuu puáhtá rähtiđ siijđon Mooduul:Population table/raavâ
local p = {}
function p.getPopulationTable(frame)
local itemId = frame.args[1] or mw.wikibase.getEntityIdForCurrentPage()
if not itemId or itemId == '' then
return "Sivulla ei ole liitettyä Wikidata-tunnistetta, eikä tunnistetta annettu mallineessa. Käytä esimerkiksi: {{Population table|Q60}}"
end
local entity = mw.wikibase.getEntity(itemId)
if not entity then
return "Tietoja ei löytynyt kohteelle " .. itemId
end
local claims = entity.claims and entity.claims["P1082"]
if not claims then
return "Väestötietoja ei löytynyt."
end
local values = {}
for _, claim in ipairs(claims) do
local amount = claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value and claim.mainsnak.datavalue.value.amount
local timeClaim = claim.qualifiers and claim.qualifiers["P585"] and claim.qualifiers["P585"][1]
local time = timeClaim and timeClaim.datavalue and timeClaim.datavalue.value and timeClaim.datavalue.value.time
if amount and time then
local year = tonumber(string.sub(time, 2, 5))
local pop = tonumber(amount)
if year and pop then
table.insert(values, { year = year, pop = math.floor(pop) })
end
end
end
if #values == 0 then
return "Ei muotoiltuja väestötietoja."
end
table.sort(values, function(a, b) return a.year < b.year end)
local total = #values
local out = {}
-- Case 1: ≤10 rows → simple single-column layout
if total <= 10 then
table.insert(out, '{| class="wikitable"\n|-\n! style="text-align:center" | Ihe\n! style="text-align:center" | Ässeeloho')
for _, entry in ipairs(values) do
local year = entry.year
local pop = mw.language.getContentLanguage():formatNum(entry.pop)
table.insert(out, string.format('\n|-\n| style="text-align:center" | %d\n| style="text-align:center" | %s', year, pop))
end
-- Add the source row spanning both columns
table.insert(out, string.format('\n|-\n| colspan="2" style="text-align:center" | Käldee: [https://www.wikidata.org/wiki/%s#P1082 Wikidata]', itemId))
table.insert(out, '\n|}')
return table.concat(out, '\n')
end
-- Case 2: >10 rows → multi-column layout
-- Decide number of columns (max 5 for readability)
local max_columns = 5
local columns = math.min(max_columns, math.ceil(total / 10)) -- 10 rows per col max target
-- Distribute values across columns evenly (±1 row difference)
local rows_per_column = math.floor(total / columns)
local remainder = total % columns
local coldata = {}
local index = 1
for c = 1, columns do
local this_col_size = rows_per_column + (c <= remainder and 1 or 0)
local col = {}
for i = 1, this_col_size do
table.insert(col, values[index])
index = index + 1
end
table.insert(coldata, col)
end
-- Build the multi-column table
table.insert(out, '{| class="wikitable"\n|-\n')
for _ = 1, columns do
table.insert(out, '! style="text-align:center" | Ihe')
table.insert(out, '! style="text-align:center" | Ässeeloho')
end
-- Determine max column height
local max_rows = 0
for _, col in ipairs(coldata) do
if #col > max_rows then
max_rows = #col
end
end
-- Render rows
for i = 1, max_rows do
table.insert(out, '\n|-')
for _, col in ipairs(coldata) do
local entry = col[i]
if entry then
local year = entry.year
local pop = mw.language.getContentLanguage():formatNum(entry.pop)
table.insert(out, string.format('| style="text-align:center" | %d', year))
table.insert(out, string.format('| style="text-align:center" | %s', pop))
else
table.insert(out, '| style="text-align:center" | ')
table.insert(out, '| style="text-align:center" | ')
end
end
end
-- Add the source row spanning all columns
local colspan = columns * 2
table.insert(out, string.format('\n|-\n| colspan="%d" style="text-align:center" | Käldee: [https://www.wikidata.org/wiki/%s#P1082 Wikidata]', colspan, itemId))
table.insert(out, '\n|}')
return table.concat(out, '\n')
end
return p