Module:MoroccoHealthTable

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
-- Module:MoroccoHealthTable (Advanced Version)
-- Builds a table of Moroccan healthcare institutions with multilingual, image, coordinates, and stats

local langTable = {'ar','zgh','ary','shi','fr','en'}
local p = {}

-- Helper function
local function def(bla)
    local zdef = {}
    for _, l in ipairs(bla) do zdef[l] = true end
    return zdef
end

function p.table(frame)
    local header = {
        '{| class="wikitable sortable" width=100%\n|-\n! № !! Institution',
        '!! ar !! zgh !! ary !! shi !! fr !! en !! Σ !! Image !! Coordinates !! Wikidata !! st.\n|-'
    }
    local resultTable = table.concat(header)
    local index = 1

    -- Initialize language counters
    local langCountTotal = {}
    for _, lang in ipairs(langTable) do langCountTotal[lang] = 0 end
    local totalRows = 0

    while frame.args[index] do
        local Id = frame.args[index]
        local entity = mw.wikibase.getEntityObject(Id)
        totalRows = totalRows + 1

        if not entity then
            resultTable = resultTable .. '\n|-\n| ' .. index .. ' || Entity ' .. Id .. ' not found'
        else
            local Label = entity:getLabel('ar') or entity:getLabel('en') or Id
            local row = '\n|-\n| ' .. index .. ' || ' .. Label
            local ct = 0

            -- Language columns
            for _, lang in ipairs(langTable) do
                local sitelink = entity:getSitelink(lang .. 'wiki')
                if sitelink then
                    row = row .. ' || style="background:#cfc;"|[[:'..lang..':'..sitelink..'|+]]'
                    ct = ct + 1
                    langCountTotal[lang] = langCountTotal[lang] + 1
                else
                    row = row .. ' || -'
                end
            end

            -- Σ column
            row = row .. ' || ' .. ct

            -- Image (P18)
            local img = entity:formatPropertyValues('P18').value
            if img ~= '' then
                row = row .. ' || [[File:'..img..'|50px]]'
            else
                row = row .. ' || -'
            end

            -- Coordinates (P625)
            local coord = entity:formatPropertyValues('P625').value
            if coord ~= '' and entity.claims.P625 then
                local lat = entity.claims.P625[1].mainsnak.datavalue.value.latitude
                local lon = entity.claims.P625[1].mainsnak.datavalue.value.longitude
                row = row .. ' || [[File:Geographylogo.svg|18px|view on maps|link=https://tools.wmflabs.org/geohack/geohack.php?language=en&pagename='.. mw.uri.encode(Label,"PATH") ..'&params='..lat..'_N_'..lon..'_E]]'
            else
                row = row .. ' || -'
            end

            -- Wikidata link
            row = row .. ' || [[:d:'..Id..'|'..Id..']]'

            -- Statements count
            local statements = 0
            if entity.claims then
                for _, claimList in pairs(entity.claims) do
                    if claimList and claimList[1] then
                        statements = statements + 1
                    end
                end
            end
            row = row .. ' || ' .. statements

            resultTable = resultTable .. row
        end
        index = index + 1
    end

    -- Footer: language statistics
    local footer = '\n|-\n! Σ per language'
    local totalLangArticles = 0
    for _, lang in ipairs(langTable) do
        footer = footer .. ' !! ' .. langCountTotal[lang]
        totalLangArticles = totalLangArticles + langCountTotal[lang]
    end
    footer = footer .. ' !! - !! - !! - !! -'
    resultTable = resultTable .. footer

    -- Footer: averages and percentages
    local avg = math.floor(totalLangArticles/totalRows + 0.5)
    local perc = math.floor(totalLangArticles / (#langTable * totalRows) * 100 + 0.5)
    resultTable = resultTable .. '\n|-\n! Average per row !! colspan='..(#langTable)..'| avg: '..avg..' ('..perc..'%) !! colspan=4|'

    resultTable = resultTable .. '\n|}'
    return resultTable
end

return p