Module:BrandSeries

Revision as of 16:11, 17 January 2026 by Arclight (talk | contribs) (Arclight changed the content model of the page Module:BrandSeries from "wikitext" to "Scribunto module")

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
			-- Build the browse link
			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 "''No series-assigned models for this brand.''"
	end
	
	-- Build the table
	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, '|-')
		table.insert(output, string.format('| [[%s]] || %d || %s', row.series, row.count, row.browse))
	end
	
	table.insert(output, '|}')
	
	return table.concat(output, '\n')
end

return p
MediaWiki Appliance - Powered by TurnKey Linux