Module:TestTim1: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 10: Line 10:
function p.buildTableFromSparql(frame)
function p.buildTableFromSparql(frame)
mw.log("Calling buildTableFromSparql")
-- mw.log("Calling buildTableFromSparql")


     local target1 = frame.args[1] or 'Q20895785' -- Default value if not provided
     local target1 = frame.args[1] or 'Q161115' -- Default value if not provided
    local target2 = frame.args[2] or 'Q70357595' -- Default value if not provided
    local combineFirstTwoColumns = trimAndLower(frame.args[3]) == "true"


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


    SELECT
SELECT
      ?publication_date
  ?publication_date
      ?work ?workLabel (CONCAT("/work/", SUBSTR(STR(?work), 32)) AS ?workUrl)
  ?work ?workLabel (CONCAT("/work/", SUBSTR(STR(?work), 32)) AS ?workUrl)
    WHERE {
 
      ?work wdt:P50 target1: ;
WHERE {
            wdt:P4510 target2: .
  ?work wdt:P16 target1: .
      OPTIONAL {
 
        ?work wdt:P577 ?publication_datetime .
  OPTIONAL {
        BIND(xsd:date(?publication_datetime) AS ?publication_date)
    ?work wdt:P28 ?publication_datetime .
      }
    BIND(xsd:date(?publication_datetime) AS ?publication_date)
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,es,fr,jp,nl,no,ru,sv,zh". }
  }
    }
 
    ORDER BY DESC(?publication_date)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
 
ORDER BY DESC(?publication_date)
     ]]
     ]]


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


-- local jsonResults = sparql.runQuery(sparqlQuery)
local jsonResults = sparql.runQuery(sparqlQuery)
local endpointUrl = ' https://query.portal.mardi4nfdi.de/proxy/wdqs/bigdata/namespace/wdq/sparql'
local jsonResults = sparql.runQuery(sparqlQuery, endpointUrl)


if jsonResults and jsonResults.error then
if jsonResults and jsonResults.error then
Line 49: Line 49:
-- logging
-- logging
-- mw.log(sparqlQuery)
-- mw.log(sparqlQuery)
mw.logObject(jsonResults)
-- mw.logObject(jsonResults)
local headers = {}
local headers = {}
Line 55: Line 55:
headers = jsonResults.head.vars
headers = jsonResults.head.vars
end
end
-- mw.logObject(headers)
local dataTable = convertJsonToTable(jsonResults)
local dataTable = convertJsonToTable(jsonResults)
return createHtmlTable(dataTable, headers, combineFirstTwoColumns)
return createHtmlTable(dataTable, headers)
end
end


Line 80: Line 83:
end
end


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


if combineFirstTwoColumns and #headers > 1 then
local headerRow = htmlTable:tag('tr')
local headerRow = htmlTable:tag('tr')
headerRow:tag('th'):wikitext(headers[0] .. " + " .. headers[1])
    -- Static headers instead of dynamic ones
for j = 2, #headers do
    -- Comment: Only 2 cols, because 1&2 are combined and 3rd is skipped
headerRow:tag('th'):wikitext(headers[j])
    headerRow:tag('th'):wikitext("Publication")
end
    headerRow:tag('th'):wikitext("Date of Publication")
for _, row in ipairs(dataTable) do
local dataRow = htmlTable:tag('tr')
for _, row in ipairs(dataTable) do
local combinedData = '[' .. row[1] .. ' ' .. row[2] .. ']'
local dataRow = htmlTable:tag('tr')
dataRow:tag('td'):wikitext(combinedData)
local combinedData = '[' .. row[1] .. ' ' .. row[2] .. ']'
for j = 3, #row do
dataRow:tag('td'):wikitext(combinedData)
dataRow:tag('td'):wikitext(row[j])
end
-- add col 4 - IMPORTANT: col 3 is skipped
end
for j = 4, #row do
else
dataRow:tag('td'):wikitext(row[j])
if #headers > 1 then
local headerRow = htmlTable:tag('tr')
for j = 0, #headers do
headerRow:tag('th'):wikitext(headers[j])
end
end
for _, row in ipairs(dataTable) do
local dataRow = htmlTable:tag('tr')
for _, data in ipairs(row) do
dataRow:tag('td'):wikitext(data)
end
end
end
end
end

Latest revision as of 12:10, 6 December 2023

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
    -- Comment: Only 2 cols, because 1&2 are combined and 3rd is skipped
    headerRow:tag('th'):wikitext("Publication")
    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