Documentation for this module may be created at Module:AVBrandList/doc
-- Module:AVBrandList
-- Dynamically generates a list of AV device brand buttons by querying
-- all unique brands from the AV devices category.
-- Uses pagination to work around server-side SMW query limits.
local p = {}
local function trim(s)
if s == nil then return nil end
s = tostring(s)
s = s:gsub('^%s+', ''):gsub('%s+$', '')
if s == '' then return nil end
return s
end
local function extractBrandsFromResult(queryResult, brandsSet, brandsList)
if not queryResult then return end
for cellContent in queryResult:gmatch('<td[^>]*>(.-)</td>') do
local brand = trim(cellContent:gsub('<[^>]+>', ''))
if brand and brand ~= '' and not brandsSet[brand] then
brandsSet[brand] = true
table.insert(brandsList, brand)
end
end
end
function p.list(frame)
local brandsSet = {}
local brandsList = {}
-- Query in batches to work around server-side limits
local batchSize = 500
local maxBatches = 20 -- Safety limit
for batch = 0, maxBatches - 1 do
local offset = batch * batchSize
local queryWikitext =
'{{#ask:\n' ..
' [[Category:AV devices]]\n' ..
' |?Has brand\n' ..
' |format=broadtable\n' ..
' |headers=hide\n' ..
' |link=none\n' ..
' |mainlabel=-\n' ..
' |limit=' .. batchSize .. '\n' ..
' |offset=' .. offset .. '\n' ..
' |searchlabel=\n' ..
'}}'
local queryResult = frame:preprocess(queryWikitext)
extractBrandsFromResult(queryResult, brandsSet, brandsList)
-- Count cells to check if we got a full batch
local cellCount = 0
if queryResult then
for _ in queryResult:gmatch('<td[^>]*>') do
cellCount = cellCount + 1
end
end
-- If we got fewer results than batch size, we're done
if cellCount < batchSize then
break
end
end
-- Sort alphabetically
table.sort(brandsList)
if #brandsList == 0 then
return '<p><em>No AV device brands found.</em></p>'
end
-- Build the output: render each brand using Template:AV brand entry
local output = {}
for _, brand in ipairs(brandsList) do
local templateCall = '{{AV brand entry|brand=' .. brand .. '}}'
table.insert(output, frame:preprocess(templateCall))
end
return table.concat(output, '\n')
end
return p