Jump to content

Module:ColorTree: Difference between revisions

From Mare Nostrum Lab Thesaurus
Created page with "local p = {} function p.display() local endpoint = "https://pac.cenagis.edu.pl/query/sparql" local sparql = [[ PREFIX wd: <https://pac.cenagis.edu.pl/entity/> PREFIX wdt: <https://pac.cenagis.edu.pl/prop/direct/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?rootLabel ?level1Label ?level2Label WHERE { BIND(wd:Q3 AS ?root) { ?level1 wdt:P2 ?root . FILTER NOT EXISTS { ?sub wdt:P2 ?level1 ...."
 
new version
 
Line 2: Line 2:


function p.display()
function p.display()
     local endpoint = "https://thesaurus.mn.cenagis.edu.pl/query/sparql"
     local rootId = "Q3"
      
      
     local sparql = [[
     local rootLabel = mw.wikibase.getLabel(rootId) or rootId
    PREFIX wd: <https://thesaurus.mn.cenagis.edu.pl/entity/>
    local html = "* '''" .. rootLabel .. "'''\n"
    PREFIX wdt: <https://thesaurus.mn.cenagis.edu.pl/prop/direct/>
 
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
     local claims = mw.wikibase.getAllStatements(rootId, "P2")
   
     if not claims then
    SELECT ?rootLabel ?level1Label ?level2Label WHERE {
         return html
      BIND(wd:Q3 AS ?root)
     
      {
        ?level1 wdt:P2 ?root .
        FILTER NOT EXISTS { ?sub wdt:P2 ?level1 . }
        BIND("" AS ?level2Label)
      }
      UNION
      {
        ?level1 wdt:P2 ?root .
        ?level2 wdt:P2 ?level1 .
        OPTIONAL { ?level2 rdfs:label ?l2 . FILTER(lang(?l2) = "en") }
        BIND(COALESCE(?l2, REPLACE(STR(?level2), ".*/", "")) AS ?level2Label)
      }
     
      OPTIONAL { ?root rdfs:label ?rL . FILTER(lang(?rL) = "en") }
      OPTIONAL { ?level1 rdfs:label ?l1 . FILTER(lang(?l1) = "en") }
     
      BIND(COALESCE(?rL, REPLACE(STR(?root), ".*/", "")) AS ?rootLabel)
      BIND(COALESCE(?l1, REPLACE(STR(?level1), ".*/", "")) AS ?level1Label)
    } ORDER BY ?level1Label ?level2Label
    ]]
   
     local url = endpoint .. "?query=" .. mw.uri.encode(sparql, "QUERY") .. "&format=json"
   
    local success, response = pcall(function() return mw.ext.http.get(url) end)
     if not success or not response then
         return "Error: HTTP request failed or extension not available."
     end
     end
      
      
     local res = mw.text.jsonDecode(response)
     local level1_items = {}
    if not res or not res.results or not res.results.bindings then
    for _, claim in ipairs(claims) do
         return "Error: Invalid data format received from SPARQL endpoint."
        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
     end
      
      
     local html = "* '''color'''\n"
     table.sort(level1_items, function(a, b) return a.label < b.label end)
    local current_l1 = ""
      
      
     for _, row in ipairs(res.results.bindings) do
     for _, l1 in ipairs(level1_items) do
         local l1 = row.level1Label.value
         html = html .. "** " .. l1.label .. "\n"
         local l2 = row.level2Label and row.level2Label.value or ""
 
       
         local sub_claims = mw.wikibase.getAllStatements(l1.id, "P2")
         if l1 ~= current_l1 then
         if sub_claims then
             html = html .. "** " .. l1 .. "\n"
             local level2_items = {}
            current_l1 = l1
            for _, sub_claim in ipairs(sub_claims) do
        end
                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 l2 ~= "" then
                    if sub_id then
             html = html .. "*** " .. l2 .. "\n"
                        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
     end
     end

Latest revision as of 19:01, 23 June 2026

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