Module:BrandSeries: Difference between revisions
Fix Unassigned link - use HTML anchor tag instead of wikitext external link (via update-page on MediaWiki MCP Server) |
Use Category:CRTs without series for unassigned link (SMW !+ queries don't work) (via update-page on MediaWiki MCP Server) |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 15: | Line 15: | ||
-- Encode a string for use in Special:Ask URL path | -- Encode a string for use in Special:Ask URL path | ||
local function encodeForAsk(s) | local function encodeForAsk(s) | ||
-- Special:Ask uses a custom encoding: [ = -5B, ] = -5D, space = _ | -- Special:Ask uses a custom encoding: [ = -5B, ] = -5D, space = _, etc. | ||
s = s:gsub('%[', '-5B') | s = s:gsub('%[', '-5B') | ||
s = s:gsub('%]', '-5D') | s = s:gsub('%]', '-5D') | ||
s = s:gsub(' ', '_') | s = s:gsub(' ', '_') | ||
s = s:gsub('%+', '-2B') | s = s:gsub('%+', '-2B') | ||
s = s:gsub('!', '-21') | |||
return s | return s | ||
end | end | ||
| Line 38: | Line 39: | ||
return '<span class="error">Error: brand parameter required</span>' | return '<span class="error">Error: brand parameter required</span>' | ||
end | end | ||
-- Query total count of all CRTs for this brand | |||
local totalQuery = string.format( | |||
'{{#ask:[[Category:CRT models]][[Has brand::%s]]|format=count}}', | |||
brand | |||
) | |||
local totalResult = frame:preprocess(totalQuery) | |||
local totalCount = tonumber(trim(totalResult)) or 0 | |||
-- Query all CRTs for this brand with their series using broadtable | -- Query all CRTs for this brand with their series using broadtable | ||
| Line 48: | Line 57: | ||
-- Count occurrences of each series by parsing HTML table | -- Count occurrences of each series by parsing HTML table | ||
local seriesCounts = {} | local seriesCounts = {} | ||
local assignedCount = 0 | |||
if crtResult and crtResult ~= '' then | if crtResult and crtResult ~= '' then | ||
-- Parse each table row | -- Parse each table row | ||
| Line 63: | Line 73: | ||
if series and series ~= '' and series ~= '+' then | if series and series ~= '' and series ~= '+' then | ||
seriesCounts[series] = (seriesCounts[series] or 0) + 1 | seriesCounts[series] = (seriesCounts[series] or 0) + 1 | ||
assignedCount = assignedCount + 1 | |||
end | end | ||
end | end | ||
| Line 68: | Line 79: | ||
end | end | ||
-- | -- Calculate unassigned as total minus assigned | ||
local unassignedCount = totalCount - assignedCount | |||
local unassignedCount = | |||
-- Convert to sorted array | -- Convert to sorted array | ||
| Line 89: | Line 95: | ||
-- If no series found and no unassigned, return nothing | -- If no series found and no unassigned, return nothing | ||
if #rows == 0 and unassignedCount | if #rows == 0 and unassignedCount <= 0 then | ||
return "" | return "" | ||
end | end | ||
| Line 107: | Line 113: | ||
-- Unassigned row (if any) with link to Special:Ask | -- Unassigned row (if any) with link to Special:Ask | ||
if unassignedCount > 0 then | if unassignedCount > 0 then | ||
-- | -- Use Category:CRTs without series instead of [[Has series::!+]] | ||
local askQuery = string.format('[[Category: | -- because SMW's negated existence queries don't work correctly | ||
local askUrl = '/wiki/Special:Ask/' .. encodeForAsk(askQuery) | local askQuery = string.format('[[Category:CRTs without series]][[Has brand::%s]]', brand) | ||
-- Use | local askUrl = 'https://wiki.everycrt.com/wiki/Special:Ask/' .. encodeForAsk(askQuery) | ||
local unassignedLink = | -- Use wikitext external link syntax with full URL | ||
local unassignedLink = "[" .. askUrl .. " ''(Unassigned)'']" | |||
table.insert(lines, '|-') | table.insert(lines, '|-') | ||