Module:Indicator datasets table

From Visual Data Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Indicator datasets table/doc

local p = {}

function p.getDatasets(frame)
    local articleids = frame.args.articleids
    if not articleids then
        return "Error: articleids not provided"
    end
    
    -- Entferne alle Kommas und Leerzeichen am Ende von articleids
    articleids = mw.ustring.gsub(articleids, '[,%s]*$', '')
    
    local ids = mw.text.split(articleids, ",")
    local datasets = {}
    local titleobject
    
    for _, id in ipairs(ids) do
        titleobject = mw.title.new(tonumber(id))
        local existspage = titleobject.exists
        
        if existspage then
            local content = titleobject:getContent()
            -- Speichere den Inhalt der Seite
            table.insert(datasets, mw.text.jsonDecode(content))
        else
            return "Error: Page not found for ID " .. id
        end
    end
    
    return datasets
end

function p.generateTable(frame)
    local datasets = p.getDatasets(frame)
    if type(datasets) == "string" then
        return datasets -- Fehlermeldung zurückgeben
    end

    local rows = {}
    
    -- Füge die Spaltenüberschriften hinzu
    local headerRow = {'Class', 'Sub-class'}
    for _, dataset in ipairs(datasets) do
        table.insert(headerRow, 'Test')
    end
    table.insert(rows, headerRow)

    -- Füge die Datenzeilen hinzu
    for _, dataset in ipairs(datasets) do
        for _, classData in ipairs(dataset.schemas.Dataset.classes) do
            local class = classData['class name']
            for _, subClassData in ipairs(classData['sub-classes']) do
                local subClass = subClassData['sub-class name']
                local row = {class, subClass}
                for _, currentSubClassData in ipairs(subClassData['sub-sub-classes']) do
                    table.insert(row, currentSubClassData.value or "N/A")
                end
                -- Füge die Zeile dem Array hinzu
                table.insert(rows, row)
            end
        end
    end

    -- Erstelle die Tabelle
    local html = mw.html.create()
    local tableNode = html:tag('table'):addClass('wikitable')

    for _, row in ipairs(rows) do
        local tr = tableNode:tag('tr')
        for _, cell in ipairs(row) do
            tr:tag('td'):wikitext(cell)
        end
    end

    return tostring(html)
end

return p