Module:TA4TK:myTest: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
 
(27 intermediate revisions by the same user not shown)
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 9: Line 11:


function p.renderFormula()
function p.renderFormula()
     local formula = "\\sin x" -- LaTeX formula
     return mw.getCurrentFrame():extensionTag{
name="math",
content= "\\sin x",
}
end
end




local sparql = require('SPARQL') -- Load the SPARQL binding


function p.querySPARQL()
function p.querySPARQL()
    -- Define the SPARQL endpoint
-- Define the SPARQL endpoint
     local endpoint = "https://query.wikidata.org/sparql"
     local endpoint = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"


     -- Define the SPARQL query
     -- Define the SPARQL query
     local sparqlQuery = [[
     local sparqlQuery = [[
         SELECT ?item ?itemLabel WHERE {
         SELECT ?item ?itemLabel WHERE {
             ?item wdt:P31 wd:Q5; -- Instance of 'human'
             ?item wdt:P31 wd:Q5;
                   rdfs:label "Albert Einstein"@en. -- Label is 'Albert Einstein'
                   rdfs:label "Albert Einstein"@en.
             SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
             SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
         } LIMIT 1
         } LIMIT 1
     ]]
     ]]
local queryResults = sparql.runQuery(sparqlQuery) -- Use the runQuery method


    -- Encode the query for URL usage
-- nil is returned if Blazegraph did not return a valid response
    local url = endpoint .. "?query=" .. mw.uri.encode(sparqlQuery)
if queryResults == nil then
 
return "No results found."
    -- Make the HTTP request
end
    local response = mw.http.get(url, {
        headers = {
            ["Accept"] = "application/json" -- Request JSON format
        }
    })


     -- Check for errors
     if response.status ~= 200 then
     -- Check if the input is a table
         return "Error: Failed to fetch data. Status code: " .. response.status
     if type(queryResults) ~= "table" then
         return "Error: Input is not a table."
     end
     end


     -- Parse the JSON response
     -- Start HTML table
     local json = mw.text.jsonDecode(response.body)
     local results = {"<table border='1'>"}
    if not json then
    table.insert(results, "<tr><th>Key</th><th>Value</th></tr>")
        return "Error: Failed to parse JSON response."
    end


    -- Process and return results
    -- Add rows for each key-value pair
     local results = json.results.bindings
     for key, value in pairs(queryResults) do
    if #results == 0 then
        table.insert(results, string.format("<tr><td>%s</td><td>%s</td></tr>", tostring(key), tostring(value)))
        return "No results found."
     end
     end


     -- Extract and format the first result
     -- End HTML table
     local item = results[1].item.value
     table.insert(results, "</table>")
    local label = results[1].itemLabel.value
     return table.concat(results, "\n")
     return string.format("Item: %s (%s)", label, item)
    --return queryResults['results']
end
end




return p
return p

Latest revision as of 09:44, 10 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()
    return mw.getCurrentFrame():extensionTag{
		name="math",
		content= "\\sin x", 
	}
end



function p.querySPARQL()
	-- Define the SPARQL endpoint
    local endpoint = "https://query.wikidata.org/bigdata/namespace/wdq/sparql"

    -- Define the SPARQL query
    local sparqlQuery = [[
        SELECT ?item ?itemLabel WHERE {
            ?item wdt:P31 wd:Q5;
                  rdfs:label "Albert Einstein"@en.
            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 "No results found."
	end

	
    -- Check if the input is a table
    if type(queryResults) ~= "table" then
        return "Error: Input is not a table."
    end

    -- Start HTML table
    local results = {"<table border='1'>"}
    table.insert(results, "<tr><th>Key</th><th>Value</th></tr>")

     -- Add rows for each key-value pair
    for key, value in pairs(queryResults) do
        table.insert(results, string.format("<tr><td>%s</td><td>%s</td></tr>", tostring(key), tostring(value)))
    end

    -- End HTML table
    table.insert(results, "</table>")
    return table.concat(results, "\n")
    --return queryResults['results']
end



return p