Module:CRTModel: Difference between revisions
Revert to revision 7019: undo input/output sorting changes from this session (via update-page on MediaWiki MCP Server) Tag: Manual revert |
Add Parts section to CRT model infobox (queries parts linked via Has related CRT model) (via update-page on MediaWiki MCP Server) |
||
| Line 60: | Line 60: | ||
-- Helper function to extract model code from "Brand Model" format | -- Helper function to extract model code from "Brand Model" format | ||
-- If the accessory starts with the same brand as the CRT, strip it for cleaner display | -- If the accessory starts with the same brand as the CRT, strip it for cleaner display | ||
local function | local function getDisplayTextWithoutBrand(itemName, deviceBrand) | ||
if not | if not itemName then return nil end | ||
if not | if not deviceBrand then return itemName end | ||
-- Check if | -- Check if item name starts with the device brand followed by a space | ||
local brandPrefix = | local brandPrefix = deviceBrand .. ' ' | ||
if | if itemName:sub(1, #brandPrefix) == brandPrefix then | ||
-- Return just the model code portion | -- Return just the model code portion | ||
return | return itemName:sub(#brandPrefix + 1) | ||
end | end | ||
-- Otherwise return the full name | -- Otherwise return the full name | ||
return | return itemName | ||
end | end | ||
| Line 420: | Line 420: | ||
-- Remote control row: use Accessory form, strip brand prefix if it matches CRT brand | -- Remote control row: use Accessory form, strip brand prefix if it matches CRT brand | ||
if remote_control then | if remote_control then | ||
local displayText = | local displayText = getDisplayTextWithoutBrand(remote_control, brand) | ||
local remoteWikitext = string.format( | local remoteWikitext = string.format( | ||
'{{#formredlink: target=%s |form=Accessory |existing page link text=%s }}', | '{{#formredlink: target=%s |form=Accessory |existing page link text=%s }}', | ||
| Line 782: | Line 782: | ||
local accLinks = {} | local accLinks = {} | ||
for _, acc in ipairs(accs) do | for _, acc in ipairs(accs) do | ||
local displayText = | local displayText = getDisplayTextWithoutBrand(acc.page, brand) | ||
local linkText = displayText | local linkText = displayText | ||
if acc.desc and acc.desc ~= '' then | if acc.desc and acc.desc ~= '' then | ||
| Line 790: | Line 790: | ||
end | end | ||
addRow(container, accType, table.concat(accLinks, '<br>'), true) | addRow(container, accType, table.concat(accLinks, '<br>'), true) | ||
end | |||
end | |||
end | |||
-- Parts section: Query for parts linked via Has related CRT model | |||
local partsCountWikitext = | |||
'{{#ask:\n' .. | |||
' [[Category:Parts]]\n' .. | |||
' [[Has related CRT model::' .. fullPageName .. ']]\n' .. | |||
' |format=count\n' .. | |||
'}}' | |||
local partsCount = frame:preprocess(partsCountWikitext) | |||
local hasParts = partsCount and tonumber(trim(partsCount)) and tonumber(trim(partsCount)) > 0 | |||
if hasParts then | |||
addSectionRow(container, 'Parts') | |||
-- Query parts and display grouped by type/subtype | |||
local partsQueryWikitext = | |||
'{{#ask:\n' .. | |||
' [[Category:Parts]]\n' .. | |||
' [[Has related CRT model::' .. fullPageName .. ']]\n' .. | |||
' |?Has part type=type\n' .. | |||
' |?Has part subtype=subtype\n' .. | |||
' |?Has model code=model\n' .. | |||
' |?Has description=desc\n' .. | |||
' |format=broadtable\n' .. | |||
' |headers=hide\n' .. | |||
' |link=none\n' .. | |||
'}}' | |||
local partsResult = frame:preprocess(partsQueryWikitext) | |||
-- Parse parts from broadtable result and group by type | |||
local partsByType = {} | |||
local partTypeOrder = {} | |||
if partsResult then | |||
for rowContent in partsResult: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] = type, cells[3] = subtype, cells[4] = model code, cells[5] = description | |||
if #cells >= 2 then | |||
local partPageName = cells[1] | |||
local partType = cells[2] or 'Part' | |||
local partSubtype = cells[3] | |||
local partDesc = cells[5] | |||
-- Use subtype + type as the grouping key if subtype exists (e.g., "Jungle IC") | |||
local groupKey = partType | |||
if partSubtype and partSubtype ~= '' then | |||
groupKey = partSubtype .. ' ' .. partType:lower() | |||
end | |||
if partPageName then | |||
-- Track type order for consistent display | |||
if not partsByType[groupKey] then | |||
partsByType[groupKey] = {} | |||
table.insert(partTypeOrder, groupKey) | |||
end | |||
table.insert(partsByType[groupKey], { | |||
page = partPageName, | |||
desc = partDesc | |||
}) | |||
end | |||
end | |||
end | |||
end | |||
-- Display as label/value rows grouped by type | |||
for _, partType in ipairs(partTypeOrder) do | |||
local parts = partsByType[partType] | |||
if parts and #parts > 0 then | |||
local partLinks = {} | |||
for _, part in ipairs(parts) do | |||
local displayText = getDisplayTextWithoutBrand(part.page, brand) | |||
local linkText = displayText | |||
if part.desc and part.desc ~= '' then | |||
linkText = displayText .. ' – ' .. part.desc | |||
end | |||
table.insert(partLinks, '[[' .. part.page .. '|' .. linkText .. ']]') | |||
end | |||
addRow(container, partType, table.concat(partLinks, '<br>'), true) | |||
end | end | ||
end | end | ||