Module:BrandSeries

Revision as of 16:14, 17 January 2026 by Arclight-MCP-Bot (talk | contribs) (Fix: preprocess final wikitext output to render links and table (via update-page on MediaWiki MCP Server))

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

-- Module:BrandSeries
-- Generates a series breakdown table for a brand's CRTs
-- Shows only series that have CRTs for the specified brand

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.seriesTable(frame)
	local args = frame.args
	local parent = frame:getParent()
	if parent then
		args = parent.args
	end
	
	local brand = trim(args.brand) or trim(args[1])
	
	if not brand then
		return '<span class="error">Error: brand parameter required</span>'
	end
	
	-- First, get all series from Category:Series
	local seriesQuery = '{{#ask:[[Category:Series]]|format=list|link=none|limit=500|sep=@@|searchlabel=}}'
	local seriesResult = frame:preprocess(seriesQuery)
	
	if not seriesResult or seriesResult == '' then
		return "''No series found.''"
	end
	
	-- Parse series names
	local seriesList = {}
	for series in seriesResult:gmatch('[^@@]+') do
		local s = trim(series)
		if s then
			table.insert(seriesList, s)
		end
	end
	
	-- Sort alphabetically
	table.sort(seriesList)
	
	-- For each series, count how many CRTs belong to this brand
	local rows = {}
	for _, series in ipairs(seriesList) do
		local countQuery = string.format(
			'{{#ask:[[Category:CRT models]][[Has brand::%s]][[Has series::%s]]|format=count}}',
			brand, series
		)
		local countResult = frame:preprocess(countQuery)
		local count = tonumber(trim(countResult)) or 0
		
		if count > 0 then
			table.insert(rows, {
				series = series,
				count = count
			})
		end
	end
	
	-- If no series found for this brand
	if #rows == 0 then
		return "''No series-assigned models for this brand.''"
	end
	
	-- Build the table as wikitext
	local output = {}
	table.insert(output, '{| class="wikitable"')
	table.insert(output, '|-')
	table.insert(output, '! Series !! Count !! Browse')
	
	for _, row in ipairs(rows) do
		table.insert(output, '|-')
		-- Build browse link inline
		local browseLink = string.format(
			'{{#queryformlink:form=CRT query|query string=CRT query[brand]=%s&CRT query[series]=%s&_run|link text=Browse}}',
			brand, row.series
		)
		table.insert(output, string.format('| [[%s]] || %d || %s', row.series, row.count, browseLink))
	end
	
	table.insert(output, '|}')
	
	-- Preprocess the entire output to render wikitext
	return frame:preprocess(table.concat(output, '\n'))
end

return p
MediaWiki Appliance - Powered by TurnKey Linux