Module:SMWUtils: Difference between revisions

Rewrite to use frame:preprocess() instead of mw.smw.ask() (Semantic Scribunto not installed) (via update-page on MediaWiki MCP Server)
Rewrite to use mw.smw.ask() now that Semantic Scribunto is installed (via update-page on MediaWiki MCP Server)
Line 1: Line 1:
-- Module:SMWUtils
-- Module:SMWUtils
-- Utility functions for Semantic MediaWiki queries
-- Utility functions for Semantic MediaWiki queries
-- Uses frame:preprocess() since Semantic Scribunto is not installed
-- Requires Semantic Scribunto extension for mw.smw.ask()


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


--- Find all results where a chained property equals its minimum value
--- Find all results where a property equals its minimum value across the result set
-- Uses a two-pass approach: first find min, then query for all matching
-- @param frame Frame object with args:
-- @param frame Frame object with args:
--  conditions: SMW query conditions (e.g., "[[Has video signal format::Component (YPbPr)]]")
--  conditions: SMW query conditions (e.g., "[[Has video signal format::Component (YPbPr)]]")
Line 21: Line 20:
--  format: Output format (table, ul, ol) - default "table"
--  format: Output format (table, ul, ol) - default "table"
--  headers: Comma-separated header names for table format
--  headers: Comma-separated header names for table format
--  intro: Optional intro text
--  intro: Optional intro text (supports wikitext)
-- @return Wikitext output
-- @return Wikitext output
function p.findAllWithMin(frame)
function p.findAllWithMin(frame)
Line 36: Line 35:
     end
     end
      
      
     -- Step 1: Find the minimum value using a sorted query with limit=1
     -- Build the printout list for the query
     local minQueryWikitext = '{{#ask:' .. conditions .. '\n' ..
     local printoutList = {}
         '|?' .. minProperty .. '\n' ..
    for prop in printouts:gmatch('[^,]+') do
        '|sort=' .. minProperty .. '\n' ..
         table.insert(printoutList, '?' .. trim(prop))
        '|order=asc\n' ..
    end
        '|limit=1\n' ..
    -- Always include the minProperty for comparison
        '|format=broadtable\n' ..
    table.insert(printoutList, '?' .. minProperty)
        '|headers=hide\n' ..
   
        '|link=none\n' ..
    -- Build and execute the query
        '}}'
    local queryString = conditions .. '\n' .. table.concat(printoutList, '\n') .. '\n|limit=500'
    local results = mw.smw.ask(queryString)
      
      
     local minResult = frame:preprocess(minQueryWikitext)
     if not results or #results == 0 then
        return "No results found."
    end
      
      
     -- Parse the minimum value from the table result
     -- Find the minimum value
     local minValue = nil
     local minValue = nil
     if minResult then
     for _, row in ipairs(results) do
        -- Extract from last <td> in the row (the property value)
        local val = row[minProperty]
        for cellContent in minResult:gmatch('<td[^>]*>(.-)</td>') do
        if val then
            local cleanContent = cellContent:gsub('<[^>]+>', '')
             local numVal = tonumber(val)
             local numVal = tonumber(trim(cleanContent))
             if numVal then
             if numVal then
                 minValue = numVal
                 if minValue == nil or numVal < minValue then
                    minValue = numVal
                end
             end
             end
         end
         end
Line 63: Line 66:
      
      
     if minValue == nil then
     if minValue == nil then
         return "No results found or could not determine minimum value."
         return "Could not determine minimum value."
     end
     end
      
      
     -- Step 2: Query for all results matching this minimum value
     -- Filter results to only those matching the minimum
     local printoutList = {}
     local filtered = {}
     for prop in printouts:gmatch('[^,]+') do
     for _, row in ipairs(results) do
         table.insert(printoutList, '|?' .. trim(prop))
         local val = row[minProperty]
    end
         if val and tonumber(val) == minValue then
   
            table.insert(filtered, row)
    local fullQueryWikitext = '{{#ask:' .. conditions .. '\n' ..
         end
        '[[' .. minProperty .. '::' .. minValue .. ']]\n' ..
         table.concat(printoutList, '\n') .. '\n' ..
        '|sort=' .. minProperty .. '\n' ..
        '|limit=100\n' ..
        '|format=broadtable\n' ..
        '|headers=hide\n' ..
        '|link=all\n' ..
        '}}'
   
    local fullResult = frame:preprocess(fullQueryWikitext)
   
    -- Parse results from HTML table
    local results = p._parseTableResults(fullResult, printouts)
   
    if #results == 0 then
         return "No results found matching minimum value " .. minValue .. "."
     end
     end
      
      
     -- Build output
     -- Build output based on format
     local headerList = {}
     local headerList = {}
     for h in headers:gmatch('[^,]+') do
     for h in headers:gmatch('[^,]+') do
Line 105: Line 92:
      
      
     if format == 'table' then
     if format == 'table' then
         return p._formatTableFromParsed(results, printoutProps, headerList, introText)
         return p._formatTable(filtered, printoutProps, headerList, introText)
     elseif format == 'ul' or format == 'ol' then
     elseif format == 'ul' or format == 'ol' then
         return p._formatListFromParsed(results, format, introText)
         return p._formatList(filtered, printoutProps, format, introText)
     else
     else
         return p._formatTableFromParsed(results, printoutProps, headerList, introText)
         return p._formatTable(filtered, printoutProps, headerList, introText)
     end
     end
end
end
Line 127: Line 114:
     end
     end
      
      
     -- Step 1: Find the maximum value using a sorted query with limit=1
     -- Build the printout list
     local maxQueryWikitext = '{{#ask:' .. conditions .. '\n' ..
     local printoutList = {}
         '|?' .. maxProperty .. '\n' ..
    for prop in printouts:gmatch('[^,]+') do
        '|sort=' .. maxProperty .. '\n' ..
         table.insert(printoutList, '?' .. trim(prop))
        '|order=desc\n' ..
    end
        '|limit=1\n' ..
    table.insert(printoutList, '?' .. maxProperty)
        '|format=broadtable\n' ..
        '|headers=hide\n' ..
        '|link=none\n' ..
        '}}'
      
      
     local maxResult = frame:preprocess(maxQueryWikitext)
    -- Build and execute the query
     local queryString = conditions .. '\n' .. table.concat(printoutList, '\n') .. '\n|limit=500'
    local results = mw.smw.ask(queryString)
      
      
     -- Parse the maximum value from the table result
    if not results or #results == 0 then
        return "No results found."
    end
   
     -- Find the maximum value
     local maxValue = nil
     local maxValue = nil
     if maxResult then
     for _, row in ipairs(results) do
        for cellContent in maxResult:gmatch('<td[^>]*>(.-)</td>') do
        local val = row[maxProperty]
            local cleanContent = cellContent:gsub('<[^>]+>', '')
        if val then
             local numVal = tonumber(trim(cleanContent))
             local numVal = tonumber(val)
             if numVal then
             if numVal then
                 maxValue = numVal
                 if maxValue == nil or numVal > maxValue then
                    maxValue = numVal
                end
             end
             end
         end
         end
Line 153: Line 144:
      
      
     if maxValue == nil then
     if maxValue == nil then
         return "No results found or could not determine maximum value."
         return "Could not determine maximum value."
     end
     end
      
      
     -- Step 2: Query for all results matching this maximum value
     -- Filter results to only those matching the maximum
     local printoutList = {}
     local filtered = {}
     for prop in printouts:gmatch('[^,]+') do
     for _, row in ipairs(results) do
         table.insert(printoutList, '|?' .. trim(prop))
         local val = row[maxProperty]
    end
         if val and tonumber(val) == maxValue then
   
            table.insert(filtered, row)
    local fullQueryWikitext = '{{#ask:' .. conditions .. '\n' ..
         end
        '[[' .. maxProperty .. '::' .. maxValue .. ']]\n' ..
         table.concat(printoutList, '\n') .. '\n' ..
        '|sort=' .. maxProperty .. '\n' ..
        '|limit=100\n' ..
        '|format=broadtable\n' ..
        '|headers=hide\n' ..
        '|link=all\n' ..
        '}}'
   
    local fullResult = frame:preprocess(fullQueryWikitext)
   
    -- Parse results from HTML table
    local results = p._parseTableResults(fullResult, printouts)
   
    if #results == 0 then
         return "No results found matching maximum value " .. maxValue .. "."
     end
     end
      
      
Line 195: Line 170:
      
      
     if format == 'table' then
     if format == 'table' then
         return p._formatTableFromParsed(results, printoutProps, headerList, introText)
         return p._formatTable(filtered, printoutProps, headerList, introText)
     elseif format == 'ul' or format == 'ol' then
     elseif format == 'ul' or format == 'ol' then
         return p._formatListFromParsed(results, format, introText)
         return p._formatList(filtered, printoutProps, format, introText)
     else
     else
         return p._formatTableFromParsed(results, printoutProps, headerList, introText)
         return p._formatTable(filtered, printoutProps, headerList, introText)
     end
     end
end
end


-- Internal: Parse HTML table results into a Lua table
-- Internal: Format results as a wikitable
function p._parseTableResults(htmlResult, printouts)
function p._formatTable(results, printouts, headers, introText)
    local results = {}
   
    if not htmlResult then
        return results
    end
   
    -- Count expected columns from printouts
    local numPrintouts = 0
    for _ in printouts:gmatch('[^,]+') do
        numPrintouts = numPrintouts + 1
    end
   
    -- Match each table row
    for rowContent in htmlResult:gmatch('<tr[^>]*>(.-)</tr>') do
        local cells = {}
        -- Extract content from each <td>, preserving HTML for links
        for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
            table.insert(cells, cellContent)
        end
       
        -- First cell is usually the page link, rest are printout values
        if #cells >= 1 then
            table.insert(results, cells)
        end
    end
   
    return results
end
 
-- Internal: Format parsed results as a wikitable
function p._formatTableFromParsed(results, printouts, headers, introText)
     local output = {}
     local output = {}
      
      
Line 251: Line 195:
     -- Data rows
     -- Data rows
     for i, row in ipairs(results) do
     for i, row in ipairs(results) do
         table.insert(output, '|-\n| ' .. tostring(i))
         local tr = '|-\n| ' .. tostring(i)
         for j = 1, #printouts do
         for _, prop in ipairs(printouts) do
             local cellVal = row[j] or ''
             local val = row[prop]
             table.insert(output, ' || ' .. cellVal)
            local displayVal = ''
             if val then
                if type(val) == 'table' then
                    -- Handle linked values (pages)
                    if val.fulltext then
                        displayVal = '[[' .. val.fulltext .. ']]'
                    elseif #val > 0 then
                        -- Array of values
                        local parts = {}
                        for _, v in ipairs(val) do
                            if type(v) == 'table' and v.fulltext then
                                table.insert(parts, '[[' .. v.fulltext .. ']]')
                            else
                                table.insert(parts, tostring(v))
                            end
                        end
                        displayVal = table.concat(parts, ', ')
                    end
                else
                    displayVal = tostring(val)
                end
            end
            tr = tr .. ' || ' .. displayVal
         end
         end
         table.insert(output, '\n')
         table.insert(output, tr .. '\n')
     end
     end
      
      
Line 264: Line 230:
end
end


-- Internal: Format parsed results as a list
-- Internal: Format results as a list
function p._formatListFromParsed(results, listType, introText)
function p._formatList(results, printouts, listType, introText)
     local output = {}
     local output = {}
     local bullet = listType == 'ol' and '#' or '*'
     local bullet = listType == 'ol' and '#' or '*'
Line 273: Line 239:
     for _, row in ipairs(results) do
     for _, row in ipairs(results) do
         local parts = {}
         local parts = {}
         for _, cell in ipairs(row) do
         for _, prop in ipairs(printouts) do
             if cell and cell ~= '' then
             local val = row[prop]
                table.insert(parts, cell)
            if val then
                if type(val) == 'table' and val.fulltext then
                    table.insert(parts, '[[' .. val.fulltext .. ']]')
                elseif type(val) ~= 'table' then
                    table.insert(parts, tostring(val))
                end
             end
             end
         end
         end
MediaWiki Appliance - Powered by TurnKey Linux