Module:SMWUtils: Difference between revisions

Fix column index calculation - account for subject column in broadtable format (via update-page on MediaWiki MCP Server)
Fix deduplication: key on printout columns instead of subject (subobject subjects are unique per-input) (via update-page on MediaWiki MCP Server)
 
(2 intermediate revisions by the same user not shown)
Line 40: Line 40:
     if not s then return nil end
     if not s then return nil end
     return trim(s:gsub('<[^>]+>', ''):gsub('&[^;]+;', ''))
     return trim(s:gsub('<[^>]+>', ''):gsub('&[^;]+;', ''))
end
-- Deduplicate rows by printout columns, keeping the first occurrence
-- Uses stripped text of printout columns (2..n+1) as the dedup key,
-- since subobject queries produce unique subjects (e.g. Page#sub1, Page#sub2)
-- but identical printout values for the same parent page
local function deduplicateRows(rows, numPrintouts)
    local seen = {}
    local deduped = {}
    for _, row in ipairs(rows) do
        local keyParts = {}
        for j = 2, numPrintouts + 1 do
            table.insert(keyParts, stripHtml(row[j]) or '')
        end
        local key = table.concat(keyParts, '|')
        if not seen[key] then
            seen[key] = true
            table.insert(deduped, row)
        end
    end
    return deduped
end
end


Line 112: Line 133:
--  printouts: Comma-separated properties to display
--  printouts: Comma-separated properties to display
--  headers: Comma-separated header names
--  headers: Comma-separated header names
--  mainlabel: Optional header for the subject/page name column
--  format: table, ul, ol (default: table)
--  format: table, ul, ol (default: table)
--  intro: Custom intro text
--  intro: Custom intro text
Line 122: Line 144:
     local headers = trim(args.headers) or ''
     local headers = trim(args.headers) or ''
     local intro = trim(args.intro)
     local intro = trim(args.intro)
    local mainlabel = trim(args.mainlabel)
      
      
     if not minProperty then
     if not minProperty then
Line 174: Line 197:
         end
         end
     end
     end
   
    -- Deduplicate (multiple subobjects on same page produce duplicate rows)
    filtered = deduplicateRows(filtered, #printoutProps)
      
      
     -- Build headers list
     -- Build headers list
Line 184: Line 210:
      
      
     if format == 'ul' then
     if format == 'ul' then
         return p._formatList(filtered, printoutProps, headerList, 'ul', introText)
         return p._formatList(filtered, printoutProps, headerList, 'ul', introText, mainlabel)
     elseif format == 'ol' then
     elseif format == 'ol' then
         return p._formatList(filtered, printoutProps, headerList, 'ol', introText)
         return p._formatList(filtered, printoutProps, headerList, 'ol', introText, mainlabel)
     else
     else
         return p._formatTable(filtered, printoutProps, headerList, introText)
         return p._formatTable(filtered, printoutProps, headerList, introText, mainlabel)
     end
     end
end
end
Line 201: Line 227:
     local headers = trim(args.headers) or ''
     local headers = trim(args.headers) or ''
     local intro = trim(args.intro)
     local intro = trim(args.intro)
    local mainlabel = trim(args.mainlabel)
      
      
     if not maxProperty then
     if not maxProperty then
Line 251: Line 278:
         end
         end
     end
     end
   
    -- Deduplicate (multiple subobjects on same page produce duplicate rows)
    filtered = deduplicateRows(filtered, #printoutProps)
      
      
     -- Build headers list
     -- Build headers list
Line 261: Line 291:
      
      
     if format == 'ul' then
     if format == 'ul' then
         return p._formatList(filtered, printoutProps, headerList, 'ul', introText)
         return p._formatList(filtered, printoutProps, headerList, 'ul', introText, mainlabel)
     elseif format == 'ol' then
     elseif format == 'ol' then
         return p._formatList(filtered, printoutProps, headerList, 'ol', introText)
         return p._formatList(filtered, printoutProps, headerList, 'ol', introText, mainlabel)
     else
     else
         return p._formatTable(filtered, printoutProps, headerList, introText)
         return p._formatTable(filtered, printoutProps, headerList, introText, mainlabel)
     end
     end
end
end
Line 271: Line 301:
-- Format results as a wikitable
-- Format results as a wikitable
-- Note: row[1] is subject, row[2..n+1] are printouts, row[n+2] is min/max value
-- Note: row[1] is subject, row[2..n+1] are printouts, row[n+2] is min/max value
function p._formatTable(rows, printouts, headers, introText)
function p._formatTable(rows, printouts, headers, introText, mainlabel)
     local output = {}
     local output = {}
      
      
Line 279: Line 309:
     -- Header row
     -- Header row
     table.insert(output, '|-\n! #')
     table.insert(output, '|-\n! #')
    if mainlabel then
        table.insert(output, ' !! ' .. mainlabel)
    end
     for i, prop in ipairs(printouts) do
     for i, prop in ipairs(printouts) do
         local headerText = headers[i] or prop:gsub('.*%.', ''):gsub('Has ', '')
         local headerText = headers[i] or prop:gsub('.*%.', ''):gsub('Has ', '')
Line 288: Line 321:
     for i, row in ipairs(rows) do
     for i, row in ipairs(rows) do
         local tr = '|-\n| ' .. tostring(i)
         local tr = '|-\n| ' .. tostring(i)
        if mainlabel then
            tr = tr .. ' || ' .. (row[1] or '')
        end
         for j = 1, #printouts do
         for j = 1, #printouts do
             -- Printout j is in column j+1 (offset by subject column)
             -- Printout j is in column j+1 (offset by subject column)
Line 301: Line 337:


-- Format results as a list
-- Format results as a list
function p._formatList(rows, printouts, headers, listType, introText)
function p._formatList(rows, printouts, headers, listType, introText, mainlabel)
     local output = {}
     local output = {}
     local bullet = listType == 'ol' and '#' or '*'
     local bullet = listType == 'ol' and '#' or '*'
Line 309: Line 345:
     for _, row in ipairs(rows) do
     for _, row in ipairs(rows) do
         local parts = {}
         local parts = {}
        if mainlabel then
            local subject = row[1]
            if subject and subject ~= '' then
                table.insert(parts, subject)
            end
        end
         for j = 1, #printouts do
         for j = 1, #printouts do
             -- Printout j is in column j+1 (offset by subject column)
             -- Printout j is in column j+1 (offset by subject column)
MediaWiki Appliance - Powered by TurnKey Linux