Module:CatMaintenanceTable

Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

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

Code

-- written using ChatGPT

local p = {}

local function normalizeCategoryTitle(raw)
	raw = mw.text.trim(raw or "")
	if raw == "" then return nil end
	raw = raw:gsub("^%s*[Cc]ategory:%s*", "")
	return mw.title.makeTitle("Category", raw)
end

local function getFileCountViaMagicWord(titleObj)
	if not titleObj then return 0 end
	local catName = titleObj.text 
	local wikitext = string.format("{{PAGESINCATEGORY:%s|files}}", catName)
	local n = tonumber(mw.getCurrentFrame():preprocess(wikitext)) or 0
	return n
end

local function parseCategories(input)
	local result = {}
	local current = ""
	local inQuotes = false

	for i = 1, #input do
		local char = input:sub(i, i)

		if char == '"' then
			inQuotes = not inQuotes
		elseif char == "," and not inQuotes then
			table.insert(result, mw.text.trim(current))
			current = ""
		else
			current = current .. char
		end
	end

	if current ~= "" then
		table.insert(result, mw.text.trim(current))
	end

	return result
end

function p.render(frame)
	local raw = frame.args.categories or ""
	if raw == "" then
		return "''(Keine Kategorie angegeben)''"
	end

	local cats = parseCategories(raw)

	local html = mw.html.create("table")
		:addClass("wikitable sortable")
		:css("width", "60%")

	-- Kopfzeile
	local head = html:tag("tr")
	head:tag("th"):wikitext("Kategorie"):done()
	head:tag("th"):wikitext("Status"):done()

	for _, cat in ipairs(cats) do
		if cat ~= "" then
			local title = normalizeCategoryTitle(cat)
			if title then
				local count = getFileCountViaMagicWord(title)
	
				local row = html:tag("tr")
				row:tag("td"):wikitext(string.format("[[:%s|%s]]", title.prefixedText, title.text)):done()
	
				local status = row:tag("td")
				if count == 0 then
					status:css({
	    				["background-color"] = "var(--background-color-success-subtle)",
	    				["color"] = "var(--color-base)",
	    				["font-weight"] = "bold"
					})
					:wikitext("Alle Fotos einsortiert")
				else
					status:css({
	    				["background-color"] = "var(--background-color-error-subtle)",
	    				["color"] = "var(--color-base)",
	    				["font-weight"] = "bold"
					})
					:wikitext(string.format("%d Fotos nicht einsortiert", count))
				end
			end
		end
	end

	return tostring(html)
end

return p