Module:TA4TK:myTest: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
Line 62: Line 62:
     end
     end


mw.logObject(imageClaims)
return mw.logObject(imageClaims)
mw.logObject(imageFilename)
end
mw.logObject(imageLegend1)
-- mw.logObject(imageFilename)
-- mw.logObject(imageLegend1)
     --return two images
     --return two images
    return string.format("[[File:%s|thumb|left|%s|%s]]  [[File:%s|thumb|center|%s|%s]]", imageFilename1, width, defaultLegend, imageFilename2, width, defaultLegend)
--     return string.format("[[File:%s|thumb|left|%s|%s]]  [[File:%s|thumb|center|%s|%s]]", imageFilename1, width, defaultLegend, imageFilename2, width, defaultLegend)
    end
--    end
      
      
end
end

Revision as of 18:08, 6 February 2025

Documentation for this module may be created at Module:TA4TK:myTest/doc

-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local mwHtml = require('mw.html')

local p = {}


local p = {}

function p.getImageWithLegend(frame)
    local propertyId = "P1088"    -- Property ID for the local image
    local qualifierIdLegend = "P648"   -- Property ID for the (qualifier) media legend
	local entityId = "Q4610"           -- The target entity
    local width = "480px"  
    local defaultLegend = "No legend available."
    
    
    -- Attempt to retrieve entity data
    local entity = mw.wikibase.getEntity(entityId)
    if not entity or not entity.claims[propertyId] then
        return "No image found"
    end
    
    local imageClaims = entity.claims and entity.claims[propertyId]

    -- Extract the first image filename from P1088
    local imageFilename1 = imageClaims[1].mainsnak.datavalue.value
    if not imageFilename1 then
        return "No valid image found"
    end

    -- Fetch the legend as a qualifier
    local imageLegend1 = nil
    if imageClaims[1] and imageClaims[1].qualifiers and imageClaims[1].qualifiers[qualifierIdLegend] then
        -- Extract the first qualifier value for P648
        local legendQualifier = imageClaims[1].qualifiers[qualifierIdLegend][1]
        if legendQualifier and legendQualifier.datavalue and legendQualifier.datavalue.value then
            imageLegend1 = legendQualifier.datavalue.value -- This should now be a string
        end
    end

    -- Use the default legend if none was found
    if not imageLegend1 or imageLegend1 == "" then
        imageLegend1 = defaultLegend
    end

    
	-- Extract the second image filename from P1088
	local imageFilename2 = imageClaims[2].mainsnak.datavalue.value
    if not imageFilename2 then
    	--return one image
	    return string.format("[[File:%s|thumb|center|%s|%s]]", imageFilename1,width, imageLegend1)
    else     	
    	    -- Fetch the legend as a qualifier
		    local imageLegend2 = nil
    		if imageClaims[2] and imageClaims[2].qualifiers and imageClaims[2].qualifiers[qualifierIdLegend] then
        -- Extract the first qualifier value for P648
        local legendQualifier = imageClaims[2].qualifiers[qualifierIdLegend][1]
        if legendQualifier and legendQualifier.datavalue and legendQualifier.datavalue.value then
            imageLegend2 = legendQualifier.datavalue.value -- This should now be a string
        end
    end

	return mw.logObject(imageClaims)
	end
--	mw.logObject(imageFilename)
--	mw.logObject(imageLegend1)
    	--return two images
--	     return string.format("[[File:%s|thumb|left|%s|%s]]  [[File:%s|thumb|center|%s|%s]]", imageFilename1, width, defaultLegend, imageFilename2, width, defaultLegend)
--    end
    
end

function convertJsonToTable(jsonResults)
	local resultsTable = {}
	if jsonResults and jsonResults.results and jsonResults.results.bindings then
		local bindings = jsonResults.results.bindings
		for j=0, #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 to build the list
function p.buildList2(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/Q6534342>
SELECT ?Computational_Task WHERE {
  target1: wdt:P147 ?URL.
  ?URL rdfs:label ?Label
  BIND(CONCAT("<a href=", STR(?URL),  ">", STR(?Label) ,"</a>") as ?Computational_Task)

}
]]
	-- Executing the SPARQL query and retrieving results in JSON format
	local jsonResults = sparql.runQuery(sparqlQuery)
	
	
    return jsonResults['results']['bindings'][0]['Computational Task']['value']
	--local dataTable = convertJsonToTable(jsonResults)
    --return testTable
    --return jsonResults
    
end


function p.hello()
	local str = "Hello World!"
	return str
end

function p.renderFormula()
    return mw.getCurrentFrame():extensionTag{
		name="math",
		content= "\\sin x", 
	}
end

-- 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 task"
                end
                
                local link = binding.value.value
                link = link:gsub("entity/Q", "wiki/Task:")
                
                local nameAndLink = "[" .. link .. " " .. name .. "]"

                resultsString = resultsString .. nameAndLink
            end
        end
    end

    return resultsString
end


-- Function to build the list
function p.buildList(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://staging.mardi4nfdi.org/entity/]] .. target1 .. [[>
SELECT ?value ?valueLabel WHERE {
  ?value wdt:P715 target1:.
  target1: rdfs:label ?valueLabel
}
    ]]
    
	-- Executing the SPARQL query and retrieving results in JSON format
	local jsonResults = sparql.runQuery(sparqlQuery)
	
	-- 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
	
	local testList = p.convertJsonToCommaSeparatedList(jsonResults)

    return testList
    
end

-- Function to build the list in the real portal
function p.buildListPortal(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 .. [[>
SELECT ?value ?valueLabel WHERE {
  ?value wdt:P715 target1:.
  target1: rdfs:label ?valueLabel
}
    ]]
    
	-- Executing the SPARQL query and retrieving results in JSON format
	local jsonResults = sparql.runQuery(sparqlQuery)
	
	-- 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
	
	local testList = p.convertJsonToCommaSeparatedList(jsonResults)

    return testList
    
end


-- Function to build the description
function p.buildDescription(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 = [[
SELECT ?Description
WHERE {
      wd:Q4610 wdt:P896 ?Description.
}
    ]]

	-- Executing the SPARQL query and retrieving results in JSON format
	local jsonResults = sparql.runQuery(sparqlQuery)
    return jsonResults['results']['bindings'][0]['Description']['value']
end



function p.getImage(frame)
    local entityId = "Q4610"  -- The target entity
    local propertyId = "P1088" -- Property assumed to store the image
    local width = "480px"      -- Desired image width

    -- Attempt to retrieve entity data
    local entity = mw.wikibase.getEntity(entityId)
    if not entity or not entity.claims[propertyId] then
        return "No image found"
    end

    -- Extract the first image filename from P1088
    local imageFilename1 = entity.claims[propertyId][1].mainsnak.datavalue.value
    if not imageFilename1 then
        return "No valid image found"
    end
	-- Extract the second image filename from P1088
	local imageFilename2 = entity.claims[propertyId][2].mainsnak.datavalue.value
    if not imageFilename2 then
    	--return one image
	    return string.format("[[File:%s|thumb|center|%s]]", imageFilename1, width)
    else     	
    	--return two images
	     return string.format("[[File:%s|left|%s]]  [[File:%s|center|%s]] \n \n  \n ", imageFilename1, width, imageFilename2, width)
    end
end



return p