Module:TA4TK:myTest: Difference between revisions
From MaRDI portal
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
Line 185: | Line 185: | ||
function p.getGalleryFromEntity(entityId, propertyId) | function p.getGalleryFromEntity(entityId, propertyId) | ||
-- Ensure entityId is a string | -- Ensure entityId is a string | ||
if type(entityId) ~= "string" then | if type(entityId) ~= "string" then |
Revision as of 11:47, 6 February 2025
Documentation for this module may be created at Module:TA4TK:myTest/doc
-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local mwHtml = require('mw.html')
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 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 list in the real portal
function p.buildListPortal(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://portal.mardi4nfdi.de/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)
return jsonResults['results']['bindings'][0]['Description']['value']
end
function p.getResizedImage(frame)
local entityId = "Q4610" -- The target entity
local propertyId = "P1088" -- Property assumed to store the image
local width = "480px" -- Desired image width
local height = "200px" -- Desired image width
-- Attempt to retrieve entity data
local entity = mw.wikibase.getEntity(entityId)
if not entity or not entity.claims[propertyId] then
return "No image found"
end
-- Extract up to two image filenames from P1088
local claims = entity.claims[propertyId]
if not claims or #claims == 0 then
return "No image found"
end
-- Get the first image (if available)
local image1 = claims[1] and claims[1].mainsnak and claims[1].mainsnak.datavalue and claims[1].mainsnak.datavalue.value
-- Get the second image (if available)
local image2 = claims[2] and claims[2].mainsnak and claims[2].mainsnak.datavalue and claims[2].mainsnak.datavalue.value
-- Ensure at least one valid image exists
if not image1 then
return "No valid images found"
elseif not image2 then
return string.format("<gallery>\nFile:%s|First Image\n</gallery>", image1) -- Only one image available
end
-- Return formatted Wiki gallery markup
return string.format("<gallery>\nFile:%s|First Image\nFile:%s|Second Image\n</gallery>", image1, image2)
end
function p.getGalleryFromEntity(entityId, propertyId)
-- Ensure entityId is a string
if type(entityId) ~= "string" then
return "Invalid entity ID"
end
-- Retrieve the entity data
local entity = mw.wikibase.getEntity(entityId)
if not entity or not entity.claims or not entity.claims[propertyId] then
return "No image found"
end
local claims = entity.claims[propertyId]
local imageList = {}
-- Extract up to two images
for i = 1, math.min(2, #claims) do
local mainsnak = claims[i].mainsnak
if mainsnak and mainsnak.datavalue and mainsnak.datavalue.value then
table.insert(imageList, mainsnak.datavalue.value)
end
end
-- If no images found, return a message
if #imageList == 0 then
return "No valid images found"
end
-- Construct Wikimedia gallery format
local galleryMarkup = "<gallery>\n"
for _, image in ipairs(imageList) do
galleryMarkup = galleryMarkup .. string.format("File:%s|Image\n", image)
end
galleryMarkup = galleryMarkup .. "</gallery>"
return galleryMarkup
end
return p