Modül:Nüfus tablosu

Vikipedi, özgür ansiklopedi
Modül belgelemesi[oluştur]
local tablo = {}
local t = {}

local wikidata_mod = require("Modül:WikidataIB")

local function yil_bagi(frame, yil, il)
	local y = ''	

	if frame.args['ülke'] == "Türkiye" then
		y = '[[' .. yil .. ' Türkiye nüfus sayımı|' .. yil .. ']]'
		
		if tonumber(yil) >= 2015 then
			il = 'Türkiye'
		end
	else
		y = yil
	end
	
	local ref = frame:expandTemplate{ title = 'Türkiye ilçe nüfus/Adres', args = {
					      ["1"] = yil,
					      ["2"] = il
					  } }
	
	return y .. ref
end

local function format_int(number)
	local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
    -- reverse the int-string and append a comma to all blocks of 3 digits
	int = int:reverse():gsub("(%d%d%d)", "%1.")
	
	if int:sub(-1) == '.'
	then
		int = int:sub(1, -2)
	end
	
	-- reverse the int-string back remove an optional comma and put the 
	-- optional minus and fractional part back
	return minus .. int:reverse():gsub("^,", "") .. fraction
end

local function tabloya_isle(claims, nufus_tipi)
	for k, v in pairs(claims) do
		local nufus = v.mainsnak.datavalue.value.amount
		nufus = nufus:gsub('+', '')
		local yil = v.qualifiers["P585"][1].datavalue.value.time
		yil = yil:gsub('+', '')
		yil = yil:sub(1, 4)
				
		if t[yil] == nil then
			t[yil] = {}
		end
				
		t[yil][nufus_tipi] = nufus
	end
end

function tablo.tablo(frame)
	local entity = {}
	
	if frame.args.qid ~= nil and frame.args.qid ~= '' then
		entity = mw.wikibase.getEntity(frame.args.qid)
	else
		entity = mw.wikibase.getEntity()
	end
	
	local cl_sehir = entity.claims["P6343"]
	local cl_kir = entity.claims["P6344"]
	
	local c_il = entity.claims["P131"]
	local item_id = "Q" .. c_il[1].mainsnak.datavalue.value["numeric-id"]
	local il = mw.wikibase.label(item_id)

	tabloya_isle(cl_sehir, 'sehir')
	tabloya_isle(cl_kir, 'kir')
	
	local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end
	
	table.sort(keys)
	
	local tablo_str = ''
	
	tablo_str = tablo_str .. '{| class="wikitable sortable" style="font-size:0.90em; width:30em; text-align:center; float: "\n'
	tablo_str = tablo_str .. '|-\n' 
	tablo_str = tablo_str .. '! style="width:5em;" | Yıl\n'
	tablo_str = tablo_str .. '! style="width:5em;" | Toplam\n'
	tablo_str = tablo_str .. '! style="width:5em;" | <span style="cursor:help; border-bottom:1px dotted;" title="İlçe merkezi nüfusu">Şehir</span>\n'
	tablo_str = tablo_str .. '! style="width:5em;" | <span style="cursor:help; border-bottom:1px dotted;" title="Köy ve belde nüfusu">Kır</span>\n'
	
	for k, v in pairs(keys) do
		local n_sehir = t[v]['sehir'] or 0
		local n_kir = t[v]['kir'] or 0
		local toplam = n_sehir + n_kir
		
		n_sehir = format_int(n_sehir)
		n_kir = format_int(n_kir)
		toplam = format_int(toplam)
		
		if n_sehir == "0" then
			n_sehir = frame:expandTemplate{ title = 'N/A', args = {
					      ["1"] = "veri yok"
					  } }
		end
		
		if n_kir == "0" then
			n_kir = frame:expandTemplate{ title = 'N/A', args = {
					      ["1"] = "veri yok"
					  } }
		end
		
		if toplam == "0" then
			toplam = frame:expandTemplate{ title = 'N/A', args = {
					      ["1"] = "veri yok"
					  } }
		end
		
		tablo_str = tablo_str .. '|-\n'
		tablo_str = tablo_str .. '| ' .. yil_bagi(frame, v, il) .. ' || ' .. toplam .. ' || ' .. n_sehir .. ' || ' .. n_kir .. '\n'
	end

	tablo_str = tablo_str .. '|}'
	
	return tablo_str
end

return tablo