Module:AVDeviceTypeList

Revision as of 22:08, 24 February 2026 by Arclight-MCP-Bot (talk | contribs) (Fix: query distinct device types from actual device pages instead of category registrations (fixes slash-in-title types like VCR/DVD-R combo) (via update-page on MediaWiki MCP Server))

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

-- Module:AVDeviceTypeList
-- Dynamically generates a list of AV device type buttons by querying
-- distinct device types from actual AV device pages.
-- This approach works for all device types, including those with
-- slashes in the name (e.g., VCR/DVD combo, VCR/DVD-R combo).

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 distinct device types from actual AV device pages
	local queryWikitext =
		'{{#ask:\n' ..
		' [[Category:AV devices]]\n' ..
		' [[Has device type::+]]\n' ..
		' |?Has device type\n' ..
		' |format=broadtable\n' ..
		' |headers=hide\n' ..
		' |link=none\n' ..
		' |mainlabel=-\n' ..
		' |limit=5000\n' ..
		' |searchlabel=\n' ..
		'}}'
	
	local queryResult = frame:preprocess(queryWikitext)
	
	-- Extract and deduplicate type names from result
	local seen = {}
	local typesList = {}
	
	if queryResult then
		for cellContent in queryResult:gmatch('<td[^>]*>(.-)</td>') do
			local deviceType = trim(cellContent:gsub('<[^>]+>', ''))
			if deviceType and deviceType ~= '' and not seen[deviceType] then
				seen[deviceType] = true
				table.insert(typesList, deviceType)
			end
		end
	end
	
	-- Sort alphabetically
	table.sort(typesList)
	
	if #typesList == 0 then
		return '<p><em>No AV device types found.</em></p>'
	end
	
	-- Build the output: render each type using Template:AV device type entry
	local output = {}
	for _, deviceType in ipairs(typesList) do
		local templateCall
		if linkType then
			templateCall = '{{AV device type entry|type=' .. deviceType .. '|link=' .. linkType .. '}}'
		else
			templateCall = '{{AV device type entry|type=' .. deviceType .. '}}'
		end
		table.insert(output, frame:preprocess(templateCall))
	end
	
	return table.concat(output, '\n')
end

return p
MediaWiki Appliance - Powered by TurnKey Linux