Module:SMWUtils: Difference between revisions
Rewrite to use frame:preprocess() with #ask queries since mw.smw.ask() doesn't support property chains (via update-page on MediaWiki MCP Server) |
Add debug function to diagnose parsing issues (via update-page on MediaWiki MCP Server) |
||
| Line 35: | Line 35: | ||
local function stripHtml(s) | local function stripHtml(s) | ||
if not s then return nil end | if not s then return nil end | ||
return s:gsub('<[^>]+>', ''):gsub('&[^;]+;', '') | return trim(s:gsub('<[^>]+>', ''):gsub('&[^;]+;', '')) | ||
end | |||
--- Debug function to see raw query output and parsing | |||
function p.debug(frame) | |||
local args = frame.args | |||
local conditions = trim(args.conditions) or '' | |||
local minProperty = trim(args.minProperty) or '' | |||
local printouts = trim(args.printouts) or '' | |||
-- Build printout list | |||
local printoutProps = {} | |||
local printoutQuery = {} | |||
for prop in printouts:gmatch('[^,]+') do | |||
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 | |||
local queryWikitext = '{{#ask:' .. conditions .. '\n' .. | |||
table.concat(printoutQuery, '\n') .. '\n' .. | |||
'|sort=' .. minProperty .. '\n' .. | |||
'|order=asc\n' .. | |||
'|limit=5\n' .. | |||
'|format=broadtable\n' .. | |||
'|headers=hide\n' .. | |||
'|link=all\n' .. | |||
'}}' | |||
local result = frame:preprocess(queryWikitext) | |||
local rows = parseHtmlTable(result) | |||
local output = {} | |||
table.insert(output, "'''Query wikitext:'''\n<pre>" .. mw.text.nowiki(queryWikitext) .. "</pre>\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(rows) do | |||
table.insert(output, "'''Row " .. i .. " (" .. #row .. " cells):'''\n") | |||
for j, cell in ipairs(row) do | |||
local stripped = stripHtml(cell) or "(nil)" | |||
table.insert(output, "* Cell " .. j .. ": <code>" .. mw.text.nowiki(cell):sub(1, 100) .. "</code> → stripped: <code>" .. stripped .. "</code>\n") | |||
end | |||
table.insert(output, "\n") | |||
end | |||
-- Show what we'd calculate as min | |||
if #rows > 0 then | |||
local minValueCol = #printoutProps + 1 | |||
table.insert(output, "'''Min value column index:''' " .. minValueCol .. "\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 | |||
return table.concat(output) | |||
end | end | ||
| Line 63: | Line 126: | ||
local printoutQuery = {} | local printoutQuery = {} | ||
for prop in printouts:gmatch('[^,]+') do | for prop in printouts:gmatch('[^,]+') do | ||
local | local propTrimmed = trim(prop) | ||
table.insert(printoutProps, | table.insert(printoutProps, propTrimmed) | ||
table.insert(printoutQuery, '|?' .. | table.insert(printoutQuery, '|?' .. propTrimmed) | ||
end | end | ||
-- Add minProperty to query | -- Add minProperty to query | ||
| Line 95: | Line 158: | ||
if not minValue 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 | ||
| Line 142: | Line 205: | ||
local printoutQuery = {} | local printoutQuery = {} | ||
for prop in printouts:gmatch('[^,]+') do | for prop in printouts:gmatch('[^,]+') do | ||
local | local propTrimmed = trim(prop) | ||
table.insert(printoutProps, | table.insert(printoutProps, propTrimmed) | ||
table.insert(printoutQuery, '|?' .. | table.insert(printoutQuery, '|?' .. propTrimmed) | ||
end | end | ||
table.insert(printoutQuery, '|?' .. maxProperty .. '=#') | table.insert(printoutQuery, '|?' .. maxProperty .. '=#') | ||
| Line 171: | Line 234: | ||
if not maxValue 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 | ||