Module:PublicationAuthorList: Difference between revisions
From MaRDI portal
No edit summary |
No edit summary |
||
| (4 intermediate revisions by one other user not shown) | |||
| Line 24: | Line 24: | ||
local name = binding.valueLabel.value | local name = binding.valueLabel.value | ||
if string.find(name, "https://") then | if string.find(name, "https://") then | ||
name = " | name = helper.titleNotAvailableStr("Author name") | ||
end | end | ||
| Line 32: | Line 32: | ||
link = link:gsub("entity/Q", "wiki/Person:") | link = link:gsub("entity/Q", "wiki/Person:") | ||
end | end | ||
-- This line to add styling | -- This line to add styling | ||
-- nameAndLink = '<span style="background-color:#f0f0f0; border:1px solid #ccc; border-radius:4px; padding:2px 6px; margin:2px; display:inline-block;">👤 ' .. nameAndLink .. '</span>' | -- nameAndLink = '<span style="background-color:#f0f0f0; border:1px solid #ccc; border-radius:4px; padding:2px 6px; margin:2px; display:inline-block;">👤 ' .. nameAndLink .. '</span>' | ||
resultsString = resultsString .. | resultsString = resultsString .. helper.makeWikiLink(link, name) | ||
end | end | ||
end | end | ||
| Line 170: | Line 162: | ||
end | end | ||
return | local no_name = helper.titleNotAvailableStr("Author name") | ||
return no_name | |||
end | end | ||
Latest revision as of 13:43, 3 June 2025
Documentation for this module may be created at Module:PublicationAuthorList/doc
-- Required module containing helper methods
local helper = require('Module:HelperMethods')
-- 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 convert JSON results into a comma-separated string
function p.convertJsonToCommaSeparatedList(jsonResults)
local resultsString = ""
if jsonResults and jsonResults.results and jsonResults.results.bindings then
local bindings = jsonResults.results.bindings
for i = 0, #bindings do
local binding = bindings[i]
if binding.valueLabel and binding.valueLabel.value then
if resultsString ~= "" then
resultsString = resultsString .. ", "
end
local name = binding.valueLabel.value
if string.find(name, "https://") then
name = helper.titleNotAvailableStr("Author name")
end
local link = binding.value.value
if binding.isHuman and binding.isHuman.value == "Y" then
link = link:gsub("entity/Q", "wiki/Person:")
end
-- This line to add styling
-- nameAndLink = '<span style="background-color:#f0f0f0; border:1px solid #ccc; border-radius:4px; padding:2px 6px; margin:2px; display:inline-block;">👤 ' .. nameAndLink .. '</span>'
resultsString = resultsString .. helper.makeWikiLink(link, name)
end
end
end
return resultsString
end
function p.convertJsonToCommaSeparatedListTextOnly(jsonResults)
local resultsString = ""
if jsonResults and jsonResults.results and jsonResults.results.bindings then
local bindings = jsonResults.results.bindings
for i = 0, #bindings do -- Lua arrays are 1-indexed!
local binding = bindings[i]
if binding.value and binding.value.value then
local name = binding.value.value
if resultsString ~= "" then
resultsString = resultsString .. ", "
end
resultsString = resultsString .. name
end
end
end
return resultsString
end
-- Function to build the list
function p.buildAuthorList(frame)
-- Retrieve target1 from frame arguments or return error message if not set
local target1 = frame.args[1]
if not target1 or target1 == '' then
return "No ID given"
end
-- Constructing the SPARQL query with dynamic entity target1
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 DISTINCT ?value ?valueLabel ?property ?propertyLabel ?isHuman ?seriesOrdinal
WHERE {
target1: p:P16 ?authorstatement .
?authorstatement ps:P16 ?value.
?value wdt:P31 ?instance.
OPTIONAL { ?authorstatement pq:P146 ?seriesOrdinal.}
BIND(wdt:P16 AS ?property)
BIND(IF(?instance = wd:Q57162, "Y", "N") AS ?isHuman)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY xsd:integer(?seriesOrdinal)
]]
-- mw.log( sparqlQuery )
-- 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
local authorList = p.convertJsonToCommaSeparatedList(jsonResults)
mw.log(authorList)
return authorList
end
-- ELSE: no authors found
-- -> Repeat with authors as text
-- Constructing the SPARQL query with dynamic entity target1
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 DISTINCT ?value ?property ?propertyLabel
WHERE {
target1: wdt:P43 ?value. # direct link to string literal
BIND(wdt:P43 AS ?property)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
]]
-- mw.log( sparqlQuery )
-- 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
local authorList = p.convertJsonToCommaSeparatedListTextOnly(jsonResults)
mw.log(authorList)
return authorList
end
local no_name = helper.titleNotAvailableStr("Author name")
return no_name
end
-- Return the created html table
return p