Jump to content
Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

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

local p = {}
local cargo = mw.ext.cargo
local chart = require("Module:Chart")

-- album colors
local releaseColors = {
	["Herzeleid"] = "#ffa500",
	["Sehnsucht"] = "#008b8b",
	["Mutter"] = "#808000",
	["Reise, Reise"] = "#ff4500",
	["Rosenrot"] = "#4682b4",
	["Liebe ist für alle da"] = "#7a653a",
	["Untitled"] = "#f7f7f7",
	["Zeit"] = "#8d9fb8",
	["Other"] = "#a52a2a"
}

-- fetch ReleaseTitle for a list of songs from Cargo
local function getReleasesForSongs(songList)
	if #songList == 0 then return {} end
	local where = "Title IN (" .. table.concat(mw.ext.cargo.sqlList(songList), ",") .. ")"
	local results = cargo.query("Songs", "Title,ReleaseTitle", { where = where })
	local map = {}
	for _, row in ipairs(results) do
		map[row.Title] = row.ReleaseTitle
	end
	return map
end

function p.pie(frame)
	local args = frame:getParent().args
	local songs = {}

	-- dynamically collect all SongN args
	for key, val in pairs(args) do
		local num = tostring(key):match("^Song(%d+)$")
		if num and val and val ~= "" then
			table.insert(songs, { idx = tonumber(num), title = val })
		end
	end

	-- preserve numeric order
	table.sort(songs, function(a, b) return a.idx < b.idx end)

	-- flatten to list of titles
	local titles = {}
	for _, entry in ipairs(songs) do
		table.insert(titles, entry.title)
	end

	-- map songs to release
	local releaseMap = getReleasesForSongs(titles)

	-- count per release
	local counts = {}
	for _, song in ipairs(titles) do
		local release = releaseMap[song] or "Other"
		counts[release] = (counts[release] or 0) + 1
	end

	-- build slices
	local slices = {}
	for release, n in pairs(counts) do
		local color = releaseColors[release] or "#ccc"
		table.insert(slices, string.format("(%d:%s:%s:%s (album))", n, release, color, release))
	end

	-- call Module:Chart directly
	return chart["pie chart"]{
		args = {
			slices = table.concat(slices, " "),
			radius = 100,
			["inner percent"] = 70
		}
	}
end

return p