Module:Categories

local p = {} local cargo = mw.ext.cargo local utilsCode = require("Module:UtilsCode") local utilsGame = require("Module:UtilsGame") local utilsNavbox = require("Module:UtilsNavbox") local utilsTable = require("Module:UtilsTable")

-- This list allows further categorization in the navbox. The module will create -- a row for all members of the main category that fits both the latter and the -- one entered here. It will then create a second row for the second category -- entered here. And so on. local CUSTOM_CATEGORIES = { {category = "Hylians", subcategories = {"Hyruleans", "Terminans"}}, {category = "Water-Related Enemies", subcategories = {"Amphibians", "Cnidarians"}} }

function p._CategorizeEntries(frame) local args = frame:getParent.args return p.CategorizeEntries(frame.args["plain"], args["bosses"], args["characters"], args["dungeons"], args["enemies"], args["items"], args["objects"], args["places"], args["songs"], args["stages"], args["sub-bosses"]) end

function p.CategorizeEntries(plain, bosses, characters, dungeons, enemies, items, objects, places, songs, stages, subbosses) local result = "" result = result .. p.PlainToNavboxes(plain) result = result .. p.PlainToCategories(plain) result = result .. p.GamesToCategories(bosses, characters, dungeons, enemies, items, objects, places, songs, stages, subbosses) return result end

function p.PlainToNavboxes(plain) result = "" local plaintable = mw.text.split(plain, '%s*,%s*') local navboxTitle = "" local rows = {} local queryResults = {} local links = {} local allPages = {} -- For every category entered in the template for key, category in ipairs(plaintable) do		rows = {} if not utilsCode.IsEmpty(category) then -- Sets the title, which is "X in " navboxTitle = "" .. category .. " in " .. mw.getCurrentFrame:expandTemplate{ title = "TLoZ", args = { "Series" } } -- Fetches all pages in main category allPages = cargo.query('_pageData', '_pageName, _categories__full', {where = '_pageNamespace = "0" AND _categories HOLDS "' .. category .. '"', orderBy = "_pageName ASC", limit = 5000}) -- If custom categories exist, handles that local customCategories = {} for key2, customCategory in ipairs(CUSTOM_CATEGORIES) do   			if customCategory["category"] == category then customCategories = customCategory["subcategories"] end end if not utilsCode.IsEmpty(customCategories) then for key2, customCategory in ipairs(customCategories) do					links = {} for key3, page in ipairs(allPages) do						if utilsTable.keyOf(mw.text.split(page["_categories__full"], "|"), customCategory) then table.insert(links, "" .. page["_pageName"] .. "") end end table.insert(rows, {title = customCategory, content = table.concat(links, " · ")}) end -- Other(s) row links = {} local foundFlag = false for key2, page in ipairs(allPages) do					foundFlag = false for key3, category in ipairs(customCategories) do						if utilsTable.keyOf(mw.text.split(page["_categories__full"], "|"), category) then foundFlag = true end end if foundFlag == false then table.insert(links, "" .. page["_pageName"] .. "") end end table.insert(rows, {title = "Other(s)", content = table.concat(links, " · ")}) -- else just outputs everything else links = {} for key2, page in ipairs(allPages) do table.insert(links, "" .. page["_pageName"] .. "") end table.insert(rows, {title = "", content = table.concat(links, " · ")}) end result = result .. utilsNavbox.CreateRowNavbox(rows, navboxTitle) end end return tostring(result) end

function p.PlainToCategories(plain) result = "" local plaintable = mw.text.split(plain, '%s*,%s*') for key, category in ipairs(plaintable) do		if not utilsCode.IsEmpty(category) then result = result .. ""		end end return result end

function p.GamesToCategories(bosses, characters, dungeons, enemies, items, objects, places, songs, stages, subbosses) local result = "" local categories = {} local sortOrder = utilsGame.GetSortOrder("canon")

if not (utilsCode.IsEmpty(bosses)) then bosses = mw.text.split(bosses, '%s*,%s*') for _, value in ipairs(bosses) do			table.insert(categories, {"Bosses", value}) end end if not (utilsCode.IsEmpty(characters)) then characters = mw.text.split(characters, '%s*,%s*') for _, value in ipairs(characters) do			table.insert(categories, {"Characters", value}) end end if not (utilsCode.IsEmpty(dungeons)) then dungeons = mw.text.split(dungeons, '%s*,%s*') for _, value in ipairs(dungeons) do			table.insert(categories, {"Dungeons", value}) end end if not utilsCode.IsEmpty(enemies) then enemies = mw.text.split(enemies, '%s*,%s*') for _, value in ipairs(enemies) do			table.insert(categories, {"Enemies", value}) end end if not utilsCode.IsEmpty(items) then items = mw.text.split(items, '%s*,%s*') for _, value in ipairs(items) do			table.insert(categories, {"Items", value}) end end if not (utilsCode.IsEmpty(objects)) then objects = mw.text.split(objects, '%s*,%s*') for _, value in ipairs(objects) do			table.insert(categories, {"Objects", value}) end end if not (utilsCode.IsEmpty(places)) then places = mw.text.split(places, '%s*,%s*') for _, value in ipairs(places) do			table.insert(categories, {"Places", value}) end end if not (utilsCode.IsEmpty(songs)) then songs = mw.text.split(songs, '%s*,%s*') for _, value in ipairs(songs) do			table.insert(categories, {"Songs", value}) end end if not (utilsCode.IsEmpty(stages)) then stages = mw.text.split(stages, '%s*,%s*') for _, value in ipairs(stages) do			table.insert(categories, {"Stages", value}) end end if not (utilsCode.IsEmpty(subbosses)) then subbosses = mw.text.split(subbosses, '%s*,%s*') for _, value in ipairs(subbosses) do			table.insert(categories, {"Sub-Bosses", value}) end end for _, game in ipairs(sortOrder) do		for _2, category in ipairs(categories) do			if game == category[2] then result = result .. ""			end end end return result end

return p