Modul:ProyekWiki Bahasa/pemeliharaan/peta/rawdata/2

Revisi sejak 6 November 2024 06.32 oleh Nyilvoskt (bicara | kontrib) (←Membuat halaman berisi 'local p = {} function p.query(frame) -- Define the endpoint and query local endpointUrl = "https://query.wikidata.org/sparql" local sparqlQuery = [[ SELECT ?id ?idLabel ?head (SAMPLE(?pop) AS ?populasi) (SAMPLE(?ison) AS ?iso) (SAMPLE(?glotton) AS ?glotto) (SAMPLE(?img) AS ?img_) (SAMPLE(?gmbr) AS ?gmbr_) (MIN(?partyId) AS ?geo) WHERE { ?id wdt:P31 wd:Q34770 . ?id...')
(beda) ← Revisi sebelumnya | Revisi terkini (beda) | Revisi selanjutnya → (beda)

local p = {}

function p.query(frame)
    -- Define the endpoint and query
    local endpointUrl = "https://query.wikidata.org/sparql"
    local sparqlQuery = [[
        SELECT ?id ?idLabel ?head
        (SAMPLE(?pop) AS ?populasi)
        (SAMPLE(?ison) AS ?iso)
        (SAMPLE(?glotton) AS ?glotto)
        (SAMPLE(?img) AS ?img_)
        (SAMPLE(?gmbr) AS ?gmbr_)
        (MIN(?partyId) AS ?geo)
        WHERE {
            ?id wdt:P31 wd:Q34770 .
            ?id wdt:P279 ?head .
            ?id wdt:P625 ?geo .
            SERVICE wikibase:label { bd:serviceParam wikibase:language "id" . ?head rdfs:label ?headLabel . ?id rdfs:label ?idLabel . }
            OPTIONAL { ?id wdt:P220 ?ison . }
            OPTIONAL { ?id wdt:P1394 ?glotton . }
            OPTIONAL { ?id wdt:P18 ?img . }
            OPTIONAL { ?id wdt:P1098 ?pop . }
            OPTIONAL { ?id wdt:P1846 ?gmbr . }
        }
        GROUP BY ?id ?idLabel ?head ?headLabel
    ]]

    -- Prepare the HTTP request
    local http = require("mw.http")
    local json = require("mw.text.json")
    local res = http.request {
        url = endpointUrl,
        method = "POST",
        headers = { ["Accept"] = "application/sparql-results+json" },
        body = "query=" .. mw.uri.encode(sparqlQuery),
    }

    -- Parse and process the response
    if res and res.status == 200 then
        local data = json.decode(res.body)
        local result = ""
        for i, binding in ipairs(data.results.bindings) do
            result = result .. binding.idLabel.value .. " (Population: " .. (binding.populasi and binding.populasi.value or "Unknown") .. ")<br/>"
        end
        return result
    else
        return "Error retrieving data from SPARQL endpoint."
    end
end

return p