Module:PersonPaperList: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
 
(34 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Required module containing helper methods
local helper = require('Module:HelperMethods')
-- Required modules for SPARQL queries and HTML table generation
-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local sparql = require('SPARQL')
Line 14: Line 17:
         return "No records found"
         return "No records found"
     end     
     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
     -- Constructing the SPARQL query with dynamic entity target1
Line 22: Line 32:


SELECT
SELECT
   ?publication_date
   ?publication_date ?work ?workLabel  
  ?work ?workLabel (CONCAT("/work/", SUBSTR(STR(?work), 32)) AS ?workUrl)
  (CONCAT("]] .. baseUrl .. [[/wiki/Publication:", REPLACE(STR(?work), "^.*/Q", "")) AS ?workUrl)
 
  (REPLACE(STR(?work), "^.*/", "") AS ?qid)
 
WHERE {
WHERE {
   ?work wdt:P16 target1: .
   ?work wdt:P16 target1: .
  ?work wdt:P1460 wd:Q5976449 .


   OPTIONAL {
   OPTIONAL {
Line 40: Line 52:


-- Executing the SPARQL query and retrieving results in JSON format
-- Executing the SPARQL query and retrieving results in JSON format
-- mw.log( sparqlQuery )
local jsonResults = sparql.runQuery(sparqlQuery)
local jsonResults = sparql.runQuery(sparqlQuery)
-- mw.logObject(jsonResults)


-- Handle error in SPARQL query execution
-- Handle error in SPARQL query execution
Line 47: Line 63:
     return nil
     return nil
end
end
if not jsonResults then
        return "Could not fetch data."
end
if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
        return "No records found."
end
-- Extracting headers from the SPARQL query results
-- local headers = {}
-- if jsonResults and jsonResults.head and jsonResults.head.vars then
-- headers = jsonResults.head.vars
-- end
-- Extracting headers from the SPARQL query results
-- Convert the JSON results into a Lua table
-- NOTE that these headers are not used since we will use hard coded ones below
    local fieldOrder = {"workUrl", "workLabel", "work", "publication_date", "qid"}
local headers = {}
local dataTable = helper.convertJsonToTableOrdered(jsonResults, fieldOrder)
if jsonResults and jsonResults.head and jsonResults.head.vars then
headers = jsonResults.head.vars
end
-- Hard-code headers
    -- Create and return HTML table from the data
     local headers = {"Publication", "Date of Publication"}
     local headers = {"Publication", "Date of Publication"}
    local htmlTable =  helper.createHtmlTableWithMergedCols(dataTable, headers, {{1, 2}, {4}})


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


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


    -- Create and return HTML table from the data
local heading = mw.html.create('h2')
    local htmlTable = createHtmlTableWithMergedCols(dataTable, headers)
    :wikitext('Publications over time')


     -- Combine table and chart in a single HTML structure
     -- Add the table and chart to the parent container
    local combinedHtml = mw.html.create('div')
parentContainer
         :wikitext(htmlTable)
    :node(htmlTable)
        :node(chartContainer)
         :node(heading)
    :node(chartContainer)


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


-- Additional function to generate the histogram data
function p.generateHistogramData(dataTable)
    -- Table to hold the count of publications per year
    local yearCounts = {}
    for _, row in ipairs(dataTable) do
        -- Assuming the date is in the second column and formatted as YYYY-MM-DD
        local year = row[2]:sub(1, 4)
        if year ~= "" then
            yearCounts[year] = (yearCounts[year] or 0) + 1
        end
    end


    -- Sorting years and preparing data for the chart
    local years = {}
    local counts = {}
    for year, count in pairs(yearCounts) do
        table.insert(years, year)
        table.insert(counts, count)
    end
    table.sort(years)
    local sortedCounts = {}
    for _, year in ipairs(years) do
        table.insert(sortedCounts, yearCounts[year])
    end
    local chartData = {
        type = 'bar',
        data = {
            labels = years, -- x-axis labels
            datasets = {{
                label = 'Number of Publications',
                data = sortedCounts, -- y-axis data
                backgroundColor = 'rgba(54, 162, 235, 0.2)',
                borderColor = 'rgba(54, 162, 235, 1)',
                borderWidth = 1
            }}
        },
        options = {
            scales = {
                y = {
                    beginAtZero = true
                }
            }
        }
    }
    return chartData
end
-- Utility function to trim and lowercase a string
function trimAndLower(str)
if str == nil then return nil end
str = str:gsub("^%s*(.-)%s*$", "%1")
return str:lower()
end
-- Function to convert JSON results into a Lua table
function convertJsonToTable(jsonResults)
    local resultsTable = {}
    if jsonResults and jsonResults.results and jsonResults.results.bindings then
        local bindings = jsonResults.results.bindings
        for j = 1, #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 create a HTML table from a Lua table
function createHtmlTableWithMergedCols(dataTable, headers)
local htmlTable = mwHtml.create('table')
htmlTable:addClass('wikitable'):attr('border', '1')
local headerRow = htmlTable:tag('tr')
-- Use the provided headers
    for _, header in ipairs(headers) do
        headerRow:tag('th'):wikitext(header)
    end
-- The first two columns are merged
for _, row in ipairs(dataTable) do
local dataRow = htmlTable:tag('tr')
local combinedData = '[' .. row[1] .. ' ' .. row[2] .. ']'
dataRow:tag('td'):wikitext(combinedData)
-- Add subsequent columns, skipping the third one
for j = 4, #row do
dataRow:tag('td'):wikitext(row[j])
end
end
return tostring(htmlTable)
end


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

Latest revision as of 11:52, 13 March 2024

Overview

This Lua module is designed for use in a MediaWiki environment. It provides functionality to execute a SPARQL query and present its results in an HTML table format. The module is particularly tailored for generating tables from SPARQL query results, relevant to specific entities identified by a dynamic parameter.

Dependencies

  • SPARQL Module: Used to execute SPARQL queries.
  • mw.html Module: Utilized for generating HTML content, specifically tables.

Module Functions

buildTableFromSparql(frame)

Generates an HTML table based on the results of a SPARQL query.

Parameters
* frame: The frame object containing arguments passed to the module.
Behavior
* Extracts target1 from frame.args[1]. If target1 is not provided or is empty, the function returns "No records found".
* Constructs and executes a SPARQL query using the target1 value. The query retrieves publication dates and work details related to target1.
* Handles errors in SPARQL query execution and logs them.
* Converts the SPARQL query results (JSON format) into a Lua table.
* Generates an HTML table with predefined headers ("Publication", "Date of Publication") and returns it as a string.

trimAndLower(str)

Utility function for string manipulation.

Parameters
* str: The string to be processed.
Returns
* The trimmed and lowercased version of the input string.

convertJsonToTable(jsonResults)

Converts SPARQL query results in JSON format into a Lua table.

Parameters
* jsonResults: JSON object containing SPARQL query results.
Returns
* A Lua table representing the SPARQL query results.

createHtmlTableWithMergedCols(dataTable, headers)

Creates an HTML table from a Lua data table, with specific column handling.

Parameters
* dataTable: The data table from which the HTML table will be generated.
* headers: A table containing header names for the HTML table.
Behavior
* Generates an HTML table with provided headers.
* Merges the first two columns of data into a single column in the resultant HTML table.
* Skips the third column and includes subsequent columns in the HTML table.
* Returns the HTML table as a string.

Return Value

The module returns a table containing the converted information (publication name and date).

Usage Example

{{#invoke:ModuleName|buildTableFromSparql|Arguments}}

Replace ModuleName with the actual name of the module when invoking it in a MediaWiki template or page. Arguments should be replaced with the appropriate arguments as required by the module's functionality.


Note: For the Debug console, you can use this:

local mockFrame = { args = { 'Q161115'} }
p.buildTableFromSparql(mockFrame)

-- 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
    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 
  (CONCAT("]] .. baseUrl .. [[/wiki/Publication:", REPLACE(STR(?work), "^.*/Q", "")) AS ?workUrl)
  (REPLACE(STR(?work), "^.*/", "") AS ?qid)
  
WHERE {
  ?work wdt:P16 target1: .
  ?work wdt:P1460 wd:Q5976449 .

  OPTIONAL {
    ?work wdt:P28 ?publication_datetime .
    BIND(xsd:date(?publication_datetime) AS ?publication_date)
  }
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

ORDER BY DESC(?publication_date)
    ]]

	-- Executing the SPARQL query and retrieving results in JSON format
	-- mw.log( sparqlQuery )
	
	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

	-- Extracting headers from the SPARQL query results
	-- local headers = {}
	-- if jsonResults and jsonResults.head and jsonResults.head.vars then
	-- 	headers = jsonResults.head.vars
	-- end
	
	-- Convert the JSON results into a Lua table
    local fieldOrder = {"workUrl", "workLabel", "work", "publication_date", "qid"}
	local dataTable = helper.convertJsonToTableOrdered(jsonResults, fieldOrder)
	
    -- Create and return HTML table from the data
    local headers = {"Publication", "Date of Publication"}
    local htmlTable =  helper.createHtmlTableWithMergedCols(dataTable, headers, {{1, 2}, {4}})

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

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

	local heading = mw.html.create('h2')
    	:wikitext('Publications over time')

    -- Add the table and chart to the parent container
	parentContainer
    	:node(htmlTable)
        :node(heading)
    	:node(chartContainer)

    return tostring(parentContainer)
end



-- Return the created html table
return p