Module:Accessory: Difference between revisions

Add Archival Links section to infobox, querying Accessory link subobjects (via update-page on MediaWiki MCP Server)
Change type badge to breadcrumb style: Accessory › [accessory_type] (via update-page on MediaWiki MCP Server)
 
(6 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 crtCountWikitext =
-- 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=count\n' ..
' |format=broadtable\n' ..
' |headers=hide\n' ..
' |link=none\n' ..
'}}'
'}}'
local crtCountFromForward = frame:preprocess(crtCountWikitext)
local forwardResult = frame:preprocess(forwardQueryWikitext)
-- Also check reverse links (Has related CRT model set on this accessory page)
if forwardResult then
local crtReverseCountWikitext =
for rowContent in forwardResult:gmatch('<tr[^>]*>(.-)</tr>') do
'{{#ask:\n' ..
for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
' [[Category:CRT models]]\n' ..
local cleanContent = cellContent:gsub('<[^>]+>', '')
' [[Has related accessory::' .. fullPageName .. ']]\n' ..
local c = trim(cleanContent)
' |format=count\n' ..
if c and not crtSeen[c] then
'}}'
crtSeen[c] = true
local crtCountFromReverse = frame:preprocess(crtReverseCountWikitext)
table.insert(crtNames, c)
end
-- Query for CRTs that have this accessory via forward link (remote_control field)
break -- only need first cell (page name)
-- OR via Has related CRT model property set on this accessory
end
local hasCRTs = (tonumber(trim(crtCountFromForward)) or 0) > 0
end
end
-- Also query CRTs listed in the accessory's crt_models field
local crt_models = trim(args.crt_models)
local hasCRTsFromField = crt_models and crt_models ~= ''
if hasCRTs or hasCRTsFromField then
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;')
-- Query CRTs that reference this accessory via Has accessory property
local ul = crtDiv:tag('ul')
-- (This handles forward references like remote_control field)
ul:css('margin', '0'):css('padding-left', '1.5em')
local crtQueryWikitext =
'{{#ask:\n' ..
' [[Category:CRT models]]\n' ..
' [[Has accessory::' .. fullPageName .. ']]\n' ..
' |?Has image#70px;x70px=Photo\n' ..
' |?Has model code=Model\n' ..
' |?Has screen size inches=Size\n' ..
' |format=table\n' ..
' |class=wikitable\n' ..
' |mainlabel=CRT model\n' ..
' |link=subject\n' ..
'}}'
-- If we have CRTs from the crt_models field, also query those
for _, crtName in ipairs(crtNames) do
if hasCRTsFromField then
local li = ul:tag('li')
-- Parse crt_models and create OR conditions
li:wikitext('[[' .. crtName .. ']]')
local crtConditions = {}
for crt in crt_models:gmatch('[^,]+') do
local c = trim(crt)
if c then
table.insert(crtConditions, '[[' .. c .. ']]')
end
end
if #crtConditions > 0 then
crtQueryWikitext =
'{{#ask:\n' ..
' [[Category:CRT models]]\n' ..
' <q>' .. table.concat(crtConditions, ' OR ') .. '</q>\n' ..
' |?Has image#70px;x70px=Photo\n' ..
' |?Has model code=Model\n' ..
' |?Has screen size inches=Size\n' ..
' |format=table\n' ..
' |class=wikitable\n' ..
' |mainlabel=CRT model\n' ..
' |link=subject\n' ..
'}}'
end
end
end
local crtHtml = frame:preprocess(crtQueryWikitext)
crtDiv:wikitext(crtHtml)
end
end


Line 225: 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
addSectionRow(container, 'Compatible AV Devices')
local avDiv = container:tag('div')
css(avDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;')
-- 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 240: 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 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
-- 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
Line 260: Line 306:
addSectionRow(container, 'Documents')
addSectionRow(container, 'Documents')
-- Build upload link
-- Build upload link with prefilled accessory
local timestamp = os.time()
local timestamp = os.time()
math.randomseed(timestamp + pageId)
math.randomseed(timestamp + pageId)
local random = math.random(1000, 9999)
local random = math.random(1000, 9999)
local target = "Document:" .. timestamp .. "-" .. random
local target = "Document:" .. timestamp .. "-" .. random
local uploadUrl = mw.uri.fullUrl(
local uploadUrl = mw.uri.fullUrl(
"Special:FormEdit",
"Special:FormEdit",
{
{
form = "Document",
form = "Document",
target = target
target = target,
["Document[accessories]"] = pageName
}
}
)
)
-- Query for document count
local documentsCountWikitext =
'{{#ask:\n' ..
' [[Category:Documents]]\n' ..
' [[Has related accessory::' .. fullPageName .. ']]\n' ..
' |format=count\n' ..
'}}'
local documentCount = frame:preprocess(documentsCountWikitext)
local hasDocuments = documentCount and tonumber(trim(documentCount)) and tonumber(trim(documentCount)) > 0
-- Create the content div
local docsDiv = container:tag('div')
local docsDiv = container:tag('div')
css(docsDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em; text-align:center;')
css(docsDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;')
docsDiv:wikitext('[' .. tostring(uploadUrl) .. ' <span class="infobox-button infobox-button-secondary">Add a document</span>]')
if hasDocuments then
-- Use #ask to get documents
local docsQueryWikitext =
'{{#ask:\n' ..
' [[Category:Documents]]\n' ..
' [[Has related accessory::' .. fullPageName .. ']]\n' ..
' |?Has document type\n' ..
' |?Has document URL\n' ..
' |?Has document file\n' ..
' |?Has document title\n' ..
' |format=broadtable\n' ..
' |headers=hide\n' ..
' |link=none\n' ..
'}}'
local docsResult = frame:preprocess(docsQueryWikitext)
-- Create UL element
local ul = mw.html.create('ul')
ul:css('margin', '0'):css('padding-left', '1.5em')
-- Parse the HTML table
if docsResult then
for rowContent in docsResult:gmatch('<tr[^>]*>(.-)</tr>') do
local cells = {}
for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
local cleanContent = cellContent:gsub('<[^>]+>', '')
table.insert(cells, cleanContent)
end
-- cells[1] = page name, cells[2] = type, cells[3] = URL, cells[4] = file, cells[5] = title
if #cells >= 2 then
local docType = trim(cells[2])
local docTitle = trim(cells[5])
local docPageName = trim(cells[1])
if docPageName then
local displayText = 'Document'
if docTitle then
displayText = docTitle
elseif docType then
displayText = docType
end
local li = ul:tag('li')
li:wikitext('[[' .. docPageName .. '|' .. displayText .. ']]')
end
end
end
end
docsDiv:node(ul)
-- Add upload link as a button
local uploadDiv = mw.html.create('div')
uploadDiv:css('margin-top', '0.5em'):css('text-align', 'center')
uploadDiv:wikitext('[' .. tostring(uploadUrl) .. ' <span class="infobox-button infobox-button-secondary">Add a document</span>]')
docsDiv:node(uploadDiv)
else
-- Just show the upload link as a button
local uploadDiv = mw.html.create('div')
uploadDiv:css('text-align', 'center')
uploadDiv:wikitext('[' .. tostring(uploadUrl) .. ' <span class="infobox-button infobox-button-secondary">Add a document</span>]')
docsDiv:node(uploadDiv)
end


-- Archival Links section: Query for link subobjects and display by type
-- Archival Links section: Query for link subobjects and display by type
MediaWiki Appliance - Powered by TurnKey Linux