Module:SMWUtils: Difference between revisions

Add debug function and improve property chain handling (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)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:SMWUtils
-- Module:SMWUtils
-- Utility functions for Semantic MediaWiki queries
-- Utility functions for Semantic MediaWiki queries
-- Requires Semantic Scribunto extension for mw.smw.ask()
-- Uses frame:preprocess() for #ask queries (supports property chains)


local p = {}
local p = {}
Line 13: Line 13:
end
end


-- Helper to get a value from a row, trying multiple key formats for property chains
-- Parse an HTML table from #ask broadtable format into rows of cells
local function getRowValue(row, prop)
-- Filters out the "further results" row
     -- Try the exact property name first
local function parseHtmlTable(html)
     if row[prop] ~= nil then
     local rows = {}
        return row[prop]
     if not html then return rows end
    end
      
      
     -- For property chains like "Has CRT model.Has brand", also try just "Has brand"
     for rowContent in html:gmatch('<tr[^>]*>(.-)</tr>') do
    local lastPart = prop:match('%.([^%.]+)$')
        -- Skip "further results" rows
    if lastPart and row[lastPart] ~= nil then
        if not rowContent:match('smw%-broadtable%-furtherresults') then
         return row[lastPart]
            local cells = {}
            for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
                -- Keep HTML intact for links
                table.insert(cells, trim(cellContent) or '')
            end
            if #cells > 0 then
                table.insert(rows, cells)
            end
         end
     end
     end
      
      
     return nil
     return rows
end
 
-- Extract plain text from HTML (for numeric comparison)
local function stripHtml(s)
    if not s then return nil end
    return trim(s:gsub('<[^>]+>', ''):gsub('&[^;]+;', ''))
end
end


-- Helper to format a value for display
-- Deduplicate rows by printout columns, keeping the first occurrence
local function formatValue(val)
-- Uses stripped text of printout columns (2..n+1) as the dedup key,
    if val == nil then
-- since subobject queries produce unique subjects (e.g. Page#sub1, Page#sub2)
        return ''
-- but identical printout values for the same parent page
    end
local function deduplicateRows(rows, numPrintouts)
   
    local seen = {}
    if type(val) == 'table' then
    local deduped = {}
        -- Handle linked values (pages)
    for _, row in ipairs(rows) do
        if val.fulltext then
        local keyParts = {}
            return '[[' .. val.fulltext .. ']]'
        for j = 2, numPrintouts + 1 do
        elseif #val > 0 then
            table.insert(keyParts, stripHtml(row[j]) or '')
            -- Array of values
        end
            local parts = {}
        local key = table.concat(keyParts, '|')
            for _, v in ipairs(val) do
        if not seen[key] then
                if type(v) == 'table' and v.fulltext then
             seen[key] = true
                    table.insert(parts, '[[' .. v.fulltext .. ']]')
             table.insert(deduped, row)
                else
                    table.insert(parts, tostring(v))
                end
             end
             return table.concat(parts, ', ')
         end
         end
        return ''
    else
        return tostring(val)
     end
     end
    return deduped
end
end


--- Debug function to show what keys mw.smw.ask() returns
--- Debug function to see raw query output and parsing
-- Usage: {{#invoke:SMWUtils|debug|query=[[Has video signal format::Component (YPbPr)]]|printouts=Has CRT model, Has CRT model.Has brand}}
function p.debug(frame)
function p.debug(frame)
     local args = frame.args
     local args = frame.args
     local query = trim(args.query) or ''
     local conditions = trim(args.conditions) or ''
    local minProperty = trim(args.minProperty) or ''
     local printouts = trim(args.printouts) or ''
     local printouts = trim(args.printouts) or ''
      
      
     -- Build printout list
     -- Build printout list
     local printoutList = {}
     local printoutProps = {}
    local printoutQuery = {}
     for prop in printouts:gmatch('[^,]+') do
     for prop in printouts:gmatch('[^,]+') do
         table.insert(printoutList, '?' .. trim(prop))
        local propTrimmed = trim(prop)
         table.insert(printoutProps, propTrimmed)
        table.insert(printoutQuery, '|?' .. propTrimmed)
    end
    -- Add minProperty to query
    if minProperty ~= '' then
        table.insert(printoutQuery, '|?' .. minProperty .. '=#')
     end
     end
      
      
     local queryString = query .. '\n' .. table.concat(printoutList, '\n') .. '\n|limit=3'
     local queryWikitext = '{{#ask:' .. conditions .. '\n' ..
    local results = mw.smw.ask(queryString)
        table.concat(printoutQuery, '\n') .. '\n' ..
        '|sort=' .. minProperty .. '\n' ..
        '|order=asc\n' ..
        '|limit=5\n' ..
        '|format=broadtable\n' ..
        '|headers=hide\n' ..
        '|link=all\n' ..
        '}}'
      
      
     if not results or #results == 0 then
     local result = frame:preprocess(queryWikitext)
        return "No results found for query: <code>" .. mw.text.nowiki(queryString) .. "</code>"
     local rows = parseHtmlTable(result)
     end
      
      
     local output = {}
     local output = {}
     table.insert(output, "'''Query:''' <code>" .. mw.text.nowiki(queryString) .. "</code>\n\n")
     table.insert(output, "'''Query wikitext:'''\n<pre>" .. mw.text.nowiki(queryWikitext) .. "</pre>\n\n")
     table.insert(output, "'''Results (" .. #results .. " shown):'''\n\n")
    table.insert(output, "'''Raw HTML result:'''\n<pre>" .. mw.text.nowiki(result):sub(1, 2000) .. "</pre>\n\n")
     table.insert(output, "'''Parsed rows (" .. #rows .. "):'''\n\n")
      
      
     for i, row in ipairs(results) do
     for i, row in ipairs(rows) do
         table.insert(output, "'''Row " .. i .. " keys:'''\n")
         table.insert(output, "'''Row " .. i .. " (" .. #row .. " cells):'''\n")
         for k, v in pairs(row) do
         for j, cell in ipairs(row) do
             local valStr
             local stripped = stripHtml(cell) or "(nil)"
            if type(v) == 'table' then
             table.insert(output, "* Cell " .. j .. ": <code>" .. mw.text.nowiki(cell):sub(1, 100) .. "</code> → stripped: <code>" .. stripped .. "</code>\n")
                if v.fulltext then
                    valStr = "table{fulltext=\"" .. v.fulltext .. "\"}"
                else
                    valStr = "table[" .. #v .. "]"
                end
            else
                valStr = tostring(v)
            end
             table.insert(output, "* <code>" .. tostring(k) .. "</code> = " .. valStr .. "\n")
         end
         end
         table.insert(output, "\n")
         table.insert(output, "\n")
    end
   
    -- Show what we'd calculate as min (corrected: +2 for subject column)
    if #rows > 0 then
        local minValueCol = #printoutProps + 2  -- +1 for subject column, +1 for minProperty column
        table.insert(output, "'''Printout count:''' " .. #printoutProps .. "\n")
        table.insert(output, "'''Min value column index:''' " .. minValueCol .. " (printouts + 2 for subject col)\n")
        if rows[1][minValueCol] then
            local minText = stripHtml(rows[1][minValueCol])
            local minNum = tonumber(minText)
            table.insert(output, "'''First row min column value:''' " .. (minText or "nil") .. " → tonumber: " .. tostring(minNum) .. "\n")
        else
            table.insert(output, "'''First row min column:''' (column doesn't exist)\n")
        end
     end
     end
      
      
Line 102: Line 127:
end
end


--- Find all results where a property equals its minimum value across the result set
--- Find all results where a property equals its minimum value
-- @param frame Frame object with args:
-- @param frame with args:
--  conditions: SMW query conditions (e.g., "[[Has video signal format::Component (YPbPr)]]")
--  conditions: SMW conditions (e.g., "[[Has video signal format::Component (YPbPr)]]")
--  minProperty: Property to find minimum of (e.g., "Has CRT model.Has screen size inches")
--  minProperty: Property to minimize (e.g., "Has CRT model.Has screen size inches")
--  printouts: Comma-separated list of properties to display
--  printouts: Comma-separated properties to display
--  format: Output format (table, ul, ol) - default "table"
--  headers: Comma-separated header names
--  headers: Comma-separated header names for table format
--  mainlabel: Optional header for the subject/page name column
--  intro: Optional intro text (supports wikitext)
--  format: table, ul, ol (default: table)
-- @return Wikitext output
--   intro: Custom intro text
function p.findAllWithMin(frame)
function p.findAllWithMin(frame)
     local args = frame.args
     local args = frame.args
Line 119: 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 124: Line 150:
     end
     end
      
      
     -- Build the printout list for the query
     -- Build printout list
     local printoutList = {}
     local printoutProps = {}
    local printoutQuery = {}
     for prop in printouts:gmatch('[^,]+') do
     for prop in printouts:gmatch('[^,]+') do
         table.insert(printoutList, '?' .. trim(prop))
        local propTrimmed = trim(prop)
        table.insert(printoutProps, propTrimmed)
         table.insert(printoutQuery, '|?' .. propTrimmed)
     end
     end
     -- Always include the minProperty for comparison
     -- Add minProperty to query
     table.insert(printoutList, '?' .. minProperty)
     table.insert(printoutQuery, '|?' .. minProperty .. '=#')
      
      
     -- Build and execute the query
     -- Query all results sorted by minProperty ascending
     local queryString = conditions .. '\n' .. table.concat(printoutList, '\n') .. '\n|limit=500'
     local queryWikitext = '{{#ask:' .. conditions .. '\n' ..
    local results = mw.smw.ask(queryString)
        table.concat(printoutQuery, '\n') .. '\n' ..
        '|sort=' .. minProperty .. '\n' ..
        '|order=asc\n' ..
        '|limit=500\n' ..
        '|format=broadtable\n' ..
        '|headers=hide\n' ..
        '|link=all\n' ..
        '}}'
      
      
     if not results or #results == 0 then
    local result = frame:preprocess(queryWikitext)
    local rows = parseHtmlTable(result)
   
     if #rows == 0 then
         return "No results found."
         return "No results found."
     end
     end
      
      
     -- Find the minimum value
     -- broadtable format: Column 1 = subject, Columns 2..n+1 = printouts, Column n+2 = minProperty
     local minValue = nil
    -- So minValueCol = #printoutProps + 2
     for _, row in ipairs(results) do
     local minValueCol = #printoutProps + 2
        local val = getRowValue(row, minProperty)
     local minValueText = stripHtml(rows[1][minValueCol])
        if val then
    local minValue = tonumber(minValueText)
            local numVal = tonumber(val)
            if numVal then
                if minValue == nil or numVal < minValue then
                    minValue = numVal
                end
            end
        end
    end
      
      
     if minValue == nil then
     if not minValue then
         return "Could not determine minimum value."
         return "Could not determine minimum value. Column " .. minValueCol .. ", raw: " .. tostring(rows[1][minValueCol]) .. ", stripped: " .. tostring(minValueText)
     end
     end
      
      
     -- Filter results to only those matching the minimum
     -- Filter to only rows matching the minimum
     local filtered = {}
     local filtered = {}
     for _, row in ipairs(results) do
     for _, row in ipairs(rows) do
         local val = getRowValue(row, minProperty)
         local val = tonumber(stripHtml(row[minValueCol]))
         if val and tonumber(val) == minValue then
         if val == minValue then
             table.insert(filtered, row)
             table.insert(filtered, row)
         end
         end
     end
     end
      
      
     -- Build output based on format
    -- Deduplicate (multiple subobjects on same page produce duplicate rows)
    filtered = deduplicateRows(filtered, #printoutProps)
   
     -- Build headers list
     local headerList = {}
     local headerList = {}
     for h in headers:gmatch('[^,]+') do
     for h in headers:gmatch('[^,]+') do
         table.insert(headerList, trim(h))
         table.insert(headerList, trim(h))
    end
   
    local printoutProps = {}
    for prop in printouts:gmatch('[^,]+') do
        table.insert(printoutProps, trim(prop))
     end
     end
      
      
     local introText = intro or ("Smallest screen size with this input: '''" .. minValue .. "\"'''")
     local introText = intro or ("Smallest screen size with this input: '''" .. minValue .. "\"'''")
      
      
     if format == 'table' then
     if format == 'ul' then
         return p._formatTable(filtered, printoutProps, headerList, introText)
         return p._formatList(filtered, printoutProps, headerList, 'ul', introText, mainlabel)
     elseif format == 'ul' or format == 'ol' then
     elseif format == 'ol' then
         return p._formatList(filtered, printoutProps, format, 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 198: 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 203: Line 233:
     end
     end
      
      
     -- Build the printout list
     -- Build printout list
     local printoutList = {}
     local printoutProps = {}
    local printoutQuery = {}
     for prop in printouts:gmatch('[^,]+') do
     for prop in printouts:gmatch('[^,]+') do
         table.insert(printoutList, '?' .. trim(prop))
        local propTrimmed = trim(prop)
        table.insert(printoutProps, propTrimmed)
         table.insert(printoutQuery, '|?' .. propTrimmed)
     end
     end
     table.insert(printoutList, '?' .. maxProperty)
     table.insert(printoutQuery, '|?' .. maxProperty .. '=#')
      
      
     -- Build and execute the query
     -- Query all results sorted by maxProperty descending
     local queryString = conditions .. '\n' .. table.concat(printoutList, '\n') .. '\n|limit=500'
     local queryWikitext = '{{#ask:' .. conditions .. '\n' ..
    local results = mw.smw.ask(queryString)
        table.concat(printoutQuery, '\n') .. '\n' ..
        '|sort=' .. maxProperty .. '\n' ..
        '|order=desc\n' ..
        '|limit=500\n' ..
        '|format=broadtable\n' ..
        '|headers=hide\n' ..
        '|link=all\n' ..
        '}}'
      
      
     if not results or #results == 0 then
    local result = frame:preprocess(queryWikitext)
    local rows = parseHtmlTable(result)
   
     if #rows == 0 then
         return "No results found."
         return "No results found."
     end
     end
      
      
     -- Find the maximum value
     -- broadtable format: Column 1 = subject, Columns 2..n+1 = printouts, Column n+2 = maxProperty
     local maxValue = nil
     local maxValueCol = #printoutProps + 2
     for _, row in ipairs(results) do
     local maxValueText = stripHtml(rows[1][maxValueCol])
        local val = getRowValue(row, maxProperty)
    local maxValue = tonumber(maxValueText)
        if val then
            local numVal = tonumber(val)
            if numVal then
                if maxValue == nil or numVal > maxValue then
                    maxValue = numVal
                end
            end
        end
    end
      
      
     if maxValue == nil then
     if not maxValue then
         return "Could not determine maximum value."
         return "Could not determine maximum value. Column " .. maxValueCol .. ", raw: " .. tostring(rows[1][maxValueCol]) .. ", stripped: " .. tostring(maxValueText)
     end
     end
      
      
     -- Filter results to only those matching the maximum
     -- Filter to only rows matching the maximum
     local filtered = {}
     local filtered = {}
     for _, row in ipairs(results) do
     for _, row in ipairs(rows) do
         local val = getRowValue(row, maxProperty)
         local val = tonumber(stripHtml(row[maxValueCol]))
         if val and tonumber(val) == maxValue then
         if val == maxValue then
             table.insert(filtered, row)
             table.insert(filtered, row)
         end
         end
     end
     end
      
      
     -- Build output
    -- Deduplicate (multiple subobjects on same page produce duplicate rows)
    filtered = deduplicateRows(filtered, #printoutProps)
   
     -- Build headers list
     local headerList = {}
     local headerList = {}
     for h in headers:gmatch('[^,]+') do
     for h in headers:gmatch('[^,]+') do
         table.insert(headerList, trim(h))
         table.insert(headerList, trim(h))
    end
   
    local printoutProps = {}
    for prop in printouts:gmatch('[^,]+') do
        table.insert(printoutProps, trim(prop))
     end
     end
      
      
     local introText = intro or ("Largest screen size with this input: '''" .. maxValue .. "\"'''")
     local introText = intro or ("Largest screen size with this input: '''" .. maxValue .. "\"'''")
      
      
     if format == 'table' then
     if format == 'ul' then
         return p._formatTable(filtered, printoutProps, headerList, introText)
         return p._formatList(filtered, printoutProps, headerList, 'ul', introText, mainlabel)
     elseif format == 'ul' or format == 'ol' then
     elseif format == 'ol' then
         return p._formatList(filtered, printoutProps, format, 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


-- Internal: Format results as a wikitable
-- Format results as a wikitable
function p._formatTable(results, printouts, headers, introText)
-- 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, mainlabel)
     local output = {}
     local output = {}
      
      
Line 276: 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 282: Line 318:
     table.insert(output, '\n')
     table.insert(output, '\n')
      
      
     -- Data rows
     -- Data rows: printout columns start at index 2 (after subject column)
     for i, row in ipairs(results) do
     for i, row in ipairs(rows) do
         local tr = '|-\n| ' .. tostring(i)
         local tr = '|-\n| ' .. tostring(i)
         for _, prop in ipairs(printouts) do
        if mainlabel then
             local val = getRowValue(row, prop)
            tr = tr .. ' || ' .. (row[1] or '')
             tr = tr .. ' || ' .. formatValue(val)
        end
         for j = 1, #printouts do
             -- Printout j is in column j+1 (offset by subject column)
             tr = tr .. ' || ' .. (row[j + 1] or '')
         end
         end
         table.insert(output, tr .. '\n')
         table.insert(output, tr .. '\n')
Line 297: Line 336:
end
end


-- Internal: Format results as a list
-- Format results as a list
function p._formatList(results, printouts, 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 304: Line 343:
     table.insert(output, introText .. "\n\n")
     table.insert(output, introText .. "\n\n")
      
      
     for _, row in ipairs(results) do
     for _, row in ipairs(rows) do
         local parts = {}
         local parts = {}
         for _, prop in ipairs(printouts) do
        if mainlabel then
             local val = getRowValue(row, prop)
            local subject = row[1]
             local formatted = formatValue(val)
            if subject and subject ~= '' then
             if formatted ~= '' then
                table.insert(parts, subject)
                 table.insert(parts, formatted)
            end
        end
         for j = 1, #printouts do
             -- Printout j is in column j+1 (offset by subject column)
             local cell = row[j + 1]
             if cell and cell ~= '' then
                 table.insert(parts, cell)
             end
             end
         end
         end
MediaWiki Appliance - Powered by TurnKey Linux