Module:ColorTree
Appearance
Documentation for this module may be created at Module:ColorTree/doc
local p = {}
function p.display()
local endpoint = "https://thesaurus.mn.cenagis.edu.pl/query/sparql"
local sparql = [[
PREFIX wd: <https://thesaurus.mn.cenagis.edu.pl/entity/>
PREFIX wdt: <https://thesaurus.mn.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 . }
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
local res = mw.text.jsonDecode(response)
if not res or not res.results or not res.results.bindings then
return "Error: Invalid data format received from SPARQL endpoint."
end
local html = "* '''color'''\n"
local current_l1 = ""
for _, row in ipairs(res.results.bindings) do
local l1 = row.level1Label.value
local l2 = row.level2Label and row.level2Label.value or ""
if l1 ~= current_l1 then
html = html .. "** " .. l1 .. "\n"
current_l1 = l1
end
if l2 ~= "" then
html = html .. "*** " .. l2 .. "\n"
end
end
return html
end
return p