Modül:Türkiye nüfus

Modül belgelemesi[oluştur]
local p = {}

-- Helper function to fetch populations from Wikidata
local function fetchPopulations(entity)
    local claims = mw.wikibase.getBestStatements(entity, 'P1082')
    if not claims then
        return {}, {}, {}, {}, false, false
    end

    local populations = {}
    local ruralPopulations = {}
    local urbanPopulations = {}
    local determinationMethods = {}
    local hasRural = false
    local hasUrban = false

    for _, statement in ipairs(claims) do
        local qualifiers = statement.qualifiers and statement.qualifiers.P585
        if qualifiers then
            for _, qualifier in ipairs(qualifiers) do
                local time = qualifier.datavalue and qualifier.datavalue.value and qualifier.datavalue.value.time
                if time then
                    local year = tonumber(mw.ustring.match(time, "%d+"))
                    local value = statement.mainsnak and statement.mainsnak.datavalue and statement.mainsnak.datavalue.value
                    if value then
                        populations[year] = tonumber(value.amount)
                        
                        -- Fetch rural and urban populations
                        local ruralQualifier = statement.qualifiers.P6344
                        if ruralQualifier then
                            ruralPopulations[year] = tonumber(ruralQualifier[1].datavalue.value.amount)
                            hasRural = true
                        end
                        local urbanQualifier = statement.qualifiers.P6343
                        if urbanQualifier then
                            urbanPopulations[year] = tonumber(urbanQualifier[1].datavalue.value.amount)
                            hasUrban = true
                        end

                        -- Fetch determination method
                        local methodQualifier = statement.qualifiers.P459
                        if methodQualifier then
                            local methodId = methodQualifier[1].datavalue and methodQualifier[1].datavalue.value and methodQualifier[1].datavalue.value.id
                            if methodId then
                                local methodLabel = mw.wikibase.getLabel(methodId)
                                determinationMethods[year] = methodLabel
                            end
                        end
                    end
                end
            end
        end
    end

    -- Sort years
    local sortedYears = {}
    for year in pairs(populations) do
        table.insert(sortedYears, year)
    end
    table.sort(sortedYears)

    return sortedYears, populations, ruralPopulations, urbanPopulations, determinationMethods, hasRural, hasUrban
end

-- Helper function to format numbers
local function formatNumber(num)
    return mw.language.getContentLanguage():formatNum(num)
end

-- Helper function to calculate percentage change
local function calculateChange(current, previous)
    if not current or not previous then
        return ''
    end
    local change = ((current - previous) / previous) * 100
    local changeIcon = change >= 0 and '[[File:Dark Green Arrow Up.svg|11px|link=]]' or '[[File:Red Arrow Down.svg|11px|link=]]'
    return string.format('%s %d%%', changeIcon, math.floor(math.abs(change) + 0.5))
end

-- Helper function to generate year link
local function generateYearLink(year)
    local pageTitle
    if year >= 2007 then
        pageTitle = string.format('%d Türkiye adrese dayalı nüfus kayıt sistemi sonuçları', year)
    else
        pageTitle = string.format('%d Türkiye nüfus sayımı', year)
    end
    
    local title = mw.title.new(pageTitle)
    if title and title.exists then
        return string.format('[[%s|%d]]', pageTitle, year)
    else
        return tostring(year)
    end
end

-- Helper function to generate determination method link
local function generateMethodLink(method)
    if method == "Türkiye'de genel nüfus sayımı" then
        return '[[Türkiye\'de genel nüfus sayımı|Genel nüfus sayımı]]'
    elseif method == "Adrese Dayalı Nüfus Kayıt Sistemi" then
        return '[[Adrese Dayalı Nüfus Kayıt Sistemi|ADNKS]]'
    else
        local pageTitle = mw.title.new(method)
        if pageTitle and pageTitle.exists then
            return string.format('[[%s|%s]]', method, method)
        else
            return method
        end
    end
end

-- Main function to generate the table
function p.populationTable(frame)
    local entity = frame.args.entity or mw.wikibase.getEntityIdForCurrentPage()
    if not entity then
        return "Kimliği bilinmeyen bir sisteme ait. Lütfen geçerli bir varlık kimliği kullanın."
    end

    local sortedYears, populations, ruralPopulations, urbanPopulations, determinationMethods, hasRural, hasUrban = fetchPopulations(entity)
    local rows = {}
    local previousPopulation

    table.insert(rows, '{| class="wikitable sortable" style="font-size:0.90em; text-align:center;"')
    table.insert(rows, '! style="padding-left:15px;padding-right:15px;" | Yıl')
    table.insert(rows, '! style="padding-left:15px;padding-right:15px;" | Değişim')
    table.insert(rows, '! style="padding-left:15px;padding-right:15px;" | Nüfus')

    if hasRural or hasUrban then
        table.insert(rows, '! style="padding-left:15px;padding-right:15px;" | Kırsal Nüfus')
        table.insert(rows, '! style="padding-left:15px;padding-right:15px;" | Kentsel Nüfus')
    end
    
    table.insert(rows, '! style="padding-left:15px;padding-right:15px;" | Tespit Yöntemi')

    for _, year in ipairs(sortedYears) do
        local population = populations[year]
        local ruralPopulation = ruralPopulations[year]
        local urbanPopulation = urbanPopulations[year]
        local determinationMethod = determinationMethods[year]

        if population then
            local change = calculateChange(population, previousPopulation)
            local yearLink = generateYearLink(year)
            local methodDisplay = determinationMethod and generateMethodLink(determinationMethod) or '<span style="color: grey;">Veri yok</span>'
            local row = string.format('|-\n| %s || %s || %s', yearLink, change, formatNumber(population))
            
            if hasRural or hasUrban then
                local ruralDisplay = ruralPopulation and formatNumber(ruralPopulation) or '<span style="color: grey;">Veri yok</span>'
                local urbanDisplay = urbanPopulation and formatNumber(urbanPopulation) or '<span style="color: grey;">Veri yok</span>'
                row = row .. string.format(' || %s || %s', ruralDisplay, urbanDisplay)
            end
            
            row = row .. string.format(' || %s', methodDisplay)
            
            table.insert(rows, row)
            previousPopulation = population
        end
    end

    table.insert(rows, '|}')
    return table.concat(rows, '\n')
end

return p