Module:PaperOfTheDay: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
Line 5: Line 5:


function p.query(frame)
function p.query(frame)
     local query = [[
     local sparqlQuery = [[
         PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
         PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
         PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
         PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
Line 15: Line 15:
         LIMIT 1
         LIMIT 1
     ]]
     ]]
     local result = sparql.query(query)
      
    if result and result[1] then
    -- Executing the SPARQL query and retrieving results in JSON format
        return string.format(
local jsonResults = sparql.runQuery(sparqlQuery)
            '<a href="%s">%s</a>',
            result[1].item.value,
-- mw.logObject(jsonResults)
            result[1].itemLabel.value
 
        )
-- Handle error in SPARQL query execution
    else
if jsonResults and jsonResults.error then
         return "No result found."
    mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
    end
    return nil
end
 
if not jsonResults then
         return "Could not fetch data."
end
return jsonResults
 
end
end


return p
return p

Revision as of 19:02, 18 November 2024

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
	
	return jsonResults

end

return p