Module:BrandSeries

Revision as of 16:17, 17 January 2026 by Arclight-MCP-Bot (talk | contribs) (Rewrite using HTML builder like CRTModel; fix count parsing (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 "<i>No series found.</i>"
	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
			-- Build browse link and preprocess it
			local browseLink = string.format(
				'{{#queryformlink:form=CRT query|query string=CRT query[brand]=%s&CRT query[series]=%s&_run|link text=Browse}}',
				brand, series
			)
			local browseHtml = frame:preprocess(browseLink)
			
			table.insert(rows, {
				series = series,
				count = count,
				browse = browseHtml
			})
		end
	end
	
	-- If no series found for this brand
	if #rows == 0 then
		return "<i>No series-assigned models for this brand.</i>"
	end
	
	-- Build the table as HTML
	local tbl = mw.html.create('table')
	tbl:addClass('wikitable')
	
	-- Header row
	local headerRow = tbl:tag('tr')
	headerRow:tag('th'):wikitext('Series')
	headerRow:tag('th'):wikitext('Count')
	headerRow:tag('th'):wikitext('Browse')
	
	-- Data rows
	for _, row in ipairs(rows) do
		local tr = tbl:tag('tr')
		tr:tag('td'):wikitext('[[' .. row.series .. ']]')
		tr:tag('td'):wikitext(tostring(row.count))
		tr:tag('td'):wikitext(row.browse)
	end
	
	return tostring(tbl)
end

return p
MediaWiki Appliance - Powered by TurnKey Linux