Module:TA4TK:myTest: Difference between revisions
From MaRDI portal
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
-- Main table to hold all functions | -- Main table to hold all functions | ||
local sparql = require('SPARQL') -- Load the SPARQL binding | |||
local p = {} | local p = {} | ||
function p.hello() | function p.hello() | ||
local str = "Hello World!" | local str = "Hello World!" | ||
return str | return str | ||
end | end | ||
function p.renderFormula() | function p.renderFormula() | ||
local formula = "\\sin x" -- LaTeX formula | local formula = "\\sin x" -- LaTeX formula | ||
end | end | ||
function p.querySPARQL() | |||
-- Define the SPARQL endpoint | |||
local endpoint = "https://query.wikidata.org/sparql" | |||
-- Define the SPARQL query | |||
local sparqlQuery = [[ | |||
SELECT ?item ?itemLabel WHERE { | |||
?item wdt:P31 wd:Q5; -- Instance of 'human' | |||
rdfs:label "Albert Einstein"@en. -- Label is 'Albert Einstein' | |||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | |||
} LIMIT 1 | |||
]] | |||
-- Encode the query for URL usage | |||
local url = endpoint .. "?query=" .. mw.uri.encode(sparqlQuery) | |||
-- Make the HTTP request | |||
local response = mw.http.get(url, { | |||
headers = { | |||
["Accept"] = "application/json" -- Request JSON format | |||
} | |||
}) | |||
-- Check for errors | |||
if response.status ~= 200 then | |||
return "Error: Failed to fetch data. Status code: " .. response.status | |||
end | |||
-- Parse the JSON response | |||
local json = mw.text.jsonDecode(response.body) | |||
if not json then | |||
return "Error: Failed to parse JSON response." | |||
end | |||
-- Process and return results | |||
local results = json.results.bindings | |||
if #results == 0 then | |||
return "No results found." | |||
end | |||
-- Extract and format the first result | |||
local item = results[1].item.value | |||
local label = results[1].itemLabel.value | |||
return string.format("Item: %s (%s)", label, item) | |||
end | |||
return p | return p |
Revision as of 14:49, 3 January 2025
Documentation for this module may be created at Module:TA4TK:myTest/doc
-- Main table to hold all functions
local sparql = require('SPARQL') -- Load the SPARQL binding
local p = {}
function p.hello()
local str = "Hello World!"
return str
end
function p.renderFormula()
local formula = "\\sin x" -- LaTeX formula
end
function p.querySPARQL()
-- Define the SPARQL endpoint
local endpoint = "https://query.wikidata.org/sparql"
-- Define the SPARQL query
local sparqlQuery = [[
SELECT ?item ?itemLabel WHERE {
?item wdt:P31 wd:Q5; -- Instance of 'human'
rdfs:label "Albert Einstein"@en. -- Label is 'Albert Einstein'
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} LIMIT 1
]]
-- Encode the query for URL usage
local url = endpoint .. "?query=" .. mw.uri.encode(sparqlQuery)
-- Make the HTTP request
local response = mw.http.get(url, {
headers = {
["Accept"] = "application/json" -- Request JSON format
}
})
-- Check for errors
if response.status ~= 200 then
return "Error: Failed to fetch data. Status code: " .. response.status
end
-- Parse the JSON response
local json = mw.text.jsonDecode(response.body)
if not json then
return "Error: Failed to parse JSON response."
end
-- Process and return results
local results = json.results.bindings
if #results == 0 then
return "No results found."
end
-- Extract and format the first result
local item = results[1].item.value
local label = results[1].itemLabel.value
return string.format("Item: %s (%s)", label, item)
end
return p