Module:BrandSeries: Difference between revisions

Rewrite using HTML builder like CRTModel; fix count parsing (via update-page on MediaWiki MCP Server)
Fix: use HTML links instead of wikitext; debug count query (via update-page on MediaWiki MCP Server)
Line 26: Line 26:
end
end
-- First, get all series from Category:Series
-- Query all CRTs for this brand with their series, then count in Lua
local seriesQuery = '{{#ask:[[Category:Series]]|format=list|link=none|limit=500|sep=@@|searchlabel=}}'
local crtQuery = string.format(
local seriesResult = frame:preprocess(seriesQuery)
'{{#ask:[[Category:CRT models]][[Has brand::%s]][[Has series::+]]|?Has series|format=list|link=none|limit=1000|sep=@@|searchlabel=}}',
brand
)
local crtResult = frame:preprocess(crtQuery)
if not seriesResult or seriesResult == '' then
-- Count occurrences of each series
return "<i>No series found.</i>"
local seriesCounts = {}
end
if crtResult and crtResult ~= '' then
for entry in crtResult:gmatch('[^@@]+') do
-- Parse series names
-- Each entry is "PageName (SeriesName)" - extract series
local seriesList = {}
local series = entry:match('%s*%((.-)%)%s*$')
for series in seriesResult:gmatch('[^@@]+') do
if series then
local s = trim(series)
series = trim(series)
if s then
if series then
table.insert(seriesList, s)
seriesCounts[series] = (seriesCounts[series] or 0) + 1
end
end
end
end
end
end
-- Sort alphabetically
-- Convert to sorted array
table.sort(seriesList)
-- For each series, count how many CRTs belong to this brand
local rows = {}
local rows = {}
for _, series in ipairs(seriesList) do
for series, count in pairs(seriesCounts) do
local countQuery = string.format(
table.insert(rows, {
'{{#ask:[[Category:CRT models]][[Has brand::%s]][[Has series::%s]]|format=count}}',
series = series,
brand, series
count = count
)
})
local countResult = frame:preprocess(countQuery)
local count = tonumber(trim(countResult)) or 0
if count > 0 then
-- Build browse link and preprocess it
local browseLink = string.format(
'{{#queryformlink:form=CRT query|query string=CRT query[brand]=%s&CRT query[series]=%s&_run|link text=Browse}}',
brand, series
)
local browseHtml = frame:preprocess(browseLink)
table.insert(rows, {
series = series,
count = count,
browse = browseHtml
})
end
end
end
-- Sort by series name
table.sort(rows, function(a, b) return a.series < b.series end)
-- If no series found for this brand
-- If no series found for this brand
Line 90: Line 78:
for _, row in ipairs(rows) do
for _, row in ipairs(rows) do
local tr = tbl:tag('tr')
local tr = tbl:tag('tr')
tr:tag('td'):wikitext('[[' .. row.series .. ']]')
-- Series link - build as HTML anchor
local seriesUrl = mw.uri.fullUrl(row.series)
local seriesLink = mw.html.create('a')
seriesLink:attr('href', '/wiki/' .. mw.uri.encode(row.series, 'WIKI'))
seriesLink:attr('title', row.series)
seriesLink:wikitext(row.series)
tr:tag('td'):node(seriesLink)
-- Count
tr:tag('td'):wikitext(tostring(row.count))
tr:tag('td'):wikitext(tostring(row.count))
tr:tag('td'):wikitext(row.browse)
-- Browse link
local browseQuery = string.format(
'{{#queryformlink:form=CRT query|query string=CRT query[brand]=%s&CRT query[series]=%s&_run|link text=Browse}}',
brand, row.series
)
local browseHtml = frame:preprocess(browseQuery)
tr:tag('td'):wikitext(browseHtml)
end
end
MediaWiki Appliance - Powered by TurnKey Linux