Module:AutomaticNavbox

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

p.cargoFields = { 'item_name', 'item_main_category', 'item_secondary_category' }

function getCargoData(category) fields = table.concat(p.cargoFields, ',') args = { where = 'item_main_category="' .. category .. '"', orderBy = 'item_secondary_category ASC, item_name ASC' } return cargo.query('items', fields, args) end

function getNavboxArgs(frameArgs) cargo_data = getCargoData(frameArgs['category']) last_category2 = "" item_list = {} navbox_row = 1 for i=1,#cargo_data do   if cargo_data[i]['item_secondary_category'] ~= "" then if cargo_data[i]['item_secondary_category'] ~= last_category2 then frameArgs['group' .. navbox_row] = last_category2 frameArgs['list' .. navbox_row] = table.concat(item_list, ' * ') last_category2 = cargo_data[i]['item_secondary_category'] item_list = {} navbox_row = navbox_row + 1 end item_list[#item_list + 1] =  .. cargo_data[i]['item_name'] ..  end end return frameArgs end

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

return p