Jump to content

Module:ColorTree

From Mare Nostrum Lab Thesaurus
Revision as of 19:01, 23 June 2026 by Admin (talk | contribs) (new version)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:ColorTree/doc

local p = {}

function p.display()
    local rootId = "Q3"
    
    local rootLabel = mw.wikibase.getLabel(rootId) or rootId
    local html = "* '''" .. rootLabel .. "'''\n"

    local claims = mw.wikibase.getAllStatements(rootId, "P2")
    if not claims then
        return html
    end
    
    local level1_items = {}
    for _, claim in ipairs(claims) do
        if claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value then
            local id = claim.mainsnak.datavalue.value.id
            if id then
                local label = mw.wikibase.getLabel(id) or id
                table.insert(level1_items, { id = id, label = label })
            end
        end
    end
    
    table.sort(level1_items, function(a, b) return a.label < b.label end)
    
    for _, l1 in ipairs(level1_items) do
        html = html .. "** " .. l1.label .. "\n"

        local sub_claims = mw.wikibase.getAllStatements(l1.id, "P2")
        if sub_claims then
            local level2_items = {}
            for _, sub_claim in ipairs(sub_claims) do
                if sub_claim.mainsnak and sub_claim.mainsnak.datavalue and sub_claim.mainsnak.datavalue.value then
                    local sub_id = sub_claim.mainsnak.datavalue.value.id
                    if sub_id then
                        local sub_label = mw.wikibase.getLabel(sub_id) or sub_id
                        table.insert(level2_items, sub_label)
                    end
                end
            end

            table.sort(level2_items)
            
            for _, l2_label in ipairs(level2_items) do
                html = html .. "*** " .. l2_label .. "\n"
            end
        end
    end
    
    return html
end

return p