Fix: use HTML links instead of wikitext; debug count query (via update-page on MediaWiki MCP Server)
Fix: use broadtable format for more reliable parsing (via update-page on MediaWiki MCP Server)
Line 26: Line 26:
end
end
-- Query all CRTs for this brand with their series, then count in Lua
-- Query all CRTs for this brand with their series using broadtable
local crtQuery = string.format(
local crtQuery = string.format(
'{{#ask:[[Category:CRT models]][[Has brand::%s]][[Has series::+]]|?Has series|format=list|link=none|limit=1000|sep=@@|searchlabel=}}',
'{{#ask:[[Category:CRT models]][[Has brand::%s]][[Has series::+]]|?Has series|format=broadtable|link=none|headers=hide|limit=1000|searchlabel=}}',
brand
brand
)
)
local crtResult = frame:preprocess(crtQuery)
local crtResult = frame:preprocess(crtQuery)
-- Count occurrences of each series
-- Count occurrences of each series by parsing HTML table
local seriesCounts = {}
local seriesCounts = {}
if crtResult and crtResult ~= '' then
if crtResult and crtResult ~= '' then
for entry in crtResult:gmatch('[^@@]+') do
-- Parse each table row
-- Each entry is "PageName (SeriesName)" - extract series
for rowContent in crtResult:gmatch('<tr[^>]*>(.-)</tr>') do
local series = entry:match('%s*%((.-)%)%s*$')
local cells = {}
if series then
for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
series = trim(series)
-- Strip HTML tags
if series then
local cleanContent = cellContent:gsub('<[^>]+>', '')
table.insert(cells, trim(cleanContent))
end
-- cells[1] = page name, cells[2] = series
if #cells >= 2 and cells[2] then
local series = cells[2]
if series and series ~= '' then
seriesCounts[series] = (seriesCounts[series] or 0) + 1
seriesCounts[series] = (seriesCounts[series] or 0) + 1
end
end
Line 80: Line 86:
-- Series link - build as HTML anchor
-- Series link - build as HTML anchor
local seriesUrl = mw.uri.fullUrl(row.series)
local seriesLink = mw.html.create('a')
local seriesLink = mw.html.create('a')
seriesLink:attr('href', '/wiki/' .. mw.uri.encode(row.series, 'WIKI'))
seriesLink:attr('href', '/wiki/' .. mw.uri.encode(row.series, 'WIKI'))

Revision as of 23:30, 17 January 2026

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
	
	-- Query all CRTs for this brand with their series using broadtable
	local crtQuery = string.format(
		'{{#ask:[[Category:CRT models]][[Has brand::%s]][[Has series::+]]|?Has series|format=broadtable|link=none|headers=hide|limit=1000|searchlabel=}}',
		brand
	)
	local crtResult = frame:preprocess(crtQuery)
	
	-- Count occurrences of each series by parsing HTML table
	local seriesCounts = {}
	if crtResult and crtResult ~= '' then
		-- Parse each table row
		for rowContent in crtResult:gmatch('<tr[^>]*>(.-)</tr>') do
			local cells = {}
			for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
				-- Strip HTML tags
				local cleanContent = cellContent:gsub('<[^>]+>', '')
				table.insert(cells, trim(cleanContent))
			end
			-- cells[1] = page name, cells[2] = series
			if #cells >= 2 and cells[2] then
				local series = cells[2]
				if series and series ~= '' then
					seriesCounts[series] = (seriesCounts[series] or 0) + 1
				end
			end
		end
	end
	
	-- Convert to sorted array
	local rows = {}
	for series, count in pairs(seriesCounts) do
		table.insert(rows, {
			series = series,
			count = count
		})
	end
	
	-- Sort by series name
	table.sort(rows, function(a, b) return a.series < b.series 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')
		
		-- Series link - build as HTML anchor
		local seriesLink = mw.html.create('a')
		seriesLink:attr('href', '/wiki/' .. mw.uri.encode(row.series, 'WIKI'))
		seriesLink:attr('title', row.series)
		seriesLink:wikitext(row.series)
		tr:tag('td'):node(seriesLink)
		
		-- Count
		tr:tag('td'):wikitext(tostring(row.count))
		
		-- Browse link
		local browseQuery = string.format(
			'{{#queryformlink:form=CRT query|query string=CRT query[brand]=%s&CRT query[series]=%s&_run|link text=Browse}}',
			brand, row.series
		)
		local browseHtml = frame:preprocess(browseQuery)
		tr:tag('td'):wikitext(browseHtml)
	end
	
	return tostring(tbl)
end

return p
MediaWiki Appliance - Powered by TurnKey Linux