Module:BacklinksList

From MaRDI portal
Revision as of 19:43, 10 September 2024 by Tconrad (talk | contribs) (item type is now converted to string)

Documentation for this module may be created at Module:BacklinksList/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 and binding.subjectLabel and binding.subjectLabel.value then
                if resultsString ~= "" then
                    resultsString = resultsString .. ", "
                end
                
                -- Extract the name
                local name = binding.subjectLabel.value
                if string.find(name, "https://") then
                	name = "Unnamed Item"
                end
                
                -- Extract the item type
                local itemType = ""
                if binding.itemType and binding.itemType.value then
                    local itemTypeLink = binding.itemType.value
                    
                    if itemTypeLink:match("Q5976449$") then
                        itemType = "Software"
                    elseif itemTypeLink:match("Q5976450$") then
                        itemType = "Dataset"
                    else
                        itemType = "Undefined"
                    end
                end
                
                -- Build the link
                local link = binding.subject.value
                link = link:gsub("entity/Q", "wiki/Item:Q")
                
                local nameAndLink = "[" .. link .. " " .. itemType .. name .. "]"

                resultsString = resultsString .. nameAndLink
            end
        end
    end

    return resultsString
end


-- Function to build the list
function p.getBacklinkList(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    
    
    -- Constructing the SPARQL query with dynamic entity target1
    local sparqlQuery = [[
	select distinct ?subject ?subjectLabel ?itemType
	{
		values (?item) {(wd:]] .. target1 .. [[)}
		?subject ?predicate ?item .
		?property wikibase:directClaim ?predicate
		OPTIONAL {
            ?subject wdt:P1460 ?itemType .
        }                  
		service wikibase:label { bd:serviceParam wikibase: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 nil
	end
	
	if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
        return nil
	end

	local linkList = p.convertJsonToCommaSeparatedList(jsonResults)
	-- mw.log(linkList) 

	local result = [[
<div class="keywords-header">
<span class="keywords-title">Related Items</span>
<div class="keywords-line"></div>
</div>
<div class="keywords-list"> ]] .. linkList .. [[</div>
]]

	
    return result
end

-- Return the created html table
return p