Module:Categories

local p = {} local h = {} local Data = mw.loadData("Module:Categories/Data")

local Franchise = require("Module:Franchise") local utilsArg = require("Module:UtilsArg") local utilsMarkup = require("Module:UtilsMarkup") local utilsPage = require("Module:UtilsPage") local utilsTable = require("Module:UtilsTable")

function p.Main(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates.Categories)

h.storeCategories(args)

local result = p.printNavboxes(args.categories) if not utilsPage.inNamespace("User") then result = result .. p.printCategories(args.categories, args) end if err then result = result .. utilsMarkup.categories(err.categories) end return result end

function p.printCategories(plainCategories, perGameCategories) local categories = plainCategories or {} local gameCategoryMap = {} -- keeps track of which games are specified for which categories for _, category in ipairs(Data.gameCategories) do		local gamesInCategory = perGameCategories[category.parameter] if gamesInCategory then table.insert(categories, category.category) gameCategoryMap[category.parameter] = utilsTable.invert(gamesInCategory) end end for _, game in ipairs(Franchise.enum) do		for _, category in ipairs(Data.gameCategories) do			if gameCategoryMap[category.parameter] and gameCategoryMap[category.parameter][game] then local categoryName = category.category .. " in " .. Franchise.shortName(game) table.insert(categories, categoryName) end end end return utilsMarkup.categories(categories) end

function p.printNavboxes(categories) local result = "" if categories then for i, category in ipairs(categories) do			local navbox = p.printNavbox(category) result = result .. navbox end end return result end

function p.printNavbox(category) local pagesInCategory = tonumber(mw.getCurrentFrame:callParserFunction("PAGESINCATEGORY", category, "R", "pages")) if pagesInCategory == 0 or pagesInCategory > Data.maxPagesPerNav then return "", pagesInCategory end local categoryLink = string.format("Category:%s", category) local dplArgs = { namespace = "", includeSubpages = false, skipthispage = "no", orderMethod = "title", category = category, }	local subcategories = Data.subcategories[category] if not subcategories then local results = utilsPage.dpl(dplArgs) local links = h.rowContent(results) local navbox = mw.getCurrentFrame:expandTemplate({			title = "Navbox",			args = {				title = category,				links1 = links,				footer = categoryLink,			}		}) return navbox, pagesInCategory end local navboxArgs = { title = category, footer = categoryLink, }	local pagesInRows = 0 for i, subcategory in ipairs(subcategories) do		local rowDplArgs = utilsTable.merge({}, dplArgs, {			category = category .. "&" .. subcategory.category		}) for _, parentCategory in ipairs(subcategory.parents or {}) do			table.insert(rowDplArgs, {				param = "notcategory",				value = parentCategory			}) end local results = utilsPage.dpl(rowDplArgs) if #results > 0 then navboxArgs["group"..i] = subcategory.display or subcategory.category navboxArgs["links"..i] = h.rowContent(results) end end -- "Other" row for _, subcategory in ipairs(subcategories) do		table.insert(dplArgs, {			param = "notcategory",			value = subcategory.category		}) end local results = utilsPage.dpl(dplArgs) if #results > 0 then navboxArgs["group"..(#subcategories+1)] = "Other" navboxArgs["links"..(#subcategories+1)] = h.rowContent(results) end local navbox = mw.getCurrentFrame:expandTemplate({		title = "Navbox",		args = navboxArgs,	}) return navbox, pagesInCategory end function h.rowContent(pages) -- creates link to the pages that do not register on Special:WhatLinksHere, to avoid spamming it	local links = utilsTable.map(pages, function(page)		if page == mw.title.getCurrentTitle.fullText then			return utilsMarkup.bold(page)		else			return utilsMarkup.link(page, page, true)		end	end) return table.concat(links, ", ") end

function h.storeCategories(args) for i, category in ipairs(Data.gameCategories) do		local games = args[category.parameter] if games then -- We only store the base game to avoid skewing appearance counts towards games with remakes games = utilsTable.map(games, Franchise.baseGame) games = utilsTable.unique(games) local canon, nonCanon = utilsTable.partition(games, Franchise.isCanon) mw.getCurrentFrame:expandTemplate({				title = "Categories/Store",				args = {					category = category.category,					canon = table.concat(canon, ", "),					nonCanon = table.concat(nonCanon, ", "),				}			}) end end for i, category in ipairs(args.categories) do		mw.getCurrentFrame:expandTemplate({			title = "Categories/Store",			args = {				category = category,			}		}) end end

function p.Data(frame) local utilsLayout = require("Module:UtilsLayout")

local maxNavboxSize = "Max navbox size: " .. Data.maxPagesPerNav .." items\n" local tableRows = {} for k in pairs(Data.subcategories) do		local navbox, pageCount = p.printNavbox(k) local categoryLink = "Category:"..k.."" table.insert(tableRows, {categoryLink, pageCount, navbox}) end tableRows = utilsTable.sortBy(tableRows, 1) local multiGroupNavboxes = utilsLayout.table({		sortable = true,		headers = {"Category", "", "Navbox"},		rows = tableRows,		styles = {			["width"] = "100%",		},	})

local tabs = utilsLayout.tabs({		{			label = "Multi-Group Navboxes",			content = multiGroupNavboxes,		}	}) local result = maxNavboxSize..tabs

return result end

local params = {} local paramOrder = {} for _, perGameCategory in ipairs(Data.gameCategories) do	params[perGameCategory.parameter] = { type = "string", enum = Franchise.enum, desc = string.format("Comma-separated list of codes representing the games or other titles in which the given article subject is one of the %s.", perGameCategory.category, perGameCategory.category), split = true, trim = true, nilIfEmpty = true, }	table.insert(paramOrder, perGameCategory.parameter) end

function p.Schemas return { Data = { required = true, type = "record", properties = { {					name = "maxPagesPerNav", required = true, type = "number", desc = "Defines the maximum number of links in any given navbox. Categories with a number of pages exceeding this limit will not have navboxes generated for them.", },				{					name = "gameCategories", required = true, type = "array", desc = "Defines the categories which are subcategorized by Data:Franchise entry.", items = { type = "record", properties = { {								name = "parameter", required = true, type = "string", desc = "The name of the parameter for Template:Categories.", },							{								name = "category", required = true, type = "string", desc = "The parent category name.", },							{								name = "common", type = "array", items = { type = "number" }, desc = "If present, Module:Categories will generate a navbox for subjects with more than N appearances in the given category.", },						},					},				},				{					name = "subcategories", required = true, type = "map", desc = 'Maps a category to a list of "subcategories". Allows for sub-categorization in the navbox. For each "subcategory", a row is created in the navbox listing the members that exist in both the "subcategory" and the "main" category. An "Other" row is created for any remaining pages that are in the "main" category but none of the "subcategories" listed.', keyPlaceholder = "navboxCategory", keys = { type = "string" }, values = { type = "array", items = { type = "record", properties = { {									name = "category", required = true, type = "string", desc = 'Name of a subcategory that intersects with the navbox category.', },								{									name = "parents", type = "array", items = { type = "string" }, desc = "Used to exclude members of one or more parent categories. Not needed for most cases." },								{									name = "display", type = "string", default = "category", desc = "Text displayed for the navbox row. Defaults to the category name, without the namespace prefix.", },							},						}					},				},			},		},	} end

p.Templates = { Categories = { purpose = "Adding categories to pages. For each category, a navbox is generated with links between the pages in the category.", usesModuleData = true, format = "block", indent = 1, paramOrder = utilsTable.concat({1}, paramOrder), params = utilsTable.merge(params, {			[1] = {				name = "categories",				type = "string",				desc = "Comma separated list of categories which are not subcategorized by game. Examples of these include Animals, Forests, Fire-Related Enemies, and so on.",				split = true,				trim = true,				nilIfEmpty = true,			}		}), examples = { vertical = true, {				desc = "Ice Keese", args = { [1] = "Keese, Ice-Related Enemies", ["enemies"] = "OoT, OoT3D, MM, MM3D, TP, TPHD, PH, ST, TFH, BotW", ["sub-bosses"] = "ST", }			},			{				desc = "Yuga", args = { [1] = "Demons, Loruleans, Sorcerers", ["bosses"] = "TLoZ, ALttP, OoT, OoT3D, OoS, OoA, FSA, TP, TPHD, TFoE, TWoG, ZA, BSTLoZ, AST, HW, HWL, HWDE", ["characters"] = "ALBW, HW", ["playable"] = "HW", },			},			{				desc = "Blue Fire", args = { [1] = "Ancient Technology", ["items"] = "OoT, OoT3D", ["objects"] = "BotW" }			},			{				desc = "Desert Temple", args = { ["dungeons"] = "OoT, OoT3D", ["levels"] = "TFH", ["locations"] = "OoT, OoT3D", },			},			{				desc = "Eldin Caves", args = { ["stages"] = "HW, HWL, HWDE", },			},			{				desc = "Song of Healing", args = { ["songs"] = "MM, MM3D, TP, TPHD, ST", },			},			{				desc = string.format("Navboxes are not generated for categories with %s+ links.", Data.maxPagesPerNav), args = { [1] = "Hylians, Swords", },			},			{				desc = "Invalid codes, duplicate cods, and improperly ordered codes are handled appropriately.", args = { characters = "TP, TP, fakeGame, OoT, OoT", },			},		},	}, }

return p