Module:PaperOfTheDay: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
-- Required module containing helper methods
local helper = require('Module:HelperMethods')
local sparql = require('SPARQL')
local sparql = require('SPARQL')
local mwHtml = require('mw.html')
local mwHtml = require('mw.html')


local p = {}


function p.query(frame)
function p.query(frame)
Line 19: Line 21:
local jsonResults = sparql.runQuery(sparqlQuery)
local jsonResults = sparql.runQuery(sparqlQuery)
mw.logObject(jsonResults)
-- mw.logObject(jsonResults)


-- Handle error in SPARQL query execution
-- Handle error in SPARQL query execution
Line 31: Line 33:
end
end
-- Parse the results and extract the content
if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
    local bindings = jsonResults.results and jsonResults.results.bindings
         return "No records found."
    if not bindings or #bindings == 0 then
end
         return "No results found."
    end


    -- Extract the first result
-- mw.logObject(jsonResults)
    local result = bindings[1]
 
    local item = result.item and result.item.value or "Unknown Item"
-- Extract the first item's label
    local itemLabel = result.itemLabel and result.itemLabel.value or "Unknown Label"
local firstBinding = jsonResults.results.bindings[0]
if firstBinding and firstBinding.itemLabel and firstBinding.itemLabel.value then
return firstBinding.itemLabel.value
end


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


return p
return p

Revision as of 22:45, 18 November 2024

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

-- Required module containing helper methods
local helper = require('Module:HelperMethods')
local sparql = require('SPARQL')
local mwHtml = require('mw.html')

local p = {}

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
	
	if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
        return "No records found."
	end

	-- mw.logObject(jsonResults)

	-- Extract the first item's label
	local firstBinding = jsonResults.results.bindings[0]
	if firstBinding and firstBinding.itemLabel and firstBinding.itemLabel.value then
		return firstBinding.itemLabel.value
	end

	return "Label not found."
	
end

return p