Module:Test: Difference between revisions
From MaRDI portal
No edit summary |
No edit summary |
||
Line 19: | Line 19: | ||
if jsonResults and jsonResults.results and jsonResults.results.bindings then | if jsonResults and jsonResults.results and jsonResults.results.bindings then | ||
local bindings = jsonResults.results.bindings | local bindings = jsonResults.results.bindings | ||
for i = 0, #bindings do | |||
local binding = bindings[i] | |||
if binding.valueLabel and binding.valueLabel.value then | |||
if resultsString ~= "" then | |||
resultsString = resultsString .. ", " | |||
end | |||
local name = binding.valueLabel.value | |||
if string.find(name, "https://") then | |||
name = "Unnamed task" | |||
end | |||
local link = binding.value.value | |||
link = link:gsub("entity/Q", "wiki/Task:") | |||
local nameAndLink = "[" .. link .. " " .. name .. "]" | |||
resultsString = resultsString .. nameAndLink | |||
end | |||
end | |||
end | end | ||
Line 82: | Line 102: | ||
local jsonResults = sparql.runQuery(sparqlQuery) | local jsonResults = sparql.runQuery(sparqlQuery) | ||
local testList = p.convertJsonToCommaSeparatedList(jsonResults) | local testList = p.convertJsonToCommaSeparatedList(jsonResults) | ||
return mw.html.create('pre'):wikitext(testList):done() | -- return mw.html.create('pre'):wikitext(testList):done() | ||
return jsonResults['results']['bindings'][0]['Description']['value'] | |||
end | end |
Revision as of 11:19, 29 January 2025
Documentation for this module may be created at Module:Test/doc
-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local mwHtml = require('mw.html')
-- Main table to hold all functions
local p = {}
function p.renderFormula()
return mw.getCurrentFrame():extensionTag{
name="math",
content= "\\sin x",
}
end
-- Function to convert JSON results into a comma-separated string
function p.convertJsonToCommaSeparatedList(jsonResults)
local resultsString = ""
if jsonResults and jsonResults.results and jsonResults.results.bindings then
local bindings = jsonResults.results.bindings
for i = 0, #bindings do
local binding = bindings[i]
if binding.valueLabel and binding.valueLabel.value then
if resultsString ~= "" then
resultsString = resultsString .. ", "
end
local name = binding.valueLabel.value
if string.find(name, "https://") then
name = "Unnamed task"
end
local link = binding.value.value
link = link:gsub("entity/Q", "wiki/Task:")
local nameAndLink = "[" .. link .. " " .. name .. "]"
resultsString = resultsString .. nameAndLink
end
end
end
return resultsString
end
-- Function to build the list
function p.buildList(frame)
-- Retrieve target1 from frame arguments or return error message if not set
local target1 = frame.args[1]
if not target1 or target1 == '' then
return "No ID given"
end
-- Constructing the SPARQL query with dynamic entity target1
local sparqlQuery = [[
PREFIX target1: <https://staging.mardi4nfdi.org/entity/]] .. target1 .. [[>
SELECT ?value ?valueLabel WHERE {
?value wdt:P715 target1:.
target1: rdfs:label ?valueLabel
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
-- Handle error in SPARQL query execution
if jsonResults and jsonResults.error then
mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
return nil
end
if not jsonResults then
return "Could not fetch data."
end
local testList = p.convertJsonToCommaSeparatedList(jsonResults)
return testList
end
-- Function to build the description
function p.buildDescription(frame)
-- Retrieve target1 from frame arguments or return error message if not set
local target1 = frame.args[1]
if not target1 or target1 == '' then
return "No ID given"
end
-- Constructing the SPARQL query with dynamic entity target1
local sparqlQuery = [[
SELECT ?Description
WHERE {
wd:Q4610 wdt:P896 ?Description.
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
local testList = p.convertJsonToCommaSeparatedList(jsonResults)
-- return mw.html.create('pre'):wikitext(testList):done()
return jsonResults['results']['bindings'][0]['Description']['value']
end
-- Return the created html table
return p