Increase query limit and suppress "further results" link (via update-page on MediaWiki MCP Server)
Implement pagination to handle server-side query limits (via update-page on MediaWiki MCP Server)
Line 2: Line 2:
-- Dynamically generates a list of CRT brand buttons by querying
-- Dynamically generates a list of CRT brand buttons by querying
-- all unique brands from the CRT models category.
-- all unique brands from the CRT models category.
-- Uses pagination to work around server-side SMW query limits.


local p = {}
local p = {}
Line 11: Line 12:
if s == '' then return nil end
if s == '' then return nil end
return s
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
end


Line 17: Line 30:
local linkType = trim(args.link)
local linkType = trim(args.link)
-- Query all CRT models and get their brands
-- Use high limit and searchlabel= to suppress "further results"
local queryWikitext =
'{{#ask:\n' ..
' [[Category:CRT models]]\n' ..
' |?Has brand\n' ..
' |format=broadtable\n' ..
' |headers=hide\n' ..
' |link=none\n' ..
' |mainlabel=-\n' ..
' |limit=5000\n' ..
' |searchlabel=\n' ..
'}}'
local queryResult = frame:preprocess(queryWikitext)
-- Extract unique brands from the result
local brandsSet = {}
local brandsSet = {}
local brandsList = {}
local brandsList = {}
if queryResult then
-- Query in batches to work around server-side limits
for cellContent in queryResult:gmatch('<td[^>]*>(.-)</td>') do
local batchSize = 500
local brand = trim(cellContent:gsub('<[^>]+>', ''))
local maxBatches = 20  -- Safety limit: 500 * 20 = 10,000 max pages
if brand and brand ~= '' and not brandsSet[brand] then
brandsSet[brand] = true
for batch = 0, maxBatches - 1 do
table.insert(brandsList, brand)
local offset = batch * batchSize
local queryWikitext =
'{{#ask:\n' ..
' [[Category:CRT models]]\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)
-- Count results in this batch
local countBefore = #brandsList
extractBrandsFromResult(queryResult, brandsSet, brandsList)
-- Check if we got any new results (not just new unique brands)
-- If the result is empty or very small, we've reached the end
local cellCount = 0
if queryResult then
for _ in queryResult:gmatch('<td[^>]*>') do
cellCount = cellCount + 1
end
end
end
-- If we got fewer results than batch size, we're done
if cellCount < batchSize then
break
end
end
end
end

Revision as of 21:27, 4 February 2026

Documentation for this module may be created at Module:CRTBrandList/doc

-- Module:CRTBrandList
-- Dynamically generates a list of CRT brand buttons by querying
-- all unique brands 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 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 args = frame.args
	local linkType = trim(args.link)
	
	local brandsSet = {}
	local brandsList = {}
	
	-- 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 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)
		
		-- Count results in this batch
		local countBefore = #brandsList
		extractBrandsFromResult(queryResult, brandsSet, brandsList)
		
		-- Check if we got any new results (not just new unique brands)
		-- If the result is empty or very small, we've reached the end
		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 CRT brands found.</em></p>'
	end
	
	-- Build the output: render each brand using Template:Brand entry
	local output = {}
	for _, brand in ipairs(brandsList) do
		local templateCall
		if linkType then
			templateCall = '{{Brand entry|brand=' .. brand .. '|link=' .. linkType .. '}}'
		else
			templateCall = '{{Brand entry|brand=' .. brand .. '}}'
		end
		table.insert(output, frame:preprocess(templateCall))
	end
	
	return table.concat(output, '\n')
end

return p
MediaWiki Appliance - Powered by TurnKey Linux