Module:TA4TK:myTest: Difference between revisions

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


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


     -- Define the SPARQL query
     -- Define the SPARQL query
Line 34: Line 39:
end
end


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


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


    -- Iterate through the table
    -- Add rows for each key-value pair
     for key, value in pairs(queryResults) do
     for key, value in pairs(queryResults) do
        -- Example processing: format the key-value pairs
         table.insert(results, string.format("<tr><td>%s</td><td>%s</td></tr>", tostring(key), tostring(value)))
         table.insert(results, string.format("Key: %s, Value: %s", tostring(key), tostring(value)))
     end
     end


     -- Concatenate results into a string for output
     -- End HTML table
    table.insert(results, "</table>")
     return table.concat(results, "\n")
     return table.concat(results, "\n")
--return queryResults['results']
    --return queryResults['results']
end
end



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