Module:BrandSeries: Difference between revisions
Fix: use HTML links instead of wikitext; debug count query (via update-page on MediaWiki MCP Server) |
Fix: use broadtable format for more reliable parsing (via update-page on MediaWiki MCP Server) |
||
| Line 26: | Line 26: | ||
end | end | ||
-- Query all CRTs for this brand with their series | -- Query all CRTs for this brand with their series using broadtable | ||
local crtQuery = string.format( | local crtQuery = string.format( | ||
'{{#ask:[[Category:CRT models]][[Has brand::%s]][[Has series::+]]|?Has series|format= | '{{#ask:[[Category:CRT models]][[Has brand::%s]][[Has series::+]]|?Has series|format=broadtable|link=none|headers=hide|limit=1000|searchlabel=}}', | ||
brand | brand | ||
) | ) | ||
local crtResult = frame:preprocess(crtQuery) | local crtResult = frame:preprocess(crtQuery) | ||
-- Count occurrences of each series | -- Count occurrences of each series by parsing HTML table | ||
local seriesCounts = {} | local seriesCounts = {} | ||
if crtResult and crtResult ~= '' then | if crtResult and crtResult ~= '' then | ||
for | -- Parse each table row | ||
-- | for rowContent in crtResult:gmatch('<tr[^>]*>(.-)</tr>') do | ||
local cells = {} | |||
if | for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do | ||
series = | -- Strip HTML tags | ||
if series then | local cleanContent = cellContent:gsub('<[^>]+>', '') | ||
table.insert(cells, trim(cleanContent)) | |||
end | |||
-- cells[1] = page name, cells[2] = series | |||
if #cells >= 2 and cells[2] then | |||
local series = cells[2] | |||
if series and series ~= '' then | |||
seriesCounts[series] = (seriesCounts[series] or 0) + 1 | seriesCounts[series] = (seriesCounts[series] or 0) + 1 | ||
end | end | ||
| Line 80: | Line 86: | ||
-- Series link - build as HTML anchor | -- Series link - build as HTML anchor | ||
local seriesLink = mw.html.create('a') | local seriesLink = mw.html.create('a') | ||
seriesLink:attr('href', '/wiki/' .. mw.uri.encode(row.series, 'WIKI')) | seriesLink:attr('href', '/wiki/' .. mw.uri.encode(row.series, 'WIKI')) | ||