Module:Indicator datasets table: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 62: | Line 62: | ||
dataRow:tag('td'):wikitext(class) | dataRow:tag('td'):wikitext(class) | ||
dataRow:tag('td'):wikitext(subClass) | dataRow:tag('td'):wikitext(subClass) | ||
-- Iteriere über jedes Dataset | -- Iteriere über jedes Dataset, um den Wert für diese Klasse und Sub-Klasse zu finden | ||
for _, | for _, otherDataset in ipairs(datasets) do | ||
local value = p.findValue(otherDataset, class, subClass) | |||
dataRow:tag('td'):wikitext(value or "N/A") | |||
end | end | ||
end | end | ||
| Line 79: | Line 72: | ||
return tostring(html) | return tostring(html) | ||
end | |||
function p.findValue(dataset, class, subClass) | |||
for _, classData in ipairs(dataset.schemas.Dataset.classes) do | |||
if classData['class name'] == class then | |||
for _, subClassData in ipairs(classData['sub-classes']) do | |||
if subClassData['sub-class name'] == subClass then | |||
return subClassData.value | |||
end | |||
end | |||
end | |||
end | |||
return nil | |||
end | end | ||
return p | return p | ||
Revision as of 15:21, 2 May 2024
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 html = mw.html.create()
-- Tabelle erstellen
local tableNode = html:tag('table'):addClass('wikitable')
-- Header-Zeile erstellen
local headerRow = tableNode:tag('tr')
headerRow:tag('th'):wikitext('Class')
headerRow:tag('th'):wikitext('Sub-class')
-- Iteriere über jedes Dataset und füge es als Spaltenüberschrift hinzu
for _, dataset in ipairs(datasets) do
headerRow:tag('th'):wikitext(dataset.unique_title)
end
-- Datenzeilen erstellen
for _, dataset in ipairs(datasets) do
-- Iteriere über jede Klasse im Dataset
for _, classData in ipairs(dataset.schemas.Dataset.classes) do
local class = classData['class name']
-- Iteriere über jede Sub-Klasse im Dataset
for _, subClassData in ipairs(classData['sub-classes']) do
local subClass = subClassData['sub-class name']
local dataRow = tableNode:tag('tr')
dataRow:tag('td'):wikitext(class)
dataRow:tag('td'):wikitext(subClass)
-- Iteriere über jedes Dataset, um den Wert für diese Klasse und Sub-Klasse zu finden
for _, otherDataset in ipairs(datasets) do
local value = p.findValue(otherDataset, class, subClass)
dataRow:tag('td'):wikitext(value or "N/A")
end
end
end
end
return tostring(html)
end
function p.findValue(dataset, class, subClass)
for _, classData in ipairs(dataset.schemas.Dataset.classes) do
if classData['class name'] == class then
for _, subClassData in ipairs(classData['sub-classes']) do
if subClassData['sub-class name'] == subClass then
return subClassData.value
end
end
end
end
return nil
end
return p