Module:PersonOtherPropertiesScholiaStyle

From MaRDI portal

This module is not used at the moment.

This is getting the properties (actually relations) from the local (MaRDI) knowledge graph, similar to the style that Scholia is using. I left it for comparison reasons.


-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local mwHtml = require('mw.html')

-- Main table to hold all functions
local p = {}

-- Function to build a HTML table from SPARQL query results
function p.buildTableFromSparql(frame)
	
    -- Retrieve target1 from frame arguments or return error message if not set
	local target = frame.args[1]
    if not target or target == '' then
        return "No records found"
    end    

    -- Constructing the SPARQL query with dynamic entity target1
    local sparqlQuery = [[
PREFIX target: <https://portal.mardi4nfdi.de/entity/>
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>

SELECT ?node ?nodeLabel ?childNode ?childNodeLabel 
WITH {
  SELECT DISTINCT ?node ?childNode WHERE {
      BIND(target:]] .. target .. [[ AS ?node)
      ?node ?p ?i.
      ?childNode ?x ?p.
      ?childNode rdf:type wikibase:Property.
      FILTER(STRSTARTS(STR(?i), "https://portal.mardi4nfdi.de/entity/Q"))
      FILTER(STRSTARTS(STR(?childNode), "https://portal.mardi4nfdi.de/entity/P"))
    }
  LIMIT 5000
} AS %nodes
WITH {
  SELECT DISTINCT ?childNode ?node WHERE {
      target:]] .. target .. [[ ?p ?childNode.
      ?node ?x ?p.
      ?node rdf:type wikibase:Property.
      FILTER(STRSTARTS(STR(?childNode), "https://portal.mardi4nfdi.de/entity/Q"))
    }
  LIMIT 5000
} AS %childNodes
WHERE {
  {
    INCLUDE %nodes
  }
  UNION
  {
    INCLUDE %childNodes
  }
  FILTER (?node != target:]] .. target .. [[)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }        
}
ORDER BY ASC(?nodeLabel) ASC(?childNodeLabel)

    ]]

	-- Executing the SPARQL query and retrieving results in JSON format
	local jsonResults = sparql.runQuery(sparqlQuery)

	-- Handle error in SPARQL query execution
	if jsonResults and jsonResults.error then
    	mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
    	return nil
	end
	
	-- Extracting headers from the SPARQL query results
	-- NOTE that these headers are not used since we will use hard coded ones below
	local headers = {}
	if jsonResults and jsonResults.head and jsonResults.head.vars then
		headers = jsonResults.head.vars
	end
	
	-- Hard-code headers
    local headers = {"Property", "Value"}

	-- Convert the JSON results into a Lua table
	local dataTable = convertJsonToTable(jsonResults)

	-- Create and return a HTML table from the data
	return createHtmlTableWithMergedCols(dataTable, headers)
end

-- Utility function to trim and lowercase a string
function trimAndLower(str)
	if str == nil then return nil end
	str = str:gsub("^%s*(.-)%s*$", "%1")
	return str:lower()
end

-- Function to convert JSON results into a Lua table
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 to create a HTML table from a Lua table
function createHtmlTableWithMergedCols(dataTable, headers)
	local htmlTable = mwHtml.create('table')
	htmlTable:addClass('wikitable'):attr('border', '1')

	local headerRow = htmlTable:tag('tr')
	
	-- Use the provided headers
    for _, header in ipairs(headers) do
        headerRow:tag('th'):wikitext(header)
    end

	-- The first two columns are merged
	for _, row in ipairs(dataTable) do

		if not string.find(row[1], "/entity/statement/") then

			-- mw.logObject(row)
		
			local dataRow = htmlTable:tag('tr')
			local combinedData1, combinedData2
			
			combinedData1 = '[' .. row[1] .. ' ' .. row[2] .. ']'
			combinedData2 = '[' .. row[3] .. ' ' .. row[4] .. ']'

			-- Add rows to table
		    dataRow:tag('td'):wikitext(combinedData1)
		    dataRow:tag('td'):wikitext(combinedData2)

		end -- if not string.find(row[1], "/entity/statement/") then
		
	end
	
	mw.log(htmlTable)

	return tostring(htmlTable)
end

-- Return the created html table
return p