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 = {} | ||
Line 12: | Line 14: | ||
end | end | ||
function p.showFirstValue() | |||
-- 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 | |||
]] | |||
local queryResults = sparql.runQuery(sparqlQuery) -- Use the runQuery method | |||
-- nil is returned if Blazegraph did not return a valid response | |||
if queryResults == nil then | |||
return '' | |||
end | |||
-- Replace "work" with the first SELECT variable in your SPARQL query | |||
return queryResults['results']['bindings'][0]['work']['value'] | |||
end | |||
function p.querySPARQL() | function p.querySPARQL() |
Revision as of 14:53, 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.showFirstValue()
-- 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
]]
local queryResults = sparql.runQuery(sparqlQuery) -- Use the runQuery method
-- nil is returned if Blazegraph did not return a valid response
if queryResults == nil then
return ''
end
-- Replace "work" with the first SELECT variable in your SPARQL query
return queryResults['results']['bindings'][0]['work']['value']
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