Module:HelperMethods: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
Line 85: Line 85:




-- Function to create a HTML table from a Lua table
-- Function to create a HTML table from a Lua table where columns are merged
function M.createHtmlTableWithTwoMergedCols(dataTable, headers)
-- mergeColumns: contains a list of which columns should be merged, e.g. {{1, 2}, {4}} or {{2, 4}, {1, 3}}
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
 
 
function M.createHtmlTableWithMergedCols(dataTable, headers, mergeColumns)
function M.createHtmlTableWithMergedCols(dataTable, headers, mergeColumns)
     local htmlTable = mwHtml.create('table')
     local htmlTable = mwHtml.create('table')

Revision as of 10:23, 13 December 2023

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

local M = {}

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

-- Utility function to trim and lowercase a string
function M.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 M.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

-- Additional function to generate the histogram data
-- colWithYear: Contains the index of the column that contains the year information. Expected format "2023-12-30"
function M.generateHistogramChartFromTable(dataTable, colWithYear)
    local yearCounts = {}

    for _, row in ipairs(dataTable) do
        -- Extract the year from the fourth column (publication date)
        local date = row[colWithYear]
        if date then  -- Check if the date exists
            local year = date:sub(1, 4)  -- Extract the year

            if year ~= "" then
                yearCounts[year] = (yearCounts[year] or 0) + 1
            end
        end
    end

    local years = {}
    local counts = {}
    for year, count in pairs(yearCounts) do
        table.insert(years, year)
        table.insert(counts, count)
    end

    -- Sort the years to maintain chronological order
    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 (years)
            datasets = {{
                label = 'Number of Publications',
                data = sortedCounts,  -- y-axis data (counts)
                backgroundColor = 'rgba(54, 162, 235, 0.2)',
                borderColor = 'rgba(54, 162, 235, 1)',
                hoverBackgroundColor = 'red',
                borderWidth = 1
            }}
        },
        options = {
            scales = {
                y = {
                    beginAtZero = true
                }
            }
        }
    }

    return chartData
end


-- Function to create a HTML table from a Lua table where columns are merged
-- mergeColumns: contains a list of which columns should be merged, e.g. {{1, 2}, {4}} or {{2, 4}, {1, 3}}
function M.createHtmlTableWithMergedCols(dataTable, headers, mergeColumns)
    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

    for _, row in ipairs(dataTable) do
        if not string.find(row[1], "/entity/statement/") then
            local dataRow = htmlTable:tag('tr')
            
            for _, cols in ipairs(mergeColumns) do
                local combinedData
                if #cols == 1 then
                    -- If only one column index is provided, use it as is
                    combinedData = row[cols[1]]
                else
                    -- If two column indices are provided, merge them
                    combinedData = '[' .. row[cols[1]] .. ' ' .. row[cols[2]] .. ']'
                end
                dataRow:tag('td'):wikitext(combinedData)
            end
        end
    end
    
    mw.log(htmlTable)

    return tostring(htmlTable)
end

return M