Module:SPARQL: Difference between revisions

From TITAF TANGOWIKI
(Created page with "local p = {} local http = require('mw.http') local json = require('mw.text').jsonDecode function p.runQuery(query) local endpoint = "https://dbpedia.org/sparql" -- Change if using a different SPARQL service local url = endpoint .. "?query=" .. mw.uri.encode(query) local response = http.request{ method = "GET", url = url, headers = { ["Accept"] = "application/json" } } if response.status ~= 200 then return "SPARQ...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
package.path = package.path .. ";/usr/local/share/lua/5.1/?.lua"
package.cpath = package.cpath .. ";/usr/local/lib/lua/5.1/?.so"
local http = require("socket.http")  -- Now try to load it
local http = require("socket.http")
local p = {}
local p = {}


local http = require('mw.http')
function p.fetchData(frame)
local json = require('mw.text').jsonDecode
    local sparqlQuery = frame.args[1]
    local url = "https://query.wikidata.org/sparql?query=" .. mw.uri.encode(sparqlQuery, "PATH")


function p.runQuery(query)
     local body, code, headers = http.request(url)
     local endpoint = "https://dbpedia.org/sparql" -- Change if using a different SPARQL service
    local url = endpoint .. "?query=" .. mw.uri.encode(query)
   
    local response = http.request{
        method = "GET",
        url = url,
        headers = { ["Accept"] = "application/json" }
    }


     if response.status ~= 200 then
     if code == 200 then
         return "SPARQL query failed! Status: " .. response.status
        return body
    else
         return "Error: HTTP " .. (code or "unknown")
     end
     end
    return json(response.body)
end
end


return p
return p

Latest revision as of 22:48, 21 March 2025

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

package.path = package.path .. ";/usr/local/share/lua/5.1/?.lua"
package.cpath = package.cpath .. ";/usr/local/lib/lua/5.1/?.so"

local http = require("socket.http")  -- Now try to load it


local http = require("socket.http")

local p = {}

function p.fetchData(frame)
    local sparqlQuery = frame.args[1]
    local url = "https://query.wikidata.org/sparql?query=" .. mw.uri.encode(sparqlQuery, "PATH")

    local body, code, headers = http.request(url)

    if code == 200 then
        return body
    else
        return "Error: HTTP " .. (code or "unknown")
    end
end

return p