Module:CRTTypeList
Documentation for this module may be created at Module:CRTTypeList/doc
-- Module:CRTTypeList
-- Dynamically generates a list of CRT type buttons by querying
-- all unique types from the CRT models 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 extractTypesFromResult(queryResult, typesSet, typesList)
if not queryResult then return end
for cellContent in queryResult:gmatch('<td[^>]*>(.-)</td>') do
local crtType = trim(cellContent:gsub('<[^>]+>', ''))
if crtType and crtType ~= '' and not typesSet[crtType] then
typesSet[crtType] = true
table.insert(typesList, crtType)
end
end
end
function p.list(frame)
local typesSet = {}
local typesList = {}
-- Query in batches to work around server-side limits
local batchSize = 500
local maxBatches = 20 -- Safety limit: 500 * 20 = 10,000 max pages
for batch = 0, maxBatches - 1 do
local offset = batch * batchSize
local queryWikitext =
'{{#ask:\n' ..
' [[Category:CRT models]]\n' ..
' |?Has CRT type\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)
extractTypesFromResult(queryResult, typesSet, typesList)
-- 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(typesList)
if #typesList == 0 then
return '<p><em>No CRT types found.</em></p>'
end
-- Build the output: render each type using Template:CRT type entry
local output = {}
for _, crtType in ipairs(typesList) do
local templateCall = '{{CRT type entry|type=' .. crtType .. '}}'
table.insert(output, frame:preprocess(templateCall))
end
return table.concat(output, '\n')
end
return p