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

Revisi sejak 6 November 2024 06.45 oleh Nyilvoskt (bicara | kontrib)

local p = {}
local json = require('mw.text').jsonDecode
local urlEncode = require('mw.uri').encode

function p.fetchData()
    -- Define the SPARQL endpoint URL and the 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)
        
    -- The rest of your query goes here
    
        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 . }
          ?link schema:about ?head .
          ?link schema:isPartOf <https://wiki-indonesia.club/> .
          ?link2 schema:about ?id .
          ?link2 schema:isPartOf <https://wiki-indonesia.club/> .
            -- Add the remaining parts of your WHERE clause here
        }
        GROUP BY ?id ?idLabel ?head ?headLabel ?link2 ?link ?geo
    ]]

    -- Build the URL with the encoded query
    local url = endpointUrl .. '?query=' .. urlEncode(sparqlQuery) .. '&format=json'

    -- Fetch data from Wikidata
    local result = mw.http.request {
        url = 'https://wiki-indonesia.club',
        headers = { ['Accept'] = 'application/sparql-results+json' },
        method = 'GET'
    }

    -- Decode JSON response and return it
    local data = json(result.body)
    return data
end

function p.showData()
    local data = p.fetchData()
    -- Convert data into wikitext or HTML for display
    local output = ""
    for _, item in ipairs(data.results.bindings) do
        output = output .. "* " .. item.idLabel.value .. ": " .. item.populasi.value .. "\n"
    end
    return output
end

return p