Modul:Gegner Personaluebersicht

Aus HuskyWiki

Die Dokumentation für dieses Modul kann unter Modul:Gegner Personaluebersicht/Doku erstellt werden

local p = {}

local function trim(s)
	return mw.text.trim(s or "")
end

local function getArg(frame, name)
	local value = trim(frame.args[name])
	if value ~= "" then
		return value
	end

	local parent = frame:getParent()
	if parent then
		return trim(parent.args[name])
	end

	return ""
end

local function stripRoleSuffix(title)
	title = trim(title)
	title = title:gsub("%s+%(Gegnerischer Trainer.-%)$", "")
	title = title:gsub("%s+%(Gegnerische Trainerin.-%)$", "")
	title = title:gsub("%s+%(Gegnerischer Torhüter.-%)$", "")
	title = title:gsub("%s+%(Gegnerische Torhüterin.-%)$", "")
	return trim(title)
end

local function surnameSortKey(name)
	local clean = trim(name)
	if clean == "" then
		return ""
	end

	if clean:find(",", 1, true) then
		local last, first = clean:match("^([^,]+),%s*(.*)$")
		return mw.ustring.lower(trim((last or "") .. " " .. (first or "")))
	end

	local parts = {}
	for part in clean:gmatch("%S+") do
		table.insert(parts, part)
	end

	if #parts <= 1 then
		return mw.ustring.lower(clean)
	end

	local particles = {
		["de"] = true,
		["del"] = true,
		["der"] = true,
		["den"] = true,
		["di"] = true,
		["la"] = true,
		["le"] = true,
		["st."] = true,
		["st"] = true,
		["van"] = true,
		["von"] = true,
	}

	local last = table.remove(parts)
	local previous = parts[#parts]
	if previous and particles[mw.ustring.lower(previous)] then
		last = previous .. " " .. last
		table.remove(parts)
	end

	return mw.ustring.lower(last .. " " .. table.concat(parts, " "))
end

local function getCategoryMembers(category)
	if not (mw.ext and mw.ext.dpl and type(mw.ext.dpl.getPagenames) == "function") then
		mw.addWarning("DynamicPageListEngine/Lua ist nicht verfügbar.")
		return {}
	end

	category = trim(category):gsub("^Kategorie:%s*", "")

	local ok, pages = pcall(mw.ext.dpl.getPagenames, {
		category = category,
		namespace = "main",
		redirects = "exclude",
		ordermethod = "sortkey",
		order = "ascending",
		count = 500,
	})

	if not ok then
		mw.addWarning("DynamicPageListEngine-Fehler: " .. tostring(pages))
		return {}
	end

	return pages or {}
end

local function buildEntries(category)
	local entries = {}
	for _, title in ipairs(getCategoryMembers(category)) do
		table.insert(entries, {
			title = title,
			label = stripRoleSuffix(title),
			sort = surnameSortKey(stripRoleSuffix(title)),
		})
	end

	table.sort(entries, function(a, b)
		return a.sort < b.sort
	end)

	return entries
end

local function renderLink(entry)
	if entry.label == entry.title then
		return string.format("* [[%s]]", entry.title)
	end
	return string.format("* [[%s|%s]]", entry.title, entry.label)
end

local function splitIntoColumns(entries, count)
	local columns = {}
	for i = 1, count do
		columns[i] = {}
	end

	if #entries == 0 then
		return columns
	end

	local perColumn = math.ceil(#entries / count)
	for index, entry in ipairs(entries) do
		local target = math.floor((index - 1) / perColumn) + 1
		if target > count then
			target = count
		end
		table.insert(columns[target], renderLink(entry))
	end

	return columns
end

local function renderColumn(lines)
	if #lines == 0 then
		return "&nbsp;"
	end
	return table.concat(lines, "<br>")
end

local function getStyleConfig(style)
	local normalized = mw.ustring.lower(trim(style))
	local legacy = {
		tableClass = "wikitable",
		tableStyle = "font-size:95%; width:60%;",
		headerStyles = {
			"background:#00205B; color:#FFFFFF; text-align:center;",
			"background:#00205B; color:#FFFFFF; text-align:center;",
		},
		cellStyles = {
			"width:10%; background:#eeeeee; vertical-align:top;",
			"width:10%; background:#eeeeee; vertical-align:top;",
			"width:10%; background:#eeeeee; vertical-align:top;",
			"width:10%; background:#eeeeee; vertical-align:top;",
		},
	}

	if normalized ~= "spielerstatistik" and normalized ~= "spielerstatistik2526" then
		return legacy
	end

	return {
		tableClass = "wikitable plainrowheaders",
		tableStyle = "font-size:95%; width:60%;",
		headerStyles = {
			"background-color:#eef3f8; text-align:center;",
			"background-color:#dde9f6; border-left:3px solid #54595d; text-align:center;",
		},
		cellStyles = {
			"width:10%; background-color:#f8fafc; vertical-align:top;",
			"width:10%; background-color:#eef3f8; vertical-align:top;",
			"width:10%; border-left:3px solid #54595d; background-color:#f8fafc; vertical-align:top;",
			"width:10%; background-color:#dde9f6; vertical-align:top;",
		},
	}
end

function p.render(frame)
	local key = getArg(frame, "key")
	if key == "" then
		return "<strong>Fehler:</strong> Parameter <code>key</code> fehlt."
	end

	local trainerCategory = getArg(frame, "trainerCategory")
	if trainerCategory == "" then
		trainerCategory = "Gegnerischer Trainer " .. key
	end

	local goalieCategory = getArg(frame, "goalieCategory")
	if goalieCategory == "" then
		goalieCategory = "Gegnerischer Torhüter " .. key
	end

	local trainers = buildEntries(trainerCategory)
	local goalies = buildEntries(goalieCategory)

	local trainerColumns = splitIntoColumns(trainers, 2)
	local goalieColumns = splitIntoColumns(goalies, 2)

	local titleTrainer = getArg(frame, "titleTrainer")
	if titleTrainer == "" then
		titleTrainer = "Trainer <small>(vs. Kassel)</small>"
	end

	local titleGoalie = getArg(frame, "titleGoalie")
	if titleGoalie == "" then
		titleGoalie = "Torhüter <small>(vs. Kassel)</small>"
	end

	local styleConfig = getStyleConfig(getArg(frame, "style"))

	return table.concat({
		string.format('{| class="%s" style="%s"', styleConfig.tableClass, styleConfig.tableStyle),
		string.format(
			'! colspan=2 style="%s"|%s!!colspan=2 style="%s"|%s',
			styleConfig.headerStyles[1],
			titleTrainer,
			styleConfig.headerStyles[2],
			titleGoalie
		),
		"|-",
		'| style="' .. styleConfig.cellStyles[1] .. '"|' .. renderColumn(trainerColumns[1]),
		'| style="' .. styleConfig.cellStyles[2] .. '"|' .. renderColumn(trainerColumns[2]),
		'| style="' .. styleConfig.cellStyles[3] .. '"|' .. renderColumn(goalieColumns[1]),
		'| style="' .. styleConfig.cellStyles[4] .. '"|' .. renderColumn(goalieColumns[2]),
		"|}",
	}, "\n")
end

return p