Module:Part: Difference between revisions

Update Documents section to query related documents via Has related part and prefill part in Add a document URL (via update-page on MediaWiki MCP Server)
Refactor Compatible CRT Models and Compatible AV Devices to use group-by-type label/value rows instead of full wikitables (via update-page on MediaWiki MCP Server)
Line 228: Line 228:
local crt_models = trim(args.crt_models)
local crt_models = trim(args.crt_models)
if crt_models and crt_models ~= '' then
if crt_models and crt_models ~= '' then
addSectionRow(container, 'Compatible CRT Models')
-- Parse crt_models names
local crtNames = {}
local crtDiv = container:tag('div')
css(crtDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;')
-- Parse crt_models and create OR conditions
local crtConditions = {}
local crtConditions = {}
for crt in crt_models:gmatch('[^,]+') do
for crt in crt_models:gmatch('[^,]+') do
local c = trim(crt)
local c = trim(crt)
if c then
if c then
table.insert(crtNames, c)
table.insert(crtConditions, '[[' .. c .. ']]')
table.insert(crtConditions, '[[' .. c .. ']]')
end
end
Line 243: Line 240:
if #crtConditions > 0 then
if #crtConditions > 0 then
-- Query for CRT types
local crtQueryWikitext =
local crtQueryWikitext =
'{{#ask:\n' ..
'{{#ask:\n' ..
' [[Category:CRT models]]\n' ..
' [[Category:CRT models]]\n' ..
' <q>' .. table.concat(crtConditions, ' OR ') .. '</q>\n' ..
' <q>' .. table.concat(crtConditions, ' OR ') .. '</q>\n' ..
' |?Has image#70px;x70px=Photo\n' ..
' |?Has CRT type\n' ..
' |?Has model code=Model\n' ..
' |format=broadtable\n' ..
' |?Has screen size inches=Size\n' ..
' |headers=hide\n' ..
' |format=table\n' ..
' |link=none\n' ..
' |class=wikitable\n' ..
' |mainlabel=CRT model\n' ..
' |link=subject\n' ..
'}}'
'}}'
local crtHtml = frame:preprocess(crtQueryWikitext)
local crtResult = frame:preprocess(crtQueryWikitext)
crtDiv:wikitext(crtHtml)
-- Parse results into a lookup of page name -> CRT type
local crtTypes = {}
if crtResult then
for rowContent in crtResult:gmatch('<tr[^>]*>(.-)</tr>') do
local cells = {}
for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
local cleanContent = cellContent:gsub('<[^>]+>', '')
table.insert(cells, trim(cleanContent))
end
if #cells >= 2 and cells[1] and cells[2] then
crtTypes[cells[1]] = cells[2]
end
end
end
-- Group by type
local byType = {}
local typeOrder = {}
local typeSeen = {}
for _, crtName in ipairs(crtNames) do
local cType = crtTypes[crtName] or 'Unknown'
if not typeSeen[cType] then
typeSeen[cType] = true
table.insert(typeOrder, cType)
byType[cType] = {}
end
table.insert(byType[cType], crtName)
end
if #typeOrder > 0 then
addSectionRow(container, 'Compatible CRT Models')
for _, cType in ipairs(typeOrder) do
local deviceLinks = {}
for _, crtName in ipairs(byType[cType]) do
table.insert(deviceLinks, '[[' .. crtName .. ']]')
end
addRow(container, cType, table.concat(deviceLinks, '<br>'), true)
end
end
end
end
end
end
Line 263: Line 299:
local av_devices = trim(args.av_devices)
local av_devices = trim(args.av_devices)
if av_devices and av_devices ~= '' then
if av_devices and av_devices ~= '' then
addSectionRow(container, 'Compatible AV Devices')
-- Parse av_devices names
local avNames = {}
local avDiv = container:tag('div')
css(avDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;')
-- Parse av_devices and create OR conditions
local avConditions = {}
local avConditions = {}
for av in av_devices:gmatch('[^,]+') do
for av in av_devices:gmatch('[^,]+') do
local a = trim(av)
local a = trim(av)
if a then
if a then
table.insert(avNames, a)
table.insert(avConditions, '[[' .. a .. ']]')
table.insert(avConditions, '[[' .. a .. ']]')
end
end
Line 278: Line 311:
if #avConditions > 0 then
if #avConditions > 0 then
-- Query for device types
local avQueryWikitext =
local avQueryWikitext =
'{{#ask:\n' ..
'{{#ask:\n' ..
' [[Category:AV devices]]\n' ..
' [[Category:AV devices]]\n' ..
' <q>' .. table.concat(avConditions, ' OR ') .. '</q>\n' ..
' <q>' .. table.concat(avConditions, ' OR ') .. '</q>\n' ..
' |?Has image#70px;x70px=Photo\n' ..
' |?Has device type\n' ..
' |?Has model code=Model\n' ..
' |format=broadtable\n' ..
' |?Has device type=Type\n' ..
' |headers=hide\n' ..
' |format=table\n' ..
' |link=none\n' ..
' |class=wikitable\n' ..
' |mainlabel=AV device\n' ..
' |link=subject\n' ..
'}}'
'}}'
local avHtml = frame:preprocess(avQueryWikitext)
local avResult = frame:preprocess(avQueryWikitext)
avDiv:wikitext(avHtml)
-- Parse results into a lookup of page name -> device type
local deviceTypes = {}
if avResult then
for rowContent in avResult:gmatch('<tr[^>]*>(.-)</tr>') do
local cells = {}
for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
local cleanContent = cellContent:gsub('<[^>]+>', '')
table.insert(cells, trim(cleanContent))
end
if #cells >= 2 and cells[1] and cells[2] then
deviceTypes[cells[1]] = cells[2]
end
end
end
-- Group by type
local byType = {}
local typeOrder = {}
local typeSeen = {}
for _, avName in ipairs(avNames) do
local dtype = deviceTypes[avName] or 'Unknown'
if not typeSeen[dtype] then
typeSeen[dtype] = true
table.insert(typeOrder, dtype)
byType[dtype] = {}
end
table.insert(byType[dtype], avName)
end
if #typeOrder > 0 then
addSectionRow(container, 'Compatible AV Devices')
for _, dtype in ipairs(typeOrder) do
local deviceLinks = {}
for _, avName in ipairs(byType[dtype]) do
table.insert(deviceLinks, '[[' .. avName .. ']]')
end
addRow(container, dtype, table.concat(deviceLinks, '<br>'), true)
end
end
end
end
end
end
MediaWiki Appliance - Powered by TurnKey Linux