Module:TestTim1
From MaRDI portal
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 = {}
local p = require('Module:TestTim1')
function p.buildTableFromSparql(frame)
mw.log("Calling buildTableFromSparql")
local target1 = frame.args[1] or 'Q20895785' -- 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 = [[
PREFIX target1: <http://www.wikidata.org/entity/]] .. target1 .. [[>
PREFIX target2: <http://www.wikidata.org/entity/]] .. target2 .. [[>
SELECT
?publication_date
?work ?workLabel (CONCAT("/work/", SUBSTR(STR(?work), 32)) AS ?workUrl)
WHERE {
?work wdt:P50 target1: ;
wdt:P4510 target2: .
OPTIONAL {
?work wdt:P577 ?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)
]]
-- PHP function sparql.runQuery(query) is called
local jsonResults = sparql.runQuery(sparqlQuery)
-- logging
mw.logObject(jsonResults)
local headers = {}
if jsonResults and jsonResults.head and jsonResults.head.vars then
headers = jsonResults.head.vars
end
local dataTable = convertJsonToTable(jsonResults)
return createHtmlTable(dataTable, headers, combineFirstTwoColumns)
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, combineFirstTwoColumns)
local htmlTable = mwHtml.create('table')
htmlTable:addClass('wikitable'):attr('border', '1')
if combineFirstTwoColumns and #headers > 1 then
local headerRow = htmlTable:tag('tr')
headerRow:tag('th'):wikitext(headers[0] .. " + " .. headers[1])
for j = 2, #headers do
headerRow:tag('th'):wikitext(headers[j])
end
for _, row in ipairs(dataTable) do
local dataRow = htmlTable:tag('tr')
local combinedData = '[' .. row[1] .. ' ' .. row[2] .. ']'
dataRow:tag('td'):wikitext(combinedData)
for j = 3, #row do
dataRow:tag('td'):wikitext(row[j])
end
end
else
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
return tostring(htmlTable)
end
return p