Module:SoftwareLicenseList

From MaRDI portal

Documentation for this module may be created at Module:SoftwareLicenseList/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 build the list
function p.buildLicenseList(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 records found"
    end

    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 ?license ?licenseLabel ?logo
WHERE {
  target1: wdt:P163 ?license .
  OPTIONAL { ?license wdt:P355 ?logo . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
]]

    local jsonResults = sparql.runQuery(sparqlQuery)

    if not jsonResults then
        return "Could not fetch data."
    end

    if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
        return "No records found."
    end

    local parts = {}

    if jsonResults.results and jsonResults.results.bindings then
        local bindings = jsonResults.results.bindings
        for i = 0, #bindings do
            local b = bindings[i]
            if b and b.licenseLabel and b.licenseLabel.value then
                local name = b.licenseLabel.value
                local link = b.license.value
                link = link:gsub("entity/Q", "wiki/Item:Q")

                local entry = helper.makeWikiLink(link, name)

                -- Prepend logo if available
                if b.logo and b.logo.value then
                    local logoFile = b.logo.value
                    -- Strip any URL prefix, keep just the filename
                    logoFile = logoFile:match("([^/]+)$") or logoFile
                    entry = "[[File:" .. logoFile .. "|60px|link=]] " .. entry
                end

                table.insert(parts, entry)
            end
        end
    end

    if #parts == 0 then
        return "No records found."
    end

    local result = table.concat(parts, ", ")
    return mw.getCurrentFrame():preprocess(result)
end

-- Return the created html table
return p