Module:CommunityResearchOutcomesList: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
 
(23 intermediate revisions by the same user not shown)
Line 11: Line 11:
-- Function to build a HTML table from SPARQL query results
-- Function to build a HTML table from SPARQL query results
function p.buildTableFromSparql(frame)
function p.buildTableFromSparql(frame)
   
     -- Retrieve target1 from frame arguments or return error message if not set
     -- Retrieve target1 from frame arguments or return error message if not set
local target1 = frame.args[1]
    local target1 = frame.args[1]
     if not target1 or target1 == '' then
     if not target1 or target1 == '' then
         return "No records found"
         return "No records found"
Line 24: Line 24:
     local baseUrl = mw.site.server
     local baseUrl = mw.site.server


 
     -- Constructing the SPARQL query with dynamic entity target1 and performance improvements
     -- Constructing the SPARQL query with dynamic entity target1
     local sparqlQuery = [[
     local sparqlQuery = [[
PREFIX target1: <https://portal.mardi4nfdi.de/entity/]] .. target1 .. [[>
PREFIX target1: <https://portal.mardi4nfdi.de/entity/]] .. target1 .. [[>
Line 32: Line 31:


SELECT
SELECT
   ?publication_date ?work ?workLabel
   ?publication_date  
   ?workUrl
  ?work  
   ?workLabel
   (REPLACE(STR(?work), "^.*/", "") AS ?qid)
   (REPLACE(STR(?work), "^.*/", "") AS ?qid)
   ?item_type
   ?item_type
   ?item_type_label
   ?item_type_label
   ?internal_project_id
   ?internal_project_item
  ?internal_project_label
    
    
WHERE {
WHERE {
   ?work wdt:P1495 target1: .
   ?work wdt:P1495 target1: .
   VALUES ?allowedValues { wd:Q5976449 wd:Q5976450 wd:Q5984635 }  
 
   ?work wdt:P1460 ?allowedValues.
  # Restrict to allowed item types using VALUES for performance
   VALUES (?item_type ?item_type_label) {  
    (wd:Q5976449 "Paper")
    (wd:Q5984635 "Dataset")
    (wd:Q5976450 "Software")
  }
 
  # Match the item type
   ?work wdt:P1460 ?item_type .


   OPTIONAL {
  # Optional publication date
    ?work wdt:P28 ?publication_datetime .
   OPTIONAL { ?work wdt:P28 ?publication_datetime. }
  }
   BIND(COALESCE(xsd:date(?publication_datetime), "N/A") AS ?publication_date)
  OPTIONAL {
    ?work wdt:P1496 ?internal_project_id .
  }
  OPTIONAL {
    ?work wdt:P170 ?last_updated .
  }
   BIND(COALESCE(xsd:date(?publication_datetime), xsd:date(?last_updated), "N/A") AS ?publication_date)


  # Optional internal project item and label retrieval
   OPTIONAL {
   OPTIONAL {
     ?work wdt:P1460 ?item_type .
     ?work wdt:P1507 ?internal_project_item .
     BIND(REPLACE(STR(?item_type), "^.*/(Q[0-9]+)$", "$1") AS ?item_type_short)
     OPTIONAL {
    BIND(IF(?item_type_short = "Q5976449", "Paper",
       ?internal_project_item rdfs:label ?internal_project_label .
        IF(?item_type_short = "Q5984635", "Dataset",
       FILTER(LANG(?internal_project_label) = "en" || LANG(?internal_project_label) = "de")
        IF(?item_type_short = "Q5976450", "Software", "Other"))) AS ?item_type_label)
     }
    BIND(CONCAT(
       IF(?item_type_short = "Q5976449", "]] .. baseUrl .. [[/wiki/Publication:",
      IF(?item_type_short = "Q5984635", "]] .. baseUrl .. [[/wiki/Dataset:",
       IF(?item_type_short = "Q5976450", "]] .. baseUrl .. [[/wiki/Software:", "]] .. baseUrl .. [[/wiki/"))),
      REPLACE(STR(?work), "^.*/Q", "")
     ) AS ?workUrl)
   }
   }


   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  # Ensure labels are fetched in English or German
   SERVICE wikibase:label { bd:serviceParam wikibase:language "de,en". }
}
}


Line 75: Line 73:
     ]]
     ]]


-- Executing the SPARQL query and retrieving results in JSON format
    -- Log and run the SPARQL query
-- mw.log( sparqlQuery )
    mw.log(sparqlQuery)
    local jsonResults = sparql.runQuery(sparqlQuery)
local jsonResults = sparql.runQuery(sparqlQuery)
   
    -- Handle errors or empty results
-- mw.logObject(jsonResults)  
    if jsonResults and jsonResults.error then
        mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
        return nil
    end


-- Handle error in SPARQL query execution
    if not jsonResults then
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."
         return "Could not fetch data."
end
    end


if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
    if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
         return "No records found."
         return "No records found."
    end
   
    -- Generate the workUrl based on the qid and item_type_label
for _, result in ipairs(jsonResults.results.bindings) do
    local qid = result.qid and result.qid.value or nil
    local itemTypeLabel = result.item_type_label and result.item_type_label.value or nil
   
    -- Remove the leading "Q" from the qid
    if qid then
        qid = string.gsub(qid, "^Q", "")
    end
    -- Check if qid is not nil before generating the workUrl
    if qid and itemTypeLabel then
        -- Dynamically generate the workUrl based on item type
        if itemTypeLabel == "Paper" then
            result.workUrl = {
                type = "uri",
                value = baseUrl .. "/wiki/Publication:" .. qid
            }
        elseif itemTypeLabel == "Dataset" then
            result.workUrl = {
                type = "uri",
                value = baseUrl .. "/wiki/Dataset:" .. qid
            }
        elseif itemTypeLabel == "Software" then
            result.workUrl = {
                type = "uri",
                value = baseUrl .. "/wiki/Software:" .. qid
            }
        else
            result.workUrl = {
                type = "uri",
                value = baseUrl .. "/wiki/Q" .. qid -- Fallback
            }
        end
    else
        -- Handle missing qid or item_type_label
        result.workUrl = {
            type = "literal",
            value = "URL not available"
        }
    end
end
end


-- Extracting headers from the SPARQL query results
-- mw.logObject(jsonResults)
-- local headers = {}
 
-- if jsonResults and jsonResults.head and jsonResults.head.vars then
    -- Convert the JSON results into a Lua table
-- headers = jsonResults.head.vars
     local fieldOrder = {"workUrl", "workLabel", "work", "publication_date", "qid", "internal_project_item", "internal_project_label", "item_type", "item_type_label"}
-- end
    local dataTable = helper.convertJsonToTableOrdered(jsonResults, fieldOrder)
   
-- Convert the JSON results into a Lua table
-- mw.logObject(dataTable)
     local fieldOrder = {"workUrl", "workLabel", "work", "publication_date", "qid", "internal_project_id", "item_type", "item_type_label"}
 
local dataTable = helper.convertJsonToTableOrdered(jsonResults, fieldOrder)
   
     -- Create and return HTML table from the data
     -- Create and return HTML table from the data
     local headers = {"Publication", "Date of Publication", "Project", "Type"}
     local headers = {"Title", "Date of Publication", "Project", "Type"}
     local htmlTable =  helper.createHtmlTableWithMergedCols(dataTable, headers, {{1, 2}, {4}, {6}, {7, 8}})
     local htmlTable =  helper.createHtmlTableWithMergedCols(dataTable, headers, {{1, 2}, {4}, {6,7}, {8, 9}})


-- Generate histogram data
    -- Generate histogram data
     local histogramChart = helper.generateHistogramChartFromTable(dataTable, 4)
     local histogramChart = helper.generateHistogramChartFromTable(dataTable, 4)
     local histogramChartJson = mw.text.jsonEncode(histogramChart)
     local histogramChartJson = mw.text.jsonEncode(histogramChart)
   
 
    -- mw.log(histogramDataJson)
   
     -- Create a parent container for both the table and the chart
     -- Create a parent container for both the table and the chart
local parentContainer = mw.html.create('div')
    local parentContainer = mw.html.create('div')
    :addClass('parent-container')
        :addClass('parent-container')
    :css('width', width) -- Set the width as needed
        :css('width', width)


     -- Create chart container
     -- Create chart container
Line 127: Line 162:
         :css('width', width)
         :css('width', width)
         :attr('data-chartdata', histogramChartJson)
         :attr('data-chartdata', histogramChartJson)
local heading = mw.html.create('h2')
    :wikitext('Research outcomes over time')


     -- Add the table and chart to the parent container
     -- Add the table and chart to the parent container
parentContainer
    parentContainer
    :node(htmlTable)
        :node(htmlTable)
         :node(heading)
         :node(mw.html.create('h2'):wikitext('Research outcomes over time'))
    :node(chartContainer)
        :node(chartContainer)


     return tostring(parentContainer)
     return tostring(parentContainer)
end
end


 
-- Return the created HTML table
 
-- Return the created html table
return p
return p

Latest revision as of 13:01, 10 September 2024

Documentation for this module may be created at Module:CommunityResearchOutcomesList/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 a HTML table from SPARQL query results
function p.buildTableFromSparql(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 height = frame.args[2] or '400px' -- Default height if not specified
    local width = frame.args[3] or '800px' -- Default width if not specified
    
    -- Get the current URL
    local baseUrl = mw.site.server

    -- Constructing the SPARQL query with dynamic entity target1 and performance improvements
    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
  ?publication_date 
  ?work 
  ?workLabel 
  (REPLACE(STR(?work), "^.*/", "") AS ?qid)
  ?item_type
  ?item_type_label
  ?internal_project_item
  ?internal_project_label
  
WHERE {
  ?work wdt:P1495 target1: .
  
  # Restrict to allowed item types using VALUES for performance
  VALUES (?item_type ?item_type_label) { 
    (wd:Q5976449 "Paper") 
    (wd:Q5984635 "Dataset") 
    (wd:Q5976450 "Software")
  }
  
  # Match the item type
  ?work wdt:P1460 ?item_type .

  # Optional publication date
  OPTIONAL { ?work wdt:P28 ?publication_datetime. }
  BIND(COALESCE(xsd:date(?publication_datetime), "N/A") AS ?publication_date)

  # Optional internal project item and label retrieval
  OPTIONAL {
    ?work wdt:P1507 ?internal_project_item .
    OPTIONAL {
      ?internal_project_item rdfs:label ?internal_project_label .
      FILTER(LANG(?internal_project_label) = "en" || LANG(?internal_project_label) = "de")
    }
  }

  # Ensure labels are fetched in English or German
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de,en". }
}

ORDER BY DESC(?publication_date)
    ]]

    -- Log and run the SPARQL query
    mw.log(sparqlQuery)
    local jsonResults = sparql.runQuery(sparqlQuery)
    
    -- Handle errors or empty results
    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
    
    -- Generate the workUrl based on the qid and item_type_label
	for _, result in ipairs(jsonResults.results.bindings) do
	    local qid = result.qid and result.qid.value or nil
	    local itemTypeLabel = result.item_type_label and result.item_type_label.value or nil
	    
	    -- Remove the leading "Q" from the qid
	    if qid then
	        qid = string.gsub(qid, "^Q", "")
	    end
		
	    -- Check if qid is not nil before generating the workUrl
	    if qid and itemTypeLabel then
	        -- Dynamically generate the workUrl based on item type
	        if itemTypeLabel == "Paper" then
	            result.workUrl = {
	                type = "uri",
	                value = baseUrl .. "/wiki/Publication:" .. qid
	            }
	        elseif itemTypeLabel == "Dataset" then
	            result.workUrl = {
	                type = "uri",
	                value = baseUrl .. "/wiki/Dataset:" .. qid
	            }
	        elseif itemTypeLabel == "Software" then
	            result.workUrl = {
	                type = "uri",
	                value = baseUrl .. "/wiki/Software:" .. qid
	            }
	        else
	            result.workUrl = {
	                type = "uri",
	                value = baseUrl .. "/wiki/Q" .. qid -- Fallback
	            }
	        end
	    else
	        -- Handle missing qid or item_type_label
	        result.workUrl = {
	            type = "literal",
	            value = "URL not available"
	        }
	    end
	end

	-- mw.logObject(jsonResults)

    -- Convert the JSON results into a Lua table
    local fieldOrder = {"workUrl", "workLabel", "work", "publication_date", "qid", "internal_project_item", "internal_project_label", "item_type", "item_type_label"}
    local dataTable = helper.convertJsonToTableOrdered(jsonResults, fieldOrder)
    
	-- mw.logObject(dataTable)

    
    -- Create and return HTML table from the data
    local headers = {"Title", "Date of Publication", "Project", "Type"}
    local htmlTable =  helper.createHtmlTableWithMergedCols(dataTable, headers, {{1, 2}, {4}, {6,7}, {8, 9}})

    -- Generate histogram data
    local histogramChart = helper.generateHistogramChartFromTable(dataTable, 4)
    local histogramChartJson = mw.text.jsonEncode(histogramChart)

    -- Create a parent container for both the table and the chart
    local parentContainer = mw.html.create('div')
        :addClass('parent-container')
        :css('width', width)

    -- Create chart container
    local chartContainer = mw.html.create('div')
        :addClass('wikiChartContainer')
        :css('height', height)
        :css('width', width)
        :attr('data-chartdata', histogramChartJson)

    -- Add the table and chart to the parent container
    parentContainer
        :node(htmlTable)
        :node(mw.html.create('h2'):wikitext('Research outcomes over time'))
        :node(chartContainer)

    return tostring(parentContainer)
end

-- Return the created HTML table
return p