Module:PaperOfTheDay

From MaRDI portal
Revision as of 19:09, 18 November 2024 by Tconrad (talk | contribs)

Documentation for this module may be created at Module:PaperOfTheDay/doc

local p = {}
local sparql = require('SPARQL')
local mwHtml = require('mw.html')


function p.query(frame)
    local sparqlQuery = [[
        PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
        PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
        SELECT ?item ?itemLabel
        WHERE {  
            ?item wdt:P31 wd:Q57162 .
            ?item rdfs:label ?itemLabel .
        }
        LIMIT 1
    ]]
    
    -- Executing the SPARQL query and retrieving results in JSON format
	local jsonResults = sparql.runQuery(sparqlQuery)
	
	mw.logObject(jsonResults)

	-- 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
	
	-- Parse the results and extract the content
    local bindings = jsonResults.results and jsonResults.results.bindings
    if not bindings or #bindings == 0 then
        return "No results found."
    end

    -- Extract the first result
    local result = bindings[1]
    local item = result.item and result.item.value or "Unknown Item"
    local itemLabel = result.itemLabel and result.itemLabel.value or "Unknown Label"

    -- Format the result as a link (HTML)
    return string.format('<a href="%s">%s</a>', item, mw.text.encode(itemLabel))
	
end

return p