Module:AutomaticNavbox

local p = {} local cargo = mw.ext.cargo

local cargoFields = { 'item_name', 'item_main_category', 'item_secondary_category' }

local cargoArgs = { where = 'item_main_category="%s"', orderBy = 'item_secondary_category ASC, item_name ASC' }

function getCargoData(category) fields = table.concat(cargoFields, ',') args = cargoArgs args['where'] = string.format(args['where'], category) return cargo.query('items', fields, args) end

function getNavboxArgs(frameArgs) navboxArgs = {} cargoData = getCargoData(frameArgs['category']) lastSubcategory = "" itemList = {} navboxRow = 1 for i=1,#cargoData do   if cargoData[i]['item_secondary_category'] ~= "" then if cargoData[i]['item_secondary_category'] ~= lastSubcategory and lastSubcategory ~= "" then navboxArgs['group' .. navboxRow] = lastSubcategory navboxArgs['list' .. navboxRow] = table.concat(itemList, ' * ') lastSubcategory = cargoData[i]['item_secondary_category'] itemList = {} navboxRow = navboxRow + 1 end itemList[#itemList + 1] =  .. cargoData[i]['item_name'] ..  if i == #cargoData then navboxArgs['group' .. navboxRow] = cargoData[i]['item_secondary_category'] navboxArgs['list' .. navboxRow] = table.concat(itemList, ' * ') end end end

for k,v in pairs(frameArgs) do   navboxArgs[k] = v  end return navboxArgs end

p.Navbox = function(frame) navboxArgs = getNavboxArgs(frame.args) return mw.getCurrentFrame:expandTemplate{title='Navbox', args=navboxArgs} end

return p