Module:Accessory: Difference between revisions
Update Documents section to query related documents via Has related accessory and prefill accessory in Add a document URL (via update-page on MediaWiki MCP Server) |
Change type badge to breadcrumb style: Accessory › [accessory_type] (via update-page on MediaWiki MCP Server) |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 72: | Line 72: | ||
local description = trim(args.description) | local description = trim(args.description) | ||
local image_main = trim(args.image_main) | local image_main = trim(args.image_main) | ||
-- Physical specifications | |||
local height = trim(args.height) | |||
local width = trim(args.width) | |||
local depth = trim(args.depth) | |||
local weight = trim(args.weight) | |||
-- Option card specific | -- Option card specific | ||
| Line 95: | Line 101: | ||
'infobox-title' | 'infobox-title' | ||
) | ) | ||
-- Type badge (breadcrumb) | |||
local badgeWikitext = '[[:Category:Accessories|Accessory]]' | |||
if accessory_type then | |||
badgeWikitext = badgeWikitext .. ' › ' .. accessory_type | |||
end | |||
addFullRow(container, badgeWikitext, '', false, 'infobox-type-badge') | |||
if image_main then | if image_main then | ||
| Line 143: | Line 156: | ||
local ecidWikitext = string.format('[{{fullurl:%s|curid=%d}} EC%d]', pageName, pageId, pageId) | local ecidWikitext = string.format('[{{fullurl:%s|curid=%d}} EC%d]', pageName, pageId, pageId) | ||
addRow(container, 'ECID', frame:preprocess(ecidWikitext), true) | addRow(container, 'ECID', frame:preprocess(ecidWikitext), true) | ||
-- Dimensions section | |||
if height or width or depth or weight then | |||
addSectionRow(container, 'Dimensions') | |||
if height or width or depth then | |||
local h = height or '–' | |||
local w = width or '–' | |||
local d = depth or '–' | |||
addRow(container, 'H × W × D', string.format('%s × %s × %s', h, w, d), false) | |||
end | |||
if weight then | |||
local weightWikitext = '{{#show: ' .. fullPageName .. ' |?Weight # lb }} / {{#show: ' .. fullPageName .. ' |?Weight # kg }}' | |||
addRow(container, 'Weight', frame:preprocess(weightWikitext), true) | |||
end | |||
end | |||
-- Compatible CRT models section | -- Compatible CRT models section | ||
local | -- Collect CRT model names from both sources: crt_models field and forward Has accessory links | ||
local crtNames = {} | |||
local crtSeen = {} | |||
-- Source 1: crt_models field on this accessory page | |||
local crt_models = trim(args.crt_models) | |||
if crt_models then | |||
for crt in crt_models:gmatch('[^,]+') do | |||
local c = trim(crt) | |||
if c and not crtSeen[c] then | |||
crtSeen[c] = true | |||
table.insert(crtNames, c) | |||
end | |||
end | |||
end | |||
-- Source 2: CRT models that reference this accessory via Has accessory (e.g. remote_control field) | |||
local forwardQueryWikitext = | |||
'{{#ask:\n' .. | '{{#ask:\n' .. | ||
' [[Category:CRT models]]\n' .. | ' [[Category:CRT models]]\n' .. | ||
' [[Has accessory::' .. fullPageName .. ']]\n' .. | ' [[Has accessory::' .. fullPageName .. ']]\n' .. | ||
' |format= | ' |format=broadtable\n' .. | ||
' |headers=hide\n' .. | |||
' |link=none\n' .. | |||
'}}' | '}}' | ||
local | local forwardResult = frame:preprocess(forwardQueryWikitext) | ||
if forwardResult then | |||
for rowContent in forwardResult:gmatch('<tr[^>]*>(.-)</tr>') do | |||
for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do | |||
local cleanContent = cellContent:gsub('<[^>]+>', '') | |||
local c = trim(cleanContent) | |||
if c and not crtSeen[c] then | |||
crtSeen[c] = true | |||
table.insert(crtNames, c) | |||
end | |||
break -- only need first cell (page name) | |||
end | |||
end | |||
end | |||
if | if #crtNames > 0 then | ||
addSectionRow(container, 'Compatible CRT Models') | addSectionRow(container, 'Compatible CRT Models') | ||
| Line 176: | Line 222: | ||
css(crtDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;') | css(crtDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;') | ||
local ul = crtDiv:tag('ul') | |||
ul:css('margin', '0'):css('padding-left', '1.5em') | |||
local | |||
for _, crtName in ipairs(crtNames) do | |||
local li = ul:tag('li') | |||
li:wikitext('[[' .. crtName .. ']]') | |||
end | end | ||
end | end | ||
| Line 221: | Line 234: | ||
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 | ||
-- Parse av_devices and create OR conditions | -- Parse av_devices and create OR conditions | ||
local avNames = {} | |||
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 236: | Line 246: | ||
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 device type\n' .. | |||
' |format=broadtable\n' .. | |||
' |?Has device type | ' |headers=hide\n' .. | ||
' |format= | ' |link=none\n' .. | ||
' | | |||
' |link= | |||
'}}' | '}}' | ||
local | local avResult = frame:preprocess(avQueryWikitext) | ||
-- 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 | |||
-- cells[1] = page name, cells[2] = device type | |||
if #cells >= 2 and cells[1] and cells[2] then | |||
deviceTypes[cells[1]] = cells[2] | |||
end | |||
end | |||
end | |||
-- Group device names 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 | ||