Create module to dynamically generate CRT brand list from semantic query (via create-page on MediaWiki MCP Server)
 
Fix link parameter handling - only include if specified (via update-page on MediaWiki MCP Server)
Line 15: Line 15:
function p.list(frame)
function p.list(frame)
local args = frame.args
local args = frame.args
local linkType = args.link or 'advanced'
local linkType = trim(args.link)
-- Query all CRT models and get their brands
-- Query all CRT models and get their brands
Line 55: Line 55:
local output = {}
local output = {}
for _, brand in ipairs(brandsList) do
for _, brand in ipairs(brandsList) do
local templateCall = '{{Brand entry|brand=' .. brand .. '|link=' .. linkType .. '}}'
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))
table.insert(output, frame:preprocess(templateCall))
end
end

Revision as of 21:21, 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.

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

function p.list(frame)
	local args = frame.args
	local linkType = trim(args.link)
	
	-- Query all CRT models and get their brands
	local queryWikitext =
		'{{#ask:\n' ..
		' [[Category:CRT models]]\n' ..
		' |?Has brand\n' ..
		' |format=broadtable\n' ..
		' |headers=hide\n' ..
		' |link=none\n' ..
		' |mainlabel=-\n' ..
		' |limit=500\n' ..
		'}}'
	
	local queryResult = frame:preprocess(queryWikitext)
	
	-- Extract unique brands from the result
	local brandsSet = {}
	local brandsList = {}
	
	if queryResult then
		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
	
	-- 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