Module:SMWUtils: Difference between revisions
Rewrite to use mw.smw.ask() now that Semantic Scribunto is installed (via update-page on MediaWiki MCP Server) |
Add debug function and improve property chain handling (via update-page on MediaWiki MCP Server) |
||
| Line 11: | Line 11: | ||
if s == '' then return nil end | if s == '' then return nil end | ||
return s | return s | ||
end | |||
-- Helper to get a value from a row, trying multiple key formats for property chains | |||
local function getRowValue(row, prop) | |||
-- Try the exact property name first | |||
if row[prop] ~= nil then | |||
return row[prop] | |||
end | |||
-- For property chains like "Has CRT model.Has brand", also try just "Has brand" | |||
local lastPart = prop:match('%.([^%.]+)$') | |||
if lastPart and row[lastPart] ~= nil then | |||
return row[lastPart] | |||
end | |||
return nil | |||
end | |||
-- Helper to format a value for display | |||
local function formatValue(val) | |||
if val == nil then | |||
return '' | |||
end | |||
if type(val) == 'table' then | |||
-- Handle linked values (pages) | |||
if val.fulltext then | |||
return '[[' .. 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 | |||
return table.concat(parts, ', ') | |||
end | |||
return '' | |||
else | |||
return tostring(val) | |||
end | |||
end | |||
--- Debug function to show what keys mw.smw.ask() returns | |||
-- Usage: {{#invoke:SMWUtils|debug|query=[[Has video signal format::Component (YPbPr)]]|printouts=Has CRT model, Has CRT model.Has brand}} | |||
function p.debug(frame) | |||
local args = frame.args | |||
local query = trim(args.query) or '' | |||
local printouts = trim(args.printouts) or '' | |||
-- Build printout list | |||
local printoutList = {} | |||
for prop in printouts:gmatch('[^,]+') do | |||
table.insert(printoutList, '?' .. trim(prop)) | |||
end | |||
local queryString = query .. '\n' .. table.concat(printoutList, '\n') .. '\n|limit=3' | |||
local results = mw.smw.ask(queryString) | |||
if not results or #results == 0 then | |||
return "No results found for query: <code>" .. mw.text.nowiki(queryString) .. "</code>" | |||
end | |||
local output = {} | |||
table.insert(output, "'''Query:''' <code>" .. mw.text.nowiki(queryString) .. "</code>\n\n") | |||
table.insert(output, "'''Results (" .. #results .. " shown):'''\n\n") | |||
for i, row in ipairs(results) do | |||
table.insert(output, "'''Row " .. i .. " keys:'''\n") | |||
for k, v in pairs(row) do | |||
local valStr | |||
if type(v) == 'table' then | |||
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 | |||
table.insert(output, "\n") | |||
end | |||
return table.concat(output) | |||
end | end | ||
| Line 54: | Line 143: | ||
local minValue = nil | local minValue = nil | ||
for _, row in ipairs(results) do | for _, row in ipairs(results) do | ||
local val = row | local val = getRowValue(row, minProperty) | ||
if val then | if val then | ||
local numVal = tonumber(val) | local numVal = tonumber(val) | ||
| Line 72: | Line 161: | ||
local filtered = {} | local filtered = {} | ||
for _, row in ipairs(results) do | for _, row in ipairs(results) do | ||
local val = row | local val = getRowValue(row, minProperty) | ||
if val and tonumber(val) == minValue then | if val and tonumber(val) == minValue then | ||
table.insert(filtered, row) | table.insert(filtered, row) | ||
| Line 132: | Line 221: | ||
local maxValue = nil | local maxValue = nil | ||
for _, row in ipairs(results) do | for _, row in ipairs(results) do | ||
local val = row | local val = getRowValue(row, maxProperty) | ||
if val then | if val then | ||
local numVal = tonumber(val) | local numVal = tonumber(val) | ||
| Line 150: | Line 239: | ||
local filtered = {} | local filtered = {} | ||
for _, row in ipairs(results) do | for _, row in ipairs(results) do | ||
local val = row | local val = getRowValue(row, maxProperty) | ||
if val and tonumber(val) == maxValue then | if val and tonumber(val) == maxValue then | ||
table.insert(filtered, row) | table.insert(filtered, row) | ||
| Line 197: | Line 286: | ||
local tr = '|-\n| ' .. tostring(i) | local tr = '|-\n| ' .. tostring(i) | ||
for _, prop in ipairs(printouts) do | for _, prop in ipairs(printouts) do | ||
local val = row | local val = getRowValue(row, prop) | ||
tr = tr .. ' || ' .. formatValue(val) | |||
tr = tr .. ' || ' .. | |||
end | end | ||
table.insert(output, tr .. '\n') | table.insert(output, tr .. '\n') | ||
| Line 240: | Line 307: | ||
local parts = {} | local parts = {} | ||
for _, prop in ipairs(printouts) do | for _, prop in ipairs(printouts) do | ||
local val = row | local val = getRowValue(row, prop) | ||
local formatted = formatValue(val) | |||
if formatted ~= '' then | |||
table.insert(parts, formatted) | |||
end | end | ||
end | end | ||