MannedTooth (talk | contribs) (removed the need to add tedious code everytime we want to have a new X in Y category (+ added playablecharacters=)) |
PhantomCaleb (talk | contribs) m (should be using some kind of auto-camel casing thing instead of doing this...) |
||
(33 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
local h = {} |
local h = {} |
||
+ | |||
− | local cargo = mw.ext.cargo |
||
− | local |
+ | local Franchise = require("Module:Franchise") |
− | local |
+ | local utilsArg = require("Module:UtilsArg") |
− | local |
+ | local utilsLayout = require("Module:UtilsLayout") |
+ | local utilsMarkup = require("Module:UtilsMarkup") |
||
+ | local utilsPage = require("Module:UtilsPage") |
||
local utilsTable = require("Module:UtilsTable") |
local utilsTable = require("Module:UtilsTable") |
||
-- See the module for documentation about subcategories. |
-- See the module for documentation about subcategories. |
||
− | local |
+ | local data = mw.loadData("Module:Categories/Data") |
+ | local PER_GAME_CATEGORIES = { |
||
+ | { |
||
+ | parameter = "bosses", |
||
+ | category = "Bosses", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "characters", |
||
+ | category = "Characters", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "dungeons", |
||
+ | category = "Dungeons", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "enemies", |
||
+ | category = "Enemies", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "items", |
||
+ | category = "Items", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "levels", |
||
+ | category = "Levels", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "mini-games", |
||
+ | cargoField = "miniGames", |
||
+ | category = "Mini-Games", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "objects", |
||
+ | category = "Objects", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "places", |
||
+ | category = "Places", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "playable", |
||
+ | category = "Playable Characters", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "songs", |
||
+ | category = "Songs", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "stages", |
||
+ | category = "Stages", |
||
+ | }, |
||
+ | { |
||
+ | parameter = "sub-bosses", |
||
+ | cargoField = "subBosses", |
||
+ | category = "Sub-Bosses", |
||
+ | }, |
||
+ | } |
||
+ | local APPEARANCES_TABLE = "Appearances" |
||
− | function p. |
+ | function p.CargoDeclare(frame) |
− | local |
+ | local fields = {} |
+ | for _, category in ipairs(PER_GAME_CATEGORIES) do |
||
− | return p.CategorizeEntries(frame.args["plain"], args) |
||
+ | fields[category.cargoField or category.parameter] = "Integer" |
||
+ | end |
||
+ | fields["mainAppearances"] = "Integer" |
||
+ | fields["totalAppearances"] = "Integer" |
||
+ | return frame:callParserFunction("#cargo_declare:_table=" .. APPEARANCES_TABLE, fields) |
||
end |
end |
||
− | function p. |
+ | function p.Main(frame) |
+ | local args, err = utilsArg.parse(frame:getParent().args, p.Templates.Categories) |
||
− | local result = "" |
||
+ | local appearances = p.appearances(args) |
||
− | result = result .. p.PlainToNavboxes(plain) |
||
+ | local result = p.printNavboxes(args.categories, args, appearances) |
||
− | result = result .. p.PlainToCategories(plain) |
||
+ | if utilsPage.inNamespace("") then |
||
− | result = result .. p.GamesToCategories(lists) |
||
+ | p.storeAppearances(frame, appearances) |
||
+ | end |
||
+ | 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 |
return result |
||
end |
end |
||
− | function p. |
+ | function p.appearances(perGameCategories) |
+ | local appearances = {} |
||
− | result = "" |
||
+ | local mainAppearances = {} |
||
− | local plaintable = mw.text.split(plain, '%s*,%s*') |
||
+ | local totalAppearances = {} |
||
+ | for _, category in ipairs(PER_GAME_CATEGORIES) do |
||
+ | local games = perGameCategories[category.parameter] |
||
+ | if games then |
||
+ | local baseGames = utilsTable.map(games, Franchise.baseGame) |
||
+ | local uniqueAppearances = utilsTable.unique(baseGames) |
||
+ | local uniqueAppearancesByType = utilsTable.groupBy(uniqueAppearances, Franchise.type) |
||
+ | local uniqueMainAppearances = uniqueAppearancesByType["main"] or {} |
||
+ | appearances[category.parameter] = uniqueMainAppearances |
||
+ | table.insert(mainAppearances, uniqueMainAppearances) |
||
+ | table.insert(totalAppearances, uniqueAppearances) |
||
+ | else |
||
+ | appearances[category.parameter] = {} |
||
+ | end |
||
+ | end |
||
+ | mainAppearances = utilsTable.union(mainAppearances) |
||
+ | totalAppearances = utilsTable.union(totalAppearances) |
||
+ | appearances["mainAppearances"] = #mainAppearances |
||
+ | appearances["totalAppearances"] = #totalAppearances |
||
+ | return appearances |
||
+ | end |
||
+ | |||
+ | function p.storeAppearances(frame, appearances) |
||
+ | local fields = {} |
||
+ | for _, category in ipairs(PER_GAME_CATEGORIES) do |
||
+ | fields[category.cargoField or category.parameter] = #appearances[category.parameter] |
||
+ | end |
||
+ | fields["mainAppearances"] = appearances["mainAppearances"] |
||
+ | fields["totalAppearances"] = appearances["totalAppearances"] |
||
+ | frame:callParserFunction("#cargo_store:_table=" .. APPEARANCES_TABLE, fields) |
||
+ | 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(PER_GAME_CATEGORIES) do |
||
− | local navboxTitle = "" |
||
+ | local gamesInCategory = perGameCategories[category.parameter] |
||
− | local rows = {} |
||
+ | if gamesInCategory then |
||
− | local rowDisplayName = "" |
||
+ | table.insert(categories, category.category) |
||
− | local notCategories = "" |
||
+ | gameCategoryMap[category.parameter] = utilsTable.invert(gamesInCategory) |
||
− | local notParentCategories = "" |
||
+ | end |
||
− | local dplQuery = "" |
||
+ | end |
||
− | local dplParameters = '|namespace=|includesubpages=false|skipthispage=no|mode=userformat|format=,%PAGE%,;,|ordermethod=titlewithoutnamespace|noresultsheader=emptyCategory}}' |
||
+ | for _, game in ipairs(Franchise.enum()) do |
||
− | -- For every category entered in the template |
||
− | + | for _, category in ipairs(PER_GAME_CATEGORIES) do |
|
+ | if gameCategoryMap[category.parameter] and gameCategoryMap[category.parameter][game] then |
||
− | rows = {} |
||
+ | local categoryName = category.category .. " in " .. Franchise.shortName(game) |
||
− | if not utilsCode.IsEmpty(category) then |
||
+ | table.insert(categories, categoryName) |
||
− | if tonumber(mw.getCurrentFrame():callParserFunction{name = 'PAGESINCATEGORY', args = {category, "pages"}}) <= 100 then |
||
− | -- Sets the title, which is "[[:Category:X|X]] in {{TLoZ|Series}}" |
||
− | navboxTitle = "[[:Category:" .. category .. "|" .. category .. "]] in " .. mw.getCurrentFrame():expandTemplate{ title = "TLoZ", args = { "Series" } } |
||
− | |||
− | -- If subcategories exist, handles that |
||
− | if not utilsCode.IsEmpty(SUBCATEGORIES[category]) then |
||
− | notCategories = "" |
||
− | for key2, subCategory in ipairs(SUBCATEGORIES[category]) do |
||
− | notParentCategories = "" |
||
− | notCategories = notCategories .. "|notcategory=" .. subCategory["category"] |
||
− | if not utilsCode.IsEmpty(subCategory["parents"]) then |
||
− | for key3, parentCategory in ipairs(subCategory["parents"]) do |
||
− | notParentCategories = notParentCategories .. "|notcategory=" .. parentCategory |
||
− | end |
||
− | end |
||
− | dplQuery = h.dplToExternalLinks(mw.getCurrentFrame():preprocess("{{#dpl:|category=" .. category .. "|category=" .. subCategory["category"] .. notParentCategories .. dplParameters)) |
||
− | |||
− | if not (dplQuery == "emptyCategory") then |
||
− | if utilsCode.IsEmpty(subCategory["display"]) then |
||
− | rowDisplayName = subCategory["category"] |
||
− | else |
||
− | rowDisplayName = subCategory["display"] |
||
− | end |
||
− | table.insert(rows, {title = rowDisplayName, content = dplQuery}) |
||
− | end |
||
− | end |
||
− | |||
− | -- Other(s) row |
||
− | dplQuery = h.dplToExternalLinks(mw.getCurrentFrame():preprocess("{{#dpl:|category=" .. category .. notCategories .. dplParameters)) |
||
− | if not (dplQuery == "emptyCategory") then |
||
− | table.insert(rows, {title = "Other(s)", content = dplQuery}) |
||
− | end |
||
− | result = result .. utilsNavbox.CreateRowNavbox(rows, navboxTitle) |
||
− | |||
− | -- else just outputs everything |
||
− | else |
||
− | dplQuery = h.dplToExternalLinks(mw.getCurrentFrame():preprocess("{{#dpl:|category=" .. category .. dplParameters)) |
||
− | if not (dplQuery == "emptyCategory") then |
||
− | result = result .. utilsNavbox.CreateNavbox(dplQuery, navboxTitle) |
||
− | end |
||
− | end |
||
end |
end |
||
end |
end |
||
end |
end |
||
+ | |||
− | return tostring(result) |
||
+ | return utilsMarkup.categories(categories) |
||
end |
end |
||
+ | function p.printNavboxes(categories, perGameCategories, appearances) |
||
− | function h.dplToExternalLinks(dplResult) |
||
local result = "" |
local result = "" |
||
+ | if categories then |
||
− | local firstLink = true |
||
+ | local collapse = #categories > 1 |
||
− | dplResult = mw.text.split(dplResult, '%s*;%s*') |
||
+ | for _, category in ipairs(categories) do |
||
− | -- Removing the last entry in the table since it's "empty" due to how DPL |
||
+ | local navbox = p.printNavbox(category, collapse) |
||
− | -- adds a ";" at the end of each entry instead of just in-between entries |
||
+ | result = result .. navbox |
||
− | table.remove(dplResult) |
||
− | for key, link in ipairs(dplResult) do |
||
− | if firstLink == true then |
||
− | firstLink = false |
||
− | else |
||
− | result = result .. ' <b>·</b> ' |
||
end |
end |
||
− | result = result .. '<span class="plainlinks">[https://zelda.gamepedia.com/' .. mw.getCurrentFrame():callParserFunction{name = 'urlencode', args = {link, "WIKI"}} .. ' ' .. link .. ']</span>' |
||
end |
end |
||
+ | -- TODO: Print navs for per-game categories. |
||
+ | -- TODO: Print navs for recurring entities (see "appearances" object) |
||
return result |
return result |
||
end |
end |
||
− | function p. |
+ | function p.printNavbox(category, collapse) |
+ | local pagesInCategory = tonumber(mw.getCurrentFrame():callParserFunction("PAGESINCATEGORY", category, "R", "pages")) |
||
− | result = "" |
||
+ | if pagesInCategory == 0 or pagesInCategory > data.maxPagesPerNav then |
||
− | local plaintable = mw.text.split(plain, '%s*,%s*') |
||
+ | return "", pagesInCategory |
||
− | for key, category in ipairs(plaintable) do |
||
− | if not utilsCode.IsEmpty(category) then |
||
− | result = result .. "[[Category:" .. category .. "]]" |
||
− | end |
||
end |
end |
||
+ | |||
− | return result |
||
+ | local categoryLink = string.format("[[:Category:%s|%s]]", category, category) |
||
− | end |
||
+ | local seriesLink = Franchise.link("Series") |
||
− | |||
+ | local navboxTitle = string.format("%s in %s", categoryLink, seriesLink) |
||
− | function p.GamesToCategories(lists) |
||
+ | |||
− | local result = "" |
||
− | local |
+ | local dplArgs = { |
+ | namespace = "", |
||
− | local acceptedLists = { |
||
+ | includeSubpages = false, |
||
− | {parameter = "bosses", display = "Bosses"}, |
||
+ | skipthispage = "no", |
||
− | {parameter = "characters", display = "Characters"}, |
||
+ | orderMethod = "title", |
||
− | {parameter = "dungeons", display = "Dungeons"}, |
||
+ | category = category, |
||
− | {parameter = "enemies", display = "Enemies"}, |
||
− | {parameter = "items", display = "Items"}, |
||
− | {parameter = "levels", display = "Levels"}, |
||
− | {parameter = "objects", display = "Objects"}, |
||
− | {parameter = "places", display = "Places"}, |
||
− | {parameter = "playablecharacters", display = "Playable Characters"}, |
||
− | {parameter = "songs", display = "Songs"}, |
||
− | {parameter = "stages", display = "Stages"}, |
||
− | {parameter = "sub-bosses", display = "Sub-Bosses"}, |
||
} |
} |
||
+ | |||
− | |||
+ | local subcategories = data.subcategories[category] |
||
− | for key, list in ipairs(acceptedLists) do |
||
− | + | if not subcategories then |
|
+ | local results = utilsPage.dpl(dplArgs) |
||
− | for key2, entry in ipairs(mw.text.split(lists[list["parameter"]], '%s*,%s*')) do |
||
+ | local rows = { |
||
− | table.insert(categories, {list["display"], entry}) |
||
+ | { title = "All", content = h.rowContent(results) } |
||
− | end |
||
− | + | } |
|
+ | return utilsLayout.CreateRowNavbox(rows, navboxTitle), pagesInCategory |
||
end |
end |
||
+ | local rows = {} |
||
− | for _, game in ipairs(utilsGame.GetSortOrder("canon")) do |
||
+ | local pagesInRows = 0 |
||
− | for _2, category in ipairs(categories) do |
||
+ | for _, subcategory in ipairs(subcategories) do |
||
− | if game == category[2] then |
||
+ | local rowDplArgs = utilsTable.merge({}, dplArgs, { |
||
− | result = result .. "[[Category:" .. category[1] .. " in " .. utilsGame.AbbToGame(category[2], "true") .. "]]" |
||
+ | category = category .. "&" .. subcategory.category |
||
− | end |
||
+ | }) |
||
+ | 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 |
||
+ | table.insert(rows, { |
||
+ | title = subcategory.display or subcategory.category, |
||
+ | content = h.rowContent(results) |
||
+ | }) |
||
end |
end |
||
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 |
||
+ | table.insert(rows, { |
||
+ | title = "Other", |
||
+ | content = h.rowContent(results) |
||
+ | }) |
||
+ | end |
||
+ | return utilsLayout.CreateRowNavbox(rows, navboxTitle, { |
||
+ | collapsed = collapse |
||
+ | }), 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, " <b>·</b> ") |
||
+ | end |
||
+ | |||
+ | function p.Data(frame) |
||
+ | local result = "" |
||
+ | result = result .. "'''Max navbox size:''' " .. data.maxPagesPerNav .." items\n" |
||
+ | local tableRows = {} |
||
+ | for k in pairs(data.subcategories) do |
||
+ | table.insert(tableRows, {utilsMarkup.link("Category:" .. k), p.printNavbox(k)}) |
||
+ | end |
||
+ | tableRows = utilsTable.sortBy(tableRows, 1) |
||
+ | result = result .. utilsLayout.table({ |
||
+ | sortable = true, |
||
+ | headers = {"Category", "Navbox", "# Pages"}, |
||
+ | rows = tableRows, |
||
+ | }) |
||
return result |
return result |
||
end |
end |
||
+ | |||
+ | local params = {} |
||
+ | local paramOrder = {} |
||
+ | for _, perGameCategory in ipairs(PER_GAME_CATEGORIES) do |
||
+ | params[perGameCategory.parameter] = { |
||
+ | type = "string", |
||
+ | enum = Franchise.enum(), |
||
+ | desc = string.format("Comma-separated list of [[Data:Franchise|codes]] representing the games or other titles in which the given article subject is one of the [[:Category:%s|%s]].", perGameCategory.category, perGameCategory.category), |
||
+ | split = true, |
||
+ | trim = true, |
||
+ | nilIfEmpty = true, |
||
+ | } |
||
+ | table.insert(paramOrder, perGameCategory.parameter) |
||
+ | end |
||
+ | |||
+ | p.Schemas = { |
||
+ | 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 = "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.', |
||
+ | keys = { type = "string" }, |
||
+ | values = { |
||
+ | type = "array", |
||
+ | items = { |
||
+ | type = "record", |
||
+ | properties = { |
||
+ | { |
||
+ | name = "category", |
||
+ | required = true, |
||
+ | type = "string", |
||
+ | desc = 'Name of a category that intersects with the "main" 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.", |
||
+ | }, |
||
+ | }, |
||
+ | } |
||
+ | }, |
||
+ | }, |
||
+ | }, |
||
+ | }, |
||
+ | } |
||
+ | |||
+ | 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 [[:Category:Animals|Animals]], [[:Category:Forests|Forests]], [[:Category:Fire-Related Enemies|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", |
||
+ | ["places"] = "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 |
return p |
Revision as of 22:15, 3 March 2021
This is the main module for the following templates:
local p = {}
local h = {}
local Franchise = require("Module:Franchise")
local utilsArg = require("Module:UtilsArg")
local utilsLayout = require("Module:UtilsLayout")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsPage = require("Module:UtilsPage")
local utilsTable = require("Module:UtilsTable")
-- See the module for documentation about subcategories.
local data = mw.loadData("Module:Categories/Data")
local PER_GAME_CATEGORIES = {
{
parameter = "bosses",
category = "Bosses",
},
{
parameter = "characters",
category = "Characters",
},
{
parameter = "dungeons",
category = "Dungeons",
},
{
parameter = "enemies",
category = "Enemies",
},
{
parameter = "items",
category = "Items",
},
{
parameter = "levels",
category = "Levels",
},
{
parameter = "mini-games",
cargoField = "miniGames",
category = "Mini-Games",
},
{
parameter = "objects",
category = "Objects",
},
{
parameter = "places",
category = "Places",
},
{
parameter = "playable",
category = "Playable Characters",
},
{
parameter = "songs",
category = "Songs",
},
{
parameter = "stages",
category = "Stages",
},
{
parameter = "sub-bosses",
cargoField = "subBosses",
category = "Sub-Bosses",
},
}
local APPEARANCES_TABLE = "Appearances"
function p.CargoDeclare(frame)
local fields = {}
for _, category in ipairs(PER_GAME_CATEGORIES) do
fields[category.cargoField or category.parameter] = "Integer"
end
fields["mainAppearances"] = "Integer"
fields["totalAppearances"] = "Integer"
return frame:callParserFunction("#cargo_declare:_table=" .. APPEARANCES_TABLE, fields)
end
function p.Main(frame)
local args, err = utilsArg.parse(frame:getParent().args, p.Templates.Categories)
local appearances = p.appearances(args)
local result = p.printNavboxes(args.categories, args, appearances)
if utilsPage.inNamespace("") then
p.storeAppearances(frame, appearances)
end
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.appearances(perGameCategories)
local appearances = {}
local mainAppearances = {}
local totalAppearances = {}
for _, category in ipairs(PER_GAME_CATEGORIES) do
local games = perGameCategories[category.parameter]
if games then
local baseGames = utilsTable.map(games, Franchise.baseGame)
local uniqueAppearances = utilsTable.unique(baseGames)
local uniqueAppearancesByType = utilsTable.groupBy(uniqueAppearances, Franchise.type)
local uniqueMainAppearances = uniqueAppearancesByType["main"] or {}
appearances[category.parameter] = uniqueMainAppearances
table.insert(mainAppearances, uniqueMainAppearances)
table.insert(totalAppearances, uniqueAppearances)
else
appearances[category.parameter] = {}
end
end
mainAppearances = utilsTable.union(mainAppearances)
totalAppearances = utilsTable.union(totalAppearances)
appearances["mainAppearances"] = #mainAppearances
appearances["totalAppearances"] = #totalAppearances
return appearances
end
function p.storeAppearances(frame, appearances)
local fields = {}
for _, category in ipairs(PER_GAME_CATEGORIES) do
fields[category.cargoField or category.parameter] = #appearances[category.parameter]
end
fields["mainAppearances"] = appearances["mainAppearances"]
fields["totalAppearances"] = appearances["totalAppearances"]
frame:callParserFunction("#cargo_store:_table=" .. APPEARANCES_TABLE, fields)
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(PER_GAME_CATEGORIES) 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(PER_GAME_CATEGORIES) 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, perGameCategories, appearances)
local result = ""
if categories then
local collapse = #categories > 1
for _, category in ipairs(categories) do
local navbox = p.printNavbox(category, collapse)
result = result .. navbox
end
end
-- TODO: Print navs for per-game categories.
-- TODO: Print navs for recurring entities (see "appearances" object)
return result
end
function p.printNavbox(category, collapse)
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|%s]]", category, category)
local seriesLink = Franchise.link("Series")
local navboxTitle = string.format("%s in %s", categoryLink, seriesLink)
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 rows = {
{ title = "All", content = h.rowContent(results) }
}
return utilsLayout.CreateRowNavbox(rows, navboxTitle), pagesInCategory
end
local rows = {}
local pagesInRows = 0
for _, 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
table.insert(rows, {
title = subcategory.display or subcategory.category,
content = 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
table.insert(rows, {
title = "Other",
content = h.rowContent(results)
})
end
return utilsLayout.CreateRowNavbox(rows, navboxTitle, {
collapsed = collapse
}), 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, " <b>·</b> ")
end
function p.Data(frame)
local result = ""
result = result .. "'''Max navbox size:''' " .. data.maxPagesPerNav .." items\n"
local tableRows = {}
for k in pairs(data.subcategories) do
table.insert(tableRows, {utilsMarkup.link("Category:" .. k), p.printNavbox(k)})
end
tableRows = utilsTable.sortBy(tableRows, 1)
result = result .. utilsLayout.table({
sortable = true,
headers = {"Category", "Navbox", "# Pages"},
rows = tableRows,
})
return result
end
local params = {}
local paramOrder = {}
for _, perGameCategory in ipairs(PER_GAME_CATEGORIES) do
params[perGameCategory.parameter] = {
type = "string",
enum = Franchise.enum(),
desc = string.format("Comma-separated list of [[Data:Franchise|codes]] representing the games or other titles in which the given article subject is one of the [[:Category:%s|%s]].", perGameCategory.category, perGameCategory.category),
split = true,
trim = true,
nilIfEmpty = true,
}
table.insert(paramOrder, perGameCategory.parameter)
end
p.Schemas = {
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 = "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.',
keys = { type = "string" },
values = {
type = "array",
items = {
type = "record",
properties = {
{
name = "category",
required = true,
type = "string",
desc = 'Name of a category that intersects with the "main" 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.",
},
},
}
},
},
},
},
}
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 [[:Category:Animals|Animals]], [[:Category:Forests|Forests]], [[:Category:Fire-Related Enemies|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",
["places"] = "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