Module:SoftwareCitationList: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 19: Line 19:
             if binding.valueLabel and binding.valueLabel.value then
             if binding.valueLabel and binding.valueLabel.value then
                 if resultsString ~= "" then
                 if resultsString ~= "" then
                     resultsString = resultsString .. ", "
                     resultsString = resultsString .. ""
                 end
                 end
                  
                  
Line 27: Line 27:
                 end
                 end
                  
                  
-- Extract QID from the full type
                local itemType = binding.type and binding.type.value or ""
local itemTypeQID = itemType:match("Q%d+")               
-- Determine namespace prefix based on type QID
local prefix = "Item:Q"  -- default
if itemTypeQID == "Q56885" then
prefix = "Dataset:"
elseif itemTypeQID == "Q56887" then
prefix = "Publication:"
elseif itemTypeQID == "Q57080" then
prefix = "Software:"
end
                 local link = binding.value.value
                 local link = binding.value.value
                 link = link:gsub("entity/Q", "wiki/Publication:")
                 link = link:gsub("entity/Q", "wiki/" .. prefix)
               
 
                 local nameAndLink = "[" .. link .. " " .. name .. "]"
                 local nameAndLink = "<li>[" .. link .. " " .. name .. "]</li>"


                 resultsString = resultsString .. nameAndLink
                 resultsString = resultsString .. nameAndLink
Line 37: Line 51:
     end
     end


     return resultsString
     return "<ul> " .. resultsString .. " </ul>"
end
end


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


SELECT ?property ?propertyLabel ?value ?valueLabel
SELECT ?property ?propertyLabel ?value ?valueLabel ?type ?typeLabel
WHERE {
WHERE {
     target1: wdt:P223 ?value .
     target: wdt:P223 ?value .                           # Find all things cited by the target entity (via P223)
 
     OPTIONAL {
     OPTIONAL {
         ?value rdfs:label ?valueLabel .
         ?value rdfs:label ?valueLabel .                 # Get English label of the cited entity
         FILTER(LANG(?valueLabel) = "en")
         FILTER(LANG(?valueLabel) = "en")
     }
     }
     BIND(wdt:P16 AS ?property)
 
     SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    OPTIONAL {
        ?value wdt:P31 ?type .                          # Get the "instance of" (P31) of the cited entity
    }
 
     BIND(wdt:P223 AS ?property)                         # Bind the property to include in output
 
     SERVICE wikibase:label {                             # Automatically get labels for all entities
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
    }
}
}
ORDER BY ?propertyLabel
     ]]
     ]]
      
      

Latest revision as of 21:05, 7 April 2025

Documentation for this module may be created at Module:SoftwareCitationList/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 = "Unnamed Publication"
                end
                
				-- Extract QID from the full type 
                local itemType = binding.type and binding.type.value or ""
				local itemTypeQID = itemType:match("Q%d+")                
				
				-- Determine namespace prefix based on type QID
				local prefix = "Item:Q"  -- default
				if itemTypeQID == "Q56885" then
					prefix = "Dataset:"
				elseif itemTypeQID == "Q56887" then
					prefix = "Publication:"
				elseif itemTypeQID == "Q57080" then
					prefix = "Software:"
				end
				
                local link = binding.value.value
                link = link:gsub("entity/Q", "wiki/" .. prefix)

                local nameAndLink = "<li>[" .. link .. " " .. name .. "]</li>"

                resultsString = resultsString .. nameAndLink
            end
        end
    end

    return "<ul> " .. resultsString .. " </ul>"
end


-- Function to build the list
function p.buildCitationList(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 = [[
PREFIX target: <https://portal.mardi4nfdi.de/entity/]] .. target1 .. [[>
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>

SELECT ?property ?propertyLabel ?value ?valueLabel ?type ?typeLabel
WHERE {
    target: wdt:P223 ?value .                            # Find all things cited by the target entity (via P223)

    OPTIONAL {
        ?value rdfs:label ?valueLabel .                  # Get English label of the cited entity
        FILTER(LANG(?valueLabel) = "en")
    }

    OPTIONAL {
        ?value wdt:P31 ?type .                           # Get the "instance of" (P31) of the cited entity
    }

    BIND(wdt:P223 AS ?property)                          # Bind the property to include in output

    SERVICE wikibase:label {                             # Automatically get labels for all entities
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
    }
}
ORDER BY ?propertyLabel
    ]]
    
    -- 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
        return "No records found."
	end

	local citationList = p.convertJsonToCommaSeparatedList(jsonResults)
	
	-- mw.log(citationList) 
	
    return citationList
end

-- Return the created html table
return p