Module:TestTim1

From MaRDI portal
Revision as of 12:10, 6 December 2023 by Tconrad (talk | contribs)

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

-- This example builds a HTML table with the results of a SPARQL query.
-- By default one column is used per SPARQL variable.
-- If the second parameter is set to "true", the first two columns are combined into one column.

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

local p = {}

function p.buildTableFromSparql(frame)
	
	-- mw.log("Calling buildTableFromSparql")

    local target1 = frame.args[1] or 'Q161115' -- Default value if not provided

    local sparqlQuery = [[
PREFIX target1: <https://portal.mardi4nfdi.de/entity/]] .. target1 .. [[>
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>

SELECT
  ?publication_date
  ?work ?workLabel (CONCAT("/work/", SUBSTR(STR(?work), 32)) AS ?workUrl)

WHERE {
  ?work wdt:P16 target1: .

  OPTIONAL {
    ?work wdt:P28 ?publication_datetime .
    BIND(xsd:date(?publication_datetime) AS ?publication_date)
  }
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

ORDER BY DESC(?publication_date)
    ]]

	-- PHP function sparql.runQuery(query) is called
	-- mw.log("Executing SPARQL query: " .. sparqlQuery)

	local jsonResults = sparql.runQuery(sparqlQuery)

	if jsonResults and jsonResults.error then
    	mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
    	return nil
	end
	
	-- logging
	-- mw.log(sparqlQuery)
	-- mw.logObject(jsonResults)
	
	local headers = {}
	if jsonResults and jsonResults.head and jsonResults.head.vars then
		headers = jsonResults.head.vars
	end
	
	-- mw.logObject(headers)

	local dataTable = convertJsonToTable(jsonResults)
	return createHtmlTable(dataTable, headers)
end

function trimAndLower(str)
	if str == nil then return nil end
	str = str:gsub("^%s*(.-)%s*$", "%1")  -- Trim spaces from both ends
	return str:lower()  -- Convert to lowercase
end

function convertJsonToTable(jsonResults)
    local resultsTable = {}
    if jsonResults and jsonResults.results and jsonResults.results.bindings then
        local bindings = jsonResults.results.bindings
        for j = 1, #bindings do
            local row = {}
            for key, value in pairs(bindings[j]) do
                table.insert(row, value.value)
            end
            table.insert(resultsTable, row)
        end
    end
    return resultsTable
end

function createHtmlTable(dataTable, headers)
	local htmlTable = mwHtml.create('table')
	htmlTable:addClass('wikitable'):attr('border', '1')

	local headerRow = htmlTable:tag('tr')
	
    -- Static headers instead of dynamic ones
    headerRow:tag('th'):wikitext("Publication")
    headerRow:tag('th'):wikitext("Internal Link")
    headerRow:tag('th'):wikitext("Date of Publication")
	
	for _, row in ipairs(dataTable) do
		local dataRow = htmlTable:tag('tr')
		local combinedData = '[' .. row[1] .. ' ' .. row[2] .. ']'
		dataRow:tag('td'):wikitext(combinedData)
		
	-- add col 4 - IMPORTANT: col 3 is skipped
		for j = 4, #row do
			dataRow:tag('td'):wikitext(row[j])
		end
	end

	return tostring(htmlTable)
end

return p